-
Notifications
You must be signed in to change notification settings - Fork 3
Editor: built-in editing context menu #153
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6080deb
Initial plan
Copilot de26290
feat: add built-in editing context menu to Editor
Copilot 60312bc
fix: add Action delegates to context menu items so they invoke commands
Copilot 5944e53
refactor: use TG's declarative MenuItem(targetView, Command) for comm…
Copilot 121f58f
test: strengthen declarative binding test with command order verifica…
Copilot 29188b3
Merge branch 'copilot/add-default-context-menu-to-editor' of https://…
tig 15fc560
code cleanup
tig b873112
merge: resolve conflicts with develop, address CR feedback
Copilot 90930ec
Merge branch 'develop' into copilot/add-default-context-menu-to-editor
tig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| using System.Drawing; | ||
| using Terminal.Gui.Input; | ||
| using Terminal.Gui.ViewBase; | ||
| using Terminal.Gui.Views; | ||
|
|
||
| namespace Terminal.Gui.Editor; | ||
|
|
||
| public partial class Editor | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the built-in context menu shown on right-click or <see cref="Command.Context" />. | ||
| /// Defaults to a standard editing menu (Undo, Redo, Cut, Copy, Paste, Select All) whose items | ||
| /// are state-aware — mutating items are disabled when <see cref="ReadOnly" /> is <see langword="true" />, | ||
| /// Copy / Cut are disabled when there is no selection, and Undo / Redo reflect the undo stack. | ||
| /// Set to <see langword="null" /> to suppress the context menu entirely; set to a custom | ||
| /// <see cref="PopoverMenu" /> to replace it. | ||
| /// </summary> | ||
| public PopoverMenu? ContextMenu | ||
| { | ||
| get; | ||
| set | ||
| { | ||
| field = value; | ||
|
|
||
| field?.Target = new WeakReference<View> (this); | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Creates the default editing context menu items using declarative command binding.</summary> | ||
| /// <remarks> | ||
| /// Each <see cref="MenuItem" /> is constructed with <c>this</c> as the target view and a | ||
| /// <see cref="Command" />. The framework resolves title, help text, and key from | ||
| /// <c>GlobalResources</c> and routes the command to this <see cref="Editor" /> via command | ||
| /// bubbling — no explicit <see cref="MenuItem.Action" /> delegates are needed. | ||
| /// </remarks> | ||
| private View[] CreateDefaultContextMenuItems () | ||
| { | ||
| return | ||
| [ | ||
| new MenuItem (this, Command.Undo), | ||
| new MenuItem (this, Command.Redo), | ||
| new Line (), | ||
| new MenuItem (this, Command.Cut), | ||
| new MenuItem (this, Command.Copy), | ||
| new MenuItem (this, Command.Paste), | ||
| new Line (), | ||
| new MenuItem (this, Command.SelectAll) | ||
| ]; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Updates the <see cref="View.Enabled" /> state of the context menu items to reflect the current | ||
| /// editor state (ReadOnly, selection, clipboard, undo/redo). | ||
| /// </summary> | ||
| private void UpdateContextMenuState () | ||
| { | ||
| if (ContextMenu?.Root is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var hasSelection = HasSelection; | ||
| var canUndo = !ReadOnly && _document is { UndoStack.CanUndo: true }; | ||
| var canRedo = !ReadOnly && _document is { UndoStack.CanRedo: true }; | ||
| var canPaste = !ReadOnly; | ||
| var canCut = !ReadOnly && hasSelection; | ||
|
|
||
| foreach (View child in ContextMenu.Root.SubViews) | ||
| { | ||
| if (child is not MenuItem menuItem) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| switch (menuItem.Command) | ||
| { | ||
| case Command.Undo: | ||
| menuItem.Enabled = canUndo; | ||
|
|
||
| break; | ||
| case Command.Redo: | ||
| menuItem.Enabled = canRedo; | ||
|
|
||
| break; | ||
| case Command.Cut: | ||
| menuItem.Enabled = canCut; | ||
|
|
||
| break; | ||
| case Command.Copy: | ||
| menuItem.Enabled = hasSelection; | ||
|
|
||
| break; | ||
| case Command.Paste: | ||
| menuItem.Enabled = canPaste; | ||
|
|
||
| break; | ||
|
|
||
| // Unknown commands (e.g. from a custom ContextMenu) are left untouched | ||
| // so the caller's intentional Enabled state is preserved. | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Shows the context menu at the given screen position, after updating item state. | ||
| /// </summary> | ||
| private void ShowContextMenu (Point? screenPosition = null) | ||
| { | ||
| if (ContextMenu is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| UpdateContextMenuState (); | ||
| ContextMenu.MakeVisible (screenPosition); | ||
| } | ||
|
|
||
| /// <summary>Builds and assigns the default context menu.</summary> | ||
| private void InitializeDefaultContextMenu () | ||
| { | ||
| ContextMenu = new PopoverMenu (CreateDefaultContextMenuItems ()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.