From e0c00e58ff7441c61d1b611162b5cec5e5dbfdf3 Mon Sep 17 00:00:00 2001 From: Larry Ewing Date: Wed, 11 Apr 2018 22:01:40 -0500 Subject: Add Material labels --- .../Controls/Custom/BrushTabViewController.cs | 111 +++++++++++++++++---- .../Controls/Custom/DrawingExtensions.cs | 3 + 2 files changed, 92 insertions(+), 22 deletions(-) (limited to 'Xamarin.PropertyEditing.Mac/Controls') diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs index 64c97b1..b45ad6c 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs @@ -39,6 +39,31 @@ namespace Xamarin.PropertyEditing.Mac { public override bool IsFlipped => true; + readonly string[] ColorNames = { + "50", + "100", + "200", + "300", + "400", + "500", + "600", + "700", + "800", + "900" + }; + + readonly string[] AccentNames = { + "A100", + "A200", + "A400", + "A700" + }; + + readonly string[] BlackWhite = { + "White", + "Black" + }; + public MaterialView () : base () { Initialize (); @@ -62,28 +87,44 @@ namespace Xamarin.PropertyEditing.Mac get => ViewModel?.MaterialDesign; } - public override void Layout () + CGRect frame; + String name; + public override void Layout() { base.Layout (); - if (Layer.Sublayers != null) + if (name == MaterialDesign.ColorName && frame == Frame) { + return; + } + name = MaterialDesign.ColorName; + frame = Frame; + + if (Subviews != null) + foreach (var v in this.Subviews) + v.RemoveFromSuperview (); + + if (Layer?.Sublayers != null) foreach (var l in Layer.Sublayers) l.RemoveFromSuperLayer (); - + if (MaterialDesign != null) { - var colors = MaterialDesign.Palettes.Select (p => new { p.Name, p.MainColor }).ToArray (); + var colors = MaterialDesign.Palettes.Select (p => new { p.Name, Color = p.MainColor }).ToArray (); int col = 0; nfloat x = 0; nfloat y = 0; - var width = Bounds.Width / 10; - var height = Bounds.Height / 5; + var width = Frame.Width / 10; + var height = Frame.Height / 5; foreach (var p in colors) { - var layer = new CALayer { - BackgroundColor = p.MainColor.ToCGColor (), + var frame = new CGRect (x, y, width, height); + var l = new CALayer { + Frame = frame, + BackgroundColor = p.Color.ToCGColor (), CornerRadius = 3, - Frame = new CGRect (x, y, width, height) + BorderColor = NSColor.SelectedControl.CGColor, + BorderWidth = MaterialDesign.ColorName == p.Name ? 3 : 0 }; - Layer.AddSublayer (layer); + + Layer.AddSublayer (l); x += width; col++; if (col >= 10) { @@ -93,29 +134,55 @@ namespace Xamarin.PropertyEditing.Mac } } + AddSubview (new NSTextField (new CGRect (x, y, Frame.Width, 30)) + { + StringValue = MaterialDesign.ColorName, + DrawsBackground = false, + Bordered = false, + Editable = false + }); y += 30; x = 0; - width = Bounds.Width / MaterialDesign.NormalColorScale.Count (); - foreach (var n in MaterialDesign.NormalColorScale) { - var layer = new CALayer { - BackgroundColor = n.Value.ToCGColor (), + width = Frame.Width / MaterialDesign.NormalColorScale.Count (); + var names = MaterialDesign.NormalColorScale.Count () > 2 ? ColorNames : BlackWhite; + foreach (var n in MaterialDesign.NormalColorScale.Zip (names, (c, n) => new { Name = n, Color = c.Value })) { + var frame = new CGRect (x, y, width, height); + var l = new CALayer { + BackgroundColor = n.Color.ToCGColor (), CornerRadius = 3, - Frame = new CGRect (x, y, width, height) + Frame = frame + }; + Layer.AddSublayer (l); + var c = new NSTextField (frame) { + StringValue = n.Name, + BackgroundColor = NSColor.Clear, + DrawsBackground = false, + Bordered = false, + Editable = false, }; - Layer.AddSublayer (layer); + AddSubview (c); x += width; } y += height; x = 0; - width = Bounds.Width / MaterialDesign.AccentColorScale.Count (); - foreach (var n in MaterialDesign.AccentColorScale) { - var layer = new CALayer { - BackgroundColor = n.Value.ToCGColor (), + width = Frame.Width / MaterialDesign.AccentColorScale.Count (); + foreach (var n in MaterialDesign.AccentColorScale.Zip (AccentNames, (c, n) => new { Name = n, Color = c.Value })) { + var frame = new CGRect (x, y, width, height); + var l = new CALayer { + BackgroundColor = n.Color.ToCGColor (), CornerRadius = 3, - Frame = new CGRect (x, y, width, height) + Frame = frame + }; + Layer.AddSublayer (l); + var c = new NSTextField (frame) { + StringValue = n.Name, + BackgroundColor = NSColor.Clear, + DrawsBackground = false, + Bordered = false, + Editable = false }; - Layer.AddSublayer (layer); + AddSubview (c); x += width; } } diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/DrawingExtensions.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/DrawingExtensions.cs index a5e9c78..e2f1075 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/DrawingExtensions.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/DrawingExtensions.cs @@ -40,6 +40,9 @@ namespace Xamarin.PropertyEditing.Mac public static CGColor ToCGColor (this CommonColor color) => new CGColor (color.R / 255f, color.G / 255f, color.B / 255f, color.A / 255f); + public static NSColor ToNSColor (this CommonColor color) + => NSColor.FromRgba (color.R, color.G, color.B, color.A); + public static CommonColor Blend (this CommonColor a, CommonColor b) { byte C (byte cb1, byte ab1, byte cb2) -- cgit v1.2.3