Skip to content

Commit be3288b

Browse files
authored
Codebase: Refactored themes (#11031)
1 parent 81d9e3d commit be3288b

File tree

9 files changed

+106
-102
lines changed

9 files changed

+106
-102
lines changed

src/Files.App/Helpers/AppThemeResourcesHelper.cs

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1+
using CommunityToolkit.Mvvm.DependencyInjection;
2+
using CommunityToolkit.WinUI.Helpers;
3+
using Files.Backend.Services.Settings;
14
using Microsoft.UI.Xaml;
5+
using System;
26
using Windows.UI;
37

48
namespace Files.App.Helpers
59
{
610
public sealed class AppThemeResourcesHelper
711
{
12+
public IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
13+
814
/// <summary>
9-
/// Forces the application to use the correct resource styles
15+
/// Applies updated resource styles
1016
/// </summary>
1117
public void ApplyResources()
1218
{
1319
// Get the index of the current theme
1420
var selTheme = ThemeHelper.RootTheme;
1521

16-
// Toggle between the themes to force the controls to use the new resource styles
22+
// Toggle between the themes to force reload the resource styles
1723
ThemeHelper.RootTheme = ElementTheme.Dark;
1824
ThemeHelper.RootTheme = ElementTheme.Light;
1925

@@ -75,16 +81,47 @@ public void SetAppThemeFontFamily(string contentControlThemeFontFamily)
7581
/// <param name="useCompactSpacing"></param>
7682
public void SetCompactSpacing(bool useCompactSpacing)
7783
{
78-
if (useCompactSpacing)
79-
{
80-
Application.Current.Resources["ListItemHeight"] = 24;
81-
Application.Current.Resources["NavigationViewItemOnLeftMinHeight"] = 20;
82-
}
84+
var listItemHeight = useCompactSpacing ? 24 : 36;
85+
var navigationViewItemOnLeftMinHeight = useCompactSpacing ? 20 : 32;
86+
87+
Application.Current.Resources["ListItemHeight"] = listItemHeight;
88+
Application.Current.Resources["NavigationViewItemOnLeftMinHeight"] = navigationViewItemOnLeftMinHeight;
89+
}
90+
91+
/// <summary>
92+
/// Loads the resource styles from settings
93+
/// </summary>
94+
public void LoadAppResources()
95+
{
96+
var useCompactStyles = UserSettingsService.AppearanceSettingsService.UseCompactStyles;
97+
var appThemeBackgroundColor = ColorHelper.ToColor(UserSettingsService.AppearanceSettingsService.AppThemeBackgroundColor);
98+
var appThemeAddressBarBackgroundColor = UserSettingsService.AppearanceSettingsService.AppThemeAddressBarBackgroundColor;
99+
var appThemeSidebarBackgroundColor = UserSettingsService.AppearanceSettingsService.AppThemeSidebarBackgroundColor;
100+
var appThemeFileAreaBackgroundColor = UserSettingsService.AppearanceSettingsService.AppThemeFileAreaBackgroundColor;
101+
var appThemeFontFamily = UserSettingsService.AppearanceSettingsService.AppThemeFontFamily;
102+
103+
SetCompactSpacing(useCompactStyles);
104+
SetAppThemeBackgroundColor(appThemeBackgroundColor);
105+
106+
if (!String.IsNullOrWhiteSpace(appThemeAddressBarBackgroundColor) && appThemeAddressBarBackgroundColor != "#00000000")
107+
SetAppThemeAddressBarBackgroundColor(ColorHelper.ToColor(appThemeAddressBarBackgroundColor));
108+
else
109+
UserSettingsService.AppearanceSettingsService.AppThemeAddressBarBackgroundColor = ""; //migrate to new default
110+
111+
if (!String.IsNullOrWhiteSpace(appThemeSidebarBackgroundColor) && appThemeAddressBarBackgroundColor != "#00000000")
112+
SetAppThemeSidebarBackgroundColor(ColorHelper.ToColor(appThemeSidebarBackgroundColor));
83113
else
84-
{
85-
Application.Current.Resources["ListItemHeight"] = 36;
86-
Application.Current.Resources["NavigationViewItemOnLeftMinHeight"] = 32;
87-
}
114+
UserSettingsService.AppearanceSettingsService.AppThemeSidebarBackgroundColor = ""; //migrate to new default
115+
116+
if (!String.IsNullOrWhiteSpace(appThemeFileAreaBackgroundColor) && appThemeAddressBarBackgroundColor != "#00000000")
117+
SetAppThemeFileAreaBackgroundColor(ColorHelper.ToColor(appThemeFileAreaBackgroundColor));
118+
else
119+
UserSettingsService.AppearanceSettingsService.AppThemeFileAreaBackgroundColor = ""; //migrate to new default
120+
121+
if (appThemeFontFamily != "Segoe UI Variable")
122+
SetAppThemeFontFamily(appThemeFontFamily);
123+
124+
ApplyResources();
88125
}
89126
}
90127
}

src/Files.App/ValueConverters/ColorToSolidColorBrushValueConverter.cs renamed to src/Files.App/ValueConverters/StringToSolidColorBrushValueConverter.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
using Microsoft.UI.Xaml.Data;
1+
using CommunityToolkit.WinUI.Helpers;
2+
using Microsoft.UI.Xaml.Data;
23
using Microsoft.UI.Xaml.Media;
34
using System;
45
using Windows.UI;
56

67
namespace Files.App.ValueConverters
78
{
8-
public class ColorToSolidColorBrushValueConverter : IValueConverter
9+
public class StringToSolidColorBrushValueConverter : IValueConverter
910
{
1011
public object? Convert(object value, Type targetType, object parameter, string language)
1112
{
1213
if (null == value)
1314
return null;
1415

15-
if (value is Color)
16+
if (value is string colorString)
1617
{
17-
Color color = (Color)value;
18+
Color color = ColorHelper.ToColor(colorString);
1819
return new SolidColorBrush(color);
1920
}
2021

src/Files.App/ViewModels/MainPageViewModel.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,10 @@ public bool IsWindowCompactOverlay
5050
}
5151

5252
public ICommand NavigateToNumberedTabKeyboardAcceleratorCommand { get; private set; }
53-
5453
public IAsyncRelayCommand OpenNewWindowAcceleratorCommand { get; private set; }
55-
5654
public ICommand CloseSelectedTabKeyboardAcceleratorCommand { get; private set; }
57-
5855
public IAsyncRelayCommand AddNewInstanceAcceleratorCommand { get; private set; }
59-
6056
public ICommand ReopenClosedTabAcceleratorCommand { get; private set; }
61-
6257
public ICommand OpenSettingsCommand { get; private set; }
6358

6459
public MainPageViewModel()
@@ -408,6 +403,10 @@ public async void OnNavigatedTo(NavigationEventArgs e)
408403
else if (e.Parameter is TabItemArguments tabArgs)
409404
await AddNewTabByParam(tabArgs.InitialPageType, tabArgs.NavigationArg);
410405
}
406+
407+
408+
// Load the app theme resources
409+
App.AppThemeResourcesHelper.LoadAppResources();
411410
}
412411

413412
public static Task AddNewTabAsync()

src/Files.App/ViewModels/SettingsViewModels/AppearanceViewModel.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
using Files.App.Views.SettingsPages.Appearance;
77
using Files.Backend.Services.Settings;
88
using Microsoft.UI.Xaml;
9-
using Microsoft.UI.Xaml.Media;
109
using System;
1110
using System.Collections.Generic;
1211
using System.Collections.ObjectModel;
1312
using System.Linq;
14-
using Windows.UI;
1513

1614
namespace Files.App.ViewModels.SettingsViewModels
1715
{
@@ -33,30 +31,33 @@ public AppearanceViewModel()
3331
};
3432

3533
AppThemeResources = AppThemeResourceFactory.AppThemeResources;
36-
UpdateSelectedBackground();
34+
UpdateSelectedResource();
3735
}
3836

3937
/// <summary>
40-
/// Selects the AppThemeResource corresponding to the AppThemeBackgroundColor setting
38+
/// Selects the AppThemeResource corresponding to the current settings
4139
/// </summary>
42-
private void UpdateSelectedBackground()
40+
private void UpdateSelectedResource()
4341
{
44-
var backgroundColor = AppThemeBackgroundColor;
42+
var themeBackgroundColor = AppThemeBackgroundColor;
4543

4644
// Add color to the collection if it's not already there
47-
if (!AppThemeResources.Any(p => p.BackgroundColor == backgroundColor))
45+
if (!AppThemeResources.Any(p => p.BackgroundColor == themeBackgroundColor))
4846
{
47+
// Remove current value before adding a new one
48+
if (AppThemeResources.Last().Name == "Custom")
49+
AppThemeResources.Remove(AppThemeResources.Last());
50+
4951
var appThemeBackgroundColor = new AppThemeResource
5052
{
51-
BackgroundColor = backgroundColor,
53+
BackgroundColor = themeBackgroundColor,
5254
Name = "Custom"
5355
};
54-
5556
AppThemeResources.Add(appThemeBackgroundColor);
5657
}
5758

5859
SelectedAppThemeResources = AppThemeResources
59-
.Where(p => p.BackgroundColor == AppThemeBackgroundColor)
60+
.Where(p => p.BackgroundColor == themeBackgroundColor)
6061
.FirstOrDefault() ?? AppThemeResources[0];
6162
}
6263

@@ -116,6 +117,7 @@ public bool UseCompactStyles
116117
{
117118
UserSettingsService.AppearanceSettingsService.UseCompactStyles = value;
118119

120+
// Apply the updated compact spacing resource
119121
App.AppThemeResourcesHelper.SetCompactSpacing(UseCompactStyles);
120122
App.AppThemeResourcesHelper.ApplyResources();
121123

@@ -124,17 +126,20 @@ public bool UseCompactStyles
124126
}
125127
}
126128

127-
public Color AppThemeBackgroundColor
129+
public string AppThemeBackgroundColor
128130
{
129-
get => ColorHelper.ToColor(UserSettingsService.AppearanceSettingsService.AppThemeBackgroundColor);
131+
get => UserSettingsService.AppearanceSettingsService.AppThemeBackgroundColor;
130132
set
131133
{
132-
if (value != ColorHelper.ToColor(UserSettingsService.AppearanceSettingsService.AppThemeBackgroundColor))
134+
if (value != UserSettingsService.AppearanceSettingsService.AppThemeBackgroundColor)
133135
{
134-
UserSettingsService.AppearanceSettingsService.AppThemeBackgroundColor = value.ToString();
136+
UserSettingsService.AppearanceSettingsService.AppThemeBackgroundColor = value;
135137

136-
App.AppThemeResourcesHelper.SetAppThemeBackgroundColor(AppThemeBackgroundColor);
138+
// Apply the updated background resource
139+
App.AppThemeResourcesHelper.SetAppThemeBackgroundColor(ColorHelper.ToColor(value));
137140
App.AppThemeResourcesHelper.ApplyResources();
141+
142+
OnPropertyChanged();
138143
}
139144
}
140145
}

src/Files.App/Views/MainPage.xaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
1313
xmlns:usercontrols="using:Files.App.UserControls.MultitaskingControl"
1414
xmlns:viewmodels="using:Files.App.ViewModels"
15+
AllowDrop="True"
1516
Background="{ThemeResource App.Theme.BackgroundBrush}"
1617
KeyboardAcceleratorPlacementMode="Hidden"
1718
Loaded="Page_Loaded"
@@ -331,14 +332,14 @@
331332
ResizeBehavior="BasedOnAlignment"
332333
Style="{StaticResource DefaultGridSplitterStyle}" />
333334

334-
<controls:PreviewPane
335+
<controls:PreviewPane
335336
x:Name="PreviewPane"
336337
Grid.Row="1"
337338
Grid.Column="2"
338339
HorizontalContentAlignment="Stretch"
339340
x:Load="{x:Bind ShouldPreviewPaneBeActive, Mode=OneWay}" />
340-
341-
<controls:StatusBarControl
341+
342+
<controls:StatusBarControl
342343
x:Name="StatusBarControl"
343344
Grid.Row="4"
344345
Grid.ColumnSpan="3"

src/Files.App/Views/MainPage.xaml.cs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
using Windows.Graphics;
2828
using Windows.Services.Store;
2929
using Windows.Storage;
30-
using ColorHelper = CommunityToolkit.WinUI.Helpers.ColorHelper;
3130

3231
namespace Files.App.Views
3332
{
@@ -65,51 +64,11 @@ public MainPage()
6564
if (flowDirectionSetting == "RTL")
6665
FlowDirection = FlowDirection.RightToLeft;
6766

68-
AllowDrop = true;
69-
7067
ToggleFullScreenAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(ToggleFullScreenAccelerator);
7168
ToggleCompactOverlayCommand = new RelayCommand(ToggleCompactOverlay);
7269
SetCompactOverlayCommand = new RelayCommand<bool>(SetCompactOverlay);
7370

7471
UserSettingsService.OnSettingChangedEvent += UserSettingsService_OnSettingChangedEvent;
75-
76-
// Load the app theme resources
77-
LoadAppResources();
78-
}
79-
80-
81-
82-
private void LoadAppResources()
83-
{
84-
var useCompactStyles = UserSettingsService.AppearanceSettingsService.UseCompactStyles;
85-
var appThemeBackgroundColor = ColorHelper.ToColor(UserSettingsService.AppearanceSettingsService.AppThemeBackgroundColor);
86-
var appThemeAddressBarBackgroundColor = UserSettingsService.AppearanceSettingsService.AppThemeAddressBarBackgroundColor;
87-
var appThemeSidebarBackgroundColor = UserSettingsService.AppearanceSettingsService.AppThemeSidebarBackgroundColor;
88-
var appThemeFileAreaBackgroundColor = UserSettingsService.AppearanceSettingsService.AppThemeFileAreaBackgroundColor;
89-
var appThemeFontFamily = UserSettingsService.AppearanceSettingsService.AppThemeFontFamily;
90-
91-
App.AppThemeResourcesHelper.SetCompactSpacing(useCompactStyles);
92-
App.AppThemeResourcesHelper.SetAppThemeBackgroundColor(appThemeBackgroundColor);
93-
94-
if (!String.IsNullOrWhiteSpace(appThemeAddressBarBackgroundColor) && appThemeAddressBarBackgroundColor != "#00000000")
95-
App.AppThemeResourcesHelper.SetAppThemeAddressBarBackgroundColor(ColorHelper.ToColor(appThemeAddressBarBackgroundColor));
96-
else
97-
UserSettingsService.AppearanceSettingsService.AppThemeAddressBarBackgroundColor = ""; //migrate to new default
98-
99-
if (!String.IsNullOrWhiteSpace(appThemeSidebarBackgroundColor) && appThemeAddressBarBackgroundColor != "#00000000")
100-
App.AppThemeResourcesHelper.SetAppThemeSidebarBackgroundColor(ColorHelper.ToColor(appThemeSidebarBackgroundColor));
101-
else
102-
UserSettingsService.AppearanceSettingsService.AppThemeSidebarBackgroundColor = ""; //migrate to new default
103-
104-
if (!String.IsNullOrWhiteSpace(appThemeFileAreaBackgroundColor) && appThemeAddressBarBackgroundColor != "#00000000")
105-
App.AppThemeResourcesHelper.SetAppThemeFileAreaBackgroundColor(ColorHelper.ToColor(appThemeFileAreaBackgroundColor));
106-
else
107-
UserSettingsService.AppearanceSettingsService.AppThemeFileAreaBackgroundColor = ""; //migrate to new default
108-
109-
if (appThemeFontFamily != "Segoe UI Variable")
110-
App.AppThemeResourcesHelper.SetAppThemeFontFamily(appThemeFontFamily);
111-
112-
App.AppThemeResourcesHelper.ApplyResources();
11372
}
11473

11574
private async Task PromptForReview()

src/Files.App/Views/SettingsPages/Appearance.xaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
xmlns:converters1="using:Files.App.ValueConverters"
99
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
1010
xmlns:helpers="using:Files.App.Helpers"
11+
xmlns:i="using:Microsoft.Xaml.Interactivity"
12+
xmlns:icore="using:Microsoft.Xaml.Interactions.Core"
1113
xmlns:local="using:Files.App.UserControls.Settings"
1214
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
1315
xmlns:settingsviewmodels="using:Files.App.ViewModels.SettingsViewModels"
@@ -19,7 +21,7 @@
1921
</ResourceDictionary.MergedDictionaries>
2022

2123
<converters:BoolNegationConverter x:Key="BoolNegationConverter" />
22-
<converters1:ColorToSolidColorBrushValueConverter x:Key="ColorToSolidColorBrushConverter" />
24+
<converters1:StringToSolidColorBrushValueConverter x:Key="StringToSolidColorBrushConverter" />
2325

2426
<DataTemplate x:Key="AppThemeResourcesItemTemplate" x:DataType="appearance:AppThemeResource">
2527
<Grid
@@ -38,7 +40,7 @@
3840
<Border
3941
Grid.RowSpan="2"
4042
Height="66"
41-
Background="{x:Bind BackgroundColor, Converter={StaticResource ColorToSolidColorBrushConverter}, Mode=OneWay}"
43+
Background="{x:Bind BackgroundColor, Converter={StaticResource StringToSolidColorBrushConverter}, Mode=OneWay}"
4244
CornerRadius="4,4,0,0" />
4345

4446
<!-- Tab Bar -->

src/Files.App/Views/SettingsPages/Appearance/AppThemeResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ namespace Files.App.Views.SettingsPages.Appearance
66
public class AppThemeResource
77
{
88
public string? Name { get; set; }
9-
public Color BackgroundColor { get; set; }
9+
public string? BackgroundColor { get; set; }
1010
}
1111
}

0 commit comments

Comments
 (0)