Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new apply style feature #284

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/Comet/EnvironmentAware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -232,7 +232,7 @@ public static T SetEnvironment<T>(this T contextualObject, Type type, string key
return contextualObject;
}

public static T SetEnvironment<T,TValue>(this T view, Type type, string key, Binding<TValue> binding, bool cascades = true, ControlState state = ControlState.Default)
public static T SetEnvironment<T, TValue>(this T view, Type type, string key, Binding<TValue> binding, bool cascades = true, ControlState state = ControlState.Default)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future lets try and avoid white noise changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be better to have some tools to format code in current code base.

where T : View
{
binding.BindToProperty(view, key);
Expand Down Expand Up @@ -270,7 +270,23 @@ public static T SetEnvironment<T>(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<T>(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<T>(this T contextualObject, IDictionary<string, object> data, bool cascades = true) where T : ContextualObject
//{
// foreach (var pair in data)
Expand All @@ -286,7 +302,7 @@ public static T SetEnvironment<T>(this T contextualObject, string key, object va

public static T GetEnvironment<T>(this View view, string key, ControlState state, bool cascades = true)
{
key = ContextualObject.GetControlStateKey(state,key);
key = ContextualObject.GetControlStateKey(state, key);
return view.GetEnvironment<T>(key, cascades);
}
public static T GetEnvironment<T>(this View view, string key, bool cascades = true)
Expand All @@ -302,7 +318,7 @@ public static T GetEnvironment<T>(this View view, Type type, string key, Control

public static T GetEnvironment<T>(this View view, Type type, string key, bool cascades = true)
=> view.GetValue<T>(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);
Expand Down
16 changes: 10 additions & 6 deletions src/Comet/EnvironmentData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this being added to Button? The Padding stuff already exists on layout, and should work for any view.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is MAUI button Padding property, I think it should be different from the padding of layout?

}

public static class Shape
{
public const string LineWidth = "Shape.LineWidth";
Expand Down Expand Up @@ -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";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are the values changing?

Copy link
Contributor Author

@jessejiang0214 jessejiang0214 May 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cause the MAUI API changed, you know the Comet.SourceGenerator project, the Slider class output file is this.

Microsoft.Maui.Graphics.Color Microsoft.Maui.ISlider.MinimumTrackColor => this.GetEnvironment<Microsoft.Maui.Graphics.Color>("MinimumTrackColor") ?? default;

Microsoft.Maui.Graphics.Color Microsoft.Maui.ISlider.MaximumTrackColor => this.GetEnvironment<Microsoft.Maui.Graphics.Color>("MaximumTrackColor") ?? default;

Microsoft.Maui.Graphics.Color Microsoft.Maui.ISlider.ThumbColor => this.GetEnvironment<Microsoft.Maui.Graphics.Color>("ThumbColor") ?? default;

Microsoft.Maui.IImageSource Microsoft.Maui.ISlider.ThumbImageSource => this.GetEnvironment<Microsoft.Maui.IImageSource>("ThumbImageSource") ?? default;

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";
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Comet/Styles/ButtonStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Comet.Styles
{
public class ButtonStyle : ViewStyle
{
public StyleAwareValue<ControlState,Color> TextColor { get; set; }
public StyleAwareValue<ControlState, Color> TextColor { get; set; }

public StyleAwareValue<ControlState, Font> TextFont { get; set; }

Expand Down
1 change: 1 addition & 0 deletions src/Comet/Styles/ControlState.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
namespace Comet
{
//TODO need to implement
public enum ControlState
{
Default,
Expand Down
1 change: 0 additions & 1 deletion src/Comet/Styles/Material/MaterialStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public MaterialStyle(ColorPalette colorPalette)
ProgressBar = new ProgressBarStyle
{
ProgressColor = colorPalette.P500,
TrackColor = colorPalette.P100,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this being deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAUI API changed, there's no TrackColor property in Progress class

};
}

Expand Down
1 change: 0 additions & 1 deletion src/Comet/Styles/ProgressBarStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Comet.Styles
{
public class ProgressBarStyle : ViewStyle
{
public StyleAwareValue<ControlState, Color> TrackColor { get; set; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this being deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAUI API changed, there's no TrackColor property in Progress class

public StyleAwareValue<ControlState, Color> ProgressColor { get; set; }
}
}
1 change: 0 additions & 1 deletion src/Comet/Styles/Style.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ protected virtual void ApplySliderStyle(ContextualObject view)

protected virtual void ApplyProgresBarStyle(ContextualObject view)
{
SetEnvironment(view, "", EnvironmentKeys.ProgressBar.TrackColor, ProgressBar?.TrackColor);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this being deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same with above

SetEnvironment(view, "", EnvironmentKeys.ProgressBar.ProgressColor, ProgressBar?.ProgressColor);
ApplyViewStyles(view, ProgressBar, typeof(ProgressBar));

Expand Down
63 changes: 61 additions & 2 deletions src/Comet/Styles/StyleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
using System;
using System.Collections.Generic;
using System.Text;

using System.Diagnostics;
namespace Comet
{
public static class StyleExtensions
{
public static T StyleId<T> (this T view, string styleId) where T : View
public static T StyleId<T>(this T view, string styleId) where T : View
{
view.StyleId = styleId;
return view;
Expand Down Expand Up @@ -89,5 +89,64 @@ public static T ApplyStyle<T>(this T view, Style style) where T : ContextualObje
style.Apply(view);
return view;
}

private static T Apply<T>(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<T>() 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<T>(this View view) where T : ViewStyle, new()
{
string styleId = GetStyleId<T>();
var result = Apply(view, styleId, new T());
result.StyleId = styleId;
return result;
}

public static Button Apply<T>(this Button button) where T : ButtonStyle, new()
{
string styleId = GetStyleId<T>();
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<T>(this ProgressBar progressBar) where T : ProgressBarStyle, new()
{
string styleId = GetStyleId<T>();
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<T>(this Slider slider) where T : SliderStyle, new()
{
string styleId = GetStyleId<T>();
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;
}
}
}