Skip to content

Commit

Permalink
Add window drag events
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Jul 25, 2021
1 parent 1f1602c commit 462f229
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 10 deletions.
19 changes: 19 additions & 0 deletions samples/AvaloniaDemo/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@ private void DebugFactoryEvents(IFactory factory)
{
Debug.WriteLine($"[WindowRemoved] Title='{args.Window?.Title}'");
};

factory.WindowMoveDragBegin += (_, args) =>
{
// NOTE: Set to True to cancel window dragging.
#if false
args.Cancel = true;
#endif
Debug.WriteLine($"[WindowMoveDragBegin] Title='{args.Window?.Title}', Cancel={args.Cancel}, X='{args.Window?.X}', Y='{args.Window?.Y}'");
};

factory.WindowMoveDrag += (_, args) =>
{
Debug.WriteLine($"[WindowMoveDrag] Title='{args.Window?.Title}', X='{args.Window?.X}', Y='{args.Window?.Y}");
};

factory.WindowMoveDragEnd += (_, args) =>
{
Debug.WriteLine($"[WindowMoveDragEnd] Title='{args.Window?.Title}', X='{args.Window?.X}', Y='{args.Window?.Y}");
};
}

public void CloseLayout()
Expand Down
19 changes: 12 additions & 7 deletions src/Dock.Avalonia/Controls/HostWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,19 @@ protected override void OnPointerPressed(PointerPressedEventArgs e)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
_mouseDown = true;
_hostWindowState.Process(e.GetPosition(this), EventType.Pressed);
if (Window?.Factory?.OnWindowMoveDragBegin(Window) == true)
{
_mouseDown = true;
_hostWindowState.Process(e.GetPosition(this), EventType.Pressed);

PseudoClasses.Set(":dragging", true);
BeginMoveDrag(e);
PseudoClasses.Set(":dragging", false);
PseudoClasses.Set(":dragging", true);
BeginMoveDrag(e);
PseudoClasses.Set(":dragging", false);

_hostWindowState.Process(e.GetPosition(this), EventType.Released);
_mouseDown = false;
Window?.Factory?.OnWindowMoveDragEnd(Window);
_hostWindowState.Process(e.GetPosition(this), EventType.Released);
_mouseDown = false;
}
}
}
}
Expand All @@ -98,6 +102,7 @@ private void HostWindow_PositionChanged(object? sender, PixelPointEventArgs e)

if (_chromeGrip is { } && _chromeGrip.IsPointerOver && _mouseDown)
{
Window.Factory?.OnWindowMoveDrag(Window);
_hostWindowState.Process(Position.ToPoint(1.0), EventType.Moved);
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/Dock.Model.ReactiveUI/Core/DockWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ public virtual bool OnClose()
return true;
}

/// <inheritdoc/>
public virtual bool OnMoveDragBegin()
{
return true;
}

/// <inheritdoc/>
public virtual void OnMoveDrag()
{
}

/// <inheritdoc/>
public virtual void OnMoveDragEnd()
{
}

/// <inheritdoc/>
public void Save()
{
Expand Down
29 changes: 29 additions & 0 deletions src/Dock.Model/Core/Events/WindowMoveDragBeginEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace Dock.Model.Core.Events
{
/// <summary>
/// Window begin dragging event args.
/// </summary>
public class WindowMoveDragBeginEventArgs : EventArgs
{
/// <summary>
/// Gets dragged window.
/// </summary>
public IDockWindow? Window { get; }

/// <summary>
/// Gets or sets flag indicating whether window dragging should be canceled.
/// </summary>
public bool Cancel { get; set; }

/// <summary>
/// Initializes new instance of the <see cref="WindowMoveDragBeginEventArgs"/> class.
/// </summary>
/// <param name="window">The dragged window.</param>
public WindowMoveDragBeginEventArgs(IDockWindow? window)
{
Window = window;
}
}
}
24 changes: 24 additions & 0 deletions src/Dock.Model/Core/Events/WindowMoveDragEndEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace Dock.Model.Core.Events
{
/// <summary>
/// Window dragging ended event args.
/// </summary>
public class WindowMoveDragEndEventArgs : EventArgs
{
/// <summary>
/// Gets dragged window.
/// </summary>
public IDockWindow? Window { get; }

/// <summary>
/// Initializes new instance of the <see cref="WindowMoveDragEndEventArgs"/> class.
/// </summary>
/// <param name="window">The dragged window.</param>
public WindowMoveDragEndEventArgs(IDockWindow? window)
{
Window = window;
}
}
}
24 changes: 24 additions & 0 deletions src/Dock.Model/Core/Events/WindowMoveDragEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace Dock.Model.Core.Events
{
/// <summary>
/// Window dragging event args.
/// </summary>
public class WindowMoveDragEventArgs : EventArgs
{
/// <summary>
/// Gets dragged window.
/// </summary>
public IDockWindow? Window { get; }

/// <summary>
/// Initializes new instance of the <see cref="WindowMoveDragEventArgs"/> class.
/// </summary>
/// <param name="window">The dragged window.</param>
public WindowMoveDragEventArgs(IDockWindow? window)
{
Window = window;
}
}
}
16 changes: 16 additions & 0 deletions src/Dock.Model/Core/IDockWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ public interface IDockWindow
/// <returns>True to accept the close, and false to cancel the close.</returns>
bool OnClose();

/// <summary>
/// Called before the window dragging start.
/// </summary>
/// <returns>True to accept the dragging, and false to cancel the dragging.</returns>
bool OnMoveDragBegin();

/// <summary>
/// Called when the window is dragged.
/// </summary>
void OnMoveDrag();

/// <summary>
/// Called after the window dragging ended.
/// </summary>
void OnMoveDragEnd();

/// <summary>
/// Saves window properties.
/// </summary>
Expand Down
36 changes: 35 additions & 1 deletion src/Dock.Model/Core/IFactory.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ public partial interface IFactory
/// </summary>
event EventHandler<WindowRemovedEventArgs>? WindowRemoved;

/// <summary>
/// Window dragging begin event handler.
/// </summary>
event EventHandler<WindowMoveDragBeginEventArgs>? WindowMoveDragBegin;

/// <summary>
/// Window dragging event handler.
/// </summary>
event EventHandler<WindowMoveDragEventArgs>? WindowMoveDrag;

/// <summary>
/// Window dragging end event handler.
/// </summary>
event EventHandler<WindowMoveDragEndEventArgs>? WindowMoveDragEnd;

/// <summary>
/// Called when the active dockable changed.
/// </summary>
Expand Down Expand Up @@ -142,7 +157,7 @@ public partial interface IFactory
/// Called when the window is closing.
/// </summary>
/// <param name="window">The closing window.</param>
/// <returns>False if canceled, otherwise true.</returns>
/// <returns>False if closing canceled, otherwise true.</returns>
bool OnWindowClosing(IDockWindow? window);

/// <summary>
Expand All @@ -162,5 +177,24 @@ public partial interface IFactory
/// </summary>
/// <param name="window">The removed window.</param>
void OnWindowRemoved(IDockWindow? window);

/// <summary>
/// Called before the window dragging starts.
/// </summary>
/// <param name="window">The dragged window.</param>
/// <returns>False if dragging canceled, otherwise true.</returns>
bool OnWindowMoveDragBegin(IDockWindow? window);

/// <summary>
/// Called when the window is dragged.
/// </summary>
/// <param name="window">The dragged window.</param>
void OnWindowMoveDrag(IDockWindow? window);

/// <summary>
/// Called after the window dragging ended.
/// </summary>
/// <param name="window">The dragged window.</param>
void OnWindowMoveDragEnd(IDockWindow? window);
}
}
41 changes: 39 additions & 2 deletions src/Dock.Model/FactoryBase.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public abstract partial class FactoryBase
/// <inheritdoc />
public event EventHandler<WindowRemovedEventArgs>? WindowRemoved;

/// <inheritdoc />
public event EventHandler<WindowMoveDragBeginEventArgs>? WindowMoveDragBegin;

/// <inheritdoc />
public event EventHandler<WindowMoveDragEventArgs>? WindowMoveDrag;

/// <inheritdoc />
public event EventHandler<WindowMoveDragEndEventArgs>? WindowMoveDragEnd;

/// <inheritdoc />
public virtual void OnActiveDockableChanged(IDockable? dockable)
{
Expand Down Expand Up @@ -98,8 +107,7 @@ public virtual void OnDockablePinned(IDockable? dockable)
{
DockablePinned?.Invoke(this, new DockablePinnedEventArgs(dockable));
}



/// <inheritdoc />
public virtual void OnDockableUnpinned(IDockable? dockable)
{
Expand Down Expand Up @@ -144,5 +152,34 @@ public virtual void OnWindowClosed(IDockWindow? window)
{
WindowClosed?.Invoke(this, new WindowClosedEventArgs(window));
}

/// <inheritdoc />
public virtual bool OnWindowMoveDragBegin(IDockWindow? window)
{
var canMoveDrag = window?.OnMoveDragBegin() ?? true;

var eventArgs = new WindowMoveDragBeginEventArgs(window)
{
Cancel = !canMoveDrag
};

WindowMoveDragBegin?.Invoke(this, eventArgs);

return !eventArgs.Cancel;
}

/// <inheritdoc />
public virtual void OnWindowMoveDrag(IDockWindow? window)
{
window?.OnMoveDrag();
WindowMoveDrag?.Invoke(this, new WindowMoveDragEventArgs(window));
}

/// <inheritdoc />
public virtual void OnWindowMoveDragEnd(IDockWindow? window)
{
window?.OnMoveDragEnd();
WindowMoveDragEnd?.Invoke(this, new WindowMoveDragEndEventArgs(window));
}
}
}

0 comments on commit 462f229

Please # to comment.