Files
community/Ink Canvas/Windows/SettingsViews/SettingsViews/LuckyRandomPanel.xaml.cs
T

365 lines
14 KiB
C#
Raw Normal View History

2026-01-02 12:22:50 +08:00
using Ink_Canvas;
2026-01-02 01:35:22 +08:00
using iNKORE.UI.WPF.Helpers;
2025-12-28 08:48:09 +08:00
using System;
2026-01-02 01:35:22 +08:00
using System.Collections.Generic;
using System.Linq;
2025-12-28 08:48:09 +08:00
using System.Windows;
using System.Windows.Controls;
2026-01-02 01:35:22 +08:00
using System.Windows.Media;
using Application = System.Windows.Application;
2025-12-28 08:48:09 +08:00
namespace Ink_Canvas.Windows.SettingsViews
{
public partial class LuckyRandomPanel : UserControl
{
2026-01-02 01:35:22 +08:00
private bool _isLoaded = false;
2025-12-28 08:48:09 +08:00
public LuckyRandomPanel()
{
InitializeComponent();
2026-01-02 01:35:22 +08:00
Loaded += LuckyRandomPanel_Loaded;
}
private void LuckyRandomPanel_Loaded(object sender, RoutedEventArgs e)
{
LoadSettings();
EnableTouchSupport();
ApplyTheme();
_isLoaded = true;
}
private void EnableTouchSupport()
{
try
{
Dispatcher.BeginInvoke(new Action(() =>
{
MainWindowSettingsHelper.EnableTouchSupportForControls(this);
}), System.Windows.Threading.DispatcherPriority.Loaded);
}
catch (Exception ex)
{
2026-01-02 12:22:50 +08:00
System.Diagnostics.Debug.WriteLine($"LuckyRandomPanel 启用触摸支持时出? {ex.Message}");
2026-01-02 01:35:22 +08:00
}
2025-12-28 08:48:09 +08:00
}
public event EventHandler<RoutedEventArgs> IsTopBarNeedShadowEffect;
public event EventHandler<RoutedEventArgs> IsTopBarNeedNoShadowEffect;
private void ScrollViewerEx_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
var scrollViewer = (ScrollViewer)sender;
if (scrollViewer.VerticalOffset >= 10)
{
IsTopBarNeedShadowEffect?.Invoke(this, new RoutedEventArgs());
}
else
{
IsTopBarNeedNoShadowEffect?.Invoke(this, new RoutedEventArgs());
}
}
2026-01-02 01:35:22 +08:00
public void LoadSettings()
{
if (MainWindow.Settings == null || MainWindow.Settings.RandSettings == null) return;
_isLoaded = false;
try
{
var randSettings = MainWindow.Settings.RandSettings;
SetToggleSwitchState(FindToggleSwitch("ToggleSwitchDisplayRandWindowNamesInputBtn"), randSettings.DisplayRandWindowNamesInputBtn);
SetToggleSwitchState(FindToggleSwitch("ToggleSwitchShowRandomAndSingleDraw"), randSettings.ShowRandomAndSingleDraw);
SetToggleSwitchState(FindToggleSwitch("ToggleSwitchEnableQuickDraw"), randSettings.EnableQuickDraw);
SetToggleSwitchState(FindToggleSwitch("ToggleSwitchExternalCaller"), randSettings.DirectCallCiRand);
SetOptionButtonState("ExternalCallerType", randSettings.ExternalCallerType);
if (RandWindowOnceCloseLatencySlider != null)
{
RandWindowOnceCloseLatencySlider.Value = randSettings.RandWindowOnceCloseLatency;
if (RandWindowOnceCloseLatencyText != null)
{
RandWindowOnceCloseLatencyText.Text = $"{randSettings.RandWindowOnceCloseLatency:F1}s";
}
}
if (RandWindowOnceMaxStudentsSlider != null)
{
RandWindowOnceMaxStudentsSlider.Value = randSettings.RandWindowOnceMaxStudents;
if (RandWindowOnceMaxStudentsText != null)
{
RandWindowOnceMaxStudentsText.Text = randSettings.RandWindowOnceMaxStudents.ToString();
}
}
SetToggleSwitchState(FindToggleSwitch("ToggleSwitchUseNewRollCallUI"), randSettings.UseNewRollCallUI);
SetToggleSwitchState(FindToggleSwitch("ToggleSwitchEnableMLAvoidance"), randSettings.EnableMLAvoidance);
if (MLAvoidanceHistorySlider != null)
{
MLAvoidanceHistorySlider.Value = randSettings.MLAvoidanceHistoryCount;
if (MLAvoidanceHistoryText != null)
{
MLAvoidanceHistoryText.Text = randSettings.MLAvoidanceHistoryCount.ToString();
}
}
if (MLAvoidanceWeightSlider != null)
{
MLAvoidanceWeightSlider.Value = randSettings.MLAvoidanceWeight;
if (MLAvoidanceWeightText != null)
{
MLAvoidanceWeightText.Text = $"{(randSettings.MLAvoidanceWeight * 100):F0}%";
}
}
SetOptionButtonState("PickNameBackground", randSettings.SelectedBackgroundIndex);
}
catch (Exception ex)
{
2026-01-02 12:22:50 +08:00
System.Diagnostics.Debug.WriteLine($"加载幸运随机设置时出? {ex.Message}");
2026-01-02 01:35:22 +08:00
}
_isLoaded = true;
}
private Border FindToggleSwitch(string name)
{
return this.FindDescendantByName(name) as Border;
}
private void SetToggleSwitchState(Border toggleSwitch, bool isOn)
{
if (toggleSwitch == null) return;
toggleSwitch.Background = isOn
2026-01-02 12:22:50 +08:00
? ThemeHelper.GetToggleSwitchOnBackgroundBrush()
: ThemeHelper.GetToggleSwitchOffBackgroundBrush();
2026-01-02 01:35:22 +08:00
var innerBorder = toggleSwitch.Child as Border;
if (innerBorder != null)
{
innerBorder.HorizontalAlignment = isOn ? HorizontalAlignment.Right : HorizontalAlignment.Left;
}
}
private void SetOptionButtonState(string group, int selectedIndex)
{
var buttons = new Dictionary<string, string[]>
{
{ "ExternalCallerType", new[] { "ClassIsland", "SecRandom", "NamePicker" } },
{ "PickNameBackground", new[] { "Default" } }
};
if (!buttons.ContainsKey(group)) return;
string[] buttonNames = buttons[group];
for (int i = 0; i < buttonNames.Length; i++)
{
var button = this.FindDescendantByName($"{group}{buttonNames[i]}Border") as Border;
if (button != null)
{
2026-01-02 12:22:50 +08:00
ThemeHelper.SetOptionButtonSelectedState(button, i == selectedIndex);
2026-01-02 01:35:22 +08:00
}
}
}
private void ToggleSwitch_Click(object sender, RoutedEventArgs e)
{
if (!_isLoaded) return;
var border = sender as Border;
if (border == null) return;
2026-01-02 12:22:50 +08:00
bool isOn = ThemeHelper.IsToggleSwitchOn(border.Background);
2026-01-02 01:35:22 +08:00
bool newState = !isOn;
SetToggleSwitchState(border, newState);
string tag = border.Tag?.ToString();
if (string.IsNullOrEmpty(tag)) return;
var randSettings = MainWindow.Settings.RandSettings;
if (randSettings == null) return;
switch (tag)
{
case "DisplayRandWindowNamesInputBtn":
MainWindowSettingsHelper.InvokeToggleSwitchToggled("ToggleSwitchDisplayRandWindowNamesInputBtn", newState);
break;
case "ShowRandomAndSingleDraw":
MainWindowSettingsHelper.InvokeToggleSwitchToggled("ToggleSwitchShowRandomAndSingleDraw", newState);
break;
case "EnableQuickDraw":
MainWindowSettingsHelper.InvokeToggleSwitchToggled("ToggleSwitchEnableQuickDraw", newState);
break;
case "ExternalCaller":
MainWindowSettingsHelper.InvokeToggleSwitchToggled("ToggleSwitchExternalCaller", newState);
break;
case "UseNewRollCallUI":
MainWindowSettingsHelper.InvokeToggleSwitchToggled("ToggleSwitchUseNewRollCallUI", newState);
break;
case "EnableMLAvoidance":
MainWindowSettingsHelper.InvokeToggleSwitchToggled("ToggleSwitchEnableMLAvoidance", newState);
break;
}
}
private void OptionButton_Click(object sender, RoutedEventArgs e)
{
if (!_isLoaded) return;
var border = sender as Border;
if (border == null) return;
string tag = border.Tag?.ToString();
if (string.IsNullOrEmpty(tag)) return;
string[] parts = tag.Split('_');
if (parts.Length < 2) return;
string group = parts[0];
string value = parts[1];
var parent = border.Parent as Panel;
if (parent != null)
{
foreach (var child in parent.Children)
{
if (child is Border childBorder && childBorder != border)
{
string childTag = childBorder.Tag?.ToString();
if (!string.IsNullOrEmpty(childTag) && childTag.StartsWith(group + "_"))
{
2026-01-02 12:22:50 +08:00
ThemeHelper.SetOptionButtonSelectedState(childBorder, false);
2026-01-02 01:35:22 +08:00
}
}
}
}
2026-01-02 12:22:50 +08:00
ThemeHelper.SetOptionButtonSelectedState(border, true);
2026-01-02 01:35:22 +08:00
var randSettings = MainWindow.Settings.RandSettings;
if (randSettings == null) return;
switch (group)
{
case "ExternalCallerType":
int callerType;
switch (value)
{
case "ClassIsland":
callerType = 0;
break;
case "SecRandom":
callerType = 1;
break;
case "NamePicker":
callerType = 2;
break;
default:
callerType = 0;
break;
}
var mainWindow = Application.Current.MainWindow as MainWindow;
if (mainWindow != null)
{
var comboBox = mainWindow.FindName("ComboBoxExternalCallerType") as System.Windows.Controls.ComboBox;
if (comboBox != null && comboBox.Items.Count > callerType)
{
comboBox.SelectedIndex = callerType;
MainWindowSettingsHelper.InvokeComboBoxSelectionChanged("ComboBoxExternalCallerType", comboBox.Items[callerType]);
}
else
{
MainWindowSettingsHelper.UpdateSettingDirectly(() =>
{
2026-01-02 09:47:34 +08:00
randSettings.ExternalCallerType = callerType;
2026-01-02 01:35:22 +08:00
}, "ComboBoxExternalCallerType");
}
}
break;
case "PickNameBackground":
MainWindowSettingsHelper.UpdateSettingDirectly(() =>
{
2026-01-02 12:22:50 +08:00
randSettings.SelectedBackgroundIndex = 0;
2026-01-02 01:35:22 +08:00
}, "PickNameBackground");
break;
}
}
private void RandWindowOnceCloseLatencySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!_isLoaded) return;
if (RandWindowOnceCloseLatencySlider != null && RandWindowOnceCloseLatencyText != null)
{
double value = RandWindowOnceCloseLatencySlider.Value;
RandWindowOnceCloseLatencyText.Text = $"{value:F1}s";
MainWindowSettingsHelper.InvokeSliderValueChanged("RandWindowOnceCloseLatencySlider", value);
}
}
private void RandWindowOnceMaxStudentsSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!_isLoaded) return;
if (RandWindowOnceMaxStudentsSlider != null && RandWindowOnceMaxStudentsText != null)
{
double value = RandWindowOnceMaxStudentsSlider.Value;
RandWindowOnceMaxStudentsText.Text = ((int)value).ToString();
MainWindowSettingsHelper.InvokeSliderValueChanged("RandWindowOnceMaxStudentsSlider", value);
}
}
private void MLAvoidanceHistorySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!_isLoaded) return;
if (MLAvoidanceHistorySlider != null && MLAvoidanceHistoryText != null)
{
double value = MLAvoidanceHistorySlider.Value;
MLAvoidanceHistoryText.Text = ((int)value).ToString();
MainWindowSettingsHelper.InvokeSliderValueChanged("MLAvoidanceHistorySlider", value);
}
}
private void MLAvoidanceWeightSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!_isLoaded) return;
if (MLAvoidanceWeightSlider != null && MLAvoidanceWeightText != null)
{
double value = MLAvoidanceWeightSlider.Value;
MLAvoidanceWeightText.Text = $"{(value * 100):F0}%";
MainWindowSettingsHelper.InvokeSliderValueChanged("MLAvoidanceWeightSlider", value);
}
}
public void ApplyTheme()
{
try
{
ThemeHelper.ApplyThemeToControl(this);
2026-01-02 12:22:50 +08:00
if (MainWindow.Settings?.RandSettings != null)
{
var randSettings = MainWindow.Settings.RandSettings;
SetOptionButtonState("ExternalCallerType", randSettings.ExternalCallerType);
SetOptionButtonState("PickNameBackground", randSettings.SelectedBackgroundIndex);
}
2026-01-02 01:35:22 +08:00
}
catch (Exception ex)
{
2026-01-02 12:22:50 +08:00
System.Diagnostics.Debug.WriteLine($"LuckyRandomPanel 应用主题时出? {ex.Message}");
2026-01-02 01:35:22 +08:00
}
}
2025-12-28 08:48:09 +08:00
}
}