-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
250 lines (218 loc) · 8.18 KB
/
MainWindow.xaml.cs
File metadata and controls
250 lines (218 loc) · 8.18 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
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
namespace stack;
public partial class MainWindow : Window
{
private NoteData _note;
private bool _isDiscarding = false;
private bool _isPushing = false;
public MainWindow(NoteData? note = null)
{
InitializeComponent();
_note = note ?? new NoteData();
if (note != null)
{
this.Left = note.X;
this.Top = note.Y;
this.Width = note.Width > 0 ? note.Width : 400;
this.Height = note.Height > 0 ? note.Height : 400;
Editor.Text = note.Text;
}
// Setup Event Handlers
this.Loaded += (s, e) => Editor.Focus();
this.Closing += MainWindow_Closing;
this.LocationChanged += (s, e) => SaveState();
this.SizeChanged += (s, e) => SaveState();
Editor.LostFocus += (s, e) => SaveState();
Editor.TextChanged += (s, e) => { SaveState(); UpdateTitle(); };
// Shift+Scroll for horizontal scrolling
Editor.PreviewMouseWheel += (s, e) =>
{
if (Keyboard.Modifiers == ModifierKeys.Shift)
{
var scrollViewer = FindScrollViewer(Editor);
if (scrollViewer != null)
{
scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset - e.Delta);
e.Handled = true;
}
}
};
// Setup Key Bindings
SetupHotkeys();
// Set initial title
UpdateTitle();
}
private void UpdateTitle()
{
string preview = Editor.Text.Replace("\r", "").Replace("\n", " ").Trim();
if (preview.Length > 10) preview = preview.Substring(0, 10);
this.Title = string.IsNullOrEmpty(preview) ? "Stack" : $"{preview} - Stack";
}
private static System.Windows.Controls.ScrollViewer? FindScrollViewer(System.Windows.DependencyObject parent)
{
for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = System.Windows.Media.VisualTreeHelper.GetChild(parent, i);
if (child is System.Windows.Controls.ScrollViewer sv) return sv;
var result = FindScrollViewer(child);
if (result != null) return result;
}
return null;
}
private void SetupHotkeys()
{
// Add InputBindings
InputBindings.Add(new KeyBinding(new RelayCommand(New_Click), Key.N, ModifierKeys.Control));
InputBindings.Add(new KeyBinding(new RelayCommand(Push_Click), Key.W, ModifierKeys.Control));
InputBindings.Add(new KeyBinding(new RelayCommand(Pop_Click), Key.R, ModifierKeys.Control));
InputBindings.Add(new KeyBinding(new RelayCommand(Discard_Click), Key.Q, ModifierKeys.Control));
InputBindings.Add(new KeyBinding(new RelayCommand(Wrap_Click), Key.J, ModifierKeys.Control));
InputBindings.Add(new KeyBinding(new RelayCommand(Minimize_Click), Key.H, ModifierKeys.Control));
InputBindings.Add(new KeyBinding(new RelayCommand(Maximize_Click), Key.M, ModifierKeys.Control));
// Alt key toggles menu visibility
this.PreviewKeyDown += (s, e) =>
{
if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt)
{
if (MainMenu.Visibility == Visibility.Collapsed)
MainMenu.Visibility = Visibility.Visible;
else
MainMenu.Visibility = Visibility.Collapsed;
}
if (e.Key == Key.Escape)
{
MainMenu.Visibility = Visibility.Collapsed;
Editor.Focus();
}
};
}
private void SaveState()
{
if (_isDiscarding || _note.IsStashed) return;
_note.Text = Editor.Text;
if (this.WindowState == WindowState.Normal)
{
_note.X = this.Left;
_note.Y = this.Top;
_note.Width = this.Width;
_note.Height = this.Height;
}
SessionManager.Update(_note);
}
private void MainWindow_Closing(object? sender, CancelEventArgs e)
{
if (_isDiscarding) return;
SaveState();
if (!_isPushing)
{
// Normal close (X button, Alt+F4): Push to stash
SessionManager.Push(_note);
}
}
private void New_Click(object? sender, RoutedEventArgs? e) => New_Click();
private void New_Click()
{
new MainWindow(null).Show();
}
private void Push_Click(object? sender, RoutedEventArgs? e) => Push_Click();
private void Push_Click()
{
_isPushing = true;
SaveState();
SessionManager.Push(_note);
_isDiscarding = true; // Prevent double-save in Closing handler
this.Close();
}
private void Pop_Click(object? sender, RoutedEventArgs? e) => Pop_Click();
private void Pop_Click()
{
var poppedNote = SessionManager.Pop();
if (poppedNote != null)
{
var window = new MainWindow(poppedNote);
window.Show();
}
}
private void Discard_Click(object? sender, RoutedEventArgs? e) => Discard_Click();
private void Discard_Click()
{
_isDiscarding = true;
SessionManager.Discard(_note.Id);
this.Close();
}
private void Wrap_Click(object? sender, RoutedEventArgs? e) => Wrap_Click();
private void Wrap_Click()
{
Editor.TextWrapping = Editor.TextWrapping == TextWrapping.Wrap ? TextWrapping.NoWrap : TextWrapping.Wrap;
}
private void Minimize_Click(object? sender, RoutedEventArgs? e) => Minimize_Click();
private void Minimize_Click()
{
this.WindowState = WindowState.Minimized;
}
private void Maximize_Click(object? sender, RoutedEventArgs? e) => Maximize_Click();
private void Maximize_Click()
{
this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
}
private void MenuList_SubmenuOpened(object sender, RoutedEventArgs e)
{
MenuList.Items.Clear();
var notes = System.Linq.Enumerable.ToList(System.Linq.Enumerable.OrderByDescending(System.Linq.Enumerable.Where(SessionManager.Load(), n => n.IsStashed), n => n.LastAccessed));
if (notes.Count == 0)
{
MenuList.Items.Add(new System.Windows.Controls.MenuItem { Header = "Empty", IsEnabled = false });
return;
}
for (int i = 0; i < notes.Count; i++)
{
var note = notes[i];
string preview = note.Text.Replace("\r", "").Replace("\n", " ");
if (preview.Length > 10) preview = preview.Substring(0, 10);
if (string.IsNullOrWhiteSpace(preview)) preview = "(Empty)";
var item = new System.Windows.Controls.MenuItem
{
Header = $"{i + 1} - {preview}"
};
item.Click += (s, args) =>
{
var popped = SessionManager.PopSpecific(note.Id);
if (popped != null)
{
new MainWindow(popped).Show();
}
};
MenuList.Items.Add(item);
}
}
private void Readme_Click(object? sender, RoutedEventArgs? e)
{
string helpText = "Help not found.";
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
using var stream = assembly.GetManifestResourceStream("stack.README.md");
if (stream != null)
{
using var reader = new System.IO.StreamReader(stream);
helpText = reader.ReadToEnd();
}
var readmeNote = new NoteData
{
Text = helpText,
Width = 400,
Height = 400
};
new MainWindow(readmeNote).Show();
}
}
// Simple RelayCommand for KeyBindings
public class RelayCommand : ICommand
{
private readonly Action _execute;
public RelayCommand(Action execute) => _execute = execute;
public event EventHandler? CanExecuteChanged { add { } remove { } }
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter) => _execute();
}