Add color picker window

This commit is contained in:
Peter Kirmeier 2023-06-04 16:41:48 +02:00
parent 28fdd938ee
commit d6bf3ed42a
6 changed files with 111 additions and 25 deletions

View file

@ -180,6 +180,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PixiEditor.ColorPicker" Version="3.3.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View file

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (c) 2023-2023 Peter Kirmeier -->
<!-- TODO: Localization of Title -->
<Window 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:colorpicker="clr-namespace:ColorPicker;assembly=ColorPicker"
xmlns:u="clr-namespace:SystemTrayMenu.Utilities"
xmlns:stm="clr-namespace:SystemTrayMenu"
x:Class="SystemTrayMenu.UserInterface.ColorPickerWindow"
mc:Ignorable="d" Title="{u:Translate 'Choose color'}" Foreground="{x:Static stm:MenuDefines.ColorForeground}" Background="{x:Static stm:MenuDefines.ColorBackground}"
WindowStartupLocation="CenterScreen" SizeToContent="WidthAndHeight" ResizeMode="CanResizeWithGrip">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0" x:Name="lblDescription" x:FieldModifier="private" Content="" Foreground="{x:Static stm:MenuDefines.ColorForeground}" Padding="5 3" VerticalAlignment="Center"/>
<!-- https://github.com/PixiEditor/ColorPicker -->
<colorpicker:StandardColorPicker Grid.Row="1" x:Name="picker" x:FieldModifier="private" Width="200" Height="380" ShowAlpha="False"/>
<StackPanel Grid.Row="2" Margin="0 0 15 0" Orientation="Horizontal" HorizontalAlignment="Right">
<Button x:Name="buttonOk" Content="{u:Translate 'OK'}" Margin="3" MinWidth="76" Click="ButtonOk_Click"/>
<Button x:Name="buttonCancel" Content="{u:Translate 'Abort'}" Margin="3" MinWidth="76" Click="ButtonCancel_Click"/>
</StackPanel>
</Grid>
</Window>

View file

@ -0,0 +1,61 @@
// <copyright file="ColorPickerWindow.xaml.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
//
// Copyright (c) 2023-2023 Peter Kirmeier
namespace SystemTrayMenu.UserInterface
{
using System;
using System.Windows;
using System.Windows.Media;
/// <summary>
/// Logic of ColorPickerWindow.xaml .
/// </summary>
public partial class ColorPickerWindow : Window
{
internal ColorPickerWindow(string description, Color initialColor)
{
InitializeComponent();
if (Config.IsDarkMode())
{
ResourceDictionary resDict = new ();
resDict.Source = new Uri("pack://application:,,,/ColorPicker;component/Styles/DefaultColorPickerStyle.xaml", UriKind.RelativeOrAbsolute);
picker.Style = (Style)resDict["DefaultColorPickerStyle"];
}
Loaded += (_, _) =>
{
MinWidth = ActualWidth;
MinHeight = ActualHeight;
// Issue: Placement of picker child elements incorrect.
// Beyond initial layout updates it requires to have a fixed size set.
// But this will force us to manually update on resize events.
// Workaround: Remove the fixed values and witch back to automatic size calculation afterwards.
SizeChanged += UnsetSize;
void UnsetSize(object sender, RoutedEventArgs e)
{
SizeChanged -= UnsetSize;
picker.Width = double.NaN;
picker.Height = double.NaN;
}
};
picker.SelectedColor = picker.SecondaryColor = initialColor;
lblDescription.Content = description;
}
public Color SelectedColor => picker.SelectedColor;
private void ButtonOk_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
Close();
}
private void ButtonCancel_Click(object sender, RoutedEventArgs e) => Close();
}
}

View file

@ -1,16 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (c) 2022-2022 Peter Kirmeier -->
<!-- Copyright (c) 2022-2023 Peter Kirmeier -->
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SystemTrayMenu.UserInterface"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Class="SystemTrayMenu.UserInterface.ColorSelector"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal">
<Border x:Name="pane" x:FieldModifier="private" d:Background="#FF007F" BorderThickness="1" BorderBrush="Gray"
Height="{Binding RelativeSource={RelativeSource Self}, Path=Parent.ActualHeight}"
Width="{Binding Path=Height, RelativeSource={RelativeSource Self}}" />
Width="{Binding Path=Height, RelativeSource={RelativeSource Self}}" MouseDown="Shape_MouseDown"/>
<TextBox x:Name="txtbox" x:FieldModifier="private" Margin="2,0,5,0" VerticalContentAlignment="Center" Width="70" TextChanged="Txtbox_TextChanged" d:Text="#FF007F"/>
<Label x:Name="label" x:FieldModifier="private" Padding="0" VerticalContentAlignment="Center" />
</StackPanel>

View file

@ -5,9 +5,6 @@
namespace SystemTrayMenu.UserInterface
{
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
@ -67,18 +64,18 @@ namespace SystemTrayMenu.UserInterface
ColorChanged?.Invoke(this);
}
#if TODO // ColorPicker
private void PictureBoxClick(object sender, EventArgs e)
private void Shape_MouseDown(object sender, MouseButtonEventArgs e)
{
PictureBox pictureBox = (PictureBox)sender;
TextBox textBox = (TextBox)pictureBox.Tag;
colorDialog.Color = pictureBox.BackColor;
if (colorDialog.ShowDialog() == DialogResult.OK)
if (e.LeftButton == MouseButtonState.Pressed)
{
textBox.Text = ColorTranslator.ToHtml(colorDialog.Color);
pictureBox.BackColor = colorDialog.Color;
ColorPickerWindow dialog = new(Description, Colors.LightYellow);
if (dialog.ShowDialog() ?? false)
{
Text = dialog.SelectedColor.ToString();
}
e.Handled = true;
}
}
#endif
}
}

View file

@ -28,7 +28,7 @@ namespace SystemTrayMenu.UserInterface
private const string MenuName = @"Software\Classes\directory\shell\SystemTrayMenu_SetAsRootFolder";
private const string Command = @"Software\Classes\directory\shell\SystemTrayMenu_SetAsRootFolder\command";
private static SettingsWindow? settingsForm;
private static SettingsWindow? singletonWindow;
public SettingsWindow()
{
@ -355,26 +355,23 @@ namespace SystemTrayMenu.UserInterface
textBoxColorArrowHoverDarkMode.Text = Settings.Default.ColorArrowHoverDarkMode;
textBoxColorArrowHoverBackgroundDarkMode.Text = Settings.Default.ColorArrowHoverBackgroundDarkMode;
Closed += (_, _) => settingsForm = null;
Closed += (_, _) => singletonWindow = null;
}
public static void ShowSingleInstance()
{
if (IsOpen())
{
settingsForm!.HandleInvoke(() => settingsForm?.Activate());
singletonWindow!.HandleInvoke(() => singletonWindow?.Activate());
}
else
{
settingsForm = new();
settingsForm.Show();
singletonWindow = new();
singletonWindow.Show();
}
}
public static bool IsOpen()
{
return settingsForm != null;
}
public static bool IsOpen() => singletonWindow != null;
[SupportedOSPlatform("windows")]
private static void AddSetFolderByWindowsContextMenu()