feat: finish color picker

This commit is contained in:
2025-09-08 16:28:08 +08:00
parent 81c46ea0f2
commit 082ceec5e2
2 changed files with 81 additions and 2 deletions

View File

@ -41,7 +41,7 @@
<TabControl Grid.Row="1" Margin="5"> <TabControl Grid.Row="1" Margin="5">
<TabItem Header="Material Design" Padding="5"> <TabItem Header="Material Design" Padding="5">
<WrapPanel Orientation="Horizontal"> <WrapPanel x:Name="uiMdColorPresetList" Orientation="Horizontal">
</WrapPanel> </WrapPanel>
</TabItem> </TabItem>

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
@ -25,8 +26,11 @@ namespace HFUTCourseSimulation.Widget {
private Color _foregroundBackup, _backgroundBackup; private Color _foregroundBackup, _backgroundBackup;
private void uiMainWindow_Loaded(object sender, RoutedEventArgs e) { private void uiMainWindow_Loaded(object sender, RoutedEventArgs e) {
// Set context and backup color for restoring // Build color list
BuildColorList();
// Set context
uiPicker.DataContext = CurrentColor; uiPicker.DataContext = CurrentColor;
// Backup color for restoring
_foregroundBackup = CurrentColor.Foreground; _foregroundBackup = CurrentColor.Foreground;
_backgroundBackup = CurrentColor.Background; _backgroundBackup = CurrentColor.Background;
} }
@ -35,6 +39,78 @@ namespace HFUTCourseSimulation.Widget {
uiPicker.DataContext = null; uiPicker.DataContext = null;
} }
#region Color Box Stuff
private Border CreateColorBox(Util.ColorPair c, string name) {
var colorBox = new Border();
colorBox.Width = 100;
colorBox.Height = 100;
colorBox.Background = new SolidColorBrush(c.Background);
colorBox.BorderBrush = new SolidColorBrush(Colors.Black);
colorBox.BorderThickness = new Thickness(1);
colorBox.CornerRadius = new CornerRadius(5);
colorBox.Margin = new Thickness(5);
colorBox.Cursor = Cursors.Hand;
var colorText = new TextBlock();
colorText.Text = name;
colorText.Foreground = new SolidColorBrush(c.Foreground);
colorText.HorizontalAlignment = HorizontalAlignment.Center;
colorText.VerticalAlignment = VerticalAlignment.Center;
colorBox.Child = colorText;
return colorBox;
}
private class ColorPropertyWrapper {
public ColorPropertyWrapper(PropertyInfo prop) {
_prop = prop;
}
public string GetName() => _prop.Name;
public Util.ColorPair GetColor() {
var value = _prop.GetValue(null);
if (value is Util.ColorPair color) {
return color;
} else {
return new Util.ColorPair();
}
}
private PropertyInfo _prop;
}
private IEnumerable<ColorPropertyWrapper> FilterColorProperties(Type t) {
return t.GetProperties(BindingFlags.Public | BindingFlags.Static)
.Where((prop) => prop.PropertyType == typeof(Util.ColorPair))
.Select((prop) => new ColorPropertyWrapper(prop));
}
private Dictionary<object, Util.ColorPair> _knownColorPresets = new Dictionary<object, Util.ColorPair>();
private void BuildColorList() {
_knownColorPresets .Clear();
uiMdColorPresetList.Children.Clear();
foreach (var prop in FilterColorProperties(typeof(Util.ColorPreset.MdColors))) {
var box = CreateColorBox(prop.GetColor(), prop.GetName());
box.MouseDown += ColorBox_Click;
uiMdColorPresetList.Children.Add(box);
_knownColorPresets.Add(box, prop.GetColor());
}
}
private void ColorBox_Click(object sender, MouseButtonEventArgs e) {
if (_knownColorPresets.TryGetValue(sender, out var value)) {
CurrentColor.Foreground = value.Foreground;
CurrentColor.Background = value.Background;
}
}
#endregion
#region Ui Handlers
private void uiForegroundPicker_MouseDown(object sender, MouseButtonEventArgs e) { private void uiForegroundPicker_MouseDown(object sender, MouseButtonEventArgs e) {
var color = Util.Win32Dialog.PickColor(); var color = Util.Win32Dialog.PickColor();
if (color is null) return; if (color is null) return;
@ -56,5 +132,8 @@ namespace HFUTCourseSimulation.Widget {
CurrentColor.Background = _backgroundBackup; CurrentColor.Background = _backgroundBackup;
this.Close(); this.Close();
} }
#endregion
} }
} }