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 pathApp.xaml.cs
173 lines (147 loc) · 6.32 KB
/
App.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
using System;
using System.Runtime.InteropServices;
using Furtherance.Activation;
using Furtherance.Contracts.Services;
using Furtherance.Core.Contracts.Services;
using Furtherance.Core.Services;
using Furtherance.Models;
using Furtherance.Services;
using Furtherance.ViewModels;
using Furtherance.Views;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Toolkit.Uwp.Notifications;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Windows.Storage;
using static PInvoke.User32;
namespace Furtherance;
public partial class App : Application
{
private static readonly IHost _host = Host
.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
// Default Activation Handler
services.AddTransient<ActivationHandler<LaunchActivatedEventArgs>, DefaultActivationHandler>();
// Services
services.AddSingleton<ILocalSettingsService, LocalSettingsServicePackaged>();
services.AddSingleton<IActivationService, ActivationService>();
services.AddSingleton<IPageService, PageService>();
services.AddSingleton<INavigationService, NavigationService>();
// Core Services
services.AddSingleton<IFileService, FileService>();
// Views and ViewModels
services.AddTransient<AddTaskViewModel>();
services.AddTransient<AddTaskPage>();
services.AddTransient<TaskEditViewModel>();
services.AddTransient<TaskEditPage>();
services.AddTransient<TaskDetailsViewModel>();
services.AddTransient<TaskDetailsPage>();
services.AddTransient<MainViewModel>();
services.AddTransient<MainPage>();
// Configuration
services.Configure<LocalSettingsOptions>(context.Configuration.GetSection(nameof(LocalSettingsOptions)));
})
.Build();
public static T GetService<T>()
where T : class
{
return _host.Services.GetService(typeof(T)) as T;
}
public static Window MainWindow { get; set; } = new Window() { Title = "Furtherance" };
public static ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
public App()
{
InitializeComponent();
UnhandledException += App_UnhandledException;
// Database.InitializeDatabase();
}
private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
// Log and handle exceptions as appropriate.
// For more details, see https://docs.microsoft.com/windows/winui/api/microsoft.ui.xaml.unhandledexceptioneventargs.
}
protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
// Get the activation args
var appArgs = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();
// Get or register the main instance
var mainInstance = Microsoft.Windows.AppLifecycle.AppInstance.FindOrRegisterForKey("main");
if (!mainInstance.IsCurrent)
{
// Redirect activation to that instance
await mainInstance.RedirectActivationToAsync(appArgs);
// And exit our instance and stop
System.Diagnostics.Process.GetCurrentProcess().Kill();
return;
}
InitializeSettings();
Database.InitializeDatabase();
base.OnLaunched(args);
var activationService = App.GetService<IActivationService>();
await activationService.ActivateAsync(args);
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(MainWindow);
SetWindowDetails(hwnd, 350, 600);
var windowId = Win32Interop.GetWindowIdFromWindow(hwnd);
var appWindow = AppWindow.GetFromWindowId(windowId);
appWindow.SetIcon("Assets/furtherance.ico");
ToastNotificationManagerCompat.OnActivated += ToastNotificationManagerCompat_OnActivated;
}
private void ToastNotificationManagerCompat_OnActivated(ToastNotificationActivatedEventArgsCompat e)
{
WindowHelper.ShowWindow(MainWindow);
}
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 static class WindowHelper
{
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
public static void ShowWindow(Window window)
{
// Bring the window to the foreground... first get the window handle...
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
// Restore window if minimized... requires DLL import above
ShowWindow(hwnd, 0x00000009);
// And call SetForegroundWindow... requires DLL import above
SetForegroundWindow(hwnd);
}
}
private static void InitializeSettings()
{
if (localSettings.Values["Idle"] == null)
{
localSettings.Values["Idle"] = true;
}
if (localSettings.Values["IdleTime"] == null)
{
localSettings.Values["IdleTime"] = Convert.ToDouble(6);
}
if (localSettings.Values["Pomodoro"] == null)
{
localSettings.Values["Pomodoro"] = false;
}
if (localSettings.Values["PomodoroTime"] == null)
{
localSettings.Values["PomodoroTime"] = Convert.ToDouble(25);
}
}
}