Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;

namespace Maui.Controls.Sample
{
Expand Down
113 changes: 59 additions & 54 deletions src/Core/src/Platform/Windows/ButtonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,55 @@ public static void UpdateStrokeColor(this Button platformButton, IButtonStroke b
var brush = buttonStroke.StrokeColor?.ToPlatform();

if (brush is null)
{
platformButton.Resources.Remove("ButtonBorderBrush");
platformButton.Resources.Remove("ButtonBorderBrushPointerOver");
platformButton.Resources.Remove("ButtonBorderBrushPressed");
platformButton.Resources.Remove("ButtonBorderBrushDisabled");

platformButton.ClearValue(Button.BorderBrushProperty);
}
platformButton.Resources.RemoveKeys(StrokeColorResourceKeys);
else
{
platformButton.Resources["ButtonBorderBrush"] = brush;
platformButton.Resources["ButtonBorderBrushPointerOver"] = brush;
platformButton.Resources["ButtonBorderBrushPressed"] = brush;
platformButton.Resources["ButtonBorderBrushDisabled"] = brush;
platformButton.Resources.SetValueForAllKey(StrokeColorResourceKeys, brush);

platformButton.BorderBrush = brush;
}
platformButton.RefreshThemeResources();
}

static readonly string[] StrokeColorResourceKeys =
{
"ButtonBorderBrush",
"ButtonBorderBrushPointerOver",
"ButtonBorderBrushPressed",
"ButtonBorderBrushDisabled",
};

public static void UpdateStrokeThickness(this Button platformButton, IButtonStroke buttonStroke)
{
if (buttonStroke.StrokeThickness >= 0)
{
platformButton.Resources["ButtonBorderThemeThickness"] = WinUIHelpers.CreateThickness(buttonStroke.StrokeThickness);
}
var thickness = buttonStroke.StrokeThickness;

if (thickness >= 0)
platformButton.Resources.SetValueForAllKey(StrokeThicknessResourceKeys, WinUIHelpers.CreateThickness(buttonStroke.StrokeThickness));
else
{
platformButton.Resources.Remove("ButtonBorderThemeThickness");
}
platformButton.Resources.RemoveKeys(StrokeThicknessResourceKeys);

platformButton.RefreshThemeResources();
}

static readonly string[] StrokeThicknessResourceKeys =
{
"ButtonBorderThemeThickness",
};

public static void UpdateCornerRadius(this Button platformButton, IButtonStroke buttonStroke)
{
if (buttonStroke.CornerRadius >= 0)
{
platformButton.Resources["ControlCornerRadius"] = WinUIHelpers.CreateCornerRadius(buttonStroke.CornerRadius);
}
var radius = buttonStroke.CornerRadius;

if (radius >= 0)
platformButton.Resources.SetValueForAllKey(CornerRadiusResourceKeys, WinUIHelpers.CreateCornerRadius(buttonStroke.CornerRadius));
else
{
platformButton.Resources.Remove("ControlCornerRadius");
}
platformButton.Resources.RemoveKeys(CornerRadiusResourceKeys);

platformButton.RefreshThemeResources();
}

static readonly string[] CornerRadiusResourceKeys =
{
"ControlCornerRadius",
};

public static void UpdateText(this Button platformButton, IText text)
{
platformButton.UpdateText(text.Text);
Expand All @@ -80,25 +86,21 @@ public static void UpdateBackground(this Button platformButton, IButton button)
var brush = button.Background?.ToPlatform();

if (brush is null)
{
platformButton.Resources.Remove("ButtonBackground");
platformButton.Resources.Remove("ButtonBackgroundPointerOver");
platformButton.Resources.Remove("ButtonBackgroundPressed");
platformButton.Resources.Remove("ButtonBackgroundDisabled");

platformButton.ClearValue(Button.BackgroundProperty);
}
platformButton.Resources.RemoveKeys(BackgroundResourceKeys);
else
{
platformButton.Resources["ButtonBackground"] = brush;
platformButton.Resources["ButtonBackgroundPointerOver"] = brush;
platformButton.Resources["ButtonBackgroundPressed"] = brush;
platformButton.Resources["ButtonBackgroundDisabled"] = brush;
platformButton.Resources.SetValueForAllKey(BackgroundResourceKeys, brush);

platformButton.Background = brush;
}
platformButton.RefreshThemeResources();
}

static readonly string[] BackgroundResourceKeys =
{
"ButtonBackground",
"ButtonBackgroundPointerOver",
"ButtonBackgroundPressed",
"ButtonBackgroundDisabled",
};

public static void UpdateTextColor(this ButtonBase platformButton, ITextStyle button)
{
UpdateTextColor(platformButton, button.TextColor);
Expand All @@ -107,30 +109,33 @@ public static void UpdateTextColor(this ButtonBase platformButton, ITextStyle bu
public static void UpdateTextColor(this ButtonBase platformButton, Color textColor)
{
var brush = textColor?.ToPlatform();

if (brush is null)
{
// Windows.Foundation.UniversalApiContract < 5
platformButton.Resources.Remove("ButtonForeground");
platformButton.Resources.Remove("ButtonForegroundPointerOver");
platformButton.Resources.Remove("ButtonForegroundPressed");
platformButton.Resources.Remove("ButtonForegroundDisabled");

platformButton.Resources.RemoveKeys(TextColorResourceKeys);
// Windows.Foundation.UniversalApiContract >= 5
platformButton.ClearValue(Button.ForegroundProperty);
}
else
{
// Windows.Foundation.UniversalApiContract < 5
platformButton.Resources["ButtonForeground"] = brush;
platformButton.Resources["ButtonForegroundPointerOver"] = brush;
platformButton.Resources["ButtonForegroundPressed"] = brush;
platformButton.Resources["ButtonForegroundDisabled"] = brush;

platformButton.Resources.SetValueForAllKey(TextColorResourceKeys, brush);
// Windows.Foundation.UniversalApiContract >= 5
platformButton.Foreground = brush;
}

platformButton.RefreshThemeResources();
}

static readonly string[] TextColorResourceKeys =
{
"ButtonForeground",
"ButtonForegroundPointerOver",
"ButtonForegroundPressed",
"ButtonForegroundDisabled",
};

public static void UpdatePadding(this Button platformButton, IPadding padding) =>
platformButton.UpdatePadding(padding, platformButton.GetResource<UI.Xaml.Thickness>("ButtonPadding"));

Expand Down
14 changes: 14 additions & 0 deletions src/Core/src/Platform/Windows/FrameworkElementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,19 @@ internal static void SetApplicationResource(this FrameworkElement frameworkEleme
.ToPlatform()
.GetLocationRelativeTo(relativeTo.ToPlatform());
}

internal static void RefreshThemeResources(this FrameworkElement nativeView)
{
var previous = nativeView.RequestedTheme;

// Workaround for https://github.com/dotnet/maui/issues/7820
nativeView.RequestedTheme = nativeView.ActualTheme switch
{
ElementTheme.Dark => ElementTheme.Light,
_ => ElementTheme.Dark
};

nativeView.RequestedTheme = previous;
}
Comment on lines +328 to +340
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is the real "fix" here and is basically a workaround with a still-workaround-but-not-totally fix coming in the core Windows App SDK.

}
}
22 changes: 8 additions & 14 deletions src/Core/src/Platform/Windows/SliderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ public static void UpdateMinimumTrackColor(this Slider platformSlider, ISlider s
{
var brush = slider.MinimumTrackColor?.ToPlatform();

if (brush == null)
{
if (brush is null)
platformSlider.Resources.RemoveKeys(MinimumTrackColorResourceKeys);
}
else
{
platformSlider.Resources.SetValueForAllKey(MinimumTrackColorResourceKeys, brush);
}

platformSlider.RefreshThemeResources();
}

static readonly string[] MinimumTrackColorResourceKeys =
Expand All @@ -59,13 +57,11 @@ public static void UpdateMaximumTrackColor(this Slider platformSlider, ISlider s
var brush = slider.MaximumTrackColor?.ToPlatform();

if (brush == null)
{
platformSlider.Resources.RemoveKeys(MaximumTrackColorResourceKeys);
}
else
{
platformSlider.Resources.SetValueForAllKey(MaximumTrackColorResourceKeys, brush);
}

platformSlider.RefreshThemeResources();
}

static readonly string[] MaximumTrackColorResourceKeys =
Expand All @@ -80,14 +76,12 @@ public static void UpdateThumbColor(this Slider platformSlider, ISlider slider)
{
var brush = slider.ThumbColor?.ToPlatform();

if (brush == null)
{
if (brush is null)
platformSlider.Resources.RemoveKeys(ThumbColorResourceKeys);
}
else
{
platformSlider.Resources.SetValueForAllKey(ThumbColorResourceKeys, brush);
}

platformSlider.RefreshThemeResources();
}

static readonly string[] ThumbColorResourceKeys =
Expand Down
73 changes: 36 additions & 37 deletions src/Core/src/Platform/Windows/TextBoxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,43 @@ public static void UpdateText(this TextBox platformControl, ITextInput textInput
public static void UpdateBackground(this TextBox textBox, IView view)
{
var brush = view.Background?.ToPlatform();

if (brush is null)
{
textBox.Resources.Remove("TextControlBackground");
textBox.Resources.Remove("TextControlBackgroundPointerOver");
textBox.Resources.Remove("TextControlBackgroundFocused");
textBox.Resources.Remove("TextControlBackgroundDisabled");
}
textBox.Resources.RemoveKeys(BackgroundResourceKeys);
else
{
textBox.Resources["TextControlBackground"] = brush;
textBox.Resources["TextControlBackgroundPointerOver"] = brush;
textBox.Resources["TextControlBackgroundFocused"] = brush;
textBox.Resources["TextControlBackgroundDisabled"] = brush;
}
textBox.Resources.SetValueForAllKey(BackgroundResourceKeys, brush);

textBox.RefreshThemeResources();
}

static readonly string[] BackgroundResourceKeys =
{
"TextControlBackground",
"TextControlBackgroundPointerOver",
"TextControlBackgroundFocused",
"TextControlBackgroundDisabled",
};

public static void UpdateTextColor(this TextBox textBox, ITextStyle textStyle)
{
var brush = textStyle.TextColor?.ToPlatform();

if (brush is null)
{
textBox.Resources.Remove("TextControlForeground");
textBox.Resources.Remove("TextControlForegroundPointerOver");
textBox.Resources.Remove("TextControlForegroundFocused");
textBox.Resources.Remove("TextControlForegroundDisabled");

textBox.ClearValue(TextBox.ForegroundProperty);
}
textBox.Resources.RemoveKeys(TextColorResourceKeys);
else
{
textBox.Resources["TextControlForeground"] = brush;
textBox.Resources["TextControlForegroundPointerOver"] = brush;
textBox.Resources["TextControlForegroundFocused"] = brush;
textBox.Resources["TextControlForegroundDisabled"] = brush;
textBox.Resources.SetValueForAllKey(TextColorResourceKeys, brush);

textBox.Foreground = brush;
}
textBox.RefreshThemeResources();
}

static readonly string[] TextColorResourceKeys =
{
"TextControlForeground",
"TextControlForegroundPointerOver",
"TextControlForegroundFocused",
"TextControlForegroundDisabled",
};

public static void UpdateCharacterSpacing(this TextBox textBox, ITextStyle textStyle)
{
textBox.CharacterSpacing = textStyle.CharacterSpacing.ToEm();
Expand All @@ -95,27 +92,29 @@ public static void UpdatePlaceholderColor(this TextBox textBox, IPlaceholder pla
if (brush is null)
{
// Windows.Foundation.UniversalApiContract < 5
textBox.Resources.Remove("TextControlPlaceholderForeground");
textBox.Resources.Remove("TextControlPlaceholderForegroundPointerOver");
textBox.Resources.Remove("TextControlPlaceholderForegroundFocused");
textBox.Resources.Remove("TextControlPlaceholderForegroundDisabled");

textBox.Resources.RemoveKeys(PlaceholderColorResourceKeys);
// Windows.Foundation.UniversalApiContract >= 5
textBox.ClearValue(TextBox.PlaceholderForegroundProperty);
}
else
{
// Windows.Foundation.UniversalApiContract < 5
textBox.Resources["TextControlPlaceholderForeground"] = brush;
textBox.Resources["TextControlPlaceholderForegroundPointerOver"] = brush;
textBox.Resources["TextControlPlaceholderForegroundFocused"] = brush;
textBox.Resources["TextControlPlaceholderForegroundDisabled"] = brush;

textBox.Resources.SetValueForAllKey(PlaceholderColorResourceKeys, brush);
// Windows.Foundation.UniversalApiContract >= 5
textBox.PlaceholderForeground = brush;
}

textBox.RefreshThemeResources();
}

static readonly string[] PlaceholderColorResourceKeys =
{
"TextControlPlaceholderForeground",
"TextControlPlaceholderForegroundPointerOver",
"TextControlPlaceholderForegroundFocused",
"TextControlPlaceholderForegroundDisabled",
};

public static void UpdateFont(this TextBox platformControl, IText text, IFontManager fontManager) =>
platformControl.UpdateFont(text.Font, fontManager);

Expand Down
Loading