Skip to content

Commit

Permalink
Merge pull request #2051 from cwensley/curtis/splitter-start-and-comp…
Browse files Browse the repository at this point in the history
…leted

Add Splitter.PositionChangeStarted/Completed events, and fix WPF events and sizing
  • Loading branch information
cwensley authored Nov 12, 2021
2 parents 576ba7a + f88a489 commit 9e5349f
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 99 deletions.
32 changes: 31 additions & 1 deletion src/Eto.Gtk/Forms/Controls/SplitterHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ protected override void RealizedSetup()
void Create()
{
Gtk.Paned old = Control;

if (orientation == Orientation.Horizontal)
Control = new EtoHPaned() { Handler = this };
else
Expand Down Expand Up @@ -369,6 +369,7 @@ void HookEvents()
Control.AddNotification("position", PositionChanged);
}
int lastPosition;
UITimer timer;

void PositionChanged(object o, GLib.NotifyArgs args)
{
Expand All @@ -386,6 +387,13 @@ void PositionChanged(object o, GLib.NotifyArgs args)
return;
}
position = lastPosition;
if (timer == null)
{
timer = new UITimer(TriggerChangeCompleted) { Interval = 0.5 };
Callback.OnPositionChangeStarted(Widget, EventArgs.Empty);
}
timer.Start();

var e = new SplitterPositionChangingEventArgs(newPosition);
Callback.OnPositionChanging(Widget, e);
position = null;
Expand All @@ -403,19 +411,41 @@ void PositionChanged(object o, GLib.NotifyArgs args)
suppressSplitterMoved--;
}

private void TriggerChangeCompleted(object sender, EventArgs e)
{
Callback.OnPositionChangeCompleted(Widget, EventArgs.Empty);
timer?.Dispose();
timer = null;
}

public override void AttachEvent(string id)
{
switch (id)
{
case Splitter.PositionChangedEvent:
case Splitter.PositionChangingEvent:
case Splitter.PositionChangeStartedEvent:
case Splitter.PositionChangeCompletedEvent:
break;
default:
base.AttachEvent(id);
break;
}
}

protected override void Initialize()
{
base.Initialize();

Widget.MouseUp += Widget_MouseUp;
}

private void Widget_MouseUp(object sender, MouseEventArgs e)
{
if (timer != null)
TriggerChangeCompleted(sender, EventArgs.Empty);
}

public override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Expand Down
55 changes: 34 additions & 21 deletions src/Eto.Mac/Forms/Controls/SplitterHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public override void AttachEvent(string id)
{
case Splitter.PositionChangedEvent:
case Splitter.PositionChangingEvent:
case Splitter.PositionChangeStartedEvent:
case Splitter.PositionChangeCompletedEvent:
// handled by delegate
break;
default:
Expand Down Expand Up @@ -297,6 +299,8 @@ public override nfloat ConstrainSplitPosition(NSSplitView splitView, nfloat prop
proposedPosition = (nfloat)Math.Min(totalSize, proposedPosition);
}

h.TriggerChangeStarted();

var args = new SplitterPositionChangingEventArgs((int)Math.Round(proposedPosition));
h.Callback.OnPositionChanging(h.Widget, args);
if (args.Cancel)
Expand Down Expand Up @@ -325,12 +329,13 @@ public override void DidResizeSubviews(NSNotification notification)
if (mainFrame.Width <= 1 || mainFrame.Height <= 1)
return;
h.position = h.Control.IsVertical ? (int)subview.Frame.Width : (int)subview.Frame.Height;
h.TriggerChangeStarted();
h.Callback.OnPositionChanged(h.Widget, EventArgs.Empty);
}
}

}
// stupid hack for OSX 10.5 so that mouse down/drag/up events fire in children properly..

public class EtoSplitView : NSSplitView, IMacControl
{
public WeakReference WeakHandler { get; set; }
Expand All @@ -352,26 +357,9 @@ public EtoSplitView(SplitterHandler handler)

public override void MouseDown(NSEvent theEvent)
{
var cursor = NSCursor.CurrentCursor;
if (cursor == NSCursor.ResizeLeftCursor || cursor == NSCursor.ResizeRightCursor || cursor == NSCursor.ResizeLeftRightCursor
|| cursor == NSCursor.ResizeUpCursor || cursor == NSCursor.ResizeDownCursor || cursor == NSCursor.ResizeUpDownCursor)
base.MouseDown(theEvent);
}

public override void MouseDragged(NSEvent theEvent)
{
var cursor = NSCursor.CurrentCursor;
if (cursor == NSCursor.ResizeLeftCursor || cursor == NSCursor.ResizeRightCursor || cursor == NSCursor.ResizeLeftRightCursor
|| cursor == NSCursor.ResizeUpCursor || cursor == NSCursor.ResizeDownCursor || cursor == NSCursor.ResizeUpDownCursor)
base.MouseDragged(theEvent);
}

public override void MouseUp(NSEvent theEvent)
{
var cursor = NSCursor.CurrentCursor;
if (cursor == NSCursor.ResizeLeftCursor || cursor == NSCursor.ResizeRightCursor || cursor == NSCursor.ResizeLeftRightCursor
|| cursor == NSCursor.ResizeUpCursor || cursor == NSCursor.ResizeDownCursor || cursor == NSCursor.ResizeUpDownCursor)
base.MouseUp(theEvent);
Handler?.StartChange();
base.MouseDown(theEvent);
Handler?.TriggerChangeCompleted();
}

public override void Layout()
Expand All @@ -383,6 +371,31 @@ public override void Layout()
base.Layout();
}
}

bool changeStarted;

private void TriggerChangeStarted()
{
if (!changeStarted)
{
changeStarted = true;
Callback.OnPositionChangeStarted(Widget, EventArgs.Empty);
}
}

private void TriggerChangeCompleted()
{
if (changeStarted)
{
changeStarted = false;
Callback.OnPositionChangeCompleted(Widget, EventArgs.Empty);
}
}

private void StartChange()
{
changeStarted = false;
}

private void PerformLayout()
{
Expand Down
18 changes: 14 additions & 4 deletions src/Eto.WinForms/Forms/Controls/SplitterHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ public SplitterHandler()
SetInitialPosition();
SetFixedPanel();
};
Control.SplitterMoved += (sender, e) => CheckSplitterPos();
Control.SplitterMoved += (sender, e) => CheckSplitterPos(true);
}

int splitterMoving;
int lastPosition;
private void CheckSplitterPos()

private void CheckSplitterPos(bool userMoved)
{
if (splitterMoving > 0)
return;
Expand All @@ -155,6 +156,10 @@ private void CheckSplitterPos()
newPosition = panel1MinimumSize;

position = lastPosition;

if (userMoved)
Callback.OnPositionChangeStarted(Widget, EventArgs.Empty);

var args = new SplitterPositionChangingEventArgs(newPosition);
Callback.OnPositionChanging(Widget, args);
position = null;
Expand All @@ -176,6 +181,9 @@ private void CheckSplitterPos()
Control.SplitterDistance = newPosition;
}
lastPosition = newPosition;

if (userMoved)
Callback.OnPositionChangeCompleted(Widget, EventArgs.Empty);

splitterMoving--;
}
Expand All @@ -186,6 +194,8 @@ public override void AttachEvent(string id)
{
case Splitter.PositionChangedEvent:
case Splitter.PositionChangingEvent:
case Splitter.PositionChangeStartedEvent:
case Splitter.PositionChangeCompletedEvent:
break;
default:
base.AttachEvent(id);
Expand Down Expand Up @@ -529,7 +539,7 @@ public int Panel1MinimumSize
set
{
panel1MinimumSize = value;
CheckSplitterPos();
CheckSplitterPos(false);
}
}

Expand All @@ -539,7 +549,7 @@ public int Panel2MinimumSize
set
{
panel2MinimumSize = value;
CheckSplitterPos();
CheckSplitterPos(false);
}
}

Expand Down
Loading

0 comments on commit 9e5349f

Please sign in to comment.