1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Color picker - Remember last 18 picked colors

This commit is contained in:
Robert 2021-03-16 19:29:18 +01:00
parent f646f64648
commit 12227b3530
4 changed files with 89 additions and 27 deletions

View File

@ -94,19 +94,20 @@
StaysOpen="{Binding StaysOpen, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
PopupAnimation="Fade"
IsOpen="{Binding PopupOpen, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
<materialDesign:Card Width="200" Height="230" Margin="30" materialDesign:ShadowAssist.ShadowDepth="Depth4">
<materialDesign:Card Width="200" Height="260" Margin="30" materialDesign:ShadowAssist.ShadowDepth="Depth4">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="160" />
<RowDefinition />
<RowDefinition />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<materialDesign:ColorPicker Grid.Row="0"
Color="{Binding Color, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
MouseUp="ColorGradient_OnMouseUp"
PreviewMouseDown="Slider_OnMouseDown"
PreviewMouseUp="Slider_OnMouseUp" />
<Slider Grid.Row="1" Margin="8"
<Slider Grid.Row="1" Margin="8 2 8 8"
IsMoveToPointEnabled="True"
Orientation="Horizontal"
Style="{DynamicResource MaterialDesignColorSlider}"
@ -115,17 +116,34 @@
PreviewMouseDown="Slider_OnMouseDown"
PreviewMouseUp="Slider_OnMouseUp"
Maximum="255" />
<ItemsControl Grid.Row="2" x:Name="RecentColorsContainer" Margin="10 0" HorizontalAlignment="Left">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border CornerRadius="4" Background="{StaticResource Checkerboard}" Width="16" Height="16" Margin="2">
<Rectangle RadiusX="4" RadiusY="4" Cursor="Hand" MouseLeftButtonDown="SelectRecentColor" ToolTip="{Binding}">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding}" />
</Rectangle.Fill>
</Rectangle>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"></WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<!-- Checkboxes don't work inside popups inside tree views, which is where the color picker is regularly used -->
<CheckBox Grid.Row="2"
<CheckBox Grid.Row="3"
x:Name="PreviewCheckBox"
Margin="10 0"
Margin="10 5 0 10"
VerticalAlignment="Center"
Style="{StaticResource MaterialDesignCheckBox}"
Click="PreviewCheckBoxClick">
Preview on devices
</CheckBox>
</Grid>
</materialDesign:Card>
</Popup>

View File

@ -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<Color>(_colorPickerService.RecentColors);
}
private void SelectRecentColor(object sender, MouseButtonEventArgs e)
{
Color = (Color) ((Rectangle) sender).DataContext;
PopupOpen = false;
}
/// <inheritdoc />
public event PropertyChangedEventHandler? PropertyChanged;

View File

@ -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<Color>());
}
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<bool> PreviewSetting { get; }
public PluginSetting<LinkedList<Color>> RecentColorsSetting { get; }
public LinkedList<Color> RecentColors => RecentColorsSetting.Value;
public Task<object> 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();
}
}
}

View File

@ -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<object> ShowGradientPicker(ColorGradient colorGradient, string dialogHost);
PluginSetting<bool> PreviewSetting { get; }
LinkedList<Color> RecentColors { get; }
PluginSetting<LinkedList<Color>> RecentColorsSetting { get; }
void StartColorDisplay();
void StopColorDisplay();
void UpdateColorDisplay(Color color);
void QueueRecentColor(Color color);
}
}