137 lines
4.9 KiB
C#
Raw Normal View History

2024-10-14 19:02:22 +02: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;
2024-11-03 22:06:38 +01:00
using System.Collections.Generic;
2024-10-14 19:02:22 +02:00
using System.Globalization;
2024-11-03 22:06:38 +01:00
using System.IO;
2024-10-14 19:02:22 +02:00
using System.Runtime.InteropServices;
2024-11-03 22:06:38 +01:00
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Library;
2024-10-14 19:02:22 +02:00
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using ShortcutGuide.Models;
using ShortcutGuide.Properties;
using Windows.Foundation;
using Windows.Graphics;
using WinUIEx;
using static NativeMethods;
namespace ShortcutGuide
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : WindowEx
{
private AppWindow _appWindow;
public MainWindow()
{
InitializeComponent();
2024-11-02 15:10:33 +01:00
2024-10-14 19:02:22 +02:00
Title = Resource.ResourceManager.GetString("Title", CultureInfo.InvariantCulture)!;
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
WindowId windowId = Win32Interop.GetWindowIdFromWindow(hwnd);
_appWindow = AppWindow.GetFromWindowId(windowId);
#if DEBUG
#else
this.SetIsAlwaysOnTop(true);
this.SetIsShownInSwitchers(false);
#endif
this.SetIsResizable(false);
this.SetIsMinimizable(false);
this.SetIsMaximizable(false);
IsTitleBarVisible = false;
// Remove the caption style from the window style. Windows App SDK 1.6 added it, which made the title bar and borders appear for Measure Tool. This code removes it.
var windowStyle = GetWindowLong(hwnd, GWL_STYLE);
windowStyle = windowStyle & (~WS_CAPTION);
_ = SetWindowLong(hwnd, GWL_STYLE, windowStyle);
#if DEBUG
#else
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
#endif
2024-11-03 22:06:38 +01:00
Activated += OnLauched;
SettingsUtils settingsUtils = new();
if (settingsUtils.SettingsExists(ShortcutGuideSettings.ModuleName, "Pinned.json"))
{
string pinnedPath = settingsUtils.GetSettingsFilePath(ShortcutGuideSettings.ModuleName, "Pinned.json");
ShortcutPageParameters.PinnedShortcuts = JsonSerializer.Deserialize<Dictionary<string, List<Shortcut>>>(File.ReadAllText(pinnedPath))!;
}
2024-10-14 19:02:22 +02:00
}
private void OnLauched(object sender, WindowActivatedEventArgs e)
{
if (!_setPosition)
{
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
WindowId windowId = Win32Interop.GetWindowIdFromWindow(hwnd);
_appWindow = AppWindow.GetFromWindowId(windowId);
GetCursorPos(out POINT lpPoint);
_appWindow.Move(lpPoint);
float dpiScale = DpiHelper.GetDPIScaleForWindow((int)hwnd);
Rect monitorRect = DisplayHelper.GetWorkAreaForDisplayWithWindow(hwnd);
this.SetWindowSize(monitorRect.Width / dpiScale, monitorRect.Height / dpiScale / 2);
// Move top of the window to the center of the monitor
_appWindow.Move(new PointInt32((int)monitorRect.X, (int)(monitorRect.Y + (int)(monitorRect.Height / 2))));
_setPosition = true;
}
2024-11-03 22:06:38 +01:00
WindowSelector.SelectedItem = WindowsSelectorBarItem;
2024-10-14 19:02:22 +02:00
}
public void WindowSelectionChanged(object sender, SelectorBarSelectionChangedEventArgs e)
{
2024-11-03 22:06:38 +01:00
ShortcutPageParameters.CurrentPageName = ((SelectorBar)sender).SelectedItem.Name switch {
"WindowsSelectorBarItem" => YmlInterpreter.GetIndexYamlFile().DefaultShellName,
"PowerToysSelectorBarItem" => "Microsoft.PowerToys",
_ => throw new NotImplementedException(),
};
ContentFrame.Loaded += (s, e) => ShortcutPageParameters.FrameHeight.OnFrameHeightChanged(ContentFrame.ActualHeight);
2024-10-14 19:02:22 +02:00
ContentFrame.Navigate(typeof(ShortcutView));
}
private bool _setPosition;
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool GetCursorPos(out POINT lpPoint);
public struct POINT
{
public int X;
public int Y;
public static implicit operator PointInt32(POINT point)
{
return new PointInt32(point.X, point.Y);
}
}
public void CloseButton_Clicked(object sender, RoutedEventArgs e)
{
Environment.Exit(0);
}
2024-11-02 15:10:33 +01:00
private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
{
ShortcutPageParameters.SearchFilter.OnFilterChanged(SearchBox.Text);
}
2024-10-14 19:02:22 +02:00
}
}