Repository navigation
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
244 lines (190 loc) · 7.71 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
244 lines (190 loc) · 7.71 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
using System;
using System.IO;
using System.Windows;
using System.Windows.Input;
using DevExpress.Mvvm.Native;
using Ookii.Dialogs.Wpf;
using Application = System.Windows.Application;
using Clipboard = System.Windows.Clipboard;
namespace QuickNoteWidget
{
public partial class MainWindow : Window
{
private MainWindowViewModel _mainWindowViewModel;
private const double FONT_MIN_SIZE = 5d;
private const double FONT_MAX_SIZE = 60d;
private const int WINDOW_RESIZE_FACTOR = 15;
private const int MIN_WINDOW_HEIGHT = 100;
private const int MIN_WINDOW_WIDTH = 470;
public MainWindow()
{
InitializeComponent();
DataContext = _mainWindowViewModel = new MainWindowViewModel();
tbxMultiLine.Focus();
_mainWindowViewModel.ResetWindowSizeAction = new Action(ResetWindowSize);
}
#region Context Menu Events
private void contextSave_Click(object sender, RoutedEventArgs e)
{
string content = this.tbxMultiLine.Document.Text;
var dialog = new VistaSaveFileDialog();
dialog.ShowDialog();
string fileName = dialog.FileName;
FileStream stream = File.Create(fileName);
stream.Close();
File.WriteAllText(fileName, content);
}
private void contextClose_Click(object sender, RoutedEventArgs e) => Application.Current.Shutdown();
private void contextMaximize_Click(object sender, RoutedEventArgs e) => WindowState = WindowState.Normal;
private void contextMinimize_Click(object sender, RoutedEventArgs e) => WindowState = WindowState.Minimized;
private void contextAdd_Click(object sender, RoutedEventArgs e)
{
string clipboard = Clipboard.GetText();
this.tbxMultiLine.Text += clipboard;
}
private void contextCopy_Click(object sender, RoutedEventArgs e)
{
if (String.IsNullOrEmpty(this.tbxMultiLine.Text))
return;
if (String.IsNullOrEmpty(this.tbxMultiLine.SelectedText))
Clipboard.SetText(this.tbxMultiLine.Text);
else
Clipboard.SetText(this.tbxMultiLine.SelectedText);
}
private void contextSelect_Click(object sender, RoutedEventArgs e)
{
if (String.IsNullOrEmpty(this.tbxMultiLine.Text))
return;
this.tbxMultiLine.SelectAll();
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
DragMove();
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
_mainWindowViewModel.SaveSettingsOnClose();
}
private void Info_Click(object sender, RoutedEventArgs e)
{
var _infoWindow = new InfoWindow(_mainWindowViewModel.SelectedAccent ?? "Cyan");
_infoWindow.Show();
}
#endregion
private void Window_KeyDown(object sender, KeyEventArgs e)
{
bool ctrlKeyDown = GetCtrlKeyDown();
bool addKeyDown = GetAddKeyDown();
bool subtractKeyDown = GetSubtractKeyDown();
if (ctrlKeyDown && addKeyDown)
UpdateFontSize(true);
else if (ctrlKeyDown && subtractKeyDown)
UpdateFontSize(false);
}
private void tbxMultiLine_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
bool ctrlKeyDown = GetCtrlKeyDown();
HandleFontSizeChange(e, ctrlKeyDown);
if (e.Handled)
return;
}
private void HandleFontSizeChange(MouseWheelEventArgs e, bool ctrlButtonPressed)
{
if (ctrlButtonPressed)
{
bool increase = e.Delta > 0;
this.UpdateFontSize(increase);
e.Handled = true;
}
}
private void UpdateFontSize(bool increase)
{
double currentSize = tbxMultiLine.FontSize;
if (increase)
{
if (currentSize < FONT_MAX_SIZE)
{
double newSize = Math.Min(FONT_MAX_SIZE, currentSize + 1);
tbxMultiLine.FontSize = newSize;
}
}
else
{
if (currentSize > FONT_MIN_SIZE)
{
double newSize = Math.Max(FONT_MIN_SIZE, currentSize - 1);
tbxMultiLine.FontSize = newSize;
}
}
}
private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
bool altKeyDown = GetAltKeyDown();
bool hKeyDown = GetHKeyDown(); // h - height
bool wKeyDown = GetWKeyDown(); // w - Width
// mouse wheel up equals e.Delta higher than 0
// mouse wheel down equals e.Delta smaller than 0
bool increase = e.Delta > 0;
HandleWindowResize(increase, altKeyDown, hKeyDown, wKeyDown);
}
private void HandleWindowResize(bool increase, bool altKeyDown, bool hKeyDown, bool wKeyDown)
{
if (altKeyDown && hKeyDown)
ResizeHeight(increase);
else if (altKeyDown && wKeyDown)
ResizeWidth(increase);
}
private void ResizeHeight(bool increase)
{
if (increase)
IncreaseHeight();
else if (this.Height > MIN_WINDOW_HEIGHT)
DecreaseHeight();
}
private void IncreaseHeight() => this.Height += WINDOW_RESIZE_FACTOR;
private void DecreaseHeight() => this.Height -= WINDOW_RESIZE_FACTOR;
private void ResizeWidth(bool increase)
{
if (increase)
IncreaseWidth();
else if (this.Width > MIN_WINDOW_WIDTH)
DecreaseWidth();
}
private void IncreaseWidth() => this.Width += WINDOW_RESIZE_FACTOR;
private void DecreaseWidth() => this.Width -= WINDOW_RESIZE_FACTOR;
private void btnIncreaseWindowSize_Click(object sender, RoutedEventArgs e)
{
ResizeHeight(increase: true);
ResizeWidth(increase: true);
}
private void btnReduceWindowSize_Click(object sender, RoutedEventArgs e)
{
ResizeHeight(increase: false);
ResizeWidth(increase: false);
}
/// <summary>
/// The "Document" Property of the Avalon TextEdit does not notify back to the VM
/// Therefore, it's necessary to catch the Textchanged event in order to update the CharacterCount
/// </summary>
private void TbxMultiLine_TextChanged(object sender, EventArgs e)
{
if (_mainWindowViewModel != null)
this._mainWindowViewModel.CharacterCount = this.tbxMultiLine.Document.TextLength.ToString();
}
private void ResetWindowSize()
{
this.Width = 650;
this.Height = 350;
}
#region GetKeyDownMethods
private static bool GetSubtractKeyDown() => Keyboard.IsKeyDown(Key.Subtract);
private static bool GetAddKeyDown() => Keyboard.IsKeyDown(Key.Add);
private static bool GetCtrlKeyDown() => (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl));
private static bool GetWKeyDown() => Keyboard.IsKeyDown(Key.W);
private static bool GetHKeyDown() => Keyboard.IsKeyDown(Key.H);
private static bool GetAltKeyDown() => (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt));
#endregion GetKeyDownMethods
}
}