-
Notifications
You must be signed in to change notification settings - Fork 703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #3778: Decouples Command
from KeyBindings
#3880
Conversation
Code cleanup
With what I've done so far all of the Keyboard and Mouse handling code in AddCommand (Command.Up, commandContext => Move (commandContext, -16));
AddCommand (Command.Down, commandContext => Move (commandContext, 16));
AddCommand (Command.Left, commandContext => Move (commandContext, -1));
AddCommand (Command.Right, commandContext => Move (commandContext, 1));
AddCommand (Command.PageUp, commandContext => Move (commandContext, -(Viewport.Height - HEADER_HEIGHT / _rowHeight) * 16));
AddCommand (Command.PageDown, commandContext => Move (commandContext, (Viewport.Height - HEADER_HEIGHT / _rowHeight) * 16));
AddCommand (Command.Start, commandContext => Move (commandContext, -SelectedCodePoint));
AddCommand (Command.End, commandContext => Move (commandContext, MAX_CODE_POINT - SelectedCodePoint));
AddCommand (Command.ScrollDown, () => ScrollVertical (1));
AddCommand (Command.ScrollUp, () => ScrollVertical (-1));
AddCommand (Command.ScrollRight, () => ScrollHorizontal (1));
AddCommand (Command.ScrollLeft, () => ScrollHorizontal (-1));
AddCommand (Command.Accept, HandleAcceptCommand);
AddCommand (Command.Select, HandleSelectCommand);
AddCommand (Command.Context, HandleContextCommand);
KeyBindings.Add (Key.CursorUp, Command.Up);
KeyBindings.Add (Key.CursorDown, Command.Down);
KeyBindings.Add (Key.CursorLeft, Command.Left);
KeyBindings.Add (Key.CursorRight, Command.Right);
KeyBindings.Add (Key.PageUp, Command.PageUp);
KeyBindings.Add (Key.PageDown, Command.PageDown);
KeyBindings.Add (Key.Home, Command.Start);
KeyBindings.Add (Key.End, Command.End);
KeyBindings.Add (ContextMenu.DefaultKey, Command.Context);
MouseBindings.Add (MouseFlags.Button1DoubleClicked, Command.Accept);
MouseBindings.ReplaceCommands(MouseFlags.Button3Clicked, Command.Context);
MouseBindings.ReplaceCommands (MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl, Command.Context);
MouseBindings.Add (MouseFlags.WheeledDown, Command.ScrollDown);
MouseBindings.Add (MouseFlags.WheeledUp, Command.ScrollUp);
MouseBindings.Add (MouseFlags.WheeledLeft, Command.ScrollLeft);
MouseBindings.Add (MouseFlags.WheeledRight, Command.ScrollRight); |
Here's the excerpt from Keyboard APIsTerminal.Gui provides the following APIs for handling keyboard input:
Each of these APIs are described more fully below. Key BindingsKey Bindings is the preferred way of handling keyboard input in View implementations. The View calls @Terminal.Gui.AddCommand to declare it supports a particular command and then uses @Terminal.Gui.KeyBindings to indicate which key presses will invoke the command. For example, if a View wants to respond to the user pressing the up arrow key to scroll up it would do this public MyView : View
{
AddCommand (Command.ScrollUp, () => ScrollVertical (-1));
KeyBindings.Add (Key.CursorUp, Command.ScrollUp);
} The The Command enum lists generic operations that are implemented by views. For example The default key for activating a button is var btn = new Button () { Title = "Press me" };
btn.KeyBindings.ReplaceKey (btn.KeyBindings.GetKeyFromCommands (Command.Accept)); Key Bindings can be added at the For Application-scoped Key Bindings there are two categories of Application-scoped Key Bindings:
Use @Terminal.Gui.Application.KeyBindings to add or modify Application-scoped Key Bindings. View-scoped Key Bindings also have two categories:
Application-Scoped Key Bindings HotKeyA HotKey is a key press that selects a visible UI item. For selecting items across By default, the ShortcutA Shortcut is an opinionated (visually & API) View for displaying a command, help text, key key press that invokes a Command. The Command can be invoked even if the
MenuBar, ContextMenu, and StatusBar support |
And here's the new Mouse APIsTerminal.Gui provides the following APIs for handling mouse input:
Each of these APIs are described more fully below. Mouse BindingsMouse Bindings is the preferred way of handling mouse input in View implementations. The View calls @Terminal.Gui.AddCommand to declare it supports a particular command and then uses @Terminal.Gui.MouseBindings to indicate which mouse events will invoke the command. For example, if a View wants to respond to the user using the mouse wheel to scroll up, it would do this: public MyView : View
{
AddCommand (Command.ScrollUp, () => ScrollVertical (-1));
MouseBindings.Add (MouseFlags.Button1DoubleClick, Command.ScrollUp);
} The Command enum lists generic operations that are implemented by views. |
@tznind, i'd love your comments on all this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lovely work.
@tznind thanks a ton! As I had hoped, you nailed it. So. Much. Better. |
Fixes
Command
is too tightly coupled withKeyBindings
#3778Proposed Changes/Todos
CommandContext
to be genericKeyBindings
to use newCommandContext<T>
KeyBindings
,ApplicationKeyBindings
, andMouseBindings
to reduce duplicate code.Pull Request checklist:
CTRL-K-D
to automatically reformat your files before committing.dotnet test
before commit///
style comments)