2022-04-20 04:00:28 +08:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
using System;
|
2022-04-20 23:08:25 +08:00
|
|
|
|
using System.Drawing;
|
2022-04-20 04:00:28 +08:00
|
|
|
|
using Microsoft.PowerLauncher.Telemetry;
|
2022-04-20 23:08:25 +08:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Helpers;
|
2022-04-20 04:00:28 +08:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Views;
|
|
|
|
|
using Microsoft.PowerToys.Telemetry;
|
|
|
|
|
using Microsoft.UI;
|
|
|
|
|
using Microsoft.UI.Windowing;
|
|
|
|
|
using Microsoft.UI.Xaml;
|
|
|
|
|
using Windows.ApplicationModel.Resources;
|
|
|
|
|
using Windows.Data.Json;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// An empty window that can be used on its own or navigated to within a Frame.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed partial class MainWindow : Window
|
|
|
|
|
{
|
2022-05-19 21:12:59 +08:00
|
|
|
|
public MainWindow(bool createHidden = false)
|
2022-04-20 04:00:28 +08:00
|
|
|
|
{
|
|
|
|
|
var bootTime = new System.Diagnostics.Stopwatch();
|
|
|
|
|
bootTime.Start();
|
|
|
|
|
|
|
|
|
|
ShellPage.SetElevationStatus(App.IsElevated);
|
|
|
|
|
ShellPage.SetIsUserAnAdmin(App.IsUserAnAdmin);
|
|
|
|
|
|
|
|
|
|
// Set window icon
|
|
|
|
|
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
|
|
|
|
|
WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd);
|
|
|
|
|
AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
|
|
|
|
|
appWindow.SetIcon("icon.ico");
|
|
|
|
|
|
2022-04-20 23:08:25 +08:00
|
|
|
|
var placement = Utils.DeserializePlacementOrDefault(hWnd);
|
2022-05-19 21:12:59 +08:00
|
|
|
|
if (createHidden)
|
|
|
|
|
{
|
|
|
|
|
placement.ShowCmd = NativeMethods.SW_HIDE;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-20 23:08:25 +08:00
|
|
|
|
NativeMethods.SetWindowPlacement(hWnd, ref placement);
|
|
|
|
|
|
2022-04-20 04:00:28 +08:00
|
|
|
|
ResourceLoader loader = ResourceLoader.GetForViewIndependentUse();
|
|
|
|
|
Title = loader.GetString("SettingsWindow_Title");
|
|
|
|
|
|
|
|
|
|
// send IPC Message
|
|
|
|
|
ShellPage.SetDefaultSndMessageCallback(msg =>
|
|
|
|
|
{
|
|
|
|
|
// IPC Manager is null when launching runner directly
|
|
|
|
|
App.GetTwoWayIPCManager()?.Send(msg);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// send IPC Message
|
|
|
|
|
ShellPage.SetRestartAdminSndMessageCallback(msg =>
|
|
|
|
|
{
|
|
|
|
|
App.GetTwoWayIPCManager()?.Send(msg);
|
|
|
|
|
Environment.Exit(0); // close application
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// send IPC Message
|
|
|
|
|
ShellPage.SetCheckForUpdatesMessageCallback(msg =>
|
|
|
|
|
{
|
|
|
|
|
App.GetTwoWayIPCManager()?.Send(msg);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// open oobe
|
|
|
|
|
ShellPage.SetOpenOobeCallback(() =>
|
|
|
|
|
{
|
|
|
|
|
if (App.GetOobeWindow() == null)
|
|
|
|
|
{
|
|
|
|
|
App.SetOobeWindow(new OobeWindow(Microsoft.PowerToys.Settings.UI.OOBE.Enums.PowerToysModules.Overview));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
App.GetOobeWindow().Activate();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.InitializeComponent();
|
|
|
|
|
|
|
|
|
|
// receive IPC Message
|
|
|
|
|
App.IPCMessageReceivedCallback = (string msg) =>
|
|
|
|
|
{
|
|
|
|
|
if (ShellPage.ShellHandler.IPCResponseHandleList != null)
|
|
|
|
|
{
|
|
|
|
|
var success = JsonObject.TryParse(msg, out JsonObject json);
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
foreach (Action<JsonObject> handle in ShellPage.ShellHandler.IPCResponseHandleList)
|
|
|
|
|
{
|
|
|
|
|
handle(json);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.LogError("Failed to parse JSON from IPC message.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bootTime.Stop();
|
|
|
|
|
|
|
|
|
|
PowerToysTelemetry.Log.WriteEvent(new SettingsBootEvent() { BootTimeMs = bootTime.ElapsedMilliseconds });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void NavigateToSection(System.Type type)
|
|
|
|
|
{
|
|
|
|
|
ShellPage.Navigate(type);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-19 21:12:59 +08:00
|
|
|
|
public void CloseHiddenWindow()
|
2022-04-20 04:00:28 +08:00
|
|
|
|
{
|
2022-05-19 21:12:59 +08:00
|
|
|
|
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
|
|
|
|
|
if (!NativeMethods.IsWindowVisible(hWnd))
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-20 23:08:25 +08:00
|
|
|
|
|
2022-05-19 21:12:59 +08:00
|
|
|
|
private void Window_Closed(object sender, WindowEventArgs args)
|
|
|
|
|
{
|
2022-04-20 23:08:25 +08:00
|
|
|
|
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
|
|
|
|
|
Utils.SerializePlacement(hWnd);
|
2022-05-19 21:12:59 +08:00
|
|
|
|
|
|
|
|
|
if (App.GetOobeWindow() == null)
|
|
|
|
|
{
|
|
|
|
|
App.ClearSettingsWindow();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
args.Handled = true;
|
|
|
|
|
NativeMethods.ShowWindow(hWnd, NativeMethods.SW_HIDE);
|
|
|
|
|
}
|
2022-04-20 04:00:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|