From 12227b35306a22ba7c778ca5ea4f54c6853d589a Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 16 Mar 2021 19:29:18 +0100 Subject: [PATCH] Color picker - Remember last 18 picked colors --- .../Controls/ColorPicker.xaml | 36 +++++++++---- .../Controls/ColorPicker.xaml.cs | 24 +++++++++ .../Services/ColorPickerService.cs | 50 ++++++++++++------- .../Interfaces/IColorPickerService.cs | 6 ++- 4 files changed, 89 insertions(+), 27 deletions(-) diff --git a/src/Artemis.UI.Shared/Controls/ColorPicker.xaml b/src/Artemis.UI.Shared/Controls/ColorPicker.xaml index 0717700b2..7b671e60d 100644 --- a/src/Artemis.UI.Shared/Controls/ColorPicker.xaml +++ b/src/Artemis.UI.Shared/Controls/ColorPicker.xaml @@ -94,19 +94,20 @@ StaysOpen="{Binding StaysOpen, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" PopupAnimation="Fade" IsOpen="{Binding PopupOpen, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"> - + - - - + + + + - + + + + + + + + + + + + + + + + + + + - - Preview on devices - diff --git a/src/Artemis.UI.Shared/Controls/ColorPicker.xaml.cs b/src/Artemis.UI.Shared/Controls/ColorPicker.xaml.cs index 6f1255b10..9a5a4fb02 100644 --- a/src/Artemis.UI.Shared/Controls/ColorPicker.xaml.cs +++ b/src/Artemis.UI.Shared/Controls/ColorPicker.xaml.cs @@ -1,9 +1,11 @@ using System; +using System.Collections.ObjectModel; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Input; using System.Windows.Media; +using System.Windows.Shapes; using Artemis.UI.Shared.Services; namespace Artemis.UI.Shared @@ -146,6 +148,12 @@ namespace Artemis.UI.Shared colorPicker._inCallback = true; colorPicker.OnPropertyChanged(nameof(PopupOpen)); + + if ((bool) e.NewValue) + colorPicker.PopupOpened(); + else + colorPicker.PopupClosed(); + colorPicker._inCallback = false; } @@ -230,6 +238,22 @@ namespace Artemis.UI.Shared PreviewCheckBox.IsChecked = _colorPickerService.PreviewSetting.Value; } + private void PopupClosed() + { + _colorPickerService?.QueueRecentColor(Color); + } + + private void PopupOpened() + { + RecentColorsContainer.ItemsSource = new ObservableCollection(_colorPickerService.RecentColors); + } + + private void SelectRecentColor(object sender, MouseButtonEventArgs e) + { + Color = (Color) ((Rectangle) sender).DataContext; + PopupOpen = false; + } + /// public event PropertyChangedEventHandler? PropertyChanged; diff --git a/src/Artemis.UI.Shared/Services/ColorPickerService.cs b/src/Artemis.UI.Shared/Services/ColorPickerService.cs index aaea09b8e..a3602c1b7 100644 --- a/src/Artemis.UI.Shared/Services/ColorPickerService.cs +++ b/src/Artemis.UI.Shared/Services/ColorPickerService.cs @@ -24,10 +24,35 @@ namespace Artemis.UI.Shared.Services PreviewSetting = settingsService.GetSetting("UI.PreviewColorPickerOnDevices", false); PreviewSetting.AutoSave = true; + RecentColorsSetting = settingsService.GetSetting("UI.ColorPickerRecentColors", new LinkedList()); + } + + private void RenderColorPickerOverlay(object? sender, FrameRenderingEventArgs e) + { + if (_mustRenderOverlay) + _overlayOpacity += 0.2f; + else + _overlayOpacity -= 0.2f; + + if (_overlayOpacity <= 0f) + { + _coreService.FrameRendering -= RenderColorPickerOverlay; + return; + } + + if (_overlayOpacity > 1f) + _overlayOpacity = 1f; + + using SKPaint overlayPaint = new() {Color = new SKColor(0, 0, 0, (byte) (255 * _overlayOpacity))}; + overlayPaint.Color = _overlayColor.WithAlpha((byte) (_overlayColor.Alpha * _overlayOpacity)); + e.Canvas.DrawRect(0, 0, e.Canvas.LocalClipBounds.Width, e.Canvas.LocalClipBounds.Height, overlayPaint); } public PluginSetting PreviewSetting { get; } - + public PluginSetting> RecentColorsSetting { get; } + + public LinkedList RecentColors => RecentColorsSetting.Value; + public Task ShowGradientPicker(ColorGradient colorGradient, string dialogHost) { if (!string.IsNullOrWhiteSpace(dialogHost)) @@ -56,25 +81,16 @@ namespace Artemis.UI.Shared.Services _overlayColor = new SKColor(color.R, color.G, color.B, color.A); } - private void RenderColorPickerOverlay(object? sender, FrameRenderingEventArgs e) + public void QueueRecentColor(Color color) { - if (_mustRenderOverlay) - _overlayOpacity += 0.2f; - else - _overlayOpacity -= 0.2f; + if (RecentColors.Contains(color)) + RecentColors.Remove(color); - if (_overlayOpacity <= 0f) - { - _coreService.FrameRendering -= RenderColorPickerOverlay; - return; - } + RecentColors.AddFirst(color); + while (RecentColors.Count > 18) + RecentColors.RemoveLast(); - if (_overlayOpacity > 1f) - _overlayOpacity = 1f; - - using SKPaint overlayPaint = new() {Color = new SKColor(0, 0, 0, (byte) (255 * _overlayOpacity))}; - overlayPaint.Color = _overlayColor.WithAlpha((byte) (_overlayColor.Alpha * _overlayOpacity)); - e.Canvas.DrawRect(0, 0, e.Canvas.LocalClipBounds.Width, e.Canvas.LocalClipBounds.Height, overlayPaint); + RecentColorsSetting.Save(); } } } \ No newline at end of file diff --git a/src/Artemis.UI.Shared/Services/Interfaces/IColorPickerService.cs b/src/Artemis.UI.Shared/Services/Interfaces/IColorPickerService.cs index 4258d7596..a5bbc26f8 100644 --- a/src/Artemis.UI.Shared/Services/Interfaces/IColorPickerService.cs +++ b/src/Artemis.UI.Shared/Services/Interfaces/IColorPickerService.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Threading.Tasks; using System.Windows.Media; using Artemis.Core; @@ -9,8 +10,11 @@ namespace Artemis.UI.Shared.Services Task ShowGradientPicker(ColorGradient colorGradient, string dialogHost); PluginSetting PreviewSetting { get; } + LinkedList RecentColors { get; } + PluginSetting> RecentColorsSetting { get; } void StartColorDisplay(); void StopColorDisplay(); void UpdateColorDisplay(Color color); + void QueueRecentColor(Color color); } } \ No newline at end of file