Skip to content
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

[Breaking] Use CommandManager from Labs #406

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/AvaloniaEdit/AvaloniaEdit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<ItemGroup>
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.Labs.CommandManager" Version="11.0.10.1" />
<PackageReference Include="System.Collections.Immutable" Version="1.6.0" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
</ItemGroup>

Expand Down
11 changes: 3 additions & 8 deletions src/AvaloniaEdit/AvaloniaEditCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
using System.Runtime.InteropServices;
using Avalonia;
using Avalonia.Input;
using Avalonia.Platform;
using Avalonia.Labs.Input;

namespace AvaloniaEdit
{
Expand Down Expand Up @@ -115,13 +115,8 @@ public static class ApplicationCommands
public static RoutedCommand Replace { get; } = new RoutedCommand(nameof(Replace), GetReplaceKeyGesture());

private static KeyModifiers GetPlatformCommandKey()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return KeyModifiers.Meta;
}

return KeyModifiers.Control;
{
return Application.Current?.PlatformSettings?.HotkeyConfiguration.CommandModifiers ?? KeyModifiers.Control;
}

private static KeyGesture GetReplaceKeyGesture()
Expand Down
5 changes: 3 additions & 2 deletions src/AvaloniaEdit/Editing/CaretNavigationCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using AvaloniaEdit.Utils;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Labs.Input;
using Avalonia.Media.TextFormatting;
using LogicalDirection = AvaloniaEdit.Document.LogicalDirection;

Expand Down Expand Up @@ -61,12 +62,12 @@ public static TextAreaInputHandler Create(TextArea textArea)
return handler;
}

static readonly List<RoutedCommandBinding> CommandBindings = new List<RoutedCommandBinding>();
static readonly List<CommandBinding> CommandBindings = new List<CommandBinding>();
static readonly List<KeyBinding> KeyBindings = new List<KeyBinding>();

private static void AddBinding(RoutedCommand command, EventHandler<ExecutedRoutedEventArgs> handler)
{
CommandBindings.Add(new RoutedCommandBinding(command, handler));
CommandBindings.Add(new CommandBinding(command, handler));
}

private static void AddBinding(RoutedCommand command, KeyModifiers modifiers, Key key, EventHandler<ExecutedRoutedEventArgs> handler)
Expand Down
7 changes: 4 additions & 3 deletions src/AvaloniaEdit/Editing/EditingCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using Avalonia.Input;
using AvaloniaEdit.Utils;
using Avalonia.Controls;
using Avalonia.Labs.Input;

namespace AvaloniaEdit.Editing
{
Expand All @@ -45,19 +46,19 @@ public static TextAreaInputHandler Create(TextArea textArea)
return handler;
}

private static readonly List<RoutedCommandBinding> CommandBindings = new List<RoutedCommandBinding>();
private static readonly List<CommandBinding> CommandBindings = new List<CommandBinding>();
private static readonly List<KeyBinding> KeyBindings = new List<KeyBinding>();

private static void AddBinding(RoutedCommand command, KeyModifiers modifiers, Key key,
EventHandler<ExecutedRoutedEventArgs> handler)
{
CommandBindings.Add(new RoutedCommandBinding(command, handler));
CommandBindings.Add(new CommandBinding(command, handler));
KeyBindings.Add(TextAreaDefaultInputHandler.CreateKeyBinding(command, modifiers, key));
}

private static void AddBinding(RoutedCommand command, EventHandler<ExecutedRoutedEventArgs> handler, EventHandler<CanExecuteRoutedEventArgs> canExecuteHandler = null)
{
CommandBindings.Add(new RoutedCommandBinding(command, handler, canExecuteHandler));
CommandBindings.Add(new CommandBinding(command, handler, canExecuteHandler));
}

static EditingCommandHandler()
Expand Down
1 change: 1 addition & 0 deletions src/AvaloniaEdit/Editing/RectangleSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Linq;
using System.Text;
using Avalonia;
using Avalonia.Labs.Input;
using AvaloniaEdit.Document;
using AvaloniaEdit.Utils;

Expand Down
5 changes: 3 additions & 2 deletions src/AvaloniaEdit/Editing/TextArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Avalonia.Labs.Input;

