44using System . Threading . Tasks ;
55using System . Windows ;
66using System . Windows . Input ;
7+ using CommunityToolkit . Mvvm . DependencyInjection ;
78using Flow . Launcher . Core . Resource ;
89using Flow . Launcher . Helper ;
910using Flow . Launcher . Infrastructure . Hotkey ;
11+ using Flow . Launcher . Infrastructure . UserSettings ;
1012
1113namespace Flow . Launcher
1214{
1315 public partial class HotkeyControl
1416 {
15- public IHotkeySettings HotkeySettings {
16- get { return ( IHotkeySettings ) GetValue ( HotkeySettingsProperty ) ; }
17- set { SetValue ( HotkeySettingsProperty , value ) ; }
18- }
19-
20- public static readonly DependencyProperty HotkeySettingsProperty = DependencyProperty . Register (
21- nameof ( HotkeySettings ) ,
22- typeof ( IHotkeySettings ) ,
23- typeof ( HotkeyControl ) ,
24- new PropertyMetadata ( )
25- ) ;
2617 public string WindowTitle {
2718 get { return ( string ) GetValue ( WindowTitleProperty ) ; }
2819 set { SetValue ( WindowTitleProperty , value ) ; }
@@ -71,8 +62,7 @@ private static void OnHotkeyChanged(DependencyObject d, DependencyPropertyChange
7162 return ;
7263 }
7364
74- hotkeyControl . SetKeysToDisplay ( new HotkeyModel ( hotkeyControl . Hotkey ) ) ;
75- hotkeyControl . CurrentHotkey = new HotkeyModel ( hotkeyControl . Hotkey ) ;
65+ hotkeyControl . RefreshHotkeyInterface ( hotkeyControl . Hotkey ) ;
7666 }
7767
7868
@@ -90,25 +80,132 @@ public ICommand? ChangeHotkey
9080 }
9181
9282
93- public static readonly DependencyProperty HotkeyProperty = DependencyProperty . Register (
94- nameof ( Hotkey ) ,
95- typeof ( string ) ,
83+ public static readonly DependencyProperty TypeProperty = DependencyProperty . Register (
84+ nameof ( Type ) ,
85+ typeof ( HotkeyType ) ,
9686 typeof ( HotkeyControl ) ,
97- new FrameworkPropertyMetadata ( "" , FrameworkPropertyMetadataOptions . BindsTwoWayByDefault , OnHotkeyChanged )
87+ new FrameworkPropertyMetadata ( HotkeyType . Hotkey , FrameworkPropertyMetadataOptions . BindsTwoWayByDefault , OnHotkeyChanged )
9888 ) ;
9989
90+ public HotkeyType Type
91+ {
92+ get { return ( HotkeyType ) GetValue ( TypeProperty ) ; }
93+ set { SetValue ( TypeProperty , value ) ; }
94+ }
95+
96+ public enum HotkeyType
97+ {
98+ Hotkey ,
99+ PreviewHotkey ,
100+ OpenContextMenuHotkey ,
101+ SettingWindowHotkey ,
102+ CycleHistoryUpHotkey ,
103+ CycleHistoryDownHotkey ,
104+ SelectPrevPageHotkey ,
105+ SelectNextPageHotkey ,
106+ AutoCompleteHotkey ,
107+ AutoCompleteHotkey2 ,
108+ SelectPrevItemHotkey ,
109+ SelectPrevItemHotkey2 ,
110+ SelectNextItemHotkey ,
111+ SelectNextItemHotkey2
112+ }
113+
114+ // We can initialize settings in static field because it has been constructed in App constuctor
115+ // and it will not construct settings instances twice
116+ private static readonly Settings _settings = Ioc . Default . GetRequiredService < Settings > ( ) ;
117+
100118 public string Hotkey
101119 {
102- get { return ( string ) GetValue ( HotkeyProperty ) ; }
103- set { SetValue ( HotkeyProperty , value ) ; }
120+ get
121+ {
122+ return Type switch
123+ {
124+ HotkeyType . Hotkey => _settings . Hotkey ,
125+ HotkeyType . PreviewHotkey => _settings . PreviewHotkey ,
126+ HotkeyType . OpenContextMenuHotkey => _settings . OpenContextMenuHotkey ,
127+ HotkeyType . SettingWindowHotkey => _settings . SettingWindowHotkey ,
128+ HotkeyType . CycleHistoryUpHotkey => _settings . CycleHistoryUpHotkey ,
129+ HotkeyType . CycleHistoryDownHotkey => _settings . CycleHistoryDownHotkey ,
130+ HotkeyType . SelectPrevPageHotkey => _settings . SelectPrevPageHotkey ,
131+ HotkeyType . SelectNextPageHotkey => _settings . SelectNextPageHotkey ,
132+ HotkeyType . AutoCompleteHotkey => _settings . AutoCompleteHotkey ,
133+ HotkeyType . AutoCompleteHotkey2 => _settings . AutoCompleteHotkey2 ,
134+ HotkeyType . SelectPrevItemHotkey => _settings . SelectPrevItemHotkey ,
135+ HotkeyType . SelectPrevItemHotkey2 => _settings . SelectPrevItemHotkey2 ,
136+ HotkeyType . SelectNextItemHotkey => _settings . SelectNextItemHotkey ,
137+ HotkeyType . SelectNextItemHotkey2 => _settings . SelectNextItemHotkey2 ,
138+ _ => string . Empty
139+ } ;
140+ }
141+ set
142+ {
143+ switch ( Type )
144+ {
145+ case HotkeyType . Hotkey :
146+ _settings . Hotkey = value ;
147+ break ;
148+ case HotkeyType . PreviewHotkey :
149+ _settings . PreviewHotkey = value ;
150+ break ;
151+ case HotkeyType . OpenContextMenuHotkey :
152+ _settings . OpenContextMenuHotkey = value ;
153+ break ;
154+ case HotkeyType . SettingWindowHotkey :
155+ _settings . SettingWindowHotkey = value ;
156+ break ;
157+ case HotkeyType . CycleHistoryUpHotkey :
158+ _settings . CycleHistoryUpHotkey = value ;
159+ break ;
160+ case HotkeyType . CycleHistoryDownHotkey :
161+ _settings . CycleHistoryDownHotkey = value ;
162+ break ;
163+ case HotkeyType . SelectPrevPageHotkey :
164+ _settings . SelectPrevPageHotkey = value ;
165+ break ;
166+ case HotkeyType . SelectNextPageHotkey :
167+ _settings . SelectNextPageHotkey = value ;
168+ break ;
169+ case HotkeyType . AutoCompleteHotkey :
170+ _settings . AutoCompleteHotkey = value ;
171+ break ;
172+ case HotkeyType . AutoCompleteHotkey2 :
173+ _settings . AutoCompleteHotkey2 = value ;
174+ break ;
175+ case HotkeyType . SelectPrevItemHotkey :
176+ _settings . SelectPrevItemHotkey = value ;
177+ break ;
178+ case HotkeyType . SelectNextItemHotkey :
179+ _settings . SelectNextItemHotkey = value ;
180+ break ;
181+ case HotkeyType . SelectPrevItemHotkey2 :
182+ _settings . SelectPrevItemHotkey2 = value ;
183+ break ;
184+ case HotkeyType . SelectNextItemHotkey2 :
185+ _settings . SelectNextItemHotkey2 = value ;
186+ break ;
187+ default :
188+ return ;
189+ }
190+
191+ // After setting the hotkey, we need to refresh the interface
192+ RefreshHotkeyInterface ( Hotkey ) ;
193+ }
104194 }
105195
106196 public HotkeyControl ( )
107197 {
108198 InitializeComponent ( ) ;
109199
110200 HotkeyList . ItemsSource = KeysToDisplay ;
111- SetKeysToDisplay ( CurrentHotkey ) ;
201+
202+ RefreshHotkeyInterface ( Hotkey ) ;
203+ }
204+
205+ private void RefreshHotkeyInterface ( string hotkey )
206+ {
207+ SetKeysToDisplay ( new HotkeyModel ( Hotkey ) ) ;
208+ CurrentHotkey = new HotkeyModel ( Hotkey ) ;
112209 }
113210
114211 private static bool CheckHotkeyAvailability ( HotkeyModel hotkey , bool validateKeyGesture ) =>
@@ -133,7 +230,7 @@ private async Task OpenHotkeyDialog()
133230 HotKeyMapper . RemoveHotkey ( Hotkey ) ;
134231 }
135232
136- var dialog = new HotkeyControlDialog ( Hotkey , DefaultHotkey , HotkeySettings , WindowTitle ) ;
233+ var dialog = new HotkeyControlDialog ( Hotkey , DefaultHotkey , WindowTitle ) ;
137234 await dialog . ShowAsync ( ) ;
138235 switch ( dialog . ResultType )
139236 {
0 commit comments