瀏覽代碼

Added mouse support to the StatusBar. (#598)

BDisp 5 年之前
父節點
當前提交
fa29269ade
共有 2 個文件被更改,包括 53 次插入3 次删除
  1. 13 2
      Example/demo.cs
  2. 40 1
      Terminal.Gui/Views/StatusBar.cs

+ 13 - 2
Example/demo.cs

@@ -375,6 +375,17 @@ static class Demo {
 		MessageBox.Query (50, 7, "Help", "This is a small help\nBe kind.", "Ok");
 	}
 
+	static void Load ()
+	{
+		MessageBox.Query (50, 7, "Load", "This is a small load\nBe kind.", "Ok");
+	}
+
+	static void Save ()
+	{
+		MessageBox.Query (50, 7, "Save", "This is a small save\nBe kind.", "Ok");
+	}
+
+
 	#region Selection Demo
 
 	static void ListSelectionDemo (bool multiple)
@@ -611,8 +622,8 @@ static class Demo {
 
 		var statusBar = new StatusBar (new StatusItem [] {
 			new StatusItem(Key.F1, "~F1~ Help", () => Help()),
-			new StatusItem(Key.F2, "~F2~ Load", null),
-			new StatusItem(Key.F3, "~F3~ Save", null),
+			new StatusItem(Key.F2, "~F2~ Load", Load),
+			new StatusItem(Key.F3, "~F3~ Save", Save),
 			new StatusItem(Key.ControlQ, "~^Q~ Quit", () => { if (Quit ()) top.Running = false; }),
 		}) {
 			Parent = null,

+ 40 - 1
Terminal.Gui/Views/StatusBar.cs

@@ -185,11 +185,50 @@ namespace Terminal.Gui {
 		{
 			foreach (var item in Items) {
 				if (kb.Key == item.Shortcut) {
-					if (item.Action != null) item.Action ();
+					item.Action?.Invoke ();
 					return true;
 				}
 			}
 			return false;
 		}
+
+		///<inheritdoc cref="MouseEvent"/>
+		public override bool MouseEvent (MouseEvent me)
+		{
+			if (me.Flags != MouseFlags.Button1Clicked)
+				return false;
+
+			int pos = 1;
+			for (int i = 0; i < Items.Length; i++) {
+				if (me.X >= pos && me.X < pos + GetItemTitleLength (Items [i].Title)) {
+					Run (Items [i].Action);
+				}
+				pos += GetItemTitleLength (Items [i].Title) + 1;
+			}
+			return true;
+		}
+
+		int GetItemTitleLength (ustring title)
+		{
+			int len = 0;
+			foreach (var ch in title) {
+				if (ch == '~')
+					continue;
+				len++;
+			}
+
+			return len;
+		}
+
+		void Run (Action action)
+		{
+			if (action == null)
+				return;
+
+			Application.MainLoop.AddIdle (() => {
+				action ();
+				return false;
+			});
+		}
 	}
 }