Skip to content

Commit

Permalink
Implement ShortcutSettingsWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
paralleltree committed May 8, 2021
1 parent 6bb50f7 commit 4c4724c
Show file tree
Hide file tree
Showing 11 changed files with 477 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Ched/Ched.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
<Compile Include="UI\Shortcuts\ShortcutKeySource.cs" />
<Compile Include="UI\Shortcuts\ShortcutManager.cs" />
<Compile Include="UI\Shortcuts\ToolStripItemBuilder.cs" />
<Compile Include="UI\Windows\Behaviors\HideWindowCloseButtonBehavior.cs" />
<Compile Include="UI\Windows\Behaviors\InitialFocusBehavior.cs" />
<Compile Include="UI\Windows\Behaviors\OpenFileBehavior.cs" />
<Compile Include="UI\Windows\Behaviors\StyleBehaviorCollection.cs" />
Expand Down Expand Up @@ -249,10 +250,14 @@
<DependentUpon>VersionInfoForm.cs</DependentUpon>
</Compile>
<Compile Include="UI\Windows\Converters\BitmapImageSourceConverter.cs" />
<Compile Include="UI\Windows\Converters\ShortcutKeyTextConverter.cs" />
<Compile Include="UI\Windows\DiagnosticsWindow.xaml.cs">
<DependentUpon>DiagnosticsWindow.xaml</DependentUpon>
</Compile>
<Compile Include="UI\Windows\EnumSourceProvider.cs" />
<Compile Include="UI\Windows\ShortcutSettingsWindow.xaml.cs">
<DependentUpon>ShortcutSettingsWindow.xaml</DependentUpon>
</Compile>
<Compile Include="UI\Windows\SusExportWindow.xaml.cs">
<DependentUpon>SusExportWindow.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -459,6 +464,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="UI\Windows\ShortcutSettingsWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="UI\Windows\SusExportWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
45 changes: 45 additions & 0 deletions Ched/Localization/MainFormStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Ched/Localization/MainFormStrings.en.resx
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,19 @@
<data name="InsertSingleBlankAtBeginning" xml:space="preserve">
<value>Insert a single blank space at the beginning</value>
</data>
<data name="KeyboardShortcuts" xml:space="preserve">
<value>Keyboard Shortcuts</value>
</data>
<data name="Clear" xml:space="preserve">
<value>Clear</value>
</data>
<data name="Command" xml:space="preserve">
<value>Command</value>
</data>
<data name="KeyCombination" xml:space="preserve">
<value>Key Combination</value>
</data>
<data name="ResetAll" xml:space="preserve">
<value>Reset All</value>
</data>
</root>
15 changes: 15 additions & 0 deletions Ched/Localization/MainFormStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,19 @@
<data name="InsertSingleBlankAtBeginning" xml:space="preserve">
<value>先頭に1小節の空白を挿入する</value>
</data>
<data name="KeyboardShortcuts" xml:space="preserve">
<value>ショートカット設定</value>
</data>
<data name="Clear" xml:space="preserve">
<value>クリア</value>
</data>
<data name="Command" xml:space="preserve">
<value>コマンド</value>
</data>
<data name="KeyCombination" xml:space="preserve">
<value>ショートカットキー</value>
</data>
<data name="ResetAll" xml:space="preserve">
<value>初期化</value>
</data>
</root>
10 changes: 10 additions & 0 deletions Ched/UI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,14 @@ private void SetupCommands(ShortcutCommandSource commandSource)
commandSource.RegisterCommand(Commands.ShowHelp, MainFormStrings.Help, () => System.Diagnostics.Process.Start("https://github.com/paralleltree/Ched/wiki"));
}

private void ConfigureKeyboardShortcut()
{
var vm = new ShortcutSettingsWindowViewModel(ShortcutManagerHost);
var window = new ShortcutSettingsWindow() { DataContext = vm };
window.ShowDialog(this);
ShortcutManager.NotifyUpdateShortcut();
}

private MenuStrip CreateMainMenu(NoteView noteView)
{
var shortcutItemBuilder = new ToolStripMenuItemBuilder(ShortcutManager);
Expand Down Expand Up @@ -580,6 +588,8 @@ private MenuStrip CreateMainMenu(NoteView noteView)
new ToolStripSeparator(),
bookPropertiesMenuItem,
new ToolStripSeparator(),
new ToolStripMenuItem(MainFormStrings.KeyboardShortcuts, null, (s, e) => ConfigureKeyboardShortcut()),
new ToolStripSeparator(),
new ToolStripMenuItem(MainFormStrings.Exit + "(&X)", null, (s, e) => this.Close())
};

Expand Down
6 changes: 6 additions & 0 deletions Ched/UI/Shortcuts/ShortcutCommandSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Ched.UI.Shortcuts
{
public interface IShortcutCommandSource
{
IEnumerable<string> Commands { get; }

/// <summary>
/// 指定のコマンドを実行します。
/// </summary>
Expand All @@ -21,6 +23,8 @@ public interface IShortcutCommandSource

public class NullShortcutCommandSource : IShortcutCommandSource
{
public IEnumerable<string> Commands => Enumerable.Empty<string>();

// Do nothing
public bool ExecuteCommand(string command) => true;

Expand All @@ -35,6 +39,8 @@ public class ShortcutCommandSource : IShortcutCommandSource
{
private Dictionary<string, (string Name, Action Action)> commands { get; } = new Dictionary<string, (string, Action)>();

public IEnumerable<string> Commands => commands.Keys;

public void RegisterCommand(string command, string name, Action action)
{
if (commands.ContainsKey(command)) throw new InvalidOperationException("The command is already registered.");
Expand Down
14 changes: 14 additions & 0 deletions Ched/UI/Shortcuts/ShortcutKeySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ protected void RegisterShortcut(string command, Keys key)
CommandMap[command].Add(key);
}

protected void UnregisterShortcut(Keys key)
{
if (!KeyMap.ContainsKey(key)) throw new InvalidOperationException("The shortcut key is not registered.");
string command = KeyMap[key];
KeyMap.Remove(key);
CommandMap[command].Remove(key);
}

public bool ResolveCommand(Keys key, out string command) => KeyMap.TryGetValue(key, out command);

public bool ResolveShortcutKey(string command, out Keys key)
Expand Down Expand Up @@ -108,6 +116,11 @@ public UserShortcutKeySource(UserShortcutKeySource other) : base(other)
base.RegisterShortcut(command, key);
}

public new void UnregisterShortcut(Keys key)
{
base.UnregisterShortcut(key);
}

public string DumpShortcutKeys()
{
var binds = ShortcutKeys.Select(p =>
Expand Down Expand Up @@ -151,6 +164,7 @@ public DefaultShortcutKeySource()
}
}

[Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)]
public class ShortcutDefinition
{
[Newtonsoft.Json.JsonProperty("command")]
Expand Down
44 changes: 44 additions & 0 deletions Ched/UI/Windows/Behaviors/HideWindowCloseButtonBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using Microsoft.Xaml.Behaviors;

namespace Ched.UI.Windows.Behaviors
{
public class HideWindowCloseButtonBehavior : Behavior<Window>
{
#region NativeMethods
private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x80000;

[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
#endregion

protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += OnLoaded;
}

protected override void OnDetaching()
{
AssociatedObject.Loaded -= OnLoaded;
base.OnDetaching();
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
var hwnd = new WindowInteropHelper(AssociatedObject).Handle;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
}
}
}
27 changes: 27 additions & 0 deletions Ched/UI/Windows/Converters/ShortcutKeyTextConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

using Ched.UI.Shortcuts;

namespace Ched.UI.Windows.Converters
{
public class ShortcutKeyTextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null) return "";
var key = (System.Windows.Forms.Keys)value;
return key.ToShortcutChar();
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Loading

0 comments on commit 4c4724c

Please sign in to comment.