Files
Terminal.Gui/UICatalog/Scenarios/ContextMenus.cs
Tig 6851b42a49 Fixes #2921 - MainLoop refactoring (#2922)
* Adds basic MainLoop unit tests

* Remove WinChange action from Curses

* Remove WinChange action from Curses

* Remove ProcessInput action from Windows MainLoop

* Simplified MainLoop/ConsoleDriver by making MainLoop internal and moving impt fns to Application

* Modernized Terminal resize events

* Modernized Terminal resize events

* Removed un used property

* for _isWindowsTerminal devenv->wininit; not sure what changed

* Modernized mouse/keyboard events (Action->EventHandler)

* Updated OnMouseEvent API docs

* Using WT_SESSION to detect WT

* removes hacky GetParentProcess

* Updates to fix #2634 (clear last line)

* removes hacky GetParentProcess2

* Addressed mac resize issue

* Addressed mac resize issue

* Removes ConsoleDriver.PrepareToRun, has Init return MainLoop

* Removes unneeded Attribute methods

* Removed GetProcesssName

* Removed GetProcesssName

* Refactored KeyEvent and KeyEventEventArgs into a single class

* Revert "Refactored KeyEvent and KeyEventEventArgs into a single class"

This reverts commit 88a00658db.

* Fixed key repeat issue; reverted stupidity on 1049/1047 confusion

* Updated CSI API Docs

* merge
2023-10-21 08:06:04 -07:00

166 lines
5.7 KiB
C#

using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using Terminal.Gui;
namespace UICatalog.Scenarios {
[ScenarioMetadata (Name: "ContextMenus", Description: "Context Menu Sample.")]
[ScenarioCategory ("Menus")]
public class ContextMenus : Scenario {
private ContextMenu _contextMenu = new ContextMenu ();
private readonly List<CultureInfo> _cultureInfos = Application.SupportedCultures;
private MenuItem _miForceMinimumPosToZero;
private bool _forceMinimumPosToZero = true;
private TextField _tfTopLeft, _tfTopRight, _tfMiddle, _tfBottomLeft, _tfBottomRight;
private MenuItem _miUseSubMenusSingleFrame;
private bool _useSubMenusSingleFrame;
public override void Setup ()
{
var text = "Context Menu";
var width = 20;
Win.Add (new Label ("Press 'Ctrl + Space' to open the Window context menu.") {
X = Pos.Center (),
Y = 1
});
_tfTopLeft = new TextField (text) {
Width = width
};
Win.Add (_tfTopLeft);
_tfTopRight = new TextField (text) {
X = Pos.AnchorEnd (width),
Width = width
};
Win.Add (_tfTopRight);
_tfMiddle = new TextField (text) {
X = Pos.Center (),
Y = Pos.Center (),
Width = width
};
Win.Add (_tfMiddle);
_tfBottomLeft = new TextField (text) {
Y = Pos.AnchorEnd (1),
Width = width
};
Win.Add (_tfBottomLeft);
_tfBottomRight = new TextField (text) {
X = Pos.AnchorEnd (width),
Y = Pos.AnchorEnd (1),
Width = width
};
Win.Add (_tfBottomRight);
Point mousePos = default;
Win.KeyPressed += (s, e) => {
if (e.KeyEvent.Key == (Key.Space | Key.CtrlMask)) {
ShowContextMenu (mousePos.X, mousePos.Y);
e.Handled = true;
}
};
Win.MouseClick += (s, e) => {
if (e.MouseEvent.Flags == _contextMenu.MouseFlags) {
ShowContextMenu (e.MouseEvent.X, e.MouseEvent.Y);
e.Handled = true;
}
};
Application.MouseEvent += ApplicationMouseEvent;
void ApplicationMouseEvent (object sender, MouseEventEventArgs a)
{
mousePos = new Point (a.MouseEvent.X, a.MouseEvent.Y);
}
Win.WantMousePositionReports = true;
Application.Top.Closed += (s,e) => {
Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en-US");
Application.MouseEvent -= ApplicationMouseEvent;
};
}
private void ShowContextMenu (int x, int y)
{
_contextMenu = new ContextMenu (x, y,
new MenuBarItem (new MenuItem [] {
new MenuItem ("_Configuration", "Show configuration", () => MessageBox.Query (50, 5, "Info", "This would open settings dialog", "Ok")),
new MenuBarItem ("More options", new MenuItem [] {
new MenuItem ("_Setup", "Change settings", () => MessageBox.Query (50, 5, "Info", "This would open setup dialog", "Ok")),
new MenuItem ("_Maintenance", "Maintenance mode", () => MessageBox.Query (50, 5, "Info", "This would open maintenance dialog", "Ok")),
}),
new MenuBarItem ("_Languages", GetSupportedCultures ()),
_miForceMinimumPosToZero = new MenuItem ("ForceMinimumPosToZero", "", () => {
_miForceMinimumPosToZero.Checked = _forceMinimumPosToZero = !_forceMinimumPosToZero;
_tfTopLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
_tfTopRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
_tfMiddle.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
_tfBottomLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
_tfBottomRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
}) { CheckType = MenuItemCheckStyle.Checked, Checked = _forceMinimumPosToZero },
_miUseSubMenusSingleFrame = new MenuItem ("Use_SubMenusSingleFrame", "",
() => _contextMenu.UseSubMenusSingleFrame = (bool)(_miUseSubMenusSingleFrame.Checked = _useSubMenusSingleFrame = !_useSubMenusSingleFrame)) {
CheckType = MenuItemCheckStyle.Checked, Checked = _useSubMenusSingleFrame
},
null,
new MenuItem ("_Quit", "", () => Application.RequestStop ())
})
) { ForceMinimumPosToZero = _forceMinimumPosToZero, UseSubMenusSingleFrame = _useSubMenusSingleFrame };
_tfTopLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
_tfTopRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
_tfMiddle.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
_tfBottomLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
_tfBottomRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
_contextMenu.Show ();
}
private MenuItem [] GetSupportedCultures ()
{
List<MenuItem> supportedCultures = new List<MenuItem> ();
var index = -1;
foreach (var c in _cultureInfos) {
var culture = new MenuItem {
CheckType = MenuItemCheckStyle.Checked
};
if (index == -1) {
culture.Title = "_English";
culture.Help = "en-US";
culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == "en-US";
CreateAction (supportedCultures, culture);
supportedCultures.Add (culture);
index++;
culture = new MenuItem {
CheckType = MenuItemCheckStyle.Checked
};
}
culture.Title = $"_{c.Parent.EnglishName}";
culture.Help = c.Name;
culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == c.Name;
CreateAction (supportedCultures, culture);
supportedCultures.Add (culture);
}
return supportedCultures.ToArray ();
void CreateAction (List<MenuItem> supportedCultures, MenuItem culture)
{
culture.Action += () => {
Thread.CurrentThread.CurrentUICulture = new CultureInfo (culture.Help);
culture.Checked = true;
foreach (var item in supportedCultures) {
item.Checked = item.Help == Thread.CurrentThread.CurrentUICulture.Name;
}
};
}
}
}
}