From ef0178592c06afa82ac07ceceb60f04473ad02b1 Mon Sep 17 00:00:00 2001 From: Jesse Date: Fri, 5 May 2023 09:59:11 +1000 Subject: [PATCH 1/2] Add new apply style feature Format code Fix ProgressBar and Slider style issue --- src/Comet/EnvironmentAware.cs | 28 +++++++--- src/Comet/EnvironmentData.cs | 16 +++--- src/Comet/Styles/ButtonStyle.cs | 2 +- src/Comet/Styles/ControlState.cs | 1 + src/Comet/Styles/Material/MaterialStyle.cs | 1 - src/Comet/Styles/ProgressBarStyle.cs | 1 - src/Comet/Styles/Style.cs | 1 - src/Comet/Styles/StyleExtensions.cs | 61 +++++++++++++++++++++- 8 files changed, 94 insertions(+), 17 deletions(-) diff --git a/src/Comet/EnvironmentAware.cs b/src/Comet/EnvironmentAware.cs index d3ce56e2f..0b8937d72 100644 --- a/src/Comet/EnvironmentAware.cs +++ b/src/Comet/EnvironmentAware.cs @@ -152,9 +152,9 @@ internal bool SetValue(string key, object value, bool cascades) //If we are setting the value to null, //there is no reason to create the dictionary if it doesnt exist if (cascades) - return Context(value != null)?.SetValue(key, value,true) ?? false; + return Context(value != null)?.SetValue(key, value, true) ?? false; else - return LocalContext(value != null)?.SetValue(key, value,false) ?? false; + return LocalContext(value != null)?.SetValue(key, value, false) ?? false; } static Dictionary<(ContextualObject view, string property, bool cascades), (object oldValue, object newValue)> monitoredChanges = null; @@ -232,7 +232,7 @@ public static T SetEnvironment(this T contextualObject, Type type, string key return contextualObject; } - public static T SetEnvironment(this T view, Type type, string key, Binding binding, bool cascades = true, ControlState state = ControlState.Default) + public static T SetEnvironment(this T view, Type type, string key, Binding binding, bool cascades = true, ControlState state = ControlState.Default) where T : View { binding.BindToProperty(view, key); @@ -270,7 +270,23 @@ public static T SetEnvironment(this T contextualObject, string key, object va return contextualObject; } - public static void SetProperty(this View view, object value, [CallerMemberName] string key = "", bool cascades = true) => view.SetEnvironment(key,value, cascades); + public static T SetEnvironment(this T contextualObject, string styleId, string key, StyleAwareValue styleValue) + where T : ContextualObject + { + if (styleValue == null) + { + contextualObject.SetEnvironment(styleId, key, null, true); + return contextualObject; + } + foreach (var pair in styleValue.ToEnvironmentValues()) + { + var newKey = pair.key == null ? key : $"{pair.key}.{key}"; + contextualObject.SetEnvironment(styleId, newKey, pair.value); + } + return contextualObject; + } + + public static void SetProperty(this View view, object value, [CallerMemberName] string key = "", bool cascades = true) => view.SetEnvironment(key, value, cascades); //public static T SetEnvironment(this T contextualObject, IDictionary data, bool cascades = true) where T : ContextualObject //{ // foreach (var pair in data) @@ -286,7 +302,7 @@ public static T SetEnvironment(this T contextualObject, string key, object va public static T GetEnvironment(this View view, string key, ControlState state, bool cascades = true) { - key = ContextualObject.GetControlStateKey(state,key); + key = ContextualObject.GetControlStateKey(state, key); return view.GetEnvironment(key, cascades); } public static T GetEnvironment(this View view, string key, bool cascades = true) @@ -302,7 +318,7 @@ public static T GetEnvironment(this View view, Type type, string key, Control public static T GetEnvironment(this View view, Type type, string key, bool cascades = true) => view.GetValue(key, view, view.Parent, ContextualObject.GetTypedStyleId(view, key), ContextualObject.GetTypedKey(type ?? view.GetType(), key), cascades); - + public static object GetEnvironment(this View view, string key, bool cascades = true) => view.GetValue(key, view, view.Parent, ContextualObject.GetTypedStyleId(view, key), ContextualObject.GetTypedKey(view, key), cascades); public static object GetEnvironment(this View view, Type type, string key, bool cascades = true) => view.GetValue(key, view, view.Parent, ContextualObject.GetTypedStyleId(view, key), ContextualObject.GetTypedKey(type ?? view.GetType(), key), cascades); diff --git a/src/Comet/EnvironmentData.cs b/src/Comet/EnvironmentData.cs index ea36b0b91..f04a7ab64 100644 --- a/src/Comet/EnvironmentData.cs +++ b/src/Comet/EnvironmentData.cs @@ -14,7 +14,7 @@ public static class EnvironmentKeys public static class Fonts { - //public const string Font = "Font"; + public const string Font = "Font"; public const string Size = "Font.Size"; public const string Weight = "Font.Weight"; public const string Family = "Font.Family"; @@ -60,6 +60,11 @@ public static class View public const string Opacity = nameof(Microsoft.Maui.IView.Opacity); } + public static class Button + { + public const string Padding = "Padding"; + } + public static class Shape { public const string LineWidth = "Shape.LineWidth"; @@ -101,14 +106,13 @@ public static class Navigation } public static class Slider { - public const string TrackColor = "SliderTrackColor"; - public const string ProgressColor = "SliderProgressColor"; - public const string ThumbColor = "SliderThumbColor"; + public const string TrackColor = "MinimumTrackColor"; + public const string ProgressColor = "MaximumTrackColor"; + public const string ThumbColor = "ThumbColor"; } public static class ProgressBar { - public const string TrackColor = "ProgressBarTrackColor"; - public const string ProgressColor = "ProgressBarProgressColor"; + public const string ProgressColor = "ProgressColor"; } } diff --git a/src/Comet/Styles/ButtonStyle.cs b/src/Comet/Styles/ButtonStyle.cs index 1613cad37..5435590ba 100644 --- a/src/Comet/Styles/ButtonStyle.cs +++ b/src/Comet/Styles/ButtonStyle.cs @@ -10,7 +10,7 @@ namespace Comet.Styles { public class ButtonStyle : ViewStyle { - public StyleAwareValue TextColor { get; set; } + public StyleAwareValue TextColor { get; set; } public StyleAwareValue TextFont { get; set; } diff --git a/src/Comet/Styles/ControlState.cs b/src/Comet/Styles/ControlState.cs index 07c07384c..0c00ac142 100644 --- a/src/Comet/Styles/ControlState.cs +++ b/src/Comet/Styles/ControlState.cs @@ -1,6 +1,7 @@ using System; namespace Comet { + //TODO need to implement public enum ControlState { Default, diff --git a/src/Comet/Styles/Material/MaterialStyle.cs b/src/Comet/Styles/Material/MaterialStyle.cs index f3db3d17b..289f1c6b3 100644 --- a/src/Comet/Styles/Material/MaterialStyle.cs +++ b/src/Comet/Styles/Material/MaterialStyle.cs @@ -86,7 +86,6 @@ public MaterialStyle(ColorPalette colorPalette) ProgressBar = new ProgressBarStyle { ProgressColor = colorPalette.P500, - TrackColor = colorPalette.P100, }; } diff --git a/src/Comet/Styles/ProgressBarStyle.cs b/src/Comet/Styles/ProgressBarStyle.cs index 9554ac184..99109217d 100644 --- a/src/Comet/Styles/ProgressBarStyle.cs +++ b/src/Comet/Styles/ProgressBarStyle.cs @@ -5,7 +5,6 @@ namespace Comet.Styles { public class ProgressBarStyle : ViewStyle { - public StyleAwareValue TrackColor { get; set; } public StyleAwareValue ProgressColor { get; set; } } } diff --git a/src/Comet/Styles/Style.cs b/src/Comet/Styles/Style.cs index 01fe9faab..890c84449 100644 --- a/src/Comet/Styles/Style.cs +++ b/src/Comet/Styles/Style.cs @@ -175,7 +175,6 @@ protected virtual void ApplySliderStyle(ContextualObject view) protected virtual void ApplyProgresBarStyle(ContextualObject view) { - SetEnvironment(view, "", EnvironmentKeys.ProgressBar.TrackColor, ProgressBar?.TrackColor); SetEnvironment(view, "", EnvironmentKeys.ProgressBar.ProgressColor, ProgressBar?.ProgressColor); ApplyViewStyles(view, ProgressBar, typeof(ProgressBar)); diff --git a/src/Comet/Styles/StyleExtensions.cs b/src/Comet/Styles/StyleExtensions.cs index 6e92bee48..60a0f0eac 100644 --- a/src/Comet/Styles/StyleExtensions.cs +++ b/src/Comet/Styles/StyleExtensions.cs @@ -7,7 +7,7 @@ namespace Comet { public static class StyleExtensions { - public static T StyleId (this T view, string styleId) where T : View + public static T StyleId(this T view, string styleId) where T : View { view.StyleId = styleId; return view; @@ -89,5 +89,64 @@ public static T ApplyStyle(this T view, Style style) where T : ContextualObje style.Apply(view); return view; } + + private static T Apply(T view, string styleId, ViewStyle style) where T : View + { + view.SetEnvironment(styleId, EnvironmentKeys.View.Border, style.Border); + view.SetEnvironment(styleId, EnvironmentKeys.Colors.Background, style.BackgroundColor); + view.SetEnvironment(styleId, EnvironmentKeys.View.Shadow, style.Shadow); + view.SetEnvironment(styleId, EnvironmentKeys.View.ClipShape, style.Border); + return view; + } + + private static string GetStyleId() where T : ViewStyle + { + var frame = new StackFrame(1); + string className = frame.GetMethod().DeclaringType.Name; + string styleId = $"{className}.{typeof(T)}"; + return styleId; + } + + public static View Apply(this View view) where T : ViewStyle, new() + { + string styleId = GetStyleId(); + var result = Apply(view, styleId, new T()); + result.StyleId = styleId; + return result; + } + + public static Button Apply(this Button button) where T : ButtonStyle, new() + { + string styleId = GetStyleId(); + T style = new T(); + var result = Apply(button, styleId, style); + result.SetEnvironment(styleId, EnvironmentKeys.Colors.Color, style.TextColor); + result.SetEnvironment(styleId, EnvironmentKeys.Button.Padding, style.Padding); + result.SetEnvironment(styleId, EnvironmentKeys.Fonts.Font, style.TextFont); + result.StyleId = styleId; + return result; + } + + public static ProgressBar Apply(this ProgressBar progressBar) where T : ProgressBarStyle, new() + { + string styleId = GetStyleId(); + T style = new T(); + var result = Apply(progressBar, styleId, style); + result.SetEnvironment(styleId, EnvironmentKeys.ProgressBar.ProgressColor, style.ProgressColor); + result.StyleId = styleId; + return result; + } + + public static Slider Apply(this Slider slider) where T : SliderStyle, new() + { + string styleId = GetStyleId(); + T style = new T(); + var result = Apply(slider, styleId, style); + result.SetEnvironment(styleId, EnvironmentKeys.Slider.TrackColor, style.TrackColor); + result.SetEnvironment(styleId, EnvironmentKeys.Slider.ProgressColor, style.ProgressColor); + result.SetEnvironment(styleId, EnvironmentKeys.Slider.ThumbColor, style.ThumbColor); + result.StyleId = styleId; + return result; + } } } From 65a36260de1a49f4ba7bc754e170be4d0b86417a Mon Sep 17 00:00:00 2001 From: Jesse Date: Fri, 5 May 2023 10:54:05 +1000 Subject: [PATCH 2/2] fix build --- src/Comet/Styles/StyleExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Comet/Styles/StyleExtensions.cs b/src/Comet/Styles/StyleExtensions.cs index 60a0f0eac..05681e5b8 100644 --- a/src/Comet/Styles/StyleExtensions.cs +++ b/src/Comet/Styles/StyleExtensions.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; using System.Text; - +using System.Diagnostics; namespace Comet { public static class StyleExtensions