浏览代码

Improves mouse functionality and fixed the CharacterMap scenario.

BDisp 4 年之前
父节点
当前提交
bf6c4ec417
共有 3 个文件被更改,包括 48 次插入27 次删除
  1. 28 20
      Terminal.Gui/Views/ScrollBarView.cs
  2. 19 6
      Terminal.Gui/Views/ScrollView.cs
  3. 1 1
      UICatalog/Scenarios/CharacterMap.cs

+ 28 - 20
Terminal.Gui/Views/ScrollBarView.cs

@@ -222,12 +222,6 @@ namespace Terminal.Gui {
 			}
 		}
 
-		void SetPosition (int newPos)
-		{
-			Position = newPos;
-			OnChangedPosition ();
-		}
-
 		/// <summary>
 		/// Virtual method to invoke the <see cref="ChangedPosition"/> action event.
 		/// </summary>
@@ -435,28 +429,42 @@ namespace Terminal.Gui {
 		public override bool MouseEvent (MouseEvent me)
 		{
 			if (me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
-				!me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
+				!me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) &&
+				me.Flags != MouseFlags.Button1Released && me.Flags != MouseFlags.WheeledDown &&
+				me.Flags != MouseFlags.WheeledUp && me.Flags != MouseFlags.WheeledRight &&
+				me.Flags != MouseFlags.WheeledLeft) {
 				return false;
 			}
 
-			if (!me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
-				lastLocation = -1;
-			}
-
 			int location = vertical ? me.Y : me.X;
 			int barsize = vertical ? Bounds.Height : Bounds.Width;
 			int posTopLeftTee = vertical ? posTopTee : posLeftTee;
 			int posBottomRightTee = vertical ? posBottomTee : posRightTee;
-
 			barsize -= 2;
 			var pos = Position;
+
+			if ((me.Flags.HasFlag (MouseFlags.Button1Pressed) ||
+				me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
+				&& (Application.mouseGrabView == null || Application.mouseGrabView != this)) {
+				Application.GrabMouse (this);
+			} else if (me.Flags == MouseFlags.Button1Released && Application.mouseGrabView != null && Application.mouseGrabView == this) {
+				Application.UngrabMouse ();
+				return true;
+			} else if (showScrollIndicator && (me.Flags == MouseFlags.WheeledDown || me.Flags == MouseFlags.WheeledUp ||
+				me.Flags == MouseFlags.WheeledRight || me.Flags == MouseFlags.WheeledLeft)) {
+				return Host.MouseEvent (me);
+			}
+
+			if (!me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
+				lastLocation = -1;
+			}
 			if (location == 0) {
 				if (pos > 0) {
-					SetPosition (pos - 1);
+					Position = pos - 1;
 				}
 			} else if (location == barsize + 1) {
 				if (CanScroll (1, out _, vertical)) {
-					SetPosition (pos + 1);
+					Position = pos + 1;
 				}
 			} else if (location > 0 && location < barsize + 1) {
 				var b1 = pos * barsize / Size;
@@ -468,11 +476,11 @@ namespace Terminal.Gui {
 				if (location > b1 && location <= b2 + 1) {
 					if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1Clicked) {
 						if (location == 1) {
-							SetPosition (0);
+							Position = 0;
 						} else if (location == barsize) {
 							CanScroll (Size - pos, out int nv, vertical);
 							if (nv > 0) {
-								SetPosition (Math.Min (pos + nv, Size));
+								Position = Math.Min (pos + nv, Size);
 							}
 						}
 					} else if (me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
@@ -481,12 +489,12 @@ namespace Terminal.Gui {
 						if ((location >= b1 && location <= ml) || (location < lastLocation && lastLocation > -1)) {
 							lastLocation = location;
 							var np = b1 * Size / barsize;
-							SetPosition (np);
+							Position = np;
 						} else if (location > lastLocation) {
 							var np = location * Size / barsize;
 							CanScroll (np - pos, out int nv, vertical);
 							if (nv > 0) {
-								SetPosition (pos + nv);
+								Position = pos + nv;
 							}
 						}
 					}
@@ -494,10 +502,10 @@ namespace Terminal.Gui {
 					if (location >= b2 + 1 && location > posTopLeftTee && location > b1 && location > posBottomRightTee && posBottomRightTee > 0) {
 						CanScroll (location, out int nv, vertical);
 						if (nv > 0) {
-							SetPosition (Math.Min (pos + nv, Size));
+							Position = Math.Min (pos + nv, Size);
 						}
 					} else if (location <= b1) {
-						SetPosition (Math.Max (pos - barsize - location, 0));
+						Position = Math.Max (pos - barsize - location, 0);
 					}
 				}
 			}

+ 19 - 6
Terminal.Gui/Views/ScrollView.cs

@@ -78,6 +78,8 @@ namespace Terminal.Gui {
 
 			MouseEnter += View_MouseEnter;
 			MouseLeave += View_MouseLeave;
+			contentView.MouseEnter += View_MouseEnter;
+			contentView.MouseLeave += View_MouseLeave;
 		}
 
 		Size contentSize;
@@ -115,11 +117,20 @@ namespace Terminal.Gui {
 				return contentOffset;
 			}
 			set {
-				contentOffset = new Point (-Math.Abs (value.X), -Math.Abs (value.Y));
-				contentView.Frame = new Rect (contentOffset, contentSize);
-				vertical.Position = Math.Max (0, -contentOffset.Y);
-				horizontal.Position = Math.Max (0, -contentOffset.X);
-				SetNeedsDisplay ();
+				var co = new Point (-Math.Abs (value.X), -Math.Abs (value.Y));
+				if (contentOffset != co) {
+					contentOffset = co;
+					contentView.Frame = new Rect (contentOffset, contentSize);
+					var p = Math.Max (0, -contentOffset.Y);
+					if (vertical.Position != p) {
+						vertical.Position = Math.Max (0, -contentOffset.Y);
+					}
+					p = Math.Max (0, -contentOffset.X);
+					if (horizontal.Position != p) {
+						horizontal.Position = Math.Max (0, -contentOffset.X);
+					}
+					SetNeedsDisplay ();
+				}
 			}
 		}
 
@@ -180,7 +191,9 @@ namespace Terminal.Gui {
 
 		void View_MouseLeave (MouseEventArgs e)
 		{
-			Application.UngrabMouse ();
+			if (Application.mouseGrabView != null && Application.mouseGrabView != vertical && Application.mouseGrabView != horizontal) {
+				Application.UngrabMouse ();
+			}
 		}
 
 		void View_MouseEnter (MouseEventArgs e)

+ 1 - 1
UICatalog/Scenarios/CharacterMap.cs

@@ -143,7 +143,7 @@ namespace UICatalog {
 				Move (viewport.X + RowHeaderWidth + (header * H_SPACE), 0);
 				Driver.AddStr ($" {header:x} ");
 			}
-			for (int row = 0, y = 0; row < viewport.Height / 2 - 1; row++, y+= V_SPACE) {
+			for (int row = -ContentOffset.Y, y = 0; row < -ContentOffset.Y + viewport.Height / 2 - 1; row++, y+= V_SPACE) {
 				int val = (-viewport.Y + row) * 16;
 				if (val < MaxCodePointVal) {
 					var rowLabel = $"U+{val / 16:x4}x";