99using Flow . Launcher . Infrastructure . Hotkey ;
1010using Flow . Launcher . Plugin ;
1111using System . Threading ;
12- using System . Windows . Interop ;
1312
1413namespace Flow . Launcher
1514{
1615 public partial class HotkeyControl : UserControl
1716 {
18- private Brush tbMsgForegroundColorOriginal ;
19-
20- private string tbMsgTextOriginal ;
21-
2217 public HotkeyModel CurrentHotkey { get ; private set ; }
2318 public bool CurrentHotkeyAvailable { get ; private set ; }
2419
@@ -29,8 +24,6 @@ public partial class HotkeyControl : UserControl
2924 public HotkeyControl ( )
3025 {
3126 InitializeComponent ( ) ;
32- tbMsgTextOriginal = tbMsg . Text ;
33- tbMsgForegroundColorOriginal = tbMsg . Foreground ;
3427 }
3528
3629 private CancellationTokenSource hotkeyUpdateSource ;
@@ -55,9 +48,7 @@ private void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e)
5548 specialKeyState . CtrlPressed ,
5649 key ) ;
5750
58- var hotkeyString = hotkeyModel . ToString ( ) ;
59-
60- if ( hotkeyString == tbHotkey . Text )
51+ if ( hotkeyModel . Equals ( CurrentHotkey ) )
6152 {
6253 return ;
6354 }
@@ -72,33 +63,32 @@ private void TbHotkey_OnPreviewKeyDown(object sender, KeyEventArgs e)
7263
7364 public async Task SetHotkeyAsync ( HotkeyModel keyModel , bool triggerValidate = true )
7465 {
75- CurrentHotkey = keyModel ;
76-
77- tbHotkey . Text = CurrentHotkey . ToString ( ) ;
66+ tbHotkey . Text = keyModel . ToString ( ) ;
7867 tbHotkey . Select ( tbHotkey . Text . Length , 0 ) ;
7968
8069 if ( triggerValidate )
8170 {
82- CurrentHotkeyAvailable = CheckHotkeyAvailability ( ) ;
83- if ( ! CurrentHotkeyAvailable )
84- {
85- tbMsg . Foreground = new SolidColorBrush ( Colors . Red ) ;
86- tbMsg . Text = InternationalizationManager . Instance . GetTranslation ( "hotkeyUnavailable" ) ;
87- }
88- else
89- {
90- tbMsg . Foreground = new SolidColorBrush ( Colors . Green ) ;
91- tbMsg . Text = InternationalizationManager . Instance . GetTranslation ( "success" ) ;
92- }
93- tbMsg . Visibility = Visibility . Visible ;
71+ bool hotkeyAvailable = CheckHotkeyAvailability ( keyModel ) ;
72+ CurrentHotkeyAvailable = hotkeyAvailable ;
73+ SetMessage ( hotkeyAvailable ) ;
9474 OnHotkeyChanged ( ) ;
9575
9676 var token = hotkeyUpdateSource . Token ;
9777 await Task . Delay ( 500 , token ) ;
9878 if ( token . IsCancellationRequested )
9979 return ;
100- FocusManager . SetFocusedElement ( FocusManager . GetFocusScope ( this ) , null ) ;
101- Keyboard . ClearFocus ( ) ;
80+
81+ if ( CurrentHotkeyAvailable )
82+ {
83+ CurrentHotkey = keyModel ;
84+ // To trigger LostFocus
85+ FocusManager . SetFocusedElement ( FocusManager . GetFocusScope ( this ) , null ) ;
86+ Keyboard . ClearFocus ( ) ;
87+ }
88+ }
89+ else
90+ {
91+ CurrentHotkey = keyModel ;
10292 }
10393 }
10494
@@ -107,14 +97,40 @@ public Task SetHotkeyAsync(string keyStr, bool triggerValidate = true)
10797 return SetHotkeyAsync ( new HotkeyModel ( keyStr ) , triggerValidate ) ;
10898 }
10999
110- private bool CheckHotkeyAvailability ( ) => HotKeyMapper . CheckAvailability ( CurrentHotkey ) ;
100+ private static bool CheckHotkeyAvailability ( HotkeyModel hotkey ) => hotkey . Validate ( ) && HotKeyMapper . CheckAvailability ( hotkey ) ;
111101
112102 public new bool IsFocused => tbHotkey . IsFocused ;
113103
114104 private void tbHotkey_LostFocus ( object sender , RoutedEventArgs e )
115105 {
116- tbMsg . Text = tbMsgTextOriginal ;
106+ tbHotkey . Text = CurrentHotkey . ToString ( ) ;
107+ tbHotkey . Select ( tbHotkey . Text . Length , 0 ) ;
108+ }
109+
110+ private void tbHotkey_GotFocus ( object sender , RoutedEventArgs e )
111+ {
112+ ResetMessage ( ) ;
113+ }
114+
115+ private void ResetMessage ( )
116+ {
117+ tbMsg . Text = InternationalizationManager . Instance . GetTranslation ( "flowlauncherPressHotkey" ) ;
117118 tbMsg . SetResourceReference ( TextBox . ForegroundProperty , "Color05B" ) ;
118119 }
120+
121+ private void SetMessage ( bool hotkeyAvailable )
122+ {
123+ if ( ! hotkeyAvailable )
124+ {
125+ tbMsg . Foreground = new SolidColorBrush ( Colors . Red ) ;
126+ tbMsg . Text = InternationalizationManager . Instance . GetTranslation ( "hotkeyUnavailable" ) ;
127+ }
128+ else
129+ {
130+ tbMsg . Foreground = new SolidColorBrush ( Colors . Green ) ;
131+ tbMsg . Text = InternationalizationManager . Instance . GetTranslation ( "success" ) ;
132+ }
133+ tbMsg . Visibility = Visibility . Visible ;
134+ }
119135 }
120136}
0 commit comments