-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support a contextmenu in AvaloniaEdit
- Loading branch information
Showing
5 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,92 @@ | ||
using ICSharpCode.AvalonEdit; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Input; | ||
|
||
namespace MdXaml.Menus | ||
{ | ||
public static class CommandsForTextEditor | ||
{ | ||
|
||
public static void Setup(TextEditor editor) | ||
{ | ||
editor.ContextMenuOpening += Editor_ContextMenuOpening; | ||
|
||
var menu = new ContextMenu(); | ||
menu.Items.Add(new MenuItem() | ||
{ | ||
Name = ApplicationCommands.Copy.Name, | ||
Header = ApplicationCommands.Copy.Text, | ||
InputGestureText = ExtractShortcut(ApplicationCommands.Copy.InputGestures), | ||
Command = new TextEditorCopyCommand(editor) | ||
}); | ||
menu.Items.Add(new MenuItem() | ||
{ | ||
Name = ApplicationCommands.SelectAll.Name, | ||
Header = ApplicationCommands.SelectAll.Text, | ||
InputGestureText = ExtractShortcut(ApplicationCommands.SelectAll.InputGestures), | ||
Command = new TextEditorSelectAllCommand(editor) | ||
}); | ||
|
||
editor.ContextMenu = menu; | ||
|
||
} | ||
|
||
private static void Editor_ContextMenuOpening(object sender, ContextMenuEventArgs e) | ||
{ | ||
if (sender is TextEditor editor) | ||
{ | ||
var viewer = WalkParent<FlowDocumentScrollViewer>(editor); | ||
|
||
if (viewer is null) | ||
return; | ||
|
||
if (viewer.Selection is not null | ||
&& !viewer.Selection.IsEmpty) | ||
{ | ||
viewer.IsSelectionEnabled = false; | ||
viewer.IsSelectionEnabled = true; | ||
} | ||
} | ||
} | ||
|
||
private static T? WalkParent<T>(FrameworkElement start) | ||
where T : class | ||
{ | ||
DependencyObject dobj = start.Parent; | ||
for (; ; ) | ||
{ | ||
if (dobj is T t) | ||
{ | ||
return t; | ||
} | ||
else if (dobj is FrameworkElement element) | ||
{ | ||
dobj = element.Parent; | ||
} | ||
else if (dobj is FrameworkContentElement content) | ||
{ | ||
dobj = content.Parent; | ||
} | ||
else return null; | ||
} | ||
} | ||
|
||
private static string? ExtractShortcut(InputGestureCollection? collection) | ||
{ | ||
if (collection is null) | ||
return ""; | ||
|
||
return collection.OfType<KeyGesture>() | ||
.Select(g => g.GetDisplayStringForCulture(CultureInfo.CurrentCulture)) | ||
.FirstOrDefault() | ||
?? ""; | ||
} | ||
} | ||
} |
This file contains 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,42 @@ | ||
using ICSharpCode.AvalonEdit; | ||
using System; | ||
using System.Windows.Controls; | ||
using System.Windows.Input; | ||
|
||
namespace MdXaml.Menus | ||
{ | ||
public class TextEditorCopyCommand : ICommand | ||
{ | ||
public event EventHandler? CanExecuteChanged; | ||
|
||
private TextEditor _editor; | ||
private bool _isExecutable; | ||
|
||
public TextEditorCopyCommand(TextEditor editor) | ||
{ | ||
_editor = editor; | ||
_editor.ContextMenuOpening += TryUpdateExecutable; | ||
} | ||
|
||
private void TryUpdateExecutable(object sender, ContextMenuEventArgs e) | ||
{ | ||
var isExecutable = _editor.SelectionLength != 0; | ||
|
||
if (_isExecutable != isExecutable) | ||
{ | ||
_isExecutable = isExecutable; | ||
CanExecuteChanged?.Invoke(this, EventArgs.Empty); | ||
} | ||
} | ||
|
||
public bool CanExecute(object parameter) | ||
{ | ||
return _isExecutable; | ||
} | ||
|
||
public void Execute(object parameter) | ||
{ | ||
_editor.Copy(); | ||
} | ||
} | ||
} |
This file contains 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,32 @@ | ||
using ICSharpCode.AvalonEdit; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows.Input; | ||
|
||
namespace MdXaml.Menus | ||
{ | ||
public class TextEditorSelectAllCommand : ICommand | ||
{ | ||
public event EventHandler? CanExecuteChanged; | ||
|
||
private TextEditor _editor; | ||
|
||
public TextEditorSelectAllCommand(TextEditor editor) | ||
{ | ||
_editor = editor; | ||
} | ||
|
||
public bool CanExecute(object parameter) | ||
{ | ||
return true; | ||
} | ||
|
||
public void Execute(object parameter) | ||
{ | ||
_editor.SelectAll(); | ||
} | ||
} | ||
} |
84d4291
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.
refs #59