-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
344 lines (303 loc) · 12.6 KB
/
MainWindow.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
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
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;
using TemporaTasks.Pages;
using TemporaTasks.Core;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Diagnostics;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using Hardcodet.Wpf.TaskbarNotification;
using TemporaTasks.Windows;
using TemporaTasks.UserControls;
namespace TemporaTasks
{
public partial class MainWindow : Window
{
public HomePage homePage;
public MainWindow()
{
InitializeComponent();
SourceInitialized += Window1_SourceInitialized;
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
if (TaskFile.saveFilePath == null)
{
string path = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\TemporaTasks\\";
TaskFile.backupPath = $"{path}backups\\";
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
if (!Directory.Exists(TaskFile.backupPath)) Directory.CreateDirectory(TaskFile.backupPath);
TaskFile.saveFilePath = $"{path}data.ttask";
TaskFile.LoadData();
}
else
{
IsHitTestVisible = false;
await Task.Delay(500);
IsHitTestVisible = true;
}
}
public void LoadPage()
{
if (homePage == null)
{
homePage = new();
FrameView.Navigate(homePage);
}
else homePage.newFileLoaded();
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Q && (Keyboard.IsKeyDown(Key.RightCtrl) || Keyboard.IsKeyDown(Key.LeftCtrl))) TaskFile.SaveData(this);
else if (e.Key == Key.F && NoTextBoxHasFocus(this)) MaximizeButton_MouseDown(null, null);
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.System && e.SystemKey == Key.F4)
{
WindowHide();
e.Handled = true;
}
}
private bool NoTextBoxHasFocus(DependencyObject parent)
{
foreach (var textBox in GetVisualChildren<TextBox>(parent))
if (textBox.IsFocused)
return false;
return true;
}
private IEnumerable<T> GetVisualChildren<T>(DependencyObject parent) where T : DependencyObject
{
if (parent != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child is T typedChild)
yield return typedChild;
foreach (var grandChild in GetVisualChildren<T>(child))
yield return grandChild;
}
}
}
private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
private void MinimizeButton_IsMouseDirectlyOverChanged(object sender, DependencyPropertyChangedEventArgs e)
{
MinimizeButton.Background.BeginAnimation(Brush.OpacityProperty, new DoubleAnimation(((bool)e.NewValue) ? 1 : 0, TimeSpan.FromMilliseconds(250)));
MinimizeX1.BeginAnimation(OpacityProperty, new DoubleAnimation(((bool)e.NewValue) ? 1 : 0.25, TimeSpan.FromMilliseconds(250)));
}
private void MinimizeButton_MouseDown(object sender, MouseButtonEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void MaximizeButton_IsMouseDirectlyOverChanged(object sender, DependencyPropertyChangedEventArgs e)
{
MaximizeButton.Background.BeginAnimation(Brush.OpacityProperty, new DoubleAnimation(((bool)e.NewValue) ? 1 : 0, TimeSpan.FromMilliseconds(250)));
MaximizeRect.BeginAnimation(OpacityProperty, new DoubleAnimation(((bool)e.NewValue) ? 1 : 0.25, TimeSpan.FromMilliseconds(250)));
}
private void MaximizeButton_MouseDown(object sender, MouseButtonEventArgs e)
{
if (WindowState == WindowState.Maximized)
{
WindowState = WindowState.Normal;
WindowBorder.BorderThickness = new Thickness(1);
}
else
{
WindowState = WindowState.Maximized;
WindowBorder.BorderThickness = new Thickness(0);
}
}
private void CloseButton_IsMouseDirectlyOverChanged(object sender, DependencyPropertyChangedEventArgs e)
{
CloseButton.Background.BeginAnimation(Brush.OpacityProperty, new DoubleAnimation(((bool)e.NewValue)? 1 : 0, TimeSpan.FromMilliseconds(250)));
X1.BeginAnimation(OpacityProperty, new DoubleAnimation(((bool)e.NewValue)? 1 : 0.25, TimeSpan.FromMilliseconds(250)));
X2.BeginAnimation(OpacityProperty, new DoubleAnimation(((bool)e.NewValue)? 1 : 0.25, TimeSpan.FromMilliseconds(250)));
}
private void CloseButton_MouseDown(object sender, MouseButtonEventArgs e)
{
if (Keyboard.IsKeyDown(Key.LeftShift))
{
TaskFile.SaveData(this);
return;
}
WindowHide();
}
public delegate void WindowUnHidden();
public event WindowUnHidden IsWindowUnHidden;
public async void WindowHide(bool hide = true)
{
if (hide)
{
BeginAnimation(OpacityProperty, new DoubleAnimation(0, TimeSpan.FromMilliseconds(200)));
await Task.Delay(201);
Hide();
BeginAnimation(OpacityProperty, new DoubleAnimation(1, TimeSpan.FromTicks(0)));
}
else
{
IsWindowUnHidden?.Invoke();
Show();
Activate();
Topmost = true;
Topmost = false;
await Task.Delay(25);
homePage.Focus();
}
}
bool balloonCalledRecently = false;
public async void OnTaskDue(IndividualTask task)
{
if (balloonCalledRecently) return;
balloonCalledRecently = true;
bool _ = TaskFile.notificationMode != TaskFile.NotificationMode.Muted && (TaskFile.notificationMode == TaskFile.NotificationMode.Normal || (TaskFile.notificationMode == TaskFile.NotificationMode.High && task.taskPriority == IndividualTask.TaskPriority.High));
if (_)
TrayIcon.ShowBalloonTip($"{((task.taskPriority == IndividualTask.TaskPriority.High) ? "⚠" : "")}Task Due!", task.IsGarbled() ? "Garbled Task" : task.Name, BalloonIcon.Info);
else
{
int count = 0;
foreach (IndividualTask _task in TaskFile.TaskList)
if (_task.IsDue && _task.taskStatus == IndividualTask.TaskStatus.Normal) count++;
if (count >= 10)
TrayIcon.ShowBalloonTip("Warning", $"{count} tasks are due!", BalloonIcon.Warning);
}
await Task.Delay(5000);
if (_ && TaskFile.notifPopupMode && !IsActive) WindowHide(false);
balloonCalledRecently = false;
}
public async void HotkeyRegFailNotif()
{
TrayIcon.ShowBalloonTip("Hotkey Registration Failed", "TemporaTasks wasn't able to register hotkeys, as another application might be using them", BalloonIcon.Error);
}
private void TrayIcon_TrayLeftMouseDown(object sender, RoutedEventArgs e)
{
WindowHide(IsActive);
}
private void TrayIcon_TrayRightMouseUp(object sender, RoutedEventArgs e)
{
TaskFile.SaveData(this);
}
private void Border_MouseMove(object sender, MouseEventArgs e)
{
Point mousePosition = e.GetPosition(sender as UIElement);
ToolTip tooltip = (ToolTip)((Border)sender).ToolTip;
tooltip.Placement = PlacementMode.Relative;
tooltip.HorizontalOffset = mousePosition.X;
tooltip.VerticalOffset = mousePosition.Y;
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
KeyboardHooks.Register();
}
protected override void OnClosed(EventArgs e)
{
KeyboardHooks.Unregister();
base.OnClosed(e);
}
// -----------------------------------------------------------------------------------
private const int WM_SYSCOMMAND = 0x112;
private HwndSource hwndSource;
private enum ResizeDirection
{
Left = 61441,
Right = 61442,
Top = 61443,
TopLeft = 61444,
TopRight = 61445,
Bottom = 61446,
BottomLeft = 61447,
BottomRight = 61448,
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
private void Window1_SourceInitialized(object sender, EventArgs e)
{
hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
}
private void ResizeWindow(ResizeDirection direction)
{
SendMessage(hwndSource.Handle, WM_SYSCOMMAND, (IntPtr)direction, IntPtr.Zero);
}
protected void ResetCursor(object sender, MouseEventArgs e)
{
if (Mouse.LeftButton != MouseButtonState.Pressed)
{
this.Cursor = Cursors.Arrow;
FrameView.IsHitTestVisible = true;
if (homePage != null) homePage.WindowIsResizing(false);
}
}
protected void Resize(object sender, MouseButtonEventArgs e)
{
if (WindowState == WindowState.Maximized) return;
FrameView.IsHitTestVisible = false;
homePage.WindowIsResizing(true);
var clickedShape = sender as Shape;
switch (clickedShape.Name)
{
case "ResizeN":
this.Cursor = Cursors.SizeNS;
ResizeWindow(ResizeDirection.Top);
break;
case "ResizeE":
this.Cursor = Cursors.SizeWE;
ResizeWindow(ResizeDirection.Right);
break;
case "ResizeS":
this.Cursor = Cursors.SizeNS;
ResizeWindow(ResizeDirection.Bottom);
break;
case "ResizeW":
this.Cursor = Cursors.SizeWE;
ResizeWindow(ResizeDirection.Left);
break;
case "ResizeNW":
this.Cursor = Cursors.SizeNWSE;
ResizeWindow(ResizeDirection.TopLeft);
break;
case "ResizeNE":
this.Cursor = Cursors.SizeNESW;
ResizeWindow(ResizeDirection.TopRight);
break;
case "ResizeSE":
this.Cursor = Cursors.SizeNWSE;
ResizeWindow(ResizeDirection.BottomRight);
break;
case "ResizeSW":
this.Cursor = Cursors.SizeNESW;
ResizeWindow(ResizeDirection.BottomLeft);
break;
default:
break;
}
}
protected void DisplayResizeCursor(object sender, MouseEventArgs e)
{
if (WindowState == WindowState.Maximized) return;
Cursor = (sender as Shape).Name switch
{
"ResizeN" or "ResizeS" => Cursors.SizeNS,
"ResizeE" or "ResizeW" => Cursors.SizeWE,
"ResizeNW" or "ResizeSE" => Cursors.SizeNWSE,
"ResizeNE" or "ResizeSW" => Cursors.SizeNESW,
_ => Cursors.Arrow
};
}
private void Window_MouseUp(object sender, MouseButtonEventArgs e)
{
if (FrameView.IsHitTestVisible) return;
FrameView.IsHitTestVisible = true;
ResetCursor(null, null);
}
// -----------------------------------------------------------------------------------
}
}