-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainView.xaml.cs
259 lines (216 loc) · 8.8 KB
/
MainView.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using Lanceur.Converters.Reactive;
using Lanceur.Core.Services;
using Lanceur.SharedKernel.Mixins;
using Lanceur.Utils;
using NHotkey;
using NHotkey.Wpf;
using ReactiveUI;
using Splat;
using System;
using System.Linq;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
namespace Lanceur.Views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainView : IViewFor<MainViewModel>
{
#region Fields
private readonly IAppSettingsService _settings;
private bool closeStoryBoardCompleted = true;
private bool openStoryBoardCompleted = true;
public readonly ILogService _log = Locator.Current.GetService<ILogService>();
#endregion Fields
#region Constructors
public MainView() : this(null, null)
{
}
public MainView(ILogService log = null, IAppSettingsService settings = null)
{
InitializeComponent();
_log ??= log;
ViewModel = Locator.Current.GetService<MainViewModel>();
_settings = settings ?? Locator.Current.GetService<IAppSettingsService>();
DataContext = ViewModel;
Loaded += OnWindowLoaded;
KeyDown += OnKeyDownWindow;
PreviewKeyDown += OnPreviewKeyDown;
MouseDown += OnWindowMouseDown;
QueryTextBox.TextChanged += OnTextChanged;
this.WhenActivated(d =>
{
this.Bind(ViewModel, vm => vm.Query, v => v.QueryTextBox.Text).DisposeWith(d);
this.OneWayBind(ViewModel, vm => vm.CurrentAliasSuggestion, v => v.AutoCompleteBox.Text).DisposeWith(d);
this.OneWayBind(ViewModel, vm => vm.CurrentSessionName, v => v.RunSession.Text).DisposeWith(d);
this.OneWayBind(ViewModel, vm => vm.Results, v => v.QueryResults.ItemsSource).DisposeWith(d);
this.OneWayBind(ViewModel, vm => vm.Results.Count, v => v.ResultPanel.Visibility, x => x.ToVisibility()).DisposeWith(d);
this.OneWayBind(ViewModel, vm => vm.Results.Count, v => v.ResultCounter.Text);
this.OneWayBind(ViewModel, vm => vm.Results.Count, v => v.StatusPanel.Visibility, x => x.ToVisibility()).DisposeWith(d);
this.Bind(ViewModel, vm => vm.KeepAlive, v => v.KeepAlive).DisposeWith(d);
//Don't forget 'using System.Windows.Controls'
QueryTextBox
.Events().KeyDown
.Where(x => x.Key == Key.Enter)
.Select(x =>
{
return new MainViewModel.ExecutionContext
{
Query = x.OriginalSource.GetTextFromTextbox(),
RunAsAdmin = Keyboard.Modifiers == ModifierKeys.Control
};
})
.InvokeCommand(ViewModel, vm => vm.ExecuteAlias);
QueryTextBox
.Events().PreviewKeyDown
.Where(x => x.Key == Key.Up)
.Select(x => Unit.Default)
.InvokeCommand(ViewModel, vm => vm.SelectPreviousResult);
QueryTextBox
.Events().PreviewKeyDown
.Where(x => x.Key == Key.Down)
.Select(x => Unit.Default)
.InvokeCommand(ViewModel, vm => vm.SelectNextResult);
QueryTextBox
.Events().PreviewKeyDown
.Where(x => x.Key == Key.Tab)
.Select(x =>
{
x.Handled = true;
return Unit.Default;
})
.InvokeCommand(ViewModel, vm => vm.AutoCompleteQuery);
QueryResults
.Events().PreviewMouseLeftButtonUp
.Select(x => x.OriginalSource.GetQueryFromDataContext())
.InvokeCommand(ViewModel, vm => vm.ExecuteAlias);
});
}
#endregion Constructors
#region Properties
public bool KeepAlive { get; set; }
#endregion Properties
#region Methods
private void OnFadeInStoryBoardCompleted(object sender, EventArgs e)
{
openStoryBoardCompleted = true;
LogService.Current.Trace("Fade in Storyboard completed...");
Visibility = Visibility.Visible;
}
private void OnFadeOutStoryBoardCompleted(object sender, EventArgs e)
{
closeStoryBoardCompleted = true;
LogService.Current.Trace("Fade out Storyboard completed...");
Visibility = Visibility.Collapsed;
}
private void OnKeyDownWindow(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape) { HideControl(force: true); }
}
private async void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Up || e.Key == Key.Down)
{
var collection = QueryResults.ItemsSource.Cast<object>();
if (collection.CanNavigate() == false) { e.Handled = false; return; }
var idx = e.Key == Key.Down
? collection.GetNextIndex(QueryResults.SelectedIndex)
: collection.GetPreviousIndex(QueryResults.SelectedIndex);
var selectedItem = collection.ElementAt(idx);
QueryResults.ScrollIntoView(selectedItem);
}
else if (e.Key == Key.Enter)
{
await Task.Delay(10);
HideControl();
}
e.Handled = false;
}
private void OnShowWindow(object sender, HotkeyEventArgs e)
{
if (ViewModel != null)
{
ViewModel.IsOnError = false;
ShowWindow();
if (e != null) { e.Handled = true; }
}
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
var length = e.Changes.ElementAt(0).AddedLength;
if (sender is TextBox t && length > 1)
{
t.CaretIndex = t.Text.Length;
}
}
private void OnWindowLoaded(object sender, RoutedEventArgs e)
{
//TODO value from settings
var stg = _settings.Load();
var hk = stg.HotKey;
SetShortcut((Key)hk.Key, (ModifierKeys)hk.ModifierKey);
//var coordinate = ScreenRuler.GetCenterCoordinate(ScreenRuler.DefaultTopOffset);
var coordinate = new Coordinate(stg.Window.Position.Left, stg.Window.Position.Top);
ScreenRuler.SetWindowPosition(coordinate);
ShowWindow();
}
private void OnWindowMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left) { DragMove(); }
}
private void SetShortcut(Key key, ModifierKeys modifyer)
{
try
{
HotkeyManager.Current.AddOrReplace("OnShowWindow", key, modifyer, OnShowWindow);
}
catch (HotkeyAlreadyRegisteredException ex)
{
//Default values
_log?.Warning(ex.Message);
SetShortcut(Key.R, ModifierKeys.Shift | ModifierKeys.Windows);
}
}
protected override void OnDeactivated(EventArgs e)
{
_log?.Info("Windows deactivated");
HideControl();
}
public void HideControl(bool force = false)
{
if (KeepAlive && !force) { return; }
else if (closeStoryBoardCompleted)
{
var storyBoard = FindResource("fadeOutStoryBoard") as Storyboard;
storyBoard.Begin(this);
QueryTextBox.Text = string.Empty;
}
}
public void ShowWindow()
{
_log?.Info("Window showing");
ViewModel.Activate.Execute().Subscribe();
if (openStoryBoardCompleted)
{
var storyBoard = FindResource("fadeInStoryBoard") as Storyboard;
storyBoard.Begin(this);
Visibility = Visibility.Visible;
QueryTextBox.Focus();
//https://stackoverflow.com/questions/3109080/focus-on-textbox-when-usercontrol-change-visibility
Dispatcher.BeginInvoke((Action)delegate { Keyboard.Focus(QueryTextBox); });
Activate();
Topmost = true;
Topmost = false;
Focus();
}
}
#endregion Methods
}
}