namespace AvaloniaEdit.Editing
{
/// <summary>
/// Control that wraps a TextView and adds support for user input and the caret.
/// </summary>
public class TextArea : TemplatedControl, ITextEditorComponent, IRoutedCommandBindable, ILogicalScrollable
public class TextArea : TemplatedControl, ITextEditorComponent, ILogicalScrollable
{
/// <summary>
/// This is the extra scrolling space that occurs after the last line.
Expand Down Expand Up @@ -1075,7 +1076,7 @@ internal void OnTextCopied(TextEventArgs e)
TextCopied?.Invoke(this, e);
}

public IList<RoutedCommandBinding> CommandBindings { get; } = new List<RoutedCommandBinding>();
public IList<CommandBinding> CommandBindings => CommandManager.GetCommandBindings(this);

bool ILogicalScrollable.IsLogicalScrollEnabled => _logicalScrollable?.IsLogicalScrollEnabled ?? default(bool);

Expand Down
3 changes: 2 additions & 1 deletion src/AvaloniaEdit/Editing/TextAreaDefaultInputHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Windows.Input;
using AvaloniaEdit.Document;
using Avalonia.Input;
using Avalonia.Labs.Input;

namespace AvaloniaEdit.Editing
{
Expand Down Expand Up @@ -58,7 +59,7 @@ public TextAreaDefaultInputHandler(TextArea textArea) : base(textArea)

private void AddBinding(RoutedCommand command, EventHandler<ExecutedRoutedEventArgs> handler, EventHandler<CanExecuteRoutedEventArgs> canExecuteHandler = null)
{
CommandBindings.Add(new RoutedCommandBinding(command, handler, canExecuteHandler));
CommandBindings.Add(new CommandBinding(command, handler, canExecuteHandler));
}

internal static KeyBinding CreateKeyBinding(ICommand command, KeyModifiers modifiers, Key key)
Expand Down
19 changes: 11 additions & 8 deletions src/AvaloniaEdit/Editing/TextAreaInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

using System;
using System.Collections.Generic;
using System.Linq;
using AvaloniaEdit.Utils;
using Avalonia.Input;
using Avalonia.Labs.Input;

namespace AvaloniaEdit.Editing
{
Expand Down Expand Up @@ -108,7 +110,7 @@ public virtual void OnPreviewKeyUp(KeyEventArgs e)
/// <remarks><inheritdoc cref="ITextAreaInputHandler"/></remarks>
public class TextAreaInputHandler : ITextAreaInputHandler
{
private readonly ObserveAddRemoveCollection<RoutedCommandBinding> _commandBindings;
private readonly ObserveAddRemoveCollection<CommandBinding> _commandBindings;
private readonly List<KeyBinding> _keyBindings;
private readonly ObserveAddRemoveCollection<ITextAreaInputHandler> _nestedInputHandlers;

Expand All @@ -118,7 +120,7 @@ public class TextAreaInputHandler : ITextAreaInputHandler
public TextAreaInputHandler(TextArea textArea)
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_commandBindings = new ObserveAddRemoveCollection<RoutedCommandBinding>(CommandBinding_Added, CommandBinding_Removed);
_commandBindings = new ObserveAddRemoveCollection<CommandBinding>(CommandBinding_Added, CommandBinding_Removed);
_keyBindings = new List<KeyBinding>();
_nestedInputHandlers = new ObserveAddRemoveCollection<ITextAreaInputHandler>(NestedInputHandler_Added, NestedInputHandler_Removed);
}
Expand All @@ -135,15 +137,15 @@ public TextAreaInputHandler(TextArea textArea)
/// <summary>
/// Gets the command bindings of this input handler.
/// </summary>
public ICollection<RoutedCommandBinding> CommandBindings => _commandBindings;
public ICollection<CommandBinding> CommandBindings => _commandBindings;

private void CommandBinding_Added(RoutedCommandBinding commandBinding)
private void CommandBinding_Added(CommandBinding commandBinding)
{
if (IsAttached)
TextArea.CommandBindings.Add(commandBinding);
}

private void CommandBinding_Removed(RoutedCommandBinding commandBinding)
private void CommandBinding_Removed(CommandBinding commandBinding)
{
if (IsAttached)
TextArea.CommandBindings.Remove(commandBinding);
Expand Down Expand Up @@ -175,7 +177,7 @@ private void CommandBinding_Removed(RoutedCommandBinding commandBinding)
/// <param name="handler">The event handler to run when the command is executed.</param>
public void AddBinding(RoutedCommand command, KeyModifiers modifiers, Key key, EventHandler<ExecutedRoutedEventArgs> handler)
{
CommandBindings.Add(new RoutedCommandBinding(command, handler));
CommandBindings.Add(new CommandBinding(command, handler));
KeyBindings.Add(new KeyBinding { Command = command, Gesture = new KeyGesture (key, modifiers) });
}
#endregion
Expand Down Expand Up @@ -218,9 +220,10 @@ private void TextAreaOnKeyDown(object sender, KeyEventArgs keyEventArgs)

foreach (var commandBinding in CommandBindings)
{
if (commandBinding.Command.Gesture?.Matches(keyEventArgs) == true)
if (commandBinding.Command is RoutedCommand routedCommand
&& routedCommand.Gestures.Any(g => g.Matches(keyEventArgs)))
{
commandBinding.Command.Execute(null, (IInputElement)sender);
routedCommand.Execute(null, (IInputElement)sender);
keyEventArgs.Handled = true;
break;
}
Expand Down
Loading