Skip to content

Commit

Permalink
enhance: add move gesture for ImageViewer.
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed Jan 5, 2023
1 parent dfa8e70 commit 0ecbd4f
Showing 1 changed file with 54 additions and 22 deletions.
76 changes: 54 additions & 22 deletions src/Shared/HandyControl_Shared/Controls/Image/ImageViewer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
Expand Down Expand Up @@ -99,9 +101,9 @@ public class ImageViewer : Control
private Point _imgCurrentPoint;

/// <summary>
/// 鼠标是否在图片上按下左键
/// 鼠标是否在图片上按下
/// </summary>
private bool _imgIsMouseLeftButtonDown;
private bool _imgIsMouseDown;

/// <summary>
/// 在图片上按下时图片的位置
Expand All @@ -119,9 +121,9 @@ public class ImageViewer : Control
private Point _imgSmallCurrentPoint;

/// <summary>
/// 鼠标是否在小图片上按下左键
/// 鼠标是否在小图片上按下
/// </summary>
private bool _imgSmallIsMouseLeftButtonDown;
private bool _imgSmallIsMouseDown;

/// <summary>
/// 在小图片上按下时图片的位置
Expand Down Expand Up @@ -162,6 +164,8 @@ public class ImageViewer : Control

private bool _isLoaded;

private MouseBinding _mouseMoveBinding;

#endregion Data

#region ctor
Expand All @@ -175,6 +179,8 @@ public ImageViewer()
CommandBindings.Add(new CommandBinding(ControlCommands.Enlarge, ButtonEnlarge_OnClick));
CommandBindings.Add(new CommandBinding(ControlCommands.RotateLeft, ButtonRotateLeft_OnClick));
CommandBindings.Add(new CommandBinding(ControlCommands.RotateRight, ButtonRotateRight_OnClick));
CommandBindings.Add(new CommandBinding(ControlCommands.MouseMove, ImageMain_OnMouseDown));
OnMoveGestureChanged(MoveGesture);

Loaded += (s, e) =>
{
Expand Down Expand Up @@ -235,6 +241,9 @@ private static void OnImageSourceChanged(DependencyObject d, DependencyPropertyC
public static readonly DependencyProperty IsFullScreenProperty = DependencyProperty.Register(
nameof(IsFullScreen), typeof(bool), typeof(ImageViewer), new PropertyMetadata(ValueBoxes.FalseBox));

public static readonly DependencyProperty MoveGestureProperty = DependencyProperty.Register(
nameof(MoveGesture), typeof(MouseGesture), typeof(ImageViewer), new UIPropertyMetadata(new MouseGesture(MouseAction.LeftClick), OnMoveGestureChanged));

internal static readonly DependencyProperty ImgPathProperty = DependencyProperty.Register(
nameof(ImgPath), typeof(string), typeof(ImageViewer), new PropertyMetadata(default(string)));

Expand Down Expand Up @@ -283,6 +292,14 @@ public bool IsFullScreen
set => SetValue(IsFullScreenProperty, ValueBoxes.BooleanBox(value));
}

[ValueSerializer(typeof(MouseGestureValueSerializer))]
[TypeConverter(typeof(MouseGestureConverter))]
public MouseGesture MoveGesture
{
get => (MouseGesture) GetValue(MoveGestureProperty);
set => SetValue(MoveGestureProperty, value);
}

public bool ShowImgMap
{
get => (bool) GetValue(ShowImgMapProperty);
Expand Down Expand Up @@ -408,8 +425,6 @@ internal bool ShowBorderBottom

public override void OnApplyTemplate()
{
if (_imageMain != null) _imageMain.MouseLeftButtonDown -= ImageMain_OnMouseLeftButtonDown;

if (_canvasSmallImg != null)
{
_canvasSmallImg.MouseLeftButtonDown -= CanvasSmallImg_OnMouseLeftButtonDown;
Expand All @@ -430,7 +445,6 @@ public override void OnApplyTemplate()
var t = new RotateTransform();
BindingOperations.SetBinding(t, RotateTransform.AngleProperty, new Binding(ImageRotateProperty.Name) { Source = this });
_imageMain.LayoutTransform = t;
_imageMain.MouseLeftButtonDown += ImageMain_OnMouseLeftButtonDown;
}

if (_canvasSmallImg != null)
Expand Down Expand Up @@ -581,12 +595,10 @@ private void ButtonSave_OnClick(object sender, RoutedEventArgs e)
if (SaveFileDialog.ShowDialog() == true)
{
var path = SaveFileDialog.FileName;
using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(ImageSource));
encoder.Save(fileStream);
}
using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(ImageSource));
encoder.Save(fileStream);
}
}

Expand Down Expand Up @@ -645,14 +657,14 @@ private void OnRenderSizeChanged()
MoveSmallImg(_imgSmallMouseDownMargin.Left, _imgSmallMouseDownMargin.Top);
}

private void ImageMain_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
private void ImageMain_OnMouseDown(object sender, ExecutedRoutedEventArgs e)
{
_imgMouseDownPoint = Mouse.GetPosition(_panelMain);
_imgMouseDownMargin = ImageMargin;
_imgIsMouseLeftButtonDown = true;
_imgIsMouseDown = true;
}

protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e) => _imgIsMouseLeftButtonDown = false;
protected override void OnPreviewMouseUp(MouseButtonEventArgs e) => _imgIsMouseDown = false;

/// <summary>
/// 右下角小图片显示切换
Expand Down Expand Up @@ -818,6 +830,14 @@ private void RotateImg(double rotate)
BeginAnimation(ImageRotateProperty, animation);
}

private MouseButtonState GetMouseButtonState() => MoveGesture.MouseAction switch
{
MouseAction.LeftClick => Mouse.LeftButton,
MouseAction.RightClick => Mouse.RightButton,
MouseAction.MiddleClick => Mouse.MiddleButton,
_ => Mouse.LeftButton
};

/// <summary>
/// 移动图片
/// </summary>
Expand All @@ -827,12 +847,12 @@ private void MoveImg()
ShowCloseButton = _imgCurrentPoint.Y < 200;
ShowBorderBottom = _imgCurrentPoint.Y > ActualHeight - 200;

if (Mouse.LeftButton == MouseButtonState.Released)
if (GetMouseButtonState() == MouseButtonState.Released)
{
return;
}

if (_imgIsMouseLeftButtonDown)
if (_imgIsMouseDown)
{
var subX = _imgCurrentPoint.X - _imgMouseDownPoint.X;
var subY = _imgCurrentPoint.Y - _imgMouseDownPoint.Y;
Expand Down Expand Up @@ -869,12 +889,12 @@ private void MoveImg()
/// </summary>
private void MoveSmallImg()
{
if (!_imgSmallIsMouseLeftButtonDown)
if (!_imgSmallIsMouseDown)
{
return;
}

if (Mouse.LeftButton == MouseButtonState.Released)
if (GetMouseButtonState() == MouseButtonState.Released)
{
return;
}
Expand Down Expand Up @@ -916,11 +936,23 @@ private void CanvasSmallImg_OnMouseLeftButtonDown(object sender, MouseButtonEven
{
_imgSmallMouseDownPoint = Mouse.GetPosition(_canvasSmallImg);
_imgSmallMouseDownMargin = _borderMove.Margin;
_imgSmallIsMouseLeftButtonDown = true;
_imgSmallIsMouseDown = true;
e.Handled = true;
}

private void CanvasSmallImg_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) => _imgSmallIsMouseLeftButtonDown = false;
private void CanvasSmallImg_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) => _imgSmallIsMouseDown = false;

private void CanvasSmallImg_OnMouseMove(object sender, MouseEventArgs e) => MoveSmallImg();

private static void OnMoveGestureChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ImageViewer) d).OnMoveGestureChanged((MouseGesture) e.NewValue);
}

private void OnMoveGestureChanged(MouseGesture newValue)
{
InputBindings.Remove(_mouseMoveBinding);
_mouseMoveBinding = new MouseBinding(ControlCommands.MouseMove, newValue);
InputBindings.Add(_mouseMoveBinding);
}
}

0 comments on commit 0ecbd4f

Please sign in to comment.