Skip to content
Merged
3 changes: 3 additions & 0 deletions Flow.Launcher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>

PluginManager.LoadPlugins(_settings.PluginSettings);
_mainVM = new MainViewModel(_settings);

HotKeyMapper.Initialize(_mainVM);

API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet);

Http.API = API;
Expand Down
46 changes: 5 additions & 41 deletions Flow.Launcher/CustomQueryHotkeySetting.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.UserSettings;
using NHotkey;
using NHotkey.Wpf;
using System;
using System.Collections.ObjectModel;
using System.Linq;
Expand Down Expand Up @@ -52,11 +51,7 @@ private void btnAdd_OnClick(object sender, RoutedEventArgs e)
};
_settings.CustomPluginHotkeys.Add(pluginHotkey);

SetHotkey(ctlHotkey.CurrentHotkey, delegate
{
App.API.ChangeQuery(pluginHotkey.ActionKeyword);
ShowMainWindow();
});
HotKeyMapper.SetCustomQueryHotkey(pluginHotkey);
}
else
{
Expand All @@ -69,22 +64,11 @@ private void btnAdd_OnClick(object sender, RoutedEventArgs e)
updateCustomHotkey.ActionKeyword = tbAction.Text;
updateCustomHotkey.Hotkey = ctlHotkey.CurrentHotkey.ToString();
//remove origin hotkey
RemoveHotkey(oldHotkey);
SetHotkey(new HotkeyModel(updateCustomHotkey.Hotkey), delegate
{
App.API.ChangeQuery(updateCustomHotkey.ActionKeyword);
ShowMainWindow();
});
HotKeyMapper.RemoveHotkey(oldHotkey);
HotKeyMapper.SetCustomQueryHotkey(updateCustomHotkey);
}

Close();

static void ShowMainWindow()
{
Window mainWindow = Application.Current.MainWindow;
mainWindow.Visibility = Visibility.Visible;
mainWindow.Focus();
}
}

public void UpdateItem(CustomPluginHotkey item)
Expand All @@ -107,28 +91,8 @@ private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e)
{
App.API.ChangeQuery(tbAction.Text);
Application.Current.MainWindow.Visibility = Visibility.Visible;
}

private void RemoveHotkey(string hotkeyStr)
{
if (!string.IsNullOrEmpty(hotkeyStr))
{
HotkeyManager.Current.Remove(hotkeyStr);
}
}
Application.Current.MainWindow.Focus();

private void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
{
string hotkeyStr = hotkey.ToString();
try
{
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
}
catch (Exception)
{
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
MessageBox.Show(errorMsg);
}
}

private void cmdEsc_OnPress(object sender, ExecutedRoutedEventArgs e)
Expand Down
136 changes: 136 additions & 0 deletions Flow.Launcher/Helper/HotKeyMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.UserSettings;
using System;
using NHotkey;
using NHotkey.Wpf;
using Flow.Launcher.Core.Resource;
using System.Windows;
using Flow.Launcher.ViewModel;

