This repository was archived by the owner on Jul 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
This repository was archived by the owner on Jul 11, 2025. It is now read-only.
Styling doesn't apply based on ControlState #263
Copy link
Copy link
Open
Description
Styles with ControlState don't seem to actually apply. For example:
public class AppStyle : Style
{
public AppStyle()
{
Button = new ButtonStyle
{
BackgroundColor = new StyleAwareValue<ControlState, Color>
{
[ControlState.Disabled] = Color.Grey,
[ControlState.Default] = Color.Green,
[ControlState.Hovered] = Colors.Red,
[ControlState.Pressed] = Color.Black,
}
};
}
}
// Then using it:
View body() =>
new VStack
{
new Button("Enabled"),
new Button("Disabled")
.Enabled(false),
}.ApplyStyle(new AppStyle());The second button should be grey. The first button should be red/black when hovered/pressed. The source generators are just getting the default value and doesn't seem like they apply the control state:
// Button.g.cs
Microsoft.Maui.Graphics.Color Microsoft.Maui.ITextStyle.TextColor => this.GetEnvironment<Microsoft.Maui.Graphics.Color>("Color") ?? default;I can look at helping PR a change to implement this but would need some guidance on how to implement it. What I am thinking:
- CometGenerator attribute needs a list of states supported by each control. I believe by default every control supports at least
Disabled,Default, andHoveredstates butITextButtonneeds to specify it also supportsPressed. ITextButtonwould need to generate abool IsPressedwhich is updated based onPressed/Releasedactions. Not sure how to best model this in theCometGenerateAttribute. Is this a one off case? Is it better to omitPressed/Releasedfrom being generated and create aButton.cswith this logic?- Source generator would emit a switch statement to fetch the correct value based on state. This could be done in a
VisualStateproperty. For example, if theITextButtonhas states[ Pressed ](default/hovered/disabled don't need to be specified) then the following switch would be created:
Microsoft.Maui.Graphics.Color Microsoft.Maui.ITextStyle.TextColor => this.GetEnvironment<Microsoft.Maui.Graphics.Color>("Color", VisualState) ?? default;
ControlState VisualState {
get {
// not sure if order matters here
if (IsPressed)
{
return ControlState.IsPressed;
}
if (IsFocused)
{
return ControlState.Hovered;
}
if (IsEnabled)
{
return ControlState.Default;
}
return ControlState.Disabled;
}
}Another option would be to add the switch inside of the GetEnvironment call. This would support the IsFocused/IsEnabled states but not sure how that would support IsPressed
saint4evasaint4eva
Metadata
Metadata
Assignees
Labels
No labels