mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 11:56:30 +00:00
Gradient editor - Implemented stop create/remove and edit
This commit is contained in:
1 parent
81ec5bfe8f
commit
83cf9a45fb
12 files changed
+318
-82
No files matched your search
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Animation;
|
||||
|
||||
namespace Artemis.UI.Shared.AnimationTimeline
|
||||
{
|
||||
public class BrushAnimation : System.Windows.Media.Animation.AnimationTimeline
|
||||
{
|
||||
public static readonly DependencyProperty FromProperty =
|
||||
DependencyProperty.Register("From", typeof(Brush), typeof(BrushAnimation));
|
||||
|
||||
public static readonly DependencyProperty ToProperty =
|
||||
DependencyProperty.Register("To", typeof(Brush), typeof(BrushAnimation));
|
||||
|
||||
public override Type TargetPropertyType => typeof(Brush);
|
||||
|
||||
//we must define From and To, AnimationTimeline does not have this properties
|
||||
public Brush From
|
||||
{
|
||||
get => (Brush) GetValue(FromProperty);
|
||||
set => SetValue(FromProperty, value);
|
||||
}
|
||||
|
||||
public Brush To
|
||||
{
|
||||
get => (Brush) GetValue(ToProperty);
|
||||
set => SetValue(ToProperty, value);
|
||||
}
|
||||
|
||||
public override object GetCurrentValue(object defaultOriginValue,
|
||||
object defaultDestinationValue,
|
||||
AnimationClock animationClock)
|
||||
{
|
||||
return GetCurrentValue(defaultOriginValue as Brush,
|
||||
defaultDestinationValue as Brush,
|
||||
animationClock);
|
||||
}
|
||||
|
||||
public object GetCurrentValue(Brush defaultOriginValue,
|
||||
Brush defaultDestinationValue,
|
||||
AnimationClock animationClock)
|
||||
{
|
||||
if (!animationClock.CurrentProgress.HasValue)
|
||||
return Brushes.Transparent;
|
||||
|
||||
//use the standard values if From and To are not set
|
||||
//(it is the value of the given property)
|
||||
defaultOriginValue = From ?? defaultOriginValue;
|
||||
defaultDestinationValue = To ?? defaultDestinationValue;
|
||||
|
||||
if (animationClock.CurrentProgress.Value == 0)
|
||||
return defaultOriginValue;
|
||||
if (animationClock.CurrentProgress.Value == 1)
|
||||
return defaultDestinationValue;
|
||||
|
||||
return new VisualBrush(new Border
|
||||
{
|
||||
Width = 1,
|
||||
Height = 1,
|
||||
Background = defaultOriginValue,
|
||||
Child = new Border
|
||||
{
|
||||
Background = defaultDestinationValue,
|
||||
Opacity = animationClock.CurrentProgress.Value
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected override Freezable CreateInstanceCore()
|
||||
{
|
||||
return new BrushAnimation();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,20 +6,61 @@
|
||||
xmlns:local="clr-namespace:Artemis.UI.Shared.Screens.GradientEditor"
|
||||
xmlns:s="https://github.com/canton7/Stylet"
|
||||
xmlns:converters="clr-namespace:Artemis.UI.Shared.Converters"
|
||||
xmlns:utilities="clr-namespace:Artemis.UI.Shared.Utilities"
|
||||
xmlns:animationTimeline="clr-namespace:Artemis.UI.Shared.AnimationTimeline"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800"
|
||||
d:DataContext="{d:DesignInstance local:ColorStopViewModel}">
|
||||
d:DataContext="{d:DesignInstance local:ColorStopViewModel}" Opacity="1">
|
||||
<UserControl.Resources>
|
||||
<converters:SKColorToColorConverter x:Key="SKColorToColorConverter" />
|
||||
<Style TargetType="Path" x:Key="ColorStopStyle">
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding WillRemoveColorStop}" Value="True">
|
||||
<DataTrigger.EnterActions>
|
||||
<BeginStoryboard>
|
||||
<Storyboard TargetProperty="Opacity">
|
||||
<DoubleAnimation From="1" To="0" Duration="0:0:0.1" />
|
||||
</Storyboard>
|
||||
</BeginStoryboard>
|
||||
</DataTrigger.EnterActions>
|
||||
<DataTrigger.ExitActions>
|
||||
<BeginStoryboard>
|
||||
<Storyboard TargetProperty="Opacity">
|
||||
<DoubleAnimation From="0" To="1" Duration="0:0:0.2" />
|
||||
</Storyboard>
|
||||
</BeginStoryboard>
|
||||
</DataTrigger.ExitActions>
|
||||
</DataTrigger>
|
||||
<DataTrigger Binding="{Binding IsSelected}" Value="True">
|
||||
<DataTrigger.EnterActions>
|
||||
<BeginStoryboard>
|
||||
<Storyboard TargetProperty="StrokeThickness">
|
||||
<DoubleAnimation To="2" Duration="0:0:0.1" />
|
||||
</Storyboard>
|
||||
</BeginStoryboard>
|
||||
</DataTrigger.EnterActions>
|
||||
<DataTrigger.ExitActions>
|
||||
<BeginStoryboard>
|
||||
<Storyboard TargetProperty="StrokeThickness">
|
||||
<DoubleAnimation To="0" Duration="0:0:0.1" />
|
||||
</Storyboard>
|
||||
</BeginStoryboard>
|
||||
</DataTrigger.ExitActions>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</UserControl.Resources>
|
||||
<Path Data="M13.437011,33.065002 C9.7268463,29.334181 7.812011,26.379009 4.874511,23.379009 1.687011,19.566509 0.12600673,17.206803 5.6843419E-14,14.127608 0.062010996,2.0027046 11.158781,-0.062991121 13.43702,0.0014351187 M13.438011,33.065016 C17.148173,29.334199 19.063008,26.379023 22.00051,23.379017 25.188007,19.566519 26.749013,17.206806 26.875018,14.127613 26.813007,2.002704 15.716239,-0.062987381 13.438,0.0014388781"
|
||||
Stroke="{DynamicResource NormalBorderBrush}"
|
||||
StrokeThickness="2"
|
||||
<Path Style="{StaticResource ColorStopStyle}"
|
||||
Data="M13.437011,33.065002 C9.7268463,29.334181 7.812011,26.379009 4.874511,23.379009 1.687011,19.566509 0.12600673,17.206803 5.6843419E-14,14.127608 0.062010996,2.0027046 11.158781,-0.062991121 13.43702,0.0014351187 M13.438011,33.065016 C17.148173,29.334199 19.063008,26.379023 22.00051,23.379017 25.188007,19.566519 26.749013,17.206806 26.875018,14.127613 26.813007,2.002704 15.716239,-0.062987381 13.438,0.0014388781"
|
||||
Stroke="{DynamicResource MaterialDesignBody}"
|
||||
StrokeThickness="0"
|
||||
Cursor="Hand"
|
||||
MouseDown="{s:Action StopMouseDown}"
|
||||
MouseUp="{s:Action StopMouseUp}"
|
||||
MouseMove="{s:Action StopMouseMove}"
|
||||
Fill="{Binding Path=Color, Converter={StaticResource SKColorToColorConverter}}">
|
||||
MouseMove="{s:Action StopMouseMove}">
|
||||
<Path.Fill>
|
||||
<SolidColorBrush Color="{Binding Path=ColorStop.Color, Converter={StaticResource SKColorToColorConverter}}" />
|
||||
</Path.Fill>
|
||||
<Path.RenderTransform>
|
||||
<TransformGroup>
|
||||
<RotateTransform Angle="180" />
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using Artemis.Core.Models.Profile;
|
||||
using PropertyChanged;
|
||||
using SkiaSharp;
|
||||
using Artemis.UI.Shared.Utilities;
|
||||
using Stylet;
|
||||
|
||||
namespace Artemis.UI.Shared.Screens.GradientEditor
|
||||
@@ -12,69 +14,107 @@ namespace Artemis.UI.Shared.Screens.GradientEditor
|
||||
public class ColorStopViewModel : PropertyChangedBase
|
||||
{
|
||||
private readonly GradientEditorViewModel _gradientEditorViewModel;
|
||||
private bool _isSelected;
|
||||
private bool _willRemoveColorStop;
|
||||
|
||||
public ColorStopViewModel(GradientEditorViewModel gradientEditorViewModel)
|
||||
public ColorStopViewModel(GradientEditorViewModel gradientEditorViewModel, ColorGradientStop colorStop)
|
||||
{
|
||||
_gradientEditorViewModel = gradientEditorViewModel;
|
||||
ColorStop = colorStop;
|
||||
ColorStop.PropertyChanged += ColorStopOnPropertyChanged;
|
||||
}
|
||||
|
||||
public ColorGradientStop ColorStop { get; set; }
|
||||
public double Offset => ColorStop.Position * _gradientEditorViewModel.PreviewWidth;
|
||||
public SKColor Color => ColorStop.Color;
|
||||
private void ColorStopOnPropertyChanged(object sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
_gradientEditorViewModel.ColorGradient.OnColorValuesUpdated();
|
||||
}
|
||||
|
||||
public ColorGradientStop ColorStop { get; }
|
||||
|
||||
public double Offset
|
||||
{
|
||||
get => ColorStop.Position * _gradientEditorViewModel.PreviewWidth;
|
||||
set
|
||||
{
|
||||
ColorStop.Position = (float) Math.Round(value / _gradientEditorViewModel.PreviewWidth, 3, MidpointRounding.AwayFromZero);
|
||||
NotifyOfPropertyChange(nameof(Offset));
|
||||
NotifyOfPropertyChange(nameof(OffsetPercent));
|
||||
}
|
||||
}
|
||||
|
||||
public float OffsetPercent
|
||||
{
|
||||
get => (float) Math.Round(ColorStop.Position * 100.0, MidpointRounding.AwayFromZero);
|
||||
set
|
||||
{
|
||||
ColorStop.Position = Math.Min(100, Math.Max(0, value)) / 100f;
|
||||
NotifyOfPropertyChange(nameof(Offset));
|
||||
NotifyOfPropertyChange(nameof(OffsetPercent));
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsSelected
|
||||
{
|
||||
get => _isSelected;
|
||||
set => SetAndNotify(ref _isSelected, value);
|
||||
}
|
||||
|
||||
public bool WillRemoveColorStop
|
||||
{
|
||||
get => _willRemoveColorStop;
|
||||
set => SetAndNotify(ref _willRemoveColorStop, value);
|
||||
}
|
||||
|
||||
#region Movement
|
||||
|
||||
private double _mouseDownOffset;
|
||||
private DateTime _mouseDownTime;
|
||||
|
||||
|
||||
public void StopMouseDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
var position = GetPositionInPreview(sender, e);
|
||||
e.Handled = true;
|
||||
|
||||
((IInputElement) sender).CaptureMouse();
|
||||
_mouseDownOffset = Offset - position.X;
|
||||
_mouseDownTime = DateTime.Now;
|
||||
_gradientEditorViewModel.SelectColorStop(this);
|
||||
}
|
||||
|
||||
public void StopMouseUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
// On regular click, select this color stop
|
||||
if (DateTime.Now - _mouseDownTime <= TimeSpan.FromMilliseconds(250))
|
||||
{
|
||||
}
|
||||
e.Handled = true;
|
||||
|
||||
((IInputElement) sender).ReleaseMouseCapture();
|
||||
e.Handled = true;
|
||||
if (WillRemoveColorStop)
|
||||
_gradientEditorViewModel.RemoveColorStop(this);
|
||||
}
|
||||
|
||||
public void StopMouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
e.Handled = true;
|
||||
|
||||
if (!((IInputElement) sender).IsMouseCaptured)
|
||||
return;
|
||||
|
||||
var position = GetPositionInPreview(sender, e);
|
||||
// Scale down with a precision of 3 decimals
|
||||
var newPosition = Math.Round((position.X + _mouseDownOffset) / _gradientEditorViewModel.PreviewWidth, 3, MidpointRounding.AwayFromZero);
|
||||
// Limit from 0.0 to 1.0
|
||||
newPosition = Math.Min(1, Math.Max(0, newPosition));
|
||||
var parent = VisualTreeUtilities.FindParent<Canvas>((DependencyObject) sender, null);
|
||||
var position = e.GetPosition(parent);
|
||||
if (position.Y > 50)
|
||||
{
|
||||
WillRemoveColorStop = true;
|
||||
return;
|
||||
}
|
||||
|
||||
ColorStop.Position = (float) newPosition;
|
||||
NotifyOfPropertyChange(() => Offset);
|
||||
NotifyOfPropertyChange(() => Color);
|
||||
}
|
||||
WillRemoveColorStop = false;
|
||||
|
||||
private Point GetPositionInPreview(object sender, MouseEventArgs e)
|
||||
{
|
||||
var parent = VisualTreeHelper.GetParent((DependencyObject) sender);
|
||||
return e.GetPosition((IInputElement) parent);
|
||||
var minValue = 0.0;
|
||||
var maxValue = _gradientEditorViewModel.PreviewWidth;
|
||||
var stops = _gradientEditorViewModel.ColorGradient.Stops.OrderBy(s => s.Position).ToList();
|
||||
var previous = stops.IndexOf(ColorStop) >= 1 ? stops[stops.IndexOf(ColorStop) - 1] : null;
|
||||
var next = stops.IndexOf(ColorStop) + 1 < stops.Count ? stops[stops.IndexOf(ColorStop) + 1] : null;
|
||||
if (previous != null)
|
||||
minValue = previous.Position * _gradientEditorViewModel.PreviewWidth;
|
||||
if (next != null)
|
||||
maxValue = next.Position * _gradientEditorViewModel.PreviewWidth;
|
||||
|
||||
Offset = Math.Max(minValue, Math.Min(maxValue, position.X));
|
||||
_gradientEditorViewModel.ColorGradient.OnColorValuesUpdated();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void Update(ColorGradientStop colorStop)
|
||||
{
|
||||
ColorStop = colorStop;
|
||||
NotifyOfPropertyChange(() => Offset);
|
||||
NotifyOfPropertyChange(() => Color);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@
|
||||
d:DataContext="{d:DesignInstance local:GradientEditorViewModel}">
|
||||
<controls:MaterialWindow.Resources>
|
||||
<converters:ColorGradientToGradientStopsConverter x:Key="ColorGradientToGradientStopsConverter" />
|
||||
<converters:SKColorToColorConverter x:Key="SKColorToColorConverter" />
|
||||
</controls:MaterialWindow.Resources>
|
||||
<StackPanel>
|
||||
<materialDesign:Card Margin="15 15 15 7" Padding="15">
|
||||
@@ -48,10 +49,10 @@
|
||||
</Rectangle.Fill>
|
||||
</Rectangle>
|
||||
|
||||
<ItemsControl ItemsSource="{Binding ColorStopViewModels}" Margin="15 0" MouseLeftButtonUp="{s:Action CanvasMouseDown}" Cursor="Cross">
|
||||
<ItemsControl ItemsSource="{Binding ColorStopViewModels}" Margin="15 0" MouseLeftButtonUp="{s:Action AddColorStop}" Cursor="Cross">
|
||||
<ItemsControl.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<Canvas Height="60" Width="440" x:Name="PreviewCanvas" Background="Transparent"/>
|
||||
<Canvas Height="16" Width="440" x:Name="PreviewCanvas" Background="Transparent"/>
|
||||
</ItemsPanelTemplate>
|
||||
</ItemsControl.ItemsPanel>
|
||||
<ItemsControl.ItemContainerStyle>
|
||||
@@ -79,15 +80,27 @@
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Label Grid.Column="0" HorizontalAlignment="Right">Color:</Label>
|
||||
<shared:ColorPicker Grid.Row="0" Grid.Column="1" x:Name="CurrentColor" Width="85" />
|
||||
<shared:ColorPicker
|
||||
Grid.Row="0"
|
||||
Grid.Column="1"
|
||||
x:Name="CurrentColor"
|
||||
Width="85"
|
||||
Color="{Binding Path=SelectedColorStopViewModel.ColorStop.Color, Converter={StaticResource SKColorToColorConverter}}"
|
||||
IsEnabled="{Binding HasSelectedColorStopViewModel}"/>
|
||||
|
||||
<Label Grid.Row="0" Grid.Column="2" HorizontalAlignment="Right">Location:</Label>
|
||||
<StackPanel Grid.Row="0" Grid.Column="3" Orientation="Horizontal">
|
||||
<TextBox x:Name="CurrentLocation" Width="40" />
|
||||
<TextBox Width="40" Text="{Binding SelectedColorStopViewModel.OffsetPercent}" IsEnabled="{Binding HasSelectedColorStopViewModel}" materialDesign:HintAssist.Hint="0"/>
|
||||
<Label>%</Label>
|
||||
</StackPanel>
|
||||
|
||||
<Button Grid.Row="0" Grid.Column="4" Style="{StaticResource MaterialDesignRaisedButton}" Width="80" Height="25">
|
||||
<Button Grid.Row="0" Grid.Column="4"
|
||||
Style="{StaticResource MaterialDesignRaisedButton}"
|
||||
Width="80"
|
||||
Height="25"
|
||||
IsEnabled="{Binding HasSelectedColorStopViewModel}"
|
||||
Command="{s:Action RemoveColorStop}"
|
||||
CommandParameter="{Binding SelectedColorStopViewModel}">
|
||||
Delete
|
||||
</Button>
|
||||
</Grid>
|
||||
|
||||
@@ -1,46 +1,79 @@
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using Artemis.Core.Models.Profile;
|
||||
using Artemis.UI.Shared.Utilities;
|
||||
using Stylet;
|
||||
|
||||
namespace Artemis.UI.Shared.Screens.GradientEditor
|
||||
{
|
||||
public class GradientEditorViewModel : Screen
|
||||
{
|
||||
private ColorStopViewModel _selectedColorStopViewModel;
|
||||
|
||||
public GradientEditorViewModel(ColorGradient colorGradient)
|
||||
{
|
||||
ColorGradient = colorGradient;
|
||||
ColorStopViewModels = new BindableCollection<ColorStopViewModel>();
|
||||
|
||||
ColorGradient.PropertyChanged += ColorGradientOnPropertyChanged;
|
||||
UpdateColorStopViewModels();
|
||||
foreach (var colorStop in ColorGradient.Stops.OrderBy(s => s.Position))
|
||||
ColorStopViewModels.Add(new ColorStopViewModel(this, colorStop));
|
||||
}
|
||||
|
||||
|
||||
public BindableCollection<ColorStopViewModel> ColorStopViewModels { get; set; }
|
||||
|
||||
public ColorGradient ColorGradient { get; }
|
||||
public double PreviewWidth => 440;
|
||||
|
||||
private void ColorGradientOnPropertyChanged(object sender, PropertyChangedEventArgs e)
|
||||
public ColorStopViewModel SelectedColorStopViewModel
|
||||
{
|
||||
if (e.PropertyName == nameof(ColorGradient.Stops))
|
||||
UpdateColorStopViewModels();
|
||||
get => _selectedColorStopViewModel;
|
||||
set
|
||||
{
|
||||
SetAndNotify(ref _selectedColorStopViewModel, value);
|
||||
NotifyOfPropertyChange(nameof(HasSelectedColorStopViewModel));
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateColorStopViewModels()
|
||||
{
|
||||
while (ColorGradient.Stops.Count > ColorStopViewModels.Count)
|
||||
ColorStopViewModels.Add(new ColorStopViewModel(this));
|
||||
while (ColorGradient.Stops.Count < ColorStopViewModels.Count)
|
||||
ColorStopViewModels.RemoveAt(0);
|
||||
public bool HasSelectedColorStopViewModel => SelectedColorStopViewModel != null;
|
||||
|
||||
var index = 0;
|
||||
foreach (var colorStop in ColorGradient.Stops.OrderBy(s => s.Position))
|
||||
{
|
||||
var viewModel = ColorStopViewModels[index];
|
||||
viewModel.Update(colorStop);
|
||||
index++;
|
||||
}
|
||||
public ColorGradient ColorGradient { get; }
|
||||
public double PreviewWidth => 437.5;
|
||||
|
||||
public void AddColorStop(object sender, MouseEventArgs e)
|
||||
{
|
||||
var child = VisualTreeUtilities.FindChild<Canvas>((DependencyObject) sender, null);
|
||||
var position = (float) (e.GetPosition(child).X / PreviewWidth);
|
||||
var stop = new ColorGradientStop(ColorGradient.GetColor(position), position);
|
||||
ColorGradient.Stops.Add(stop);
|
||||
ColorGradient.OnColorValuesUpdated();
|
||||
|
||||
var index = ColorGradient.Stops.OrderBy(s => s.Position).ToList().IndexOf(stop);
|
||||
var viewModel = new ColorStopViewModel(this, stop);
|
||||
ColorStopViewModels.Insert(index, viewModel);
|
||||
|
||||
SelectColorStop(viewModel);
|
||||
}
|
||||
|
||||
public void RemoveColorStop(ColorStopViewModel colorStopViewModel)
|
||||
{
|
||||
ColorStopViewModels.Remove(colorStopViewModel);
|
||||
ColorGradient.Stops.Remove(colorStopViewModel.ColorStop);
|
||||
ColorGradient.OnColorValuesUpdated();
|
||||
|
||||
SelectColorStop(null);
|
||||
}
|
||||
|
||||
public Point GetPositionInPreview(object sender, MouseEventArgs e)
|
||||
{
|
||||
var parent = VisualTreeUtilities.FindParent<Canvas>((DependencyObject) sender, null);
|
||||
return e.GetPosition(parent);
|
||||
}
|
||||
|
||||
public void SelectColorStop(ColorStopViewModel colorStopViewModel)
|
||||
{
|
||||
SelectedColorStopViewModel = colorStopViewModel;
|
||||
foreach (var stopViewModel in ColorStopViewModels)
|
||||
stopViewModel.IsSelected = stopViewModel == SelectedColorStopViewModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Artemis.UI.Shared.Utilities
|
||||
{
|
||||
public static class HitTestUtilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Runs a hit test on children of the container within the rectangle matching all elements that have a data context of <see cref="T"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="container"></param>
|
||||
/// <param name="rectangleGeometry"></param>
|
||||
/// <returns></returns>
|
||||
public static List<T> GetHitViewModels<T>(Visual container, RectangleGeometry rectangleGeometry)
|
||||
{
|
||||
var result = new List<T>();
|
||||
var hitTestParams = new GeometryHitTestParameters(rectangleGeometry);
|
||||
var resultCallback = new HitTestResultCallback(r => HitTestResultBehavior.Continue);
|
||||
var filterCallback = new HitTestFilterCallback(e =>
|
||||
{
|
||||
if (e is FrameworkElement fe && fe.DataContext.GetType() == typeof(T) && !result.Contains((T) fe.DataContext))
|
||||
result.Add((T) fe.DataContext);
|
||||
return HitTestFilterBehavior.Continue;
|
||||
});
|
||||
VisualTreeHelper.HitTest(container, filterCallback, resultCallback, hitTestParams);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Artemis.UI.Shared.Utilities
|
||||
{
|
||||
public class ShortcutUtilities
|
||||
{
|
||||
|
||||
private static Type m_type = Type.GetTypeFromProgID("WScript.Shell");
|
||||
private static object m_shell = Activator.CreateInstance(m_type);
|
||||
|
||||
[ComImport, TypeLibType((short)0x1040), Guid("F935DC23-1CF0-11D0-ADB9-00C04FD58A0B")]
|
||||
private interface IWshShortcut
|
||||
{
|
||||
[DispId(0)]
|
||||
string FullName { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0)] get; }
|
||||
[DispId(0x3e8)]
|
||||
string Arguments { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3e8)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3e8)] set; }
|
||||
[DispId(0x3e9)]
|
||||
string Description { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3e9)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3e9)] set; }
|
||||
[DispId(0x3ea)]
|
||||
string Hotkey { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ea)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ea)] set; }
|
||||
[DispId(0x3eb)]
|
||||
string IconLocation { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3eb)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3eb)] set; }
|
||||
[DispId(0x3ec)]
|
||||
string RelativePath { [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ec)] set; }
|
||||
[DispId(0x3ed)]
|
||||
string TargetPath { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ed)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ed)] set; }
|
||||
[DispId(0x3ee)]
|
||||
int WindowStyle { [DispId(0x3ee)] get; [param: In] [DispId(0x3ee)] set; }
|
||||
[DispId(0x3ef)]
|
||||
string WorkingDirectory { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ef)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ef)] set; }
|
||||
[TypeLibFunc((short)0x40), DispId(0x7d0)]
|
||||
void Load([In, MarshalAs(UnmanagedType.BStr)] string PathLink);
|
||||
[DispId(0x7d1)]
|
||||
void Save();
|
||||
}
|
||||
|
||||
public static void Create(string fileName, string targetPath, string arguments, string workingDirectory, string description, string hotkey, string iconPath)
|
||||
{
|
||||
IWshShortcut shortcut = (IWshShortcut)m_type.InvokeMember("CreateShortcut", System.Reflection.BindingFlags.InvokeMethod, null, m_shell, new object[] { fileName });
|
||||
shortcut.Description = description;
|
||||
shortcut.Hotkey = hotkey;
|
||||
shortcut.TargetPath = targetPath;
|
||||
shortcut.WorkingDirectory = workingDirectory;
|
||||
shortcut.Arguments = arguments;
|
||||
if (!string.IsNullOrEmpty(iconPath))
|
||||
shortcut.IconLocation = iconPath;
|
||||
shortcut.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace Artemis.UI.Shared.Utilities
|
||||
{
|
||||
public static class SizeObserver
|
||||
{
|
||||
public static readonly DependencyProperty ObserveProperty = DependencyProperty.RegisterAttached(
|
||||
"Observe",
|
||||
typeof(bool),
|
||||
typeof(SizeObserver),
|
||||
new FrameworkPropertyMetadata(OnObserveChanged));
|
||||
|
||||
public static readonly DependencyProperty ObservedWidthProperty = DependencyProperty.RegisterAttached(
|
||||
"ObservedWidth",
|
||||
typeof(double),
|
||||
typeof(SizeObserver));
|
||||
|
||||
public static readonly DependencyProperty ObservedHeightProperty = DependencyProperty.RegisterAttached(
|
||||
"ObservedHeight",
|
||||
typeof(double),
|
||||
typeof(SizeObserver));
|
||||
|
||||
public static bool GetObserve(FrameworkElement frameworkElement)
|
||||
{
|
||||
return (bool) frameworkElement.GetValue(ObserveProperty);
|
||||
}
|
||||
|
||||
public static void SetObserve(FrameworkElement frameworkElement, bool observe)
|
||||
{
|
||||
frameworkElement.SetValue(ObserveProperty, observe);
|
||||
}
|
||||
|
||||
public static double GetObservedWidth(FrameworkElement frameworkElement)
|
||||
{
|
||||
return (double) frameworkElement.GetValue(ObservedWidthProperty);
|
||||
}
|
||||
|
||||
public static void SetObservedWidth(FrameworkElement frameworkElement, double observedWidth)
|
||||
{
|
||||
frameworkElement.SetValue(ObservedWidthProperty, observedWidth);
|
||||
}
|
||||
|
||||
public static double GetObservedHeight(FrameworkElement frameworkElement)
|
||||
{
|
||||
return (double) frameworkElement.GetValue(ObservedHeightProperty);
|
||||
}
|
||||
|
||||
public static void SetObservedHeight(FrameworkElement frameworkElement, double observedHeight)
|
||||
{
|
||||
frameworkElement.SetValue(ObservedHeightProperty, observedHeight);
|
||||
}
|
||||
|
||||
private static void OnObserveChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var frameworkElement = (FrameworkElement) dependencyObject;
|
||||
|
||||
if ((bool) e.NewValue)
|
||||
{
|
||||
frameworkElement.SizeChanged += OnFrameworkElementSizeChanged;
|
||||
UpdateObservedSizesForFrameworkElement(frameworkElement);
|
||||
}
|
||||
else
|
||||
frameworkElement.SizeChanged -= OnFrameworkElementSizeChanged;
|
||||
}
|
||||
|
||||
private static void OnFrameworkElementSizeChanged(object sender, SizeChangedEventArgs e)
|
||||
{
|
||||
UpdateObservedSizesForFrameworkElement((FrameworkElement) sender);
|
||||
}
|
||||
|
||||
private static void UpdateObservedSizesForFrameworkElement(FrameworkElement frameworkElement)
|
||||
{
|
||||
frameworkElement.SetCurrentValue(ObservedWidthProperty, frameworkElement.ActualWidth);
|
||||
frameworkElement.SetCurrentValue(ObservedHeightProperty, frameworkElement.ActualHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
using System.Diagnostics;
|
||||
using System.Windows;
|
||||
using System.Windows.Markup;
|
||||
using System.Windows.Media.Animation;
|
||||
|
||||
// Code from http://www.wpfmentor.com/2009/01/how-to-debug-triggers-using-trigger.html
|
||||
// No license specified - this code is trimmed out from Release build anyway so it should be ok using it this way
|
||||
|
||||
// HOWTO: add the following attached property to any trigger and you will see when it is activated/deactivated in the output window
|
||||
// TriggerTracing.TriggerName="your debug name"
|
||||
// TriggerTracing.TraceEnabled="True"
|
||||
|
||||
// Example:
|
||||
// <Trigger my:TriggerTracing.TriggerName="BoldWhenMouseIsOver"
|
||||
// my:TriggerTracing.TraceEnabled="True"
|
||||
// Property="IsMouseOver"
|
||||
// Value="True">
|
||||
// <Setter Property = "FontWeight" Value="Bold"/>
|
||||
// </Trigger>
|
||||
//
|
||||
// As this works on anything that inherits from TriggerBase, it will also work on <MultiTrigger>.
|
||||
|
||||
namespace Artemis.UI.Shared.Utilities
|
||||
{
|
||||
#if DEBUG
|
||||
|
||||
/// <summary>
|
||||
/// Contains attached properties to activate Trigger Tracing on the specified Triggers.
|
||||
/// This file alone should be dropped into your app.
|
||||
/// </summary>
|
||||
public static class TriggerTracing
|
||||
{
|
||||
static TriggerTracing()
|
||||
{
|
||||
// Initialise WPF Animation tracing and add a TriggerTraceListener
|
||||
PresentationTraceSources.Refresh();
|
||||
PresentationTraceSources.AnimationSource.Listeners.Clear();
|
||||
PresentationTraceSources.AnimationSource.Listeners.Add(new TriggerTraceListener());
|
||||
PresentationTraceSources.AnimationSource.Switch.Level = SourceLevels.All;
|
||||
}
|
||||
|
||||
private enum TriggerTraceStoryboardType
|
||||
{
|
||||
Enter,
|
||||
Exit
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A dummy storyboard for tracing purposes
|
||||
/// </summary>
|
||||
private class TriggerTraceStoryboard : Storyboard
|
||||
{
|
||||
public TriggerTraceStoryboard(TriggerBase triggerBase, TriggerTraceStoryboardType storyboardType)
|
||||
{
|
||||
TriggerBase = triggerBase;
|
||||
StoryboardType = storyboardType;
|
||||
}
|
||||
|
||||
public TriggerTraceStoryboardType StoryboardType { get; }
|
||||
public TriggerBase TriggerBase { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A custom tracelistener.
|
||||
/// </summary>
|
||||
private class TriggerTraceListener : TraceListener
|
||||
{
|
||||
public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
|
||||
{
|
||||
base.TraceEvent(eventCache, source, eventType, id, format, args);
|
||||
|
||||
if (format.StartsWith("Storyboard has begun;"))
|
||||
{
|
||||
var storyboard = args[1] as TriggerTraceStoryboard;
|
||||
if (storyboard != null)
|
||||
{
|
||||
// add a breakpoint here to see when your trigger has been
|
||||
// entered or exited
|
||||
|
||||
// the element being acted upon
|
||||
var targetElement = args[5];
|
||||
|
||||
// the namescope of the element being acted upon
|
||||
var namescope = (INameScope) args[7];
|
||||
|
||||
var triggerBase = storyboard.TriggerBase;
|
||||
var triggerName = GetTriggerName(storyboard.TriggerBase);
|
||||
|
||||
Debug.WriteLine("Element: {0}, {1}: {2}: {3}", targetElement, triggerBase.GetType().Name, triggerName, storyboard.StoryboardType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(string message)
|
||||
{
|
||||
}
|
||||
|
||||
public override void WriteLine(string message)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#region TriggerName attached property
|
||||
|
||||
/// <summary>
|
||||
/// Gets the trigger name for the specified trigger. This will be used
|
||||
/// to identify the trigger in the debug output.
|
||||
/// </summary>
|
||||
/// <param name="trigger">The trigger.</param>
|
||||
/// <returns></returns>
|
||||
public static string GetTriggerName(TriggerBase trigger)
|
||||
{
|
||||
return (string) trigger.GetValue(TriggerNameProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the trigger name for the specified trigger. This will be used
|
||||
/// to identify the trigger in the debug output.
|
||||
/// </summary>
|
||||
/// <param name="trigger">The trigger.</param>
|
||||
/// <returns></returns>
|
||||
public static void SetTriggerName(TriggerBase trigger, string value)
|
||||
{
|
||||
trigger.SetValue(TriggerNameProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty TriggerNameProperty =
|
||||
DependencyProperty.RegisterAttached(
|
||||
"TriggerName",
|
||||
typeof(string),
|
||||
typeof(TriggerTracing),
|
||||
new UIPropertyMetadata(string.Empty));
|
||||
|
||||
#endregion
|
||||
|
||||
#region TraceEnabled attached property
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indication whether trace is enabled for the specified trigger.
|
||||
/// </summary>
|
||||
/// <param name="trigger">The trigger.</param>
|
||||
/// <returns></returns>
|
||||
public static bool GetTraceEnabled(TriggerBase trigger)
|
||||
{
|
||||
return (bool) trigger.GetValue(TraceEnabledProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a value specifying whether trace is enabled for the specified trigger
|
||||
/// </summary>
|
||||
/// <param name="trigger"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void SetTraceEnabled(TriggerBase trigger, bool value)
|
||||
{
|
||||
trigger.SetValue(TraceEnabledProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty TraceEnabledProperty =
|
||||
DependencyProperty.RegisterAttached(
|
||||
"TraceEnabled",
|
||||
typeof(bool),
|
||||
typeof(TriggerTracing),
|
||||
new UIPropertyMetadata(false, OnTraceEnabledChanged));
|
||||
|
||||
private static void OnTraceEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var triggerBase = d as TriggerBase;
|
||||
|
||||
if (triggerBase == null)
|
||||
return;
|
||||
|
||||
if (!(e.NewValue is bool))
|
||||
return;
|
||||
|
||||
if ((bool) e.NewValue)
|
||||
{
|
||||
// insert dummy story-boards which can later be traced using WPF animation tracing
|
||||
|
||||
var storyboard = new TriggerTraceStoryboard(triggerBase, TriggerTraceStoryboardType.Enter);
|
||||
triggerBase.EnterActions.Insert(0, new BeginStoryboard {Storyboard = storyboard});
|
||||
|
||||
storyboard = new TriggerTraceStoryboard(triggerBase, TriggerTraceStoryboardType.Exit);
|
||||
triggerBase.ExitActions.Insert(0, new BeginStoryboard {Storyboard = storyboard});
|
||||
}
|
||||
else
|
||||
{
|
||||
// remove the dummy storyboards
|
||||
|
||||
foreach (var actionCollection in new[] {triggerBase.EnterActions, triggerBase.ExitActions})
|
||||
{
|
||||
foreach (var triggerAction in actionCollection)
|
||||
{
|
||||
var bsb = triggerAction as BeginStoryboard;
|
||||
|
||||
if (bsb != null && bsb.Storyboard != null && bsb.Storyboard is TriggerTraceStoryboard)
|
||||
{
|
||||
actionCollection.Remove(bsb);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
#endif
|
||||
}
|
||||
+37
-5
@@ -4,7 +4,7 @@ using System.Windows.Media;
|
||||
namespace Artemis.UI.Shared.Utilities
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static class UIUtilities
|
||||
public static class VisualTreeUtilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Finds a Child of a given item in the visual tree.
|
||||
@@ -29,8 +29,7 @@ namespace Artemis.UI.Shared.Utilities
|
||||
{
|
||||
var child = VisualTreeHelper.GetChild(parent, i);
|
||||
// If the child is not of the request child type child
|
||||
var childType = child as T;
|
||||
if (childType == null)
|
||||
if (!(child is T))
|
||||
{
|
||||
// recursively drill down the tree
|
||||
foundChild = FindChild<T>(child, childName);
|
||||
@@ -40,9 +39,8 @@ namespace Artemis.UI.Shared.Utilities
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(childName))
|
||||
{
|
||||
var frameworkElement = child as FrameworkElement;
|
||||
// If the child's name is set for search
|
||||
if (frameworkElement != null && frameworkElement.Name == childName)
|
||||
if (child is FrameworkElement frameworkElement && frameworkElement.Name == childName)
|
||||
{
|
||||
// if the child's name is of the request name
|
||||
foundChild = (T) child;
|
||||
@@ -59,5 +57,39 @@ namespace Artemis.UI.Shared.Utilities
|
||||
|
||||
return foundChild;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds a parent of a given item in the visual tree.
|
||||
/// </summary>
|
||||
/// <param name="child">A child of the queried item.</param>
|
||||
/// <typeparam name="T">The type of the queried item.</typeparam>
|
||||
/// <param name="parentName">x:Name or Name of parent. </param>
|
||||
/// <returns>
|
||||
/// The first parent item that matches the submitted type parameter.
|
||||
/// If not matching item can be found,
|
||||
/// a null parent is being returned.
|
||||
/// </returns>
|
||||
public static T FindParent<T>(DependencyObject child, string parentName) where T : DependencyObject
|
||||
{
|
||||
// Get parent item
|
||||
var parentObject = VisualTreeHelper.GetParent(child);
|
||||
|
||||
// We've reached the end of the tree
|
||||
if (parentObject == null)
|
||||
return null;
|
||||
|
||||
// Check if the parent matches the type we're looking for
|
||||
if (!(parentObject is T parent))
|
||||
return FindParent<T>(parentObject, parentName);
|
||||
|
||||
// If no name is set the first matching type is a match
|
||||
if (string.IsNullOrEmpty(parentName))
|
||||
return parent;
|
||||
|
||||
// If the parent's name is set for search that must match as well
|
||||
if (parent is FrameworkElement frameworkElement && frameworkElement.Name == parentName)
|
||||
return parent;
|
||||
return FindParent<T>(parentObject, parentName);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user