This commit is contained in:
NaBian committed 2023-09-26 22:52:38 +08:00
1 parent ef5ab72301
commit 8e1e8263f7
4 files changed
+221 -5

No files matched your search

@@ -1,9 +1,11 @@
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using HandyControl.Data;
using HandyControl.Input;
using HandyControl.Interactivity;
namespace HandyControl.Controls;
@@ -19,8 +21,6 @@ public class ToggleBlock : Control
public static readonly DependencyProperty IndeterminateContentProperty = DependencyProperty.Register(nameof(IndeterminateContent), typeof(object), typeof(ToggleBlock), new PropertyMetadata(default(object)));
public static readonly DependencyProperty ToggleGestureProperty = DependencyProperty.Register(nameof(ToggleGesture), typeof(MouseGesture), typeof(ToggleBlock), new UIPropertyMetadata(new MouseGesture(MouseAction.None), OnToggleGestureChanged));
private MouseBinding _toggleBinding;
[Category("Appearance")]
[TypeConverter(typeof(NullableBoolConverter))]
[Localizability(LocalizationCategory.None, Readability = Readability.Unreadable)]
@@ -75,9 +75,12 @@ public class ToggleBlock : Control
private void OnToggleGestureChanged(MouseGesture newValue)
{
InputBindings.Remove(_toggleBinding);
_toggleBinding = new MouseBinding(ControlCommands.Toggle, newValue);
InputBindings.Add(_toggleBinding);
foreach (var binding in InputBindings.OfType<SimpleMouseBinding>().ToList())
{
InputBindings.Remove(binding);
}
InputBindings.Add(new SimpleMouseBinding(ControlCommands.Toggle, newValue));
}
private void OnToggled(object sender, ExecutedRoutedEventArgs e)
@@ -99,6 +99,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Data\Enum\GrowlShowMode.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\Enum\LinearLayout.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\Enum\RunningDirection.cs" />
<Compile Include="..\..\Shared\HandyControl_Shared\Input\SimpleMouseBinding.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\EventToCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\IEventArgsConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\MouseDragElementBehaviorEx.cs" />
@@ -0,0 +1,107 @@
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
namespace HandyControl.Input;
internal class InternalMouseBinding : InputBinding
{
public static readonly DependencyProperty MouseActionProperty = DependencyProperty.Register(nameof(MouseAction),
typeof(MouseAction), typeof(MouseBinding), new UIPropertyMetadata(MouseAction.None, new PropertyChangedCallback(OnMouseActionPropertyChanged)));
private bool _settingGesture = false;
[TypeConverter(typeof(MouseGestureConverter))]
[ValueSerializer(typeof(MouseGestureValueSerializer))]
public override InputGesture Gesture
{
get => base.Gesture as MouseGesture;
set
{
if (value is MouseGesture mouseGesture)
{
base.Gesture = mouseGesture;
SynchronizePropertiesFromGesture(mouseGesture);
}
}
}
public MouseAction MouseAction
{
get => (MouseAction) GetValue(MouseActionProperty);
set => SetValue(MouseActionProperty, value);
}
public InternalMouseBinding()
{
}
internal InternalMouseBinding(ICommand command, MouseAction mouseAction) : this(command, new MouseGesture(mouseAction))
{
}
public InternalMouseBinding(ICommand command, MouseGesture gesture) : base(command, gesture)
{
SynchronizePropertiesFromGesture(gesture);
}
private static void OnMouseActionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((InternalMouseBinding) d).SynchronizeGestureFromProperties((MouseAction) e.NewValue);
}
protected override Freezable CreateInstanceCore()
{
return new InternalMouseBinding();
}
private void SynchronizePropertiesFromGesture(MouseGesture mouseGesture)
{
if (!_settingGesture)
{
_settingGesture = true;
try
{
MouseAction = mouseGesture.MouseAction;
}
finally
{
_settingGesture = false;
}
}
}
private void SynchronizeGestureFromProperties(MouseAction mouseAction)
{
if (!_settingGesture)
{
_settingGesture = true;
try
{
if (Gesture == null)
{
Gesture = new MouseGesture(mouseAction);
}
else
{
((MouseGesture) Gesture).MouseAction = mouseAction;
}
}
finally
{
_settingGesture = false;
}
}
}
private void OnMouseGesturePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (string.Compare(e.PropertyName, "MouseAction", StringComparison.Ordinal) == 0 &&
Gesture is MouseGesture mouseGesture)
{
SynchronizePropertiesFromGesture(mouseGesture);
}
}
}
@@ -0,0 +1,105 @@
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
namespace HandyControl.Input;
internal sealed class SimpleMouseBinding : InputBinding
{
public static readonly DependencyProperty MouseActionProperty = DependencyProperty.Register(nameof(MouseAction),
typeof(MouseAction), typeof(MouseBinding), new UIPropertyMetadata(MouseAction.None, OnMouseActionPropertyChanged));
private bool _settingGesture;
[TypeConverter(typeof(MouseGestureConverter))]
[ValueSerializer(typeof(MouseGestureValueSerializer))]
public override InputGesture Gesture
{
get => base.Gesture as MouseGesture;
set
{
if (value is not MouseGesture mouseGesture)
{
return;
}
base.Gesture = mouseGesture;
SynchronizePropertiesFromGesture(mouseGesture);
}
}
public MouseAction MouseAction
{
get => (MouseAction) GetValue(MouseActionProperty);
set => SetValue(MouseActionProperty, value);
}
public SimpleMouseBinding()
{
}
internal SimpleMouseBinding(ICommand command, MouseAction mouseAction) : this(command, new MouseGesture(mouseAction))
{
}
public SimpleMouseBinding(ICommand command, MouseGesture gesture) : base(command, gesture)
{
SynchronizePropertiesFromGesture(gesture);
}
private static void OnMouseActionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((SimpleMouseBinding) d).SynchronizeGestureFromProperties((MouseAction) e.NewValue);
}
protected override Freezable CreateInstanceCore()
{
return new SimpleMouseBinding();
}
private void SynchronizePropertiesFromGesture(MouseGesture mouseGesture)
{
if (_settingGesture)
{
return;
}
_settingGesture = true;
try
{
MouseAction = mouseGesture.MouseAction;
}
finally
{
_settingGesture = false;
}
}
private void SynchronizeGestureFromProperties(MouseAction mouseAction)
{
if (_settingGesture)
{
return;
}
_settingGesture = true;
try
{
if (Gesture is null)
{
Gesture = new MouseGesture(mouseAction);
}
else
{
((MouseGesture) Gesture).MouseAction = mouseAction;
}
}
finally
{
_settingGesture = false;
}
}
}