This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSettingsWindow.xaml.cs
190 lines (166 loc) · 6.57 KB
/
SettingsWindow.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
using System;
using System.Collections.Generic;
using Furtherance.Views;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Windows.Storage;
using Windows.Storage.Pickers;
using static PInvoke.User32;
namespace Furtherance;
/// <summary>
/// Furtherance's settings window.
/// </summary>
public sealed partial class SettingsWindow : Window
{
public SettingsWindow()
{
InitializeComponent();
Title = "Settings";
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
SetWindowDetails(hwnd, 500, 600);
var windowId = Win32Interop.GetWindowIdFromWindow(hwnd);
var appWindow = AppWindow.GetFromWindowId(windowId);
appWindow.SetIcon("Assets/furtherance.ico");
// Idle settings
if (App.localSettings.Values["Idle"] != null)
{
idleToggle.IsOn = (bool)App.localSettings.Values["Idle"];
}
if (App.localSettings.Values["IdleTime"] != null)
{
idleMinutesSpin.Value = (double)App.localSettings.Values["IdleTime"];
}
if (App.localSettings.Values["Pomodoro"] != null)
{
pomodoroToggle.IsOn = (bool)App.localSettings.Values["Pomodoro"];
}
if (App.localSettings.Values["PomodoroTime"] != null)
{
pomodoroMinutesSpin.Value = (double)App.localSettings.Values["PomodoroTime"];
}
if (App.localSettings.Values["DatabaseLocation"] != null)
{
databaseLocText.Text = "Location: " + (string)App.localSettings.Values["DatabaseLocation"];
}
}
private void Idle_Toggled(object sender, RoutedEventArgs e)
{
var toggleSwitch = sender as ToggleSwitch;
if (toggleSwitch != null && idleMinutesStack != null)
{
if (toggleSwitch.IsOn == true)
{
App.localSettings.Values["Idle"] = true;
idleMinutesStack.Visibility = Visibility.Visible;
}
else
{
App.localSettings.Values["Idle"] = false;
idleMinutesStack.Visibility = Visibility.Collapsed;
}
}
}
private void IdleSpin_Changed(object sender, NumberBoxValueChangedEventArgs e)
{
var numberBox = sender as NumberBox;
App.localSettings.Values["IdleTime"] = numberBox.Value;
}
private void Pomodoro_Toggled(object sender, RoutedEventArgs e)
{
var toggleSwitch = sender as ToggleSwitch;
if (toggleSwitch != null && pomodoroMinutesStack != null)
{
if (toggleSwitch.IsOn == true)
{
App.localSettings.Values["Pomodoro"] = true;
pomodoroMinutesStack.Visibility = Visibility.Visible;
}
else
{
App.localSettings.Values["Pomodoro"] = false;
pomodoroMinutesStack.Visibility = Visibility.Collapsed;
}
MainPage.pomodoroEnabled = toggleSwitch.IsOn;
MainPage.mainPage.ResetTimer();
}
}
private void PomodoroSpin_Changed(object sender, NumberBoxValueChangedEventArgs e)
{
var numberBox = sender as NumberBox;
App.localSettings.Values["PomodoroTime"] = numberBox.Value;
MainPage.pomodoroTime = numberBox.Value;
MainPage.mainPage.ResetTimer();
}
private static void SetWindowDetails(IntPtr hwnd, int width, int height)
{
var dpi = GetDpiForWindow(hwnd);
var scalingFactor = (float)dpi / 96;
width = (int)(width * scalingFactor);
height = (int)(height * scalingFactor);
_ = SetWindowPos(hwnd, SpecialWindowHandles.HWND_TOP,
0, 0, width, height,
SetWindowPosFlags.SWP_NOMOVE);
_ = SetWindowLong(hwnd,
WindowLongIndexFlags.GWL_STYLE,
(SetWindowLongFlags)(GetWindowLong(hwnd,
WindowLongIndexFlags.GWL_STYLE) &
~(int)SetWindowLongFlags.WS_MAXIMIZEBOX));
}
private async void Change_DB_Location_Click(object sender, RoutedEventArgs e)
{
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
var savePicker = new Windows.Storage.Pickers.FileSavePicker()
{
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
};
WinRT.Interop.InitializeWithWindow.Initialize(savePicker, hwnd);
savePicker.FileTypeChoices.Add("Database", new List<string>() { ".db" });
savePicker.SuggestedFileName = "furtherance.db";
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
Database.BackupDB(file.Path);
databaseLocText.Text = "Location: " + file.Path;
App.localSettings.Values["DatabaseLocation"] = file.Path;
Database.GetDatabasePath();
}
}
private async void Backup_DB_Click(object sender, RoutedEventArgs e)
{
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
var savePicker = new Windows.Storage.Pickers.FileSavePicker()
{
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
};
WinRT.Interop.InitializeWithWindow.Initialize(savePicker, hwnd);
savePicker.FileTypeChoices.Add("Database", new List<string>() { ".db" });
savePicker.SuggestedFileName = "furtherance_bkup.db";
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
Database.BackupDB(file.Path);
}
}
private async void Import_DB_Click(object sender, RoutedEventArgs e)
{
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
var openPicker = new Windows.Storage.Pickers.FileOpenPicker()
{
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
};
WinRT.Interop.InitializeWithWindow.Initialize(openPicker, hwnd);
openPicker.FileTypeFilter.Add(".db");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
Database.ImportDB(file.Path);
MainPage.mainPage.RefreshTaskList();
}
}
private void Window_Closed(object sender, WindowEventArgs args)
{
MainPage.mainPage.settingsWindow = null;
}
}