namespace Flow.Launcher.Helper
{
internal static class HotKeyMapper
{
private static Settings settings;
private static MainViewModel mainViewModel;

internal static void Initialize(MainViewModel mainVM)
{
mainViewModel = mainVM;
settings = mainViewModel._settings;

SetHotkey(settings.Hotkey, OnHotkey);
LoadCustomPluginHotkey();
}

private static void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
{
var hotkey = new HotkeyModel(hotkeyStr);
SetHotkey(hotkey, action);
}

internal static void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
{
string hotkeyStr = hotkey.ToString();
try
{
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
}
catch (Exception)
{
string errorMsg =
string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"),
hotkeyStr);
MessageBox.Show(errorMsg);
}
}

internal static void RemoveHotkey(string hotkeyStr)
{
if (!string.IsNullOrEmpty(hotkeyStr))
{
HotkeyManager.Current.Remove(hotkeyStr);
}
}

internal static void OnHotkey(object sender, HotkeyEventArgs e)
{
if (!ShouldIgnoreHotkeys())
{
UpdateLastQUeryMode();

mainViewModel.ToggleFlowLauncher();
e.Handled = true;
}
}

/// <summary>
/// Checks if Flow Launcher should ignore any hotkeys
/// </summary>
private static bool ShouldIgnoreHotkeys()
{
return settings.IgnoreHotkeysOnFullscreen && WindowsInteropHelper.IsWindowFullscreen();
}

private static void UpdateLastQUeryMode()
{
switch(settings.LastQueryMode)
{
case LastQueryMode.Empty:
mainViewModel.ChangeQueryText(string.Empty);
break;
case LastQueryMode.Preserved:
mainViewModel.LastQuerySelected = true;
break;
case LastQueryMode.Selected:
mainViewModel.LastQuerySelected = false;
break;
default:
throw new ArgumentException($"wrong LastQueryMode: <{settings.LastQueryMode}>");

}
}

internal static void LoadCustomPluginHotkey()
{
if (settings.CustomPluginHotkeys == null)
return;

foreach (CustomPluginHotkey hotkey in settings.CustomPluginHotkeys)
{
SetCustomQueryHotkey(hotkey);
}
}

internal static void SetCustomQueryHotkey(CustomPluginHotkey hotkey)
{
SetHotkey(hotkey.Hotkey, (s, e) =>
{
if (ShouldIgnoreHotkeys())
return;

mainViewModel.MainWindowVisibility = Visibility.Visible;
mainViewModel.ChangeQueryText(hotkey.ActionKeyword);
});
}

internal static bool CheckAvailability(HotkeyModel currentHotkey)
{
try
{
HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", currentHotkey.CharKey, currentHotkey.ModifierKeys, (sender, e) => { });

return true;
}
catch
{
}
Comment on lines +125 to +127
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove the catch

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cant remove it, catch is needed to go to return false at the bottom, you can just use catch but you duplicate code for no benefit, so may be just leave it?

finally
{
HotkeyManager.Current.Remove("HotkeyAvailabilityTest");
}

return false;
}
}
}
2 changes: 1 addition & 1 deletion Flow.Launcher/HotkeyControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" />
<ColumnDefinition Width="120" />
<ColumnDefinition Width="125" />
</Grid.ColumnDefinitions>
<TextBox x:Name="tbHotkey" TabIndex="100" VerticalContentAlignment="Center" Grid.Column="0"
PreviewKeyDown="TbHotkey_OnPreviewKeyDown" input:InputMethod.IsInputMethodEnabled="False"/>
Expand Down
27 changes: 3 additions & 24 deletions Flow.Launcher/HotkeyControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using NHotkey.Wpf;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Plugin;

Expand All @@ -18,11 +18,7 @@ public partial class HotkeyControl : UserControl

public event EventHandler HotkeyChanged;

protected virtual void OnHotkeyChanged()
{
EventHandler handler = HotkeyChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
protected virtual void OnHotkeyChanged() => HotkeyChanged?.Invoke(this, EventArgs.Empty);

public HotkeyControl()
{
Expand Down Expand Up @@ -90,24 +86,7 @@ public void SetHotkey(string keyStr, bool triggerValidate = true)
SetHotkey(new HotkeyModel(keyStr), triggerValidate);
}

private bool CheckHotkeyAvailability()
{
try
{
HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", CurrentHotkey.CharKey, CurrentHotkey.ModifierKeys, (sender, e) => { });

return true;
}
catch
{
}
finally
{
HotkeyManager.Current.Remove("HotkeyAvailabilityTest");
}

return false;
}
private bool CheckHotkeyAvailability() => HotKeyMapper.CheckAvailability(CurrentHotkey);

public new bool IsFocused
{
Expand Down
4 changes: 3 additions & 1 deletion Flow.Launcher/Languages/en.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<system:String x:Key="newActionKeyword">New action keyword:</system:String>
<system:String x:Key="currentPriority">Current Priority:</system:String>
<system:String x:Key="newPriority">New Priority:</system:String>
<system:String x:Key="priority">Priority:</system:String>
<system:String x:Key="pluginDirectory">Plugin Directory</system:String>
<system:String x:Key="author">Author</system:String>
<system:String x:Key="plugin_init_time">Init time:</system:String>
Expand All @@ -73,6 +74,7 @@
<system:String x:Key="openResultModifiers">Open Result Modifiers</system:String>
<system:String x:Key="showOpenResultHotkey">Show Hotkey</system:String>
<system:String x:Key="customQueryHotkey">Custom Query Hotkey</system:String>
<system:String x:Key="customQuery">Query</system:String>
<system:String x:Key="delete">Delete</system:String>
<system:String x:Key="edit">Edit</system:String>
<system:String x:Key="add">Add</system:String>
Expand Down Expand Up @@ -137,7 +139,7 @@
<system:String x:Key="update">Update</system:String>

<!--Hotkey Control-->
<system:String x:Key="hotkeyUnavailable">Hotkey unavailable</system:String>
<system:String x:Key="hotkeyUnavailable">Hotkey Unavailable</system:String>

<!--Crash Reporter-->
<system:String x:Key="reportWindow_version">Version</system:String>
Expand Down
4 changes: 2 additions & 2 deletions Flow.Launcher/SettingWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
Text="{Binding PluginPair.Metadata.Description}"
Grid.Row="1" Opacity="0.5" Grid.Column="2" />
<DockPanel Grid.ColumnSpan="2" Grid.Row="2" Margin="0 10 0 8" HorizontalAlignment="Right">
<TextBlock Text="Priority" Margin="15,0,0,0" MaxWidth="100"/>
<TextBlock Text="{DynamicResource priority}" Margin="15,0,0,0" MaxWidth="100"/>
<TextBlock Text="{Binding Priority}"
ToolTip="Change Plugin Results Priority"
Margin="5 0 0 0" Cursor="Hand" Foreground="Blue"
Expand Down Expand Up @@ -365,7 +365,7 @@
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="{DynamicResource actionKeywords}" Width="546">
<GridViewColumn Header="{DynamicResource customQuery}" Width="546">
<GridViewColumn.CellTemplate>
<DataTemplate DataType="userSettings:CustomPluginHotkey">
<TextBlock Text="{Binding ActionKeyword}" />
Expand Down
44 changes: 5 additions & 39 deletions Flow.Launcher/SettingWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
using System.Windows.Interop;
using System.Windows.Navigation;
using Microsoft.Win32;
using NHotkey;
using NHotkey.Wpf;
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedCommands;
using Flow.Launcher.ViewModel;
using Flow.Launcher.Helper;

namespace Flow.Launcher
{
Expand Down Expand Up @@ -127,42 +125,10 @@ void OnHotkeyChanged(object sender, EventArgs e)
{
if (HotkeyControl.CurrentHotkeyAvailable)
{
SetHotkey(HotkeyControl.CurrentHotkey, (o, args) =>
{
if (!Application.Current.MainWindow.IsVisible)
{
Application.Current.MainWindow.Visibility = Visibility.Visible;
}
else
{
Application.Current.MainWindow.Visibility = Visibility.Hidden;
}
});
RemoveHotkey(settings.Hotkey);
settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
}
}

void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
{
string hotkeyStr = hotkey.ToString();
try
{
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
}
catch (Exception)
{
string errorMsg =
string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
MessageBox.Show(errorMsg);
}
}

void RemoveHotkey(string hotkeyStr)
{
if (!string.IsNullOrEmpty(hotkeyStr))
{
HotkeyManager.Current.Remove(hotkeyStr);
HotKeyMapper.SetHotkey(HotkeyControl.CurrentHotkey, HotKeyMapper.OnHotkey);
HotKeyMapper.RemoveHotkey(settings.Hotkey);
settings.Hotkey = HotkeyControl.CurrentHotkey.ToString();
}
}

Expand All @@ -183,7 +149,7 @@ private void OnDeleteCustomHotkeyClick(object sender, RoutedEventArgs e)
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
settings.CustomPluginHotkeys.Remove(item);
RemoveHotkey(item.Hotkey);
HotKeyMapper.RemoveHotkey(item.Hotkey);
}
}

Expand Down
Loading