Files
community/Ink Canvas/MainWindow_cs/MW_AutoTheme.cs
T

400 lines
15 KiB
C#
Raw Normal View History

using iNKORE.UI.WPF.Modern;
2025-08-31 11:43:52 +08:00
using Microsoft.Win32;
using System;
2025-09-21 00:25:09 +08:00
using System.Collections.Generic;
using System.Threading.Tasks;
2025-05-25 09:29:48 +08:00
using System.Windows;
2025-10-04 17:32:17 +08:00
using System.Windows.Controls;
2025-05-25 09:29:48 +08:00
using System.Windows.Media;
using System.Windows.Threading;
2025-05-25 09:29:48 +08:00
using Application = System.Windows.Application;
using ui = iNKORE.UI.WPF.Controls;
2025-05-25 09:29:48 +08:00
2025-08-03 16:46:33 +08:00
namespace Ink_Canvas
{
public partial class MainWindow : Window
{
2026-04-11 17:03:20 +08:00
private const string ThemeLight = "Light";
private const string ThemeDark = "Dark";
private const string LightThemePath = "Resources/Styles/Light.xaml";
private const string DarkThemePath = "Resources/Styles/Dark.xaml";
private const string DrawShapeImagePath = "Resources/DrawShapeImageDictionary.xaml";
private const string SeewoImagePath = "Resources/SeewoImageDictionary.xaml";
private const string IconImagePath = "Resources/IconImageDictionary.xaml";
2025-09-21 00:25:09 +08:00
private Color FloatBarForegroundColor;
2025-05-25 09:29:48 +08:00
2025-10-04 17:14:14 +08:00
private void SetTheme(string theme, bool autoSwitchIcon = false)
2025-08-03 16:46:33 +08:00
{
2025-09-21 00:25:09 +08:00
var resourcesToRemove = new List<ResourceDictionary>();
foreach (var dict in Application.Current.Resources.MergedDictionaries)
{
2025-10-03 17:08:46 +08:00
if (dict.Source != null &&
(dict.Source.ToString().Contains("Light.xaml") ||
2025-09-21 00:25:09 +08:00
dict.Source.ToString().Contains("Dark.xaml")))
{
resourcesToRemove.Add(dict);
}
}
2025-10-03 17:08:46 +08:00
2025-09-21 00:25:09 +08:00
foreach (var dict in resourcesToRemove)
{
Application.Current.Resources.MergedDictionaries.Remove(dict);
}
2026-04-11 17:03:20 +08:00
var isLightTheme = theme == ThemeLight;
var themePath = isLightTheme ? LightThemePath : DarkThemePath;
var elementTheme = isLightTheme ? ElementTheme.Light : ElementTheme.Dark;
2025-05-25 09:29:48 +08:00
2026-04-11 17:03:20 +08:00
var rd1 = new ResourceDictionary { Source = new Uri(themePath, UriKind.Relative) };
Application.Current.Resources.MergedDictionaries.Add(rd1);
2025-10-03 17:08:46 +08:00
2026-04-11 17:03:20 +08:00
_ = Task.Run(async () =>
2025-08-03 16:46:33 +08:00
{
2026-04-11 17:03:20 +08:00
await Task.Delay(100);
Dispatcher.Invoke(() =>
{
2026-04-11 17:03:20 +08:00
LoadImageResourceDictionary(DrawShapeImagePath);
LoadImageResourceDictionary(SeewoImagePath);
LoadImageResourceDictionary(IconImagePath);
});
2026-04-11 17:03:20 +08:00
});
2025-05-25 09:29:48 +08:00
2026-04-11 17:03:20 +08:00
ThemeManager.SetRequestedTheme(window, elementTheme);
2025-10-05 08:55:18 +08:00
2026-04-11 17:03:20 +08:00
InitializeFloatBarForegroundColor();
RefreshQuickPanelIcons();
RefreshStrokeSelectionIcons();
RefreshImageSelectionIcons();
RefreshGestureButtonIcon();
RefreshFloatingBarHighlightColors();
2025-10-05 09:14:29 +08:00
2026-04-11 17:03:20 +08:00
if (autoSwitchIcon)
{
AutoSwitchFloatingBarIconForTheme(theme);
}
2025-10-04 17:12:14 +08:00
2026-04-11 17:03:20 +08:00
window.InvalidateVisual();
RefreshOtherWindowsTheme();
}
2025-10-06 18:29:12 +08:00
2026-04-11 17:03:20 +08:00
void LoadImageResourceDictionary(string path)
{
var rd = new ResourceDictionary { Source = new Uri(path, UriKind.Relative) };
Application.Current.Resources.MergedDictionaries.Add(rd);
2025-09-21 00:25:09 +08:00
}
private void InitializeFloatBarForegroundColor()
{
try
{
2025-05-25 09:29:48 +08:00
FloatBarForegroundColor = (Color)Application.Current.FindResource("FloatBarForegroundColor");
2025-09-21 00:39:30 +08:00
RefreshFloatingBarButtonColors();
2025-05-25 09:29:48 +08:00
}
2025-09-21 00:39:30 +08:00
catch (Exception)
2025-09-21 00:25:09 +08:00
{
2025-10-03 17:08:46 +08:00
FloatBarForegroundColor = Color.FromRgb(0, 0, 0);
2025-09-21 00:39:30 +08:00
}
}
2025-10-03 17:08:46 +08:00
2025-10-01 09:13:48 +08:00
private void RefreshQuickPanelIcons()
{
try
{
2026-04-11 17:03:20 +08:00
LeftUnFoldButtonQuickPanel?.InvalidateVisual();
RightUnFoldButtonQuickPanel?.InvalidateVisual();
LeftSidePanel?.InvalidateVisual();
RightSidePanel?.InvalidateVisual();
2025-10-01 09:13:48 +08:00
}
catch (Exception)
{
}
}
2025-10-04 17:32:17 +08:00
private void RefreshFloatingBarHighlightColors()
{
try
{
if (FloatingbarSelectionBG != null && FloatingbarSelectionBG.Visibility == Visibility.Visible)
{
2026-04-11 17:03:20 +08:00
bool isDarkTheme = IsCurrentThemeDark();
2025-10-04 17:32:17 +08:00
Color highlightBackgroundColor;
Color highlightBarColor;
2025-10-06 18:29:12 +08:00
2025-10-04 17:32:17 +08:00
if (isDarkTheme)
{
2025-10-06 18:29:12 +08:00
highlightBackgroundColor = Color.FromArgb(21, 102, 204, 255);
highlightBarColor = Color.FromRgb(102, 204, 255);
2025-10-04 17:32:17 +08:00
}
else
{
2025-10-06 18:29:12 +08:00
highlightBackgroundColor = Color.FromArgb(21, 59, 130, 246);
highlightBarColor = Color.FromRgb(37, 99, 235);
2025-10-04 17:32:17 +08:00
}
FloatingbarSelectionBG.Background = new SolidColorBrush(highlightBackgroundColor);
if (FloatingbarSelectionBG.Child is System.Windows.Controls.Canvas canvas && canvas.Children.Count > 0)
{
var firstChild = canvas.Children[0];
if (firstChild is Border innerBorder)
{
innerBorder.Background = new SolidColorBrush(highlightBarColor);
}
}
}
}
catch (Exception)
{
}
}
2026-04-11 17:03:20 +08:00
private bool IsCurrentThemeDark()
{
return Settings.Appearance.Theme == 1 ||
(Settings.Appearance.Theme == 2 && !IsSystemThemeLight());
}
2025-09-21 00:39:30 +08:00
private void RefreshFloatingBarButtonColors()
{
try
{
2026-04-11 17:03:20 +08:00
SymbolIconDelete.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.DeleteIcon);
ShapeDrawFloatingBarBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.ShapesIcon);
SymbolIconUndo.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.UndoIcon);
SymbolIconRedo.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.RedoIcon);
CursorWithDelFloatingBarBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.CursorWithDelFloatingBarBtnIcon);
WhiteboardFloatingBarBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.WhiteboardFloatingBarBtnIcon);
ToolsFloatingBarBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.ToolsFloatingBarBtnIcon);
Fold_Icon.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.FoldIcon);
2025-10-06 18:29:12 +08:00
2026-04-13 13:33:08 +08:00
TimerToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.TimerIconGeometry);
RandomDrawToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.RandomDrawIconGeometry);
SingleDrawToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.SingleDrawIconGeometry);
SaveToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.SaveIconGeometry);
OpenToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.OpenIconGeometry);
ReplayToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.ReplayIconGeometry);
ScreenshotToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.ScreenshotIconGeometry);
ManualToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.ManualIconGeometry);
SettingsToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.SettingsIconGeometry);
BoardTimerToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.TimerIconGeometry);
BoardRandomDrawToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.RandomDrawIconGeometry);
BoardSingleDrawToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.SingleDrawIconGeometry);
BoardSaveToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.SaveIconGeometry);
BoardOpenToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.OpenIconGeometry);
BoardReplayToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.ReplayIconGeometry);
BoardScreenshotToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.ScreenshotIconGeometry);
BoardManualToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.ManualIconGeometry);
BoardSettingsToolBtn.Icon.Geometry = Geometry.Parse(XamlGraphicsIconGeometries.SettingsIconGeometry);
2026-04-11 17:03:20 +08:00
bool isDarkTheme = IsCurrentThemeDark();
Color selectedColor = isDarkTheme ? Color.FromRgb(102, 204, 255) : Color.FromRgb(30, 58, 138);
SetAllFloatingBarButtonsToColor(FloatBarForegroundColor);
2025-09-21 00:39:30 +08:00
switch (_currentToolMode)
{
case "cursor":
2026-04-11 12:44:40 +08:00
Cursor_Icon.Icon.Brush = new SolidColorBrush(selectedColor);
2025-09-21 00:39:30 +08:00
break;
case "pen":
case "color":
2026-04-11 12:44:40 +08:00
Pen_Icon.Icon.Brush = new SolidColorBrush(selectedColor);
2025-09-21 00:39:30 +08:00
break;
case "eraser":
2026-04-11 12:44:40 +08:00
Eraser_Icon.Icon.Brush = new SolidColorBrush(selectedColor);
2025-09-21 00:39:30 +08:00
break;
case "eraserByStrokes":
2026-04-11 12:44:40 +08:00
EraserByStrokes_Icon.Icon.Brush = new SolidColorBrush(selectedColor);
2025-09-21 00:39:30 +08:00
break;
case "select":
2026-04-11 12:44:40 +08:00
SymbolIconSelect.Icon.Brush = new SolidColorBrush(selectedColor);
2025-09-21 00:39:30 +08:00
break;
}
}
catch (Exception)
{
2025-09-21 00:25:09 +08:00
}
2025-05-25 09:29:48 +08:00
}
2026-04-11 17:03:20 +08:00
void SetAllFloatingBarButtonsToColor(Color color)
{
var brush = new SolidColorBrush(color);
Cursor_Icon.Icon.Brush = brush;
Pen_Icon.Icon.Brush = brush;
EraserByStrokes_Icon.Icon.Brush = brush;
Eraser_Icon.Icon.Brush = brush;
SymbolIconSelect.Icon.Brush = brush;
ShapeDrawFloatingBarBtn.Icon.Brush = brush;
SymbolIconUndo.Icon.Brush = brush;
SymbolIconRedo.Icon.Brush = brush;
CursorWithDelFloatingBarBtn.Icon.Brush = brush;
WhiteboardFloatingBarBtn.Icon.Brush = brush;
ToolsFloatingBarBtn.Icon.Brush = brush;
Fold_Icon.Icon.Brush = brush;
}
2025-08-03 16:46:33 +08:00
private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
switch (Settings.Appearance.Theme)
{
2025-05-25 09:29:48 +08:00
case 0:
2026-04-11 17:03:20 +08:00
SetTheme(ThemeLight);
2025-05-25 09:29:48 +08:00
break;
case 1:
2026-04-11 17:03:20 +08:00
SetTheme(ThemeDark);
2025-05-25 09:29:48 +08:00
break;
case 2:
2026-04-11 17:03:20 +08:00
SetTheme(IsSystemThemeLight() ? ThemeLight : ThemeDark);
2025-05-25 09:29:48 +08:00
break;
}
}
2025-08-03 16:46:33 +08:00
private bool IsSystemThemeLight()
{
try
{
2025-05-25 09:29:48 +08:00
var registryKey = Registry.CurrentUser;
2026-04-11 17:03:20 +08:00
var themeKey = registryKey.OpenSubKey("software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize");
if (themeKey != null)
{
int keyValue = (int)themeKey.GetValue("SystemUsesLightTheme");
return keyValue == 1;
}
2025-05-25 09:29:48 +08:00
}
2026-02-21 16:51:34 +08:00
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); }
2026-04-11 17:03:20 +08:00
return false;
2025-05-25 09:29:48 +08:00
}
2025-10-04 17:12:14 +08:00
private void AutoSwitchFloatingBarIconForTheme(string theme)
{
try
{
2026-04-11 17:03:20 +08:00
Settings.Appearance.FloatingBarImg = theme == ThemeLight ? 0 : 3;
2025-10-04 17:12:14 +08:00
UpdateFloatingBarIcon();
2025-10-04 17:15:58 +08:00
UpdateFloatingBarIconComboBox();
}
catch (Exception)
{
}
}
private void UpdateFloatingBarIconComboBox()
{
2025-10-04 17:12:14 +08:00
}
2025-10-05 08:55:18 +08:00
private void RefreshStrokeSelectionIcons()
{
try
{
if (BorderStrokeSelectionControl != null)
{
BorderStrokeSelectionControl.InvalidateVisual();
var viewbox = BorderStrokeSelectionControl.Child as Viewbox;
if (viewbox?.Child is ui.SimpleStackPanel stackPanel)
{
2026-04-11 17:03:20 +08:00
RefreshIconsRecursive(stackPanel);
2025-10-05 08:55:18 +08:00
}
}
}
catch (Exception)
{
}
}
2025-10-05 09:14:29 +08:00
private void RefreshImageSelectionIcons()
{
try
{
if (BorderImageSelectionControl != null)
{
BorderImageSelectionControl.InvalidateVisual();
var viewbox = BorderImageSelectionControl.Child as Viewbox;
if (viewbox?.Child is ui.SimpleStackPanel stackPanel)
{
2026-04-11 17:03:20 +08:00
RefreshIconsRecursive(stackPanel);
2025-10-05 09:14:29 +08:00
}
}
}
catch (Exception)
{
}
}
2026-04-11 17:03:20 +08:00
private void RefreshIconsRecursive(System.Windows.Controls.Panel panel)
2025-10-05 09:14:29 +08:00
{
try
{
foreach (var child in panel.Children)
{
if (child is Image image)
{
image.InvalidateVisual();
}
else if (child is System.Windows.Controls.Panel childPanel)
{
2026-04-11 17:03:20 +08:00
RefreshIconsRecursive(childPanel);
2025-10-05 09:14:29 +08:00
}
else if (child is Border border && border.Child is System.Windows.Controls.Panel borderPanel)
{
2026-04-11 17:03:20 +08:00
RefreshIconsRecursive(borderPanel);
2025-10-05 09:14:29 +08:00
}
else if (child is Grid grid)
{
foreach (var gridChild in grid.Children)
{
if (gridChild is Image gridImage)
{
gridImage.InvalidateVisual();
}
}
}
}
}
catch (Exception)
{
}
}
2025-10-05 09:16:36 +08:00
2025-11-01 20:26:52 +08:00
private void RefreshGestureButtonIcon()
{
try
{
CheckEnableTwoFingerGestureBtnColorPrompt();
}
catch (Exception)
{
}
}
2025-10-05 09:16:36 +08:00
private void RefreshOtherWindowsTheme()
{
try
{
foreach (Window window in Application.Current.Windows)
{
if (window is CountdownTimerWindow timerWindow)
{
timerWindow.RefreshTheme();
}
else if (window is RandWindow randWindow)
{
randWindow.RefreshTheme();
}
2025-10-05 09:33:25 +08:00
else if (window is OperatingGuideWindow operatingGuideWindow)
{
operatingGuideWindow.RefreshTheme();
}
2025-10-05 09:16:36 +08:00
}
2025-12-20 13:56:46 +08:00
2026-04-11 17:03:20 +08:00
TimerControl?.RefreshTheme();
MinimizedTimerControl?.RefreshTheme();
2025-10-05 09:16:36 +08:00
}
catch (Exception)
{
}
}
2025-05-25 09:29:48 +08:00
}
2026-04-11 17:03:20 +08:00
}