Skip to content

Commit

Permalink
refactor: 将主题绑定从index改为item
Browse files Browse the repository at this point in the history
  • Loading branch information
textGamex committed Oct 8, 2024
1 parent f423c79 commit d8324b3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 41 deletions.
1 change: 0 additions & 1 deletion Moder.Core/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using Moder.Core.Views.Menus;
using Moder.Core.ViewsModels.Menus;
using Windows.Foundation;
using WinUIEx;

namespace Moder.Core;

Expand Down
2 changes: 1 addition & 1 deletion Moder.Core/Views/Menus/SettingsControlView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
Margin="0,16,0,0"
Spacing="4">
<controls:SettingsCard Header="App 主题" HeaderIcon="{winUi:FontIcon Glyph=&#xE790;}">
<ComboBox ItemsSource="{x:Bind ViewModel.ThemeMode}" SelectedIndex="{x:Bind ViewModel.SelectedThemeModeIndex, Mode=TwoWay}" />
<ComboBox ItemsSource="{x:Bind ViewModel.ThemeMode}" SelectedItem="{x:Bind ViewModel.SelectedThemeMode, Mode=TwoWay}" />
</controls:SettingsCard>

<controls:SettingsCard Header="游戏本地化语言" HeaderIcon="{winUi:FontIcon Glyph=&#xF2B7;}">
Expand Down
54 changes: 15 additions & 39 deletions Moder.Core/ViewsModels/Menus/SettingsControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ namespace Moder.Core.ViewsModels.Menus;

public sealed partial class SettingsControlViewModel : ObservableObject
{
// TODO: Tag 改为 Enum
public ComboBoxItem[] ThemeMode { get; } =
[
new() { Content = "跟随系统设置", Tag = "Default" },
new() { Content = "明亮", Tag = "Light" },
new() { Content = "暗黑", Tag = "Dark" }
new() { Content = "跟随系统设置", Tag = ElementTheme.Default },
new() { Content = "明亮", Tag = ElementTheme.Light },
new() { Content = "暗黑", Tag = ElementTheme.Dark }
];

public ComboBoxItem[] Languages { get; } =
Expand All @@ -33,7 +32,7 @@ public sealed partial class SettingsControlViewModel : ObservableObject
];

[ObservableProperty]
private int _selectedThemeModeIndex;
private ComboBoxItem _selectedThemeMode;

[ObservableProperty]
private ComboBoxItem _selectedLanguage;
Expand All @@ -43,8 +42,8 @@ public sealed partial class SettingsControlViewModel : ObservableObject
public SettingsControlViewModel(GlobalSettingService globalSettingService)
{
_globalSettingService = globalSettingService;
_selectedThemeModeIndex = GetSelectedThemeModeIndex();
_selectedLanguage = GetSelectedLanguage();
_selectedThemeMode = GetSelectedThemeMode();
}

private ComboBoxItem GetSelectedLanguage()
Expand All @@ -60,46 +59,23 @@ private ComboBoxItem GetSelectedLanguage()
return Languages[0];
}

private int GetSelectedThemeModeIndex()
private ComboBoxItem GetSelectedThemeMode()
{
var themeMode = _globalSettingService.AppThemeMode;
if (themeMode == ElementTheme.Default)
foreach (var item in ThemeMode)
{
return 0;
}

if (themeMode == ElementTheme.Light)
{
return 1;
}

if (themeMode == ElementTheme.Dark)
{
return 2;
if ((ElementTheme)item.Tag == themeMode)
{
return item;
}
}

return 0;
return ThemeMode[0];
}

partial void OnSelectedThemeModeIndexChanged(int value)
partial void OnSelectedThemeModeChanged(ComboBoxItem value)
{
var tag = ThemeMode[value].Tag.ToString();
if (tag == "Default")
{
SetThemeMode(ElementTheme.Default);
}
else if (tag == "Light")
{
SetThemeMode(ElementTheme.Light);
}
else if (tag == "Dark")
{
SetThemeMode(ElementTheme.Dark);
}
else
{
throw new ArgumentException("Invalid theme mode tag.");
}
var theme = (ElementTheme)value.Tag;
SetThemeMode(theme);
}

private void SetThemeMode(ElementTheme theme)
Expand Down

0 comments on commit d8324b3

Please sign in to comment.