-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
458 lines (396 loc) · 16.4 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
458 lines (396 loc) · 16.4 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using GlassNotes.Helpers;
using GlassNotes.ViewModels;
namespace GlassNotes;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const int HOTKEY_ID = 9000;
private MainViewModel ViewModel => (MainViewModel)DataContext;
public MainWindow()
{
DataContext = new MainViewModel();
InitializeComponent();
// Load saved window position and size
LoadWindowState();
Loaded += MainWindow_Loaded;
Closing += MainWindow_Closing;
}
private void MainWindow_SourceInitialized(object? sender, EventArgs e)
{
// Enable full edge/corner resizing for transparent windows via WM_NCHITTEST hook
ResizeHelper.Attach(this);
// Apply Windows 10/11 dark mode title bar
try
{
// Dark mode is handled by Windows 11 Fluent Design automatically now.
// WindowBlurHelper was removed as it is no longer used.
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error applying dark mode: {ex.Message}");
}
// Register global hotkey Ctrl + Shift + Z
if (PresentationSource.FromVisual(this) is HwndSource source)
{
source.AddHook(HwndHook);
GlobalHotkeyHelper.RegisterHotKey(source, HOTKEY_ID, ModifierKeys.Control | ModifierKeys.Shift, Key.Z);
}
}
private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == GlobalHotkeyHelper.WM_HOTKEY && wParam.ToInt32() == HOTKEY_ID)
{
ViewModel.ToggleAllNotesVisibilityCommand.Execute(null);
handled = true;
}
return IntPtr.Zero;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// Apply theme
ThemeHelper.ApplyTheme(ViewModel.Settings.Theme);
// Set always on top
SetCurrentValue(TopmostProperty, ViewModel.Settings.AlwaysOnTop);
// Set initial opacity handled in InitializeComboBoxes -> ApplyBackgroundColor
// Initialize ComboBoxes
InitializeComboBoxes();
}
private void InitializeComboBoxes()
{
// Apply initial colors
ApplyBackgroundColor(ViewModel.Settings.BackgroundColor);
ApplyTextColor(ViewModel.Settings.TextColor);
}
private void MainWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
{
// Save window state
SaveWindowState();
// Cleanup ViewModel
ViewModel.Cleanup();
// Unregister global hotkey
if (PresentationSource.FromVisual(this) is HwndSource source)
{
GlobalHotkeyHelper.UnregisterHotKey(source, HOTKEY_ID);
source.RemoveHook(HwndHook);
}
}
private void LoadWindowState()
{
const double minW = 480, minH = 360;
if (ViewModel.Settings.WindowWidth >= minW && ViewModel.Settings.WindowHeight >= minH)
{
SetCurrentValue(WidthProperty, ViewModel.Settings.WindowWidth);
SetCurrentValue(HeightProperty, ViewModel.Settings.WindowHeight);
}
else
{
SetCurrentValue(WidthProperty, 680.0);
SetCurrentValue(HeightProperty, 520.0);
}
// Restore position only if it's plausibly on-screen
if (ViewModel.Settings.WindowLeft >= 0 && ViewModel.Settings.WindowTop >= 0)
{
SetCurrentValue(LeftProperty, ViewModel.Settings.WindowLeft);
SetCurrentValue(TopProperty, ViewModel.Settings.WindowTop);
}
}
private void SaveWindowState()
{
ViewModel.Settings.WindowWidth = Width;
ViewModel.Settings.WindowHeight = Height;
ViewModel.Settings.WindowLeft = Left;
ViewModel.Settings.WindowTop = Top;
ViewModel.SaveSettings();
}
private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
// Double-click to maximize/restore (optional)
SetCurrentValue(WindowStateProperty, WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized);
}
else
{
// Drag to move window
DragMove();
}
}
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
SetCurrentValue(WindowStateProperty, WindowState.Minimized);
}
private void MaximizeButton_Click(object sender, RoutedEventArgs e)
{
SetCurrentValue(WindowStateProperty, WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized);
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
protected override void OnStateChanged(EventArgs e)
{
base.OnStateChanged(e);
if (MaximizeButton != null)
{
MaximizeButton.SetCurrentValue(ContentProperty, WindowState == WindowState.Maximized ? "❐" : "☐");
MaximizeButton.SetCurrentValue(ToolTipProperty, WindowState == WindowState.Maximized ? "Restore" : "Maximize");
}
}
// ── Settings Overlay ─────────────────────────────────────────────────────
private string _soActiveTab = "General";
private bool _soInitialized = false;
private void SettingsButton_Click(object sender, RoutedEventArgs e)
{
if (SettingsOverlay.Visibility == Visibility.Visible)
{
SettingsOverlay.SetCurrentValue(VisibilityProperty, Visibility.Collapsed);
}
else
{
if (!_soInitialized)
{
SO_LoadSettings();
SO_AttachEventHandlers();
_soInitialized = true;
}
SettingsOverlay.SetCurrentValue(VisibilityProperty, Visibility.Visible);
SO_SelectTab("General");
}
}
private void SettingsHeader_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Dragging the settings header moves the MAIN window
if (e.ButtonState == MouseButtonState.Pressed)
DragMove();
}
private void SettingsCloseButton_Click(object sender, RoutedEventArgs e)
{
SettingsOverlay.SetCurrentValue(VisibilityProperty, Visibility.Collapsed);
}
private void SOTab_Click(object sender, RoutedEventArgs e)
{
if (sender is Button btn && btn.Tag is string tabName)
SO_SelectTab(tabName);
}
private void SO_SelectTab(string tabName)
{
_soActiveTab = tabName;
SO_PanelGeneral.SetCurrentValue(VisibilityProperty, tabName == "General" ? Visibility.Visible : Visibility.Collapsed);
SO_PanelColors.SetCurrentValue(VisibilityProperty, tabName == "Colors" ? Visibility.Visible : Visibility.Collapsed);
SO_PanelFont.SetCurrentValue(VisibilityProperty, tabName == "Font" ? Visibility.Visible : Visibility.Collapsed);
SO_PanelAbout.SetCurrentValue(VisibilityProperty, tabName == "About" ? Visibility.Visible : Visibility.Collapsed);
var gray = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(0x88, 0x88, 0x88));
var black = System.Windows.Media.Brushes.Black;
SO_TabGeneral.SetCurrentValue(ForegroundProperty, tabName == "General" ? black : gray);
SO_TabColors.SetCurrentValue(ForegroundProperty, tabName == "Colors" ? black : gray);
SO_TabFont.SetCurrentValue(ForegroundProperty, tabName == "Font" ? black : gray);
SO_TabAbout.SetCurrentValue(ForegroundProperty, tabName == "About" ? black : gray);
// Move underline
Button? targetButton = tabName switch
{
"General" => SO_TabGeneral,
"Colors" => SO_TabColors,
"Font" => SO_TabFont,
"About" => SO_TabAbout,
_ => null
};
if (targetButton != null)
{
// Ensure layout is up to date to get accurate position
if (SettingsOverlay.Visibility == Visibility.Visible)
{
SettingsOverlay.UpdateLayout();
}
// Calculate position relative to the first tab (General) to determine left offset
// The tabs are in a StackPanel, so we can just use TranslatePoint relative to the first tab
// Offset of General is 0.
try
{
// We want the position relative to the container (StackPanel)
// The underline is in a generic Grid below, which shares the same left alignment as the StackPanel
// So the X position of the button within the StackPanel is the Left Margin for the underline.
// Fallback for initial layout if needed
if (targetButton.ActualWidth == 0 && tabName == "General")
{
SO_TabUnderline.SetCurrentValue(MarginProperty, new Thickness(0, 0, 0, 0));
SO_TabUnderline.SetCurrentValue(WidthProperty, 60.0); // Default estimate
}
else
{
// Get parent stackpanel
var parent = System.Windows.Media.VisualTreeHelper.GetParent(targetButton) as UIElement;
if (parent != null)
{
var offset = targetButton.TranslatePoint(new Point(0, 0), parent);
SO_TabUnderline.SetCurrentValue(MarginProperty, new Thickness(offset.X, 0, 0, 0));
SO_TabUnderline.SetCurrentValue(WidthProperty, targetButton.ActualWidth > 0 ? targetButton.ActualWidth : 60.0);
}
}
}
catch
{
// Fallback if visual tree is not ready
}
}
}
private void SO_LoadSettings()
{
SO_TransparencySlider.SetCurrentValue(System.Windows.Controls.Primitives.RangeBase.ValueProperty, ViewModel.Settings.Opacity);
SO_TransparencyLabel.SetCurrentValue(TextBlock.TextProperty, $"{(int)(ViewModel.Settings.Opacity * 100)}%");
SO_AlwaysOnTopCheckBox.SetCurrentValue(System.Windows.Controls.Primitives.ToggleButton.IsCheckedProperty, ViewModel.Settings.AlwaysOnTop);
SO_FontSizeSlider.SetCurrentValue(System.Windows.Controls.Primitives.RangeBase.ValueProperty, ViewModel.Settings.FontSize);
SO_FontSizeLabel.SetCurrentValue(TextBlock.TextProperty, $"{(int)ViewModel.Settings.FontSize}px");
SelectComboByTag(SO_BackgroundColorComboBox, ViewModel.Settings.BackgroundColor);
SelectComboByTag(SO_TextColorComboBox, ViewModel.Settings.TextColor);
SelectComboByTag(SO_FontFamilyComboBox, ViewModel.Settings.FontFamily);
}
private static void SelectComboByTag(ComboBox box, string? value)
{
foreach (ComboBoxItem item in box.Items)
if (item.Tag?.ToString() == value) { box.SetCurrentValue(System.Windows.Controls.Primitives.Selector.SelectedItemProperty, item); return; }
}
private void SO_AttachEventHandlers()
{
SO_TransparencySlider.ValueChanged += (s, e) =>
{
ViewModel.Settings.Opacity = e.NewValue;
SO_TransparencyLabel.SetCurrentValue(TextBlock.TextProperty, $"{(int)(e.NewValue * 100)}%");
ApplyBackgroundColor(ViewModel.Settings.BackgroundColor);
ViewModel.SaveSettings();
};
SO_AlwaysOnTopCheckBox.Checked += (s, e) => { SetCurrentValue(TopmostProperty, true); ViewModel.Settings.AlwaysOnTop = true; ViewModel.SaveSettings(); };
SO_AlwaysOnTopCheckBox.Unchecked += (s, e) => { SetCurrentValue(TopmostProperty, false); ViewModel.Settings.AlwaysOnTop = false; ViewModel.SaveSettings(); };
SO_FontSizeSlider.ValueChanged += (s, e) =>
{
ViewModel.Settings.FontSize = e.NewValue;
SO_FontSizeLabel.SetCurrentValue(TextBlock.TextProperty, $"{(int)e.NewValue}px");
ViewModel.SaveSettings();
};
SO_BackgroundColorComboBox.SelectionChanged += (s, e) =>
{
if (SO_BackgroundColorComboBox.SelectedItem is ComboBoxItem item)
{
var color = item.Tag?.ToString() ?? "White";
ViewModel.Settings.BackgroundColor = color;
ApplyBackgroundColor(color);
ViewModel.SaveSettings();
}
};
SO_TextColorComboBox.SelectionChanged += (s, e) =>
{
if (SO_TextColorComboBox.SelectedItem is ComboBoxItem item)
{
var color = item.Tag?.ToString() ?? "Black";
ViewModel.Settings.TextColor = color;
ApplyTextColor(color);
ViewModel.SaveSettings();
}
};
SO_FontFamilyComboBox.SelectionChanged += (s, e) =>
{
if (SO_FontFamilyComboBox.SelectedItem is ComboBoxItem item)
{
ViewModel.Settings.FontFamily = item.Tag?.ToString() ?? "Segoe UI";
ViewModel.SaveSettings();
}
};
}
public void UpdateOpacity(double opacity)
{
ViewModel.Settings.Opacity = opacity;
ApplyBackgroundColor(ViewModel.Settings.BackgroundColor);
}
public void UpdateBackgroundColor(string colorName)
{
ApplyBackgroundColor(colorName);
}
public void UpdateTextColor(string colorName)
{
ApplyTextColor(colorName);
}
private void ApplyBackgroundColor(string colorName)
{
var brush = Helpers.ColorHelper.GetBackgroundBrush(colorName);
brush.Opacity = ViewModel.Settings.Opacity;
// Apply to the main content grid
var grid = this.FindName("ContentGrid") as Grid;
if (grid != null)
{
grid.Background = brush;
}
// Apply to sidebar
var notesSidebar = this.FindName("NotesSidebar") as Border;
if (notesSidebar != null)
{
notesSidebar.Background = brush;
}
// Keep text editor transparent
if (NoteTextBox != null)
{
NoteTextBox.SetCurrentValue(Control.BackgroundProperty, System.Windows.Media.Brushes.Transparent);
}
}
private void ApplyTextColor(string colorName)
{
if (NoteTextBox != null)
{
NoteTextBox.SetCurrentValue(Control.ForegroundProperty, Helpers.ColorHelper.GetTextBrush(colorName));
}
}
// Drag and drop support
private Models.Note? _draggedNote;
private void NotesListBox_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed && NotesListBox.SelectedItem is Models.Note note)
{
_draggedNote = note;
DragDrop.DoDragDrop(NotesListBox, note, DragDropEffects.Move);
}
}
private void NotesListBox_Drop(object sender, DragEventArgs e)
{
if (_draggedNote != null && e.Data.GetDataPresent(typeof(Models.Note)))
{
var targetItem = GetNoteFromPoint(e.GetPosition(NotesListBox));
if (targetItem != null && targetItem != _draggedNote)
{
int oldIndex = ViewModel.Notes.IndexOf(_draggedNote);
int newIndex = ViewModel.Notes.IndexOf(targetItem);
if (oldIndex != -1 && newIndex != -1)
{
ViewModel.Notes.Move(oldIndex, newIndex);
// Save custom order
ViewModel.Settings.CustomNoteOrder = ViewModel.Notes.Select(n => n.Id).ToList();
ViewModel.SaveSettings();
}
}
_draggedNote = null;
}
}
private Models.Note? GetNoteFromPoint(System.Windows.Point point)
{
var element = NotesListBox.InputHitTest(point) as DependencyObject;
while (element != null)
{
if (element is ListBoxItem item)
{
return item.DataContext as Models.Note;
}
element = System.Windows.Media.VisualTreeHelper.GetParent(element);
}
return null;
}
private void NotesListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (NotesListBox.SelectedItem is Models.Note note)
{
ViewModel.OpenNoteInNewWindowCommand.Execute(note);
}
}
}