-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
267 lines (225 loc) · 8.27 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
267 lines (225 loc) · 8.27 KB
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
260
261
262
263
264
265
266
267
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using WindowManager.Services;
namespace WindowManager;
public partial class MainWindow : Window
{
private sealed class LayoutCard
{
public required Layout Layout { get; init; }
public required string Info { get; init; }
public required string Tooltip { get; init; }
public string Name => Layout.Name;
}
private readonly DispatcherTimer _statusTimer;
private readonly DispatcherTimer _deleteTimer;
private Layout? _pendingDelete;
private Layout? _renameTarget;
private bool _initialized;
public MainWindow()
{
InitializeComponent();
_statusTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(5) };
_statusTimer.Tick += (_, _) => { StatusText.Visibility = Visibility.Collapsed; _statusTimer.Stop(); };
_deleteTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(4) };
_deleteTimer.Tick += (_, _) => { _pendingDelete = null; _deleteTimer.Stop(); };
App.Store.Changed += () => Dispatcher.Invoke(Refresh);
SettingsStore.Changed += () => Dispatcher.Invoke(Refresh);
Refresh();
AutostartBox.IsChecked = Autostart.IsEnabled();
HotkeysBox.IsChecked = SettingsStore.Current.HotkeysEnabled;
LaunchMissingBox.IsChecked = SettingsStore.Current.LaunchMissingApps;
_initialized = true;
}
private void Refresh()
{
bool hotkeys = SettingsStore.Current.HotkeysEnabled;
LayoutsList.ItemsSource = App.Store.Layouts.Select((l, i) =>
{
string info = $"{l.Windows.Count} {WindowService.WindowsWord(l.Windows.Count)}";
if (hotkeys && i < 9) info += $" · Ctrl+Alt+{i + 1}";
var lines = l.Windows.Take(12).Select(w => $"• {w.ProcessName} — {Truncate(w.TitleHint, 58)}");
string tip = L.SavedAtPrefix(l.SavedAt) + "\n" + string.Join("\n", lines);
if (l.Windows.Count > 12) tip += "\n" + L.AndMore(l.Windows.Count - 12);
return new LayoutCard { Layout = l, Info = info, Tooltip = tip };
}).ToList();
EmptyState.Visibility = App.Store.Layouts.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
}
private static string Truncate(string s, int max) =>
s.Length <= max ? s : s[..(max - 1)] + "…";
private void ShowStatus(string message)
{
StatusText.Text = message;
StatusText.Visibility = Visibility.Visible;
_statusTimer.Stop();
_statusTimer.Start();
}
// --- zapis nowego układu / zmiana nazwy ---
private void Save_Click(object sender, RoutedEventArgs e)
{
_renameTarget = null;
NamePanel.Visibility = Visibility.Visible;
NameBox.Text = L.DefaultLayoutName(App.Store.Layouts.Count + 1);
NameBox.SelectAll();
NameBox.Focus();
}
private void StartRename(Layout layout)
{
_renameTarget = layout;
NamePanel.Visibility = Visibility.Visible;
NameBox.Text = layout.Name;
NameBox.SelectAll();
NameBox.Focus();
}
private void NameBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter) ConfirmName();
else if (e.Key == Key.Escape) NamePanel.Visibility = Visibility.Collapsed;
}
private void ConfirmSave_Click(object sender, RoutedEventArgs e) => ConfirmName();
private void ConfirmName()
{
string name = NameBox.Text.Trim();
if (name.Length == 0) return;
if (_renameTarget is { } target)
{
App.Store.Rename(target, name);
_renameTarget = null;
NamePanel.Visibility = Visibility.Collapsed;
ShowStatus(L.RenamedTo(name));
return;
}
var layout = WindowService.Capture(name);
if (layout.Windows.Count == 0)
{
ShowStatus(L.NoWindowsToSave);
return;
}
App.Store.Add(layout);
NamePanel.Visibility = Visibility.Collapsed;
ShowStatus(L.SavedLayout(name, layout.Windows.Count));
}
// --- akcje na kartach ---
private static Layout? LayoutOf(object sender) =>
(sender as FrameworkElement)?.DataContext is LayoutCard card ? card.Layout : null;
private void Card_Click(object sender, MouseButtonEventArgs e)
{
if (LayoutOf(sender) is { } layout) ApplyLayout(layout);
}
private void Apply_Click(object sender, RoutedEventArgs e)
{
if (LayoutOf(sender) is { } layout) ApplyLayout(layout);
}
private async void ApplyLayout(Layout layout)
{
try
{
var result = await WindowService.ApplyAsync(layout, SettingsStore.Current.LaunchMissingApps,
msg => Dispatcher.Invoke(() => ShowStatus(msg)));
ShowStatus(WindowService.Describe(layout, result));
}
catch (Exception ex)
{
Log.Write("Błąd przywracania układu: " + ex);
ShowStatus(L.ApplyFailed);
}
}
private void Overwrite_Click(object sender, RoutedEventArgs e)
{
if (LayoutOf(sender) is { } layout) OverwriteLayout(layout);
}
private void OverwriteLayout(Layout layout)
{
var captured = WindowService.Capture(layout.Name);
if (captured.Windows.Count == 0)
{
ShowStatus(L.NoWindowsToSave);
return;
}
layout.Windows = captured.Windows;
layout.SavedAt = captured.SavedAt;
layout.MonitorSignature = captured.MonitorSignature;
App.Store.Save();
ShowStatus(L.Overwritten(layout.Name));
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
if (LayoutOf(sender) is not { } layout) return;
if (_pendingDelete == layout)
{
_pendingDelete = null;
_deleteTimer.Stop();
App.Store.Remove(layout);
ShowStatus(L.DeletedLayout(layout.Name));
}
else
{
_pendingDelete = layout;
_deleteTimer.Stop();
_deleteTimer.Start();
ShowStatus(L.ClickAgainToDelete(layout.Name));
}
}
// --- menu kontekstowe ---
private void Menu_Apply(object sender, RoutedEventArgs e)
{
if (LayoutOf(sender) is { } layout) ApplyLayout(layout);
}
private void Menu_Rename(object sender, RoutedEventArgs e)
{
if (LayoutOf(sender) is { } layout) StartRename(layout);
}
private void Menu_Overwrite(object sender, RoutedEventArgs e)
{
if (LayoutOf(sender) is { } layout) OverwriteLayout(layout);
}
private void Menu_MoveUp(object sender, RoutedEventArgs e)
{
if (LayoutOf(sender) is { } layout) App.Store.Move(layout, -1);
}
private void Menu_MoveDown(object sender, RoutedEventArgs e)
{
if (LayoutOf(sender) is { } layout) App.Store.Move(layout, +1);
}
private void Menu_Delete(object sender, RoutedEventArgs e)
{
if (LayoutOf(sender) is not { } layout) return;
App.Store.Remove(layout);
ShowStatus(L.DeletedLayout(layout.Name));
}
// --- okno / zasobnik / ustawienia ---
private void Minimize_Click(object sender, RoutedEventArgs e) => WindowState = WindowState.Minimized;
private void HideToTray_Click(object sender, RoutedEventArgs e) => Close();
protected override void OnClosing(CancelEventArgs e)
{
if (!App.IsExiting)
{
e.Cancel = true;
Hide();
((App)System.Windows.Application.Current).NotifyHiddenToTray();
}
base.OnClosing(e);
}
private void Autostart_Changed(object sender, RoutedEventArgs e)
{
if (!_initialized) return;
try
{
Autostart.Set(AutostartBox.IsChecked == true);
}
catch (Exception ex)
{
Log.Write("Błąd autostartu: " + ex.Message);
ShowStatus(L.AutostartFailed);
}
}
private void Settings_Changed(object sender, RoutedEventArgs e)
{
if (!_initialized) return;
SettingsStore.Current.HotkeysEnabled = HotkeysBox.IsChecked == true;
SettingsStore.Current.LaunchMissingApps = LaunchMissingBox.IsChecked == true;
SettingsStore.Save();
}
}