From 0c2872979d4df1b2c7044f37e2360549d3f8048c Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 21 May 2020 00:36:20 +0100 Subject: [PATCH] Added wheeling feature to the ScrollView. --- Terminal.Gui/Core.cs | 13 +++++++-- Terminal.Gui/Views/ScrollView.cs | 45 ++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/Terminal.Gui/Core.cs b/Terminal.Gui/Core.cs index 4bed3fbf6..6ab3314d2 100644 --- a/Terminal.Gui/Core.cs +++ b/Terminal.Gui/Core.cs @@ -2267,8 +2267,12 @@ namespace Terminal.Gui { OfY = me.Y - newxy.Y, View = view }; - mouseGrabView.MouseEvent (nme); - return; + if (OutsideFrame (new Point (nme.X, nme.Y), mouseGrabView.Frame)) + lastMouseOwnerView.OnMouseLeave (me); + if (mouseGrabView != null) { + mouseGrabView.MouseEvent (nme); + return; + } } if (view != null) { @@ -2303,6 +2307,11 @@ namespace Terminal.Gui { } } + static bool OutsideFrame (Point p, Rect r) + { + return p.X < 0 || p.X > r.Width - 1 || p.Y < 0 || p.Y > r.Height - 1; + } + /// /// Action that is invoked once at beginning. /// diff --git a/Terminal.Gui/Views/ScrollView.cs b/Terminal.Gui/Views/ScrollView.cs index b10e567b2..e67f43eed 100644 --- a/Terminal.Gui/Views/ScrollView.cs +++ b/Terminal.Gui/Views/ScrollView.cs @@ -14,6 +14,8 @@ // - Perhaps allow an option to not display the scrollbar arrow indicators? using System; +using System.Reflection; + namespace Terminal.Gui { /// /// ScrollBarViews are views that display a 1-character scrollbar, either horizontal or vertical @@ -73,7 +75,7 @@ namespace Terminal.Gui { /// Frame for the scrollbar. /// The size that this scrollbar represents. /// The position within this scrollbar. - /// If set to true this is a vertical scrollbar, otherwize, the scrollbar is horizontal. + /// If set to true this is a vertical scrollbar, otherwise, the scrollbar is horizontal. public ScrollBarView (Rect rect, int size, int position, bool isVertical) : base (rect) { vertical = isVertical; @@ -314,9 +316,35 @@ namespace Terminal.Gui { /// The view to add to the scrollview. public override void Add (View view) { + if (!IsOverridden (view)) { + view.MouseEnter += View_MouseEnter; + view.MouseLeave += View_MouseLeave; + vertical.MouseEnter += View_MouseEnter; + vertical.MouseLeave += View_MouseLeave; + horizontal.MouseEnter += View_MouseEnter; + horizontal.MouseLeave += View_MouseLeave; + } contentView.Add (view); } + void View_MouseLeave (object sender, MouseEvent e) + { + Application.UngrabMouse (); + } + + void View_MouseEnter (object sender, MouseEvent e) + { + Application.GrabMouse (this); + } + + bool IsOverridden (View view) + { + Type t = view.GetType (); + MethodInfo m = t.GetMethod ("MouseEvent"); + + return m.DeclaringType == t && m.GetBaseDefinition ().DeclaringType == typeof (Responder); + } + /// /// Gets or sets the visibility for the horizontal scroll indicator. /// @@ -463,7 +491,7 @@ namespace Terminal.Gui { switch (kb.Key) { case Key.CursorUp: return ScrollUp (1); - case (Key) 'v' | Key.AltMask: + case (Key)'v' | Key.AltMask: case Key.PageUp: return ScrollUp (Bounds.Height); @@ -489,5 +517,18 @@ namespace Terminal.Gui { } return false; } + + public override bool MouseEvent (MouseEvent me) + { + if (me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp) + return false; + + if (me.Flags == MouseFlags.WheeledDown) + ScrollDown (1); + else if (me.Flags == MouseFlags.WheeledUp) + ScrollUp (1); + + return true; + } } }