Files

474 lines
18 KiB
C#
Raw Permalink Normal View History

2026-01-02 01:35:22 +08:00
using System;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Media = System.Windows.Media;
2026-04-19 08:35:22 +08:00
namespace Ink_Canvas.Windows.SettingsViews.Helpers
2026-01-02 01:35:22 +08:00
{
public static class MainWindowSettingsHelper
{
private static MainWindow GetMainWindow()
{
return Application.Current.MainWindow as MainWindow;
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void InvokeMainWindowMethod(string methodName, params object[] parameters)
{
try
{
var mainWindow = GetMainWindow();
if (mainWindow == null) return;
var method = mainWindow.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (method != null)
{
method.Invoke(mainWindow, parameters);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"调用 MainWindow 方法 {methodName} 失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void InvokeToggleSwitchToggled(string toggleSwitchName, bool isOn)
{
try
{
var mainWindow = GetMainWindow();
if (mainWindow == null) return;
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
var toggleSwitch = mainWindow.FindName(toggleSwitchName);
if (toggleSwitch == null)
{
var property = mainWindow.GetType().GetProperty(toggleSwitchName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (property != null && property.PropertyType.Name.Contains("ToggleSwitch"))
{
var isOnProperty = property.PropertyType.GetProperty("IsOn");
if (isOnProperty != null)
{
var toggleSwitchInstance = property.GetValue(mainWindow);
if (toggleSwitchInstance != null)
{
isOnProperty.SetValue(toggleSwitchInstance, isOn);
}
}
}
var toggledMethodName = toggleSwitchName + "_Toggled";
2026-01-10 17:32:14 +08:00
InvokeMainWindowMethod(toggledMethodName, null, new RoutedEventArgs());
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
NotifySettingsPanelsSyncState(toggleSwitchName);
return;
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
var toggleSwitchType = toggleSwitch.GetType();
var isOnProp = toggleSwitchType.GetProperty("IsOn");
if (isOnProp != null)
{
isOnProp.SetValue(toggleSwitch, isOn);
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
var toggledMethodName2 = toggleSwitchName + "_Toggled";
InvokeMainWindowMethod(toggledMethodName2, toggleSwitch, new RoutedEventArgs());
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
NotifySettingsPanelsSyncState(toggleSwitchName);
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
NotifyThemeUpdateIfNeeded(toggleSwitchName);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"调用 ToggleSwitch {toggleSwitchName} 失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void InvokeComboBoxSelectionChanged(string comboBoxName, object selectedItem)
{
try
{
var mainWindow = GetMainWindow();
if (mainWindow == null) return;
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
var comboBox = mainWindow.FindName(comboBoxName) as System.Windows.Controls.ComboBox;
if (comboBox != null)
{
comboBox.SelectedItem = selectedItem;
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
var selectionChangedMethodName = comboBoxName + "_SelectionChanged";
InvokeMainWindowMethod(selectionChangedMethodName, comboBox, new System.Windows.Controls.SelectionChangedEventArgs(
2026-03-03 16:04:20 +08:00
System.Windows.Controls.Primitives.Selector.SelectionChangedEvent,
new System.Collections.IList[0],
2026-01-02 01:35:22 +08:00
new System.Collections.IList[0]));
}
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
NotifySettingsPanelsSyncState(comboBoxName);
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
NotifyThemeUpdateIfNeeded(comboBoxName);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"调用 ComboBox {comboBoxName} 失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void InvokeSliderValueChanged(string sliderName, double value)
{
try
{
var mainWindow = GetMainWindow();
if (mainWindow == null) return;
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
var slider = mainWindow.FindName(sliderName) as System.Windows.Controls.Slider;
if (slider != null)
{
var oldValue = slider.Value;
2026-01-10 17:31:46 +08:00
slider.Value = value;
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
var valueChangedMethodName = sliderName + "_ValueChanged";
2026-03-03 16:04:20 +08:00
InvokeMainWindowMethod(valueChangedMethodName, slider,
2026-01-02 01:35:22 +08:00
new System.Windows.RoutedPropertyChangedEventArgs<double>(oldValue, value));
}
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
NotifySettingsPanelsSyncState(sliderName);
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
NotifyThemeUpdateIfNeeded(sliderName);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"调用 Slider {sliderName} 失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void InvokeCheckBoxCheckedChanged(string checkBoxName, bool isChecked)
{
try
{
var mainWindow = GetMainWindow();
if (mainWindow == null) return;
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
var checkBox = mainWindow.FindName(checkBoxName) as System.Windows.Controls.CheckBox;
if (checkBox != null)
{
checkBox.IsChecked = isChecked;
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
var methodNames = new[]
{
checkBoxName + "_IsCheckChanged",
checkBoxName + "_IsCheckChange",
checkBoxName + "_Checked",
checkBoxName + "_Unchecked"
};
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
foreach (var methodName in methodNames)
{
var method = mainWindow.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (method != null)
{
InvokeMainWindowMethod(methodName, checkBox, new RoutedEventArgs());
break;
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"调用 CheckBox {checkBoxName} 失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void UpdateSettingSafely(Action action, string eventHandlerName = null, string controlName = null, params object[] eventHandlerParams)
{
try
{
if (!string.IsNullOrEmpty(eventHandlerName))
{
var mainWindow = GetMainWindow();
if (mainWindow != null)
{
var method = mainWindow.GetType().GetMethod(eventHandlerName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (method != null)
{
method.Invoke(mainWindow, eventHandlerParams);
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
if (!string.IsNullOrEmpty(controlName))
{
NotifySettingsPanelsSyncState(controlName);
}
return;
}
}
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
action?.Invoke();
MainWindow.SaveSettingsToFile();
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
if (!string.IsNullOrEmpty(controlName))
{
NotifySettingsPanelsSyncState(controlName);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"更新设置失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void UpdateSettingDirectly(Action action, string controlName = null)
{
try
{
action?.Invoke();
MainWindow.SaveSettingsToFile();
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
if (!string.IsNullOrEmpty(controlName))
{
NotifySettingsPanelsSyncState(controlName);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"直接更新设置失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void InvokeTextBoxTextChanged(string textBoxName, string text)
{
try
{
var mainWindow = GetMainWindow();
if (mainWindow == null) return;
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
var textBox = mainWindow.FindName(textBoxName) as System.Windows.Controls.TextBox;
if (textBox != null)
{
textBox.Text = text;
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
var textChangedMethodName = textBoxName + "_TextChanged";
InvokeMainWindowMethod(textChangedMethodName, textBox, new System.Windows.Controls.TextChangedEventArgs(
System.Windows.Controls.Primitives.TextBoxBase.TextChangedEvent,
System.Windows.Controls.UndoAction.None));
}
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
NotifySettingsPanelsSyncState(textBoxName);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"调用 TextBox {textBoxName} 失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void NotifySettingsPanelsSyncState(string controlName)
{
try
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
foreach (Window window in Application.Current.Windows)
{
2026-04-05 21:50:53 +08:00
if (window.GetType().Name == "SettingsWindow")
2026-01-02 01:35:22 +08:00
{
2026-04-05 17:58:57 +08:00
SyncAllPanels(window);
2026-04-19 08:35:22 +08:00
break;
2026-01-02 01:35:22 +08:00
}
}
}), System.Windows.Threading.DispatcherPriority.Background);
}
catch (Exception ex)
{
2026-01-10 17:31:55 +08:00
System.Diagnostics.Debug.WriteLine($"通知设置面板同步状态失败: {ex.Message}");
2026-01-02 01:35:22 +08:00
}
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
private static void SyncAllPanels(Window settingsWindow)
{
try
{
2026-04-05 17:58:57 +08:00
var pageProperties = settingsWindow.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.PropertyType.Name.EndsWith("Page") &&
p.PropertyType.IsSubclassOf(typeof(System.Windows.Controls.Page)));
2026-01-02 01:35:22 +08:00
2026-04-05 17:58:57 +08:00
foreach (var pageProp in pageProperties)
2026-01-02 01:35:22 +08:00
{
try
{
2026-04-05 17:58:57 +08:00
var page = pageProp.GetValue(settingsWindow) as System.Windows.Controls.Page;
if (page != null)
2026-01-02 01:35:22 +08:00
{
2026-04-05 17:58:57 +08:00
var loadMethod = page.GetType().GetMethod("LoadSettings",
2026-01-02 01:35:22 +08:00
BindingFlags.Public | BindingFlags.Instance);
if (loadMethod != null)
{
2026-04-05 17:58:57 +08:00
loadMethod.Invoke(page, null);
2026-01-02 01:35:22 +08:00
}
}
}
catch (Exception ex)
{
2026-04-05 17:58:57 +08:00
System.Diagnostics.Debug.WriteLine($"同步页面 {pageProp.Name} 状态失败: {ex.Message}");
2026-01-02 01:35:22 +08:00
}
}
}
catch (Exception ex)
{
2026-04-05 17:58:57 +08:00
System.Diagnostics.Debug.WriteLine($"同步所有页面状态失败: {ex.Message}");
2026-01-02 01:35:22 +08:00
}
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void NotifyThemeUpdateIfNeeded(string controlName)
{
try
{
if (string.IsNullOrEmpty(controlName)) return;
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
var themeRelatedControls = new[]
{
2026-04-19 08:35:22 +08:00
"ComboBoxTheme",
"ToggleSwitchEnableTrayIcon",
"ComboBoxFloatingBarImg",
"ComboBoxUnFoldBtnImg",
"ComboBoxSplashScreenStyle",
"ToggleSwitchEnableSplashScreen",
"ViewboxFloatingBarScaleTransformValueSlider",
"ViewboxFloatingBarOpacityValueSlider",
"ViewboxFloatingBarOpacityInPPTValueSlider",
"UnFoldBtnImg",
"FloatingBarImg",
"Theme"
2026-01-02 01:35:22 +08:00
};
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
bool isThemeRelated = false;
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
foreach (var themeControl in themeRelatedControls)
{
2026-05-02 10:06:21 +08:00
// OrdinalIgnoreCase 避免在循环里反复 ToLower() 生成中间字符串。
if (controlName.IndexOf(themeControl, StringComparison.OrdinalIgnoreCase) >= 0 ||
themeControl.IndexOf(controlName, StringComparison.OrdinalIgnoreCase) >= 0)
2026-01-02 01:35:22 +08:00
{
isThemeRelated = true;
break;
}
}
if (isThemeRelated)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
NotifySettingsWindowThemeUpdate();
}), System.Windows.Threading.DispatcherPriority.Background);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"通知主题更新失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
private static void NotifySettingsWindowThemeUpdate()
{
try
{
foreach (Window window in Application.Current.Windows)
{
2026-04-05 21:50:53 +08:00
if (window.GetType().Name == "SettingsWindow")
2026-01-02 01:35:22 +08:00
{
2026-03-03 16:04:20 +08:00
var applyThemeMethod = window.GetType().GetMethod("ApplyTheme",
2026-01-02 01:35:22 +08:00
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (applyThemeMethod != null)
{
applyThemeMethod.Invoke(window, null);
}
2026-03-03 16:04:20 +08:00
2026-04-19 08:35:22 +08:00
break;
2026-01-02 01:35:22 +08:00
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"通知设置窗口主题更新失败: {ex.Message}");
}
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void ForceUpdateAllPanelsTheme()
{
NotifySettingsWindowThemeUpdate();
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void InvokeComboBoxSelectionChangedWithThemeCheck(string comboBoxName, object selectedItem)
{
InvokeComboBoxSelectionChanged(comboBoxName, selectedItem);
NotifyThemeUpdateIfNeeded(comboBoxName);
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void InvokeToggleSwitchToggledWithThemeCheck(string toggleSwitchName, bool isOn)
{
InvokeToggleSwitchToggled(toggleSwitchName, isOn);
NotifyThemeUpdateIfNeeded(toggleSwitchName);
}
2026-01-10 17:31:55 +08:00
2026-01-02 01:35:22 +08:00
public static void EnableTouchSupportForControls(DependencyObject parent)
{
if (parent == null) return;
for (int i = 0; i < Media.VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = Media.VisualTreeHelper.GetChild(parent, i);
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
if (child is Border border)
{
if (border.Tag != null || border.Cursor == Cursors.Hand)
{
border.IsManipulationEnabled = true;
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
border.TouchDown += (s, e) =>
{
var touchPoint = e.GetTouchPoint(border);
var mouseEvent = new MouseButtonEventArgs(Mouse.PrimaryDevice, Environment.TickCount, MouseButton.Left)
{
RoutedEvent = UIElement.MouseLeftButtonDownEvent,
Source = border
};
border.RaiseEvent(mouseEvent);
border.CaptureTouch(e.TouchDevice);
e.Handled = true;
};
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
border.TouchUp += (s, e) =>
{
border.ReleaseTouchCapture(e.TouchDevice);
e.Handled = true;
};
}
}
else if (child is Button button)
{
button.IsManipulationEnabled = true;
}
else if (child is ComboBox comboBox)
{
comboBox.IsManipulationEnabled = true;
}
else if (child is Slider slider)
{
slider.IsManipulationEnabled = true;
}
else if (child is TextBox textBox)
{
textBox.IsManipulationEnabled = true;
}
else if (child is CheckBox checkBox)
{
checkBox.IsManipulationEnabled = true;
}
else if (child is RadioButton radioButton)
{
radioButton.IsManipulationEnabled = true;
}
2026-03-03 16:04:20 +08:00
2026-01-02 01:35:22 +08:00
EnableTouchSupportForControls(child);
}
}
}
}