Skip to content

Commit 9a37c20

Browse files
authored
Code Quality: Replace ThemeHelper with AppThemeModeService (#15150)
1 parent 98d5e9f commit 9a37c20

38 files changed

+239
-196
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
using Microsoft.UI.Windowing;
5+
using Microsoft.UI.Xaml;
6+
7+
namespace Files.App.Data.Contracts
8+
{
9+
public interface IAppThemeModeService
10+
{
11+
/// <summary>
12+
/// Gets invoked when application theme mode is changed.
13+
/// </summary>
14+
public event EventHandler? AppThemeModeChanged;
15+
16+
/// <summary>
17+
/// Gets or sets application theme mode.
18+
/// </summary>
19+
public ElementTheme AppThemeMode { get; set; }
20+
21+
/// <summary>
22+
/// Refreshes the application theme mode only for the main window.
23+
/// </summary>
24+
/// <remarks>
25+
/// This is a workaround for <a href="https://github.com/microsoft/microsoft-ui-xaml/issues/4651">a WinUI bug</a>.
26+
/// </remarks>
27+
public void ApplyResources();
28+
29+
/// <summary>
30+
/// Sets application theme mode to the main window or a specific window.
31+
/// </summary>
32+
/// <param name="window">A window instance to set application theme mode.</param>
33+
/// <param name="titleBar">A window's title bar instance to adjust contrast of action buttons.</param>
34+
/// <param name="rootTheme">A theme mode to set.</param>
35+
/// <param name="callThemeModeChangedEvent">Whether the method should notify the theme mode changed. If this called for a specific window, doesn't have to call.</param>
36+
public void SetAppThemeMode(Window? window = null, AppWindowTitleBar? titleBar = null, ElementTheme? rootTheme = null, bool callThemeModeChangedEvent = true);
37+
}
38+
}

src/Files.App/Dialogs/AddBranchDialog.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
IsPrimaryButtonEnabled="{x:Bind ViewModel.IsBranchValid, Mode=OneWay}"
1616
PrimaryButtonStyle="{StaticResource AccentButtonStyle}"
1717
PrimaryButtonText="{helpers:ResourceString Name=Create}"
18-
RequestedTheme="{x:Bind helpers:ThemeHelper.RootTheme}"
18+
RequestedTheme="{x:Bind RootAppElement.RequestedTheme, Mode=OneWay}"
1919
SecondaryButtonText="{helpers:ResourceString Name=Cancel}"
2020
Style="{StaticResource DefaultContentDialogStyle}"
2121
mc:Ignorable="d">

src/Files.App/Dialogs/AddBranchDialog.xaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
// Licensed under the MIT License. See the LICENSE.
33

44
using Files.App.ViewModels.Dialogs;
5+
using Microsoft.UI.Xaml;
56
using Microsoft.UI.Xaml.Controls;
67

78
namespace Files.App.Dialogs
89
{
910
public sealed partial class AddBranchDialog : ContentDialog, IDialog<AddBranchDialogViewModel>
1011
{
12+
private FrameworkElement RootAppElement
13+
=> (FrameworkElement)MainWindow.Instance.Content;
14+
1115
public AddBranchDialogViewModel ViewModel
1216
{
1317
get => (AddBranchDialogViewModel)DataContext;

src/Files.App/Dialogs/AddItemDialog.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
CornerRadius="{StaticResource OverlayCornerRadius}"
1616
HighContrastAdjustment="None"
1717
Loaded="AddItemDialog_Loaded"
18-
RequestedTheme="{x:Bind helpers:ThemeHelper.RootTheme}"
18+
RequestedTheme="{x:Bind RootAppElement.RequestedTheme, Mode=OneWay}"
1919
SecondaryButtonText="{helpers:ResourceString Name=Cancel}"
2020
Style="{StaticResource DefaultContentDialogStyle}"
2121
mc:Ignorable="d">

src/Files.App/Dialogs/AddItemDialog.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public sealed partial class AddItemDialog : ContentDialog, IDialog<AddItemDialog
1212
{
1313
private readonly IAddItemService addItemService = Ioc.Default.GetRequiredService<IAddItemService>();
1414

15+
private FrameworkElement RootAppElement
16+
=> (FrameworkElement)MainWindow.Instance.Content;
17+
1518
public AddItemDialogViewModel ViewModel
1619
{
1720
get => (AddItemDialogViewModel)DataContext;

src/Files.App/Dialogs/CreateArchiveDialog.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
IsPrimaryButtonEnabled="{x:Bind ViewModel.IsNameValid, Mode=OneWay}"
1919
Loaded="ContentDialog_Loaded"
2020
PrimaryButtonText="{helpers:ResourceString Name=Create}"
21-
RequestedTheme="{x:Bind helpers:ThemeHelper.RootTheme}"
21+
RequestedTheme="{x:Bind RootAppElement.RequestedTheme, Mode=OneWay}"
2222
Style="{StaticResource DefaultContentDialogStyle}"
2323
mc:Ignorable="d">
2424

src/Files.App/Dialogs/CreateArchiveDialog.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ namespace Files.App.Dialogs
1010
{
1111
public sealed partial class CreateArchiveDialog : ContentDialog
1212
{
13+
private FrameworkElement RootAppElement
14+
=> (FrameworkElement)MainWindow.Instance.Content;
15+
1316
private bool canCreate = false;
1417
public bool CanCreate => canCreate;
1518

src/Files.App/Dialogs/CreateShortcutDialog.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
IsPrimaryButtonEnabled="{x:Bind ViewModel.IsLocationValid, Mode=OneWay}"
1313
PrimaryButtonCommand="{x:Bind ViewModel.PrimaryButtonCommand}"
1414
PrimaryButtonText="{helpers:ResourceString Name=Create}"
15-
RequestedTheme="{x:Bind helpers:ThemeHelper.RootTheme}"
15+
RequestedTheme="{x:Bind RootAppElement.RequestedTheme, Mode=OneWay}"
1616
SecondaryButtonText="{helpers:ResourceString Name=Cancel}"
1717
Style="{StaticResource DefaultContentDialogStyle}"
1818
mc:Ignorable="d">

src/Files.App/Dialogs/CreateShortcutDialog.xaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33

44
using Files.App.ViewModels.Dialogs;
55
using Files.App.ViewModels.Dialogs;
6+
using Microsoft.UI.Xaml;
67
using Microsoft.UI.Xaml.Controls;
78
using Microsoft.UI.Xaml.Data;
89

910
namespace Files.App.Dialogs
1011
{
1112
public sealed partial class CreateShortcutDialog : ContentDialog, IDialog<CreateShortcutDialogViewModel>
1213
{
14+
private FrameworkElement RootAppElement
15+
=> (FrameworkElement)MainWindow.Instance.Content;
16+
1317
public CreateShortcutDialogViewModel ViewModel
1418
{
1519
get => (CreateShortcutDialogViewModel)DataContext;

src/Files.App/Dialogs/CredentialDialog.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
1414
PrimaryButtonStyle="{StaticResource AccentButtonStyle}"
1515
PrimaryButtonText="{helpers:ResourceString Name=OK}"
16-
RequestedTheme="{x:Bind helpers:ThemeHelper.RootTheme}"
16+
RequestedTheme="{x:Bind RootAppElement.RequestedTheme, Mode=OneWay}"
1717
SecondaryButtonText="{helpers:ResourceString Name=Cancel}"
1818
Style="{StaticResource DefaultContentDialogStyle}"
1919
mc:Ignorable="d">

0 commit comments

Comments
 (0)