mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-17 01:59:33 +08:00
perf: 使用HandyControl的DarkMode
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<Application
|
||||
<Application
|
||||
x:Class="MaaWpfGui.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
@@ -6,7 +6,8 @@
|
||||
xmlns:s="https://github.com/canton7/Stylet"
|
||||
xmlns:main="clr-namespace:MaaWpfGui.Main"
|
||||
xmlns:styles="clr-namespace:MaaWpfGui.Styles"
|
||||
xmlns:controls="clr-namespace:MaaWpfGui.Styles.Controls">
|
||||
xmlns:controls="clr-namespace:MaaWpfGui.Styles.Controls"
|
||||
xmlns:hc="https://handyorg.github.io/handycontrol">
|
||||
<Application.Resources>
|
||||
<!--<ResourceDictionary>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
@@ -26,8 +27,7 @@
|
||||
<main:Bootstrapper />
|
||||
</s:ApplicationLoader.Bootstrapper>
|
||||
</s:ApplicationLoader>
|
||||
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml" />
|
||||
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml" />
|
||||
<hc:Theme Name="HandyTheme"/>
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
<!-- A Style that affects all TextBlocks -->
|
||||
<Style BasedOn="{StaticResource TextBlockDefault}" TargetType="controls:TextBlock" />
|
||||
|
||||
@@ -13,17 +13,14 @@
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Media;
|
||||
using HandyControl.Tools;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Themes;
|
||||
using MaaWpfGui.Constants;
|
||||
using MaaWpfGui.Helper;
|
||||
using MaaWpfGui.ViewModels.UI;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace MaaWpfGui
|
||||
{
|
||||
@@ -32,9 +29,6 @@ namespace MaaWpfGui
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
[DllImport("DwmApi.dll")]
|
||||
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int value, int attrLen);
|
||||
|
||||
public void Hyperlink_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Hyperlink link = sender as Hyperlink;
|
||||
@@ -44,12 +38,21 @@ namespace MaaWpfGui
|
||||
}
|
||||
}
|
||||
|
||||
private readonly SolidColorBrush black = new SolidColorBrush(Color.FromRgb(49, 51, 56));
|
||||
private readonly SolidColorBrush white = new SolidColorBrush(Color.FromRgb(181, 186, 193));
|
||||
private void UpdateTheme(SkinType skin)
|
||||
{
|
||||
SharedResourceDictionary.SharedDictionaries.Clear();
|
||||
Theme.GetTheme("HandyTheme", Resources).Skin = skin;
|
||||
Current.MainWindow?.OnApplyTemplate();
|
||||
}
|
||||
|
||||
private static bool SetColors => ShouldDarkMode();
|
||||
private void UpdateTheme(bool syncWithSystem)
|
||||
{
|
||||
SharedResourceDictionary.SharedDictionaries.Clear();
|
||||
Theme.GetTheme("HandyTheme", Resources).SyncWithSystem = syncWithSystem;
|
||||
Current.MainWindow?.OnApplyTemplate();
|
||||
}
|
||||
|
||||
private static bool ShouldDarkMode()
|
||||
private void SwitchDarkMode()
|
||||
{
|
||||
SettingsViewModel.DarkModeType darkModeType =
|
||||
Enum.TryParse(ConfigurationHelper.GetValue(ConfigurationKeys.DarkMode, SettingsViewModel.DarkModeType.Light.ToString()),
|
||||
@@ -59,138 +62,26 @@ namespace MaaWpfGui
|
||||
switch (darkModeType)
|
||||
{
|
||||
case SettingsViewModel.DarkModeType.Light:
|
||||
return false;
|
||||
UpdateTheme(skin: SkinType.Default);
|
||||
return;
|
||||
|
||||
case SettingsViewModel.DarkModeType.Dark:
|
||||
return true;
|
||||
UpdateTheme(skin: SkinType.Dark);
|
||||
return;
|
||||
|
||||
case SettingsViewModel.DarkModeType.SyncWithOS:
|
||||
var isLight = true;
|
||||
try
|
||||
{
|
||||
var registryValue =
|
||||
Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize",
|
||||
"AppsUseLightTheme", null);
|
||||
isLight = Convert.ToBoolean(registryValue);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
|
||||
return !isLight;
|
||||
|
||||
default:
|
||||
// should never reach
|
||||
return false;
|
||||
UpdateTheme(syncWithSystem: true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void DarkToStart()
|
||||
{
|
||||
if (!SetColors)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int darkModeEnabled = 1;
|
||||
DwmSetWindowAttribute(this.MainWindow.GetHandle(), 20, ref darkModeEnabled, sizeof(int));
|
||||
}
|
||||
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
ConfigurationHelper.Load();
|
||||
LocalizationHelper.Load();
|
||||
SwitchDarkMode();
|
||||
|
||||
base.OnStartup(e);
|
||||
|
||||
if (!SetColors)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 在应用程序启动时,遍历所有控件并设置它们的颜色
|
||||
SetAllControlColors(this.MainWindow);
|
||||
|
||||
// 修改下拉框的颜色
|
||||
EventManager.RegisterClassHandler(typeof(ComboBox), UIElement.PreviewMouseLeftButtonDownEvent, new RoutedEventHandler(ComboBox_DropDownOpened));
|
||||
}
|
||||
|
||||
private void ComboBox_DropDownOpened(object sender, EventArgs e)
|
||||
{
|
||||
if (!(sender is ComboBox comboBox) || !(comboBox.Template.FindName("PART_Popup", comboBox) is Popup popup) || popup.Child == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
SetAllControlColors(popup.Child);
|
||||
}));
|
||||
}
|
||||
|
||||
public static void SetAllControlColors(DependencyObject obj)
|
||||
{
|
||||
if (!SetColors)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 遍历控件并设置颜色
|
||||
if (obj is DependencyObject container)
|
||||
{
|
||||
int count = VisualTreeHelper.GetChildrenCount(container);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
DependencyObject child = VisualTreeHelper.GetChild(container, i);
|
||||
App app = (App)Current;
|
||||
app.SetControlColors(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetControlColors(DependencyObject obj)
|
||||
{
|
||||
// 检查控件是否为null
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取控件类型
|
||||
Type type = obj.GetType();
|
||||
|
||||
// 如果控件具有 Background 属性,则将其背景颜色设置为黑色
|
||||
if (type.GetProperty("Background") != null && (obj as Control)?.Background != null)
|
||||
{
|
||||
(obj as Control).Background = black;
|
||||
}
|
||||
|
||||
// 如果控件具有 Foreground 属性,则将其前景色设置为白色
|
||||
if (type.GetProperty("Foreground") != null && (obj as Control)?.Foreground != null)
|
||||
{
|
||||
(obj as Control).Foreground = white;
|
||||
}
|
||||
|
||||
// 如果控件具有 BorderBrush 属性,则将其边框颜色设置为白色
|
||||
if (type.GetProperty("BorderBrush") != null && (obj as Control)?.BorderBrush != null)
|
||||
{
|
||||
(obj as Control).BorderBrush = white;
|
||||
}
|
||||
|
||||
if (obj is TextBlock)
|
||||
{
|
||||
(obj as TextBlock).Foreground = white;
|
||||
}
|
||||
else if (obj is DockPanel)
|
||||
{
|
||||
(obj as DockPanel).Background = black;
|
||||
}
|
||||
|
||||
// 遍历子控件并递归调用此方法
|
||||
int count = VisualTreeHelper.GetChildrenCount(obj);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
|
||||
SetControlColors(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,9 +103,6 @@ namespace MaaWpfGui.Helper
|
||||
window.ShowInTaskbar = false;
|
||||
window.Visibility = Visibility.Hidden;
|
||||
}
|
||||
|
||||
var app = Application.Current as App;
|
||||
app!.DarkToStart();
|
||||
}
|
||||
else if (!isDialog && ownerViewModel == null)
|
||||
{
|
||||
|
||||
@@ -135,8 +135,6 @@ namespace MaaWpfGui.Main
|
||||
}
|
||||
|
||||
base.OnStart();
|
||||
ConfigurationHelper.Load();
|
||||
LocalizationHelper.Load();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace MaaWpfGui.ViewModels.UI
|
||||
_httpService = httpService;
|
||||
DisplayName = LocalizationHelper.GetString("Copilot");
|
||||
LogItemViewModels = new ObservableCollection<LogItemViewModel>();
|
||||
AddLog(LocalizationHelper.GetString("CopilotTip"), UiLogColor.Message);
|
||||
AddLog(LocalizationHelper.GetString("CopilotTip"));
|
||||
}
|
||||
|
||||
protected override void OnInitialActivate()
|
||||
@@ -72,10 +72,6 @@ namespace MaaWpfGui.ViewModels.UI
|
||||
base.OnInitialActivate();
|
||||
_asstProxy = _container.Get<AsstProxy>();
|
||||
_settingsViewModel = _container.Get<SettingsViewModel>();
|
||||
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
App.SetAllControlColors(Application.Current.MainWindow);
|
||||
}));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -49,10 +49,6 @@ namespace MaaWpfGui.ViewModels.UI
|
||||
{
|
||||
base.OnInitialActivate();
|
||||
_asstProxy = _container.Get<AsstProxy>();
|
||||
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
App.SetAllControlColors(Application.Current.MainWindow);
|
||||
}));
|
||||
}
|
||||
|
||||
private string _depotInfo = LocalizationHelper.GetString("DepotRecognitionTip");
|
||||
|
||||
@@ -49,10 +49,6 @@ namespace MaaWpfGui.ViewModels.UI
|
||||
{
|
||||
base.OnInitialActivate();
|
||||
_asstProxy = _container.Get<AsstProxy>();
|
||||
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
App.SetAllControlColors(Application.Current.MainWindow);
|
||||
}));
|
||||
}
|
||||
|
||||
private string _recruitInfo = LocalizationHelper.GetString("RecruitmentRecognitionTip");
|
||||
|
||||
@@ -152,11 +152,6 @@ namespace MaaWpfGui.ViewModels.UI
|
||||
{
|
||||
ConnectAddressHistory = JsonConvert.DeserializeObject<ObservableCollection<string>>(addressListJson);
|
||||
}
|
||||
|
||||
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
App.SetAllControlColors(Application.Current.MainWindow);
|
||||
}));
|
||||
}
|
||||
|
||||
private List<string> _listTitle = new List<string>();
|
||||
|
||||
@@ -148,11 +148,6 @@ namespace MaaWpfGui.ViewModels.UI
|
||||
{
|
||||
Application.Current.MainWindow!.Closing += _settingsViewModel.SaveGUIParameters;
|
||||
}
|
||||
|
||||
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
App.SetAllControlColors(Application.Current.MainWindow);
|
||||
}));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
<Window
|
||||
<hc:Window
|
||||
x:Class="MaaWpfGui.Views.UI.ErrorView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:MaaWpfGui"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:hc="https://handyorg.github.io/handycontrol"
|
||||
xmlns:helper="clr-namespace:MaaWpfGui.Helper"
|
||||
xmlns:constants="clr-namespace:MaaWpfGui.Constants"
|
||||
xmlns:styles="clr-namespace:MaaWpfGui.Styles"
|
||||
@@ -145,4 +146,4 @@
|
||||
</Hyperlink>
|
||||
</controls:TextBlock>
|
||||
</Grid>
|
||||
</Window>
|
||||
</hc:Window>
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
<Window
|
||||
<hc:Window
|
||||
x:Class="MaaWpfGui.Views.UI.RootView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:s="https://github.com/canton7/Stylet"
|
||||
xmlns:hc="https://handyorg.github.io/handycontrol"
|
||||
xmlns:vm="clr-namespace:MaaWpfGui"
|
||||
xmlns:viewModels="clr-namespace:MaaWpfGui.ViewModels"
|
||||
xmlns:ui="clr-namespace:MaaWpfGui.ViewModels.UI"
|
||||
@@ -13,6 +14,7 @@
|
||||
Height="600"
|
||||
MinWidth="800"
|
||||
MinHeight="600"
|
||||
Icon="../../newlogo.ico"
|
||||
d:DataContext="{d:DesignInstance {x:Type ui:RootViewModel}}"
|
||||
mc:Ignorable="d">
|
||||
<DockPanel>
|
||||
@@ -32,4 +34,4 @@
|
||||
</TabControl.ContentTemplate>
|
||||
</TabControl>
|
||||
</DockPanel>
|
||||
</Window>
|
||||
</hc:Window>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Window
|
||||
<hc:Window
|
||||
x:Class="MaaWpfGui.Views.UI.VersionUpdateView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
@@ -6,6 +6,7 @@
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:mdg="clr-namespace:Neo.Markdig.Xaml;assembly=Neo.Markdig.Xaml"
|
||||
xmlns:s="https://github.com/canton7/Stylet"
|
||||
xmlns:hc="https://handyorg.github.io/handycontrol"
|
||||
xmlns:vm="clr-namespace:MaaWpfGui"
|
||||
xmlns:viewModels="clr-namespace:MaaWpfGui.ViewModels"
|
||||
xmlns:ui="clr-namespace:MaaWpfGui.ViewModels.UI"
|
||||
@@ -68,4 +69,4 @@
|
||||
</FlowDocumentScrollViewer.CommandBindings>
|
||||
</FlowDocumentScrollViewer>
|
||||
</Grid>
|
||||
</Window>
|
||||
</hc:Window>
|
||||
|
||||
Reference in New Issue
Block a user