Răsfoiți Sursa

Added wheeling feature to the ScrollView.

BDisp 5 ani în urmă
părinte
comite
686e6d5df6
2 a modificat fișierele cu 54 adăugiri și 4 ștergeri
  1. 11 2
      Terminal.Gui/Core.cs
  2. 43 2
      Terminal.Gui/Views/ScrollView.cs

+ 11 - 2
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;
+		}
+
 		/// <summary>
 		/// Action that is invoked once at beginning.
 		/// </summary>

+ 43 - 2
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 {
 	/// <summary>
 	/// ScrollBarViews are views that display a 1-character scrollbar, either horizontal or vertical
@@ -73,7 +75,7 @@ namespace Terminal.Gui {
 		/// <param name="rect">Frame for the scrollbar.</param>
 		/// <param name="size">The size that this scrollbar represents.</param>
 		/// <param name="position">The position within this scrollbar.</param>
-		/// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwize, the scrollbar is horizontal.</param>
+		/// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</param>
 		public ScrollBarView (Rect rect, int size, int position, bool isVertical) : base (rect)
 		{
 			vertical = isVertical;
@@ -314,9 +316,35 @@ namespace Terminal.Gui {
 		/// <param name="view">The view to add to the scrollview.</param>
 		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);
+		}
+
 		/// <summary>
 		/// Gets or sets the visibility for the horizontal scroll indicator.
 		/// </summary>
@@ -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;
+		}
 	}
 }