mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-11-23 19:49:17 +08:00
Fix error messages and read application titles out of index manifest
This commit is contained in:
parent
a14c458f19
commit
da7b789bfe
@ -8,7 +8,9 @@ namespace ShortcutGuide.Models
|
||||
{
|
||||
public struct IndexItem
|
||||
{
|
||||
public string Filter { get; set; }
|
||||
public string WindowFilter { get; set; }
|
||||
|
||||
public bool BackgroundProcess { get; set; }
|
||||
|
||||
public string[] Apps { get; set; }
|
||||
}
|
||||
|
@ -13,5 +13,7 @@ namespace ShortcutGuide.Models
|
||||
public string WindowFilter { get; set; }
|
||||
|
||||
public bool BackgroundProcess { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,11 @@ namespace ShortcutGuide.Models
|
||||
|
||||
public void OnFrameHeightChanged(double height)
|
||||
{
|
||||
if (height <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
FrameHeightChanged?.Invoke(this, height);
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using Windows.Graphics;
|
||||
|
||||
internal static partial class NativeMethods
|
||||
@ -40,6 +41,12 @@ internal static partial class NativeMethods
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static partial bool GetCursorPos(out POINT lpPoint);
|
||||
|
||||
[LibraryImport("user32.dll")]
|
||||
public static partial IntPtr GetForegroundWindow();
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
|
||||
|
||||
public struct POINT
|
||||
{
|
||||
public int X;
|
||||
|
@ -21,17 +21,6 @@
|
||||
<RowDefinition Height="*" />
|
||||
</Grid.RowDefinitions>
|
||||
<SelectorBar Grid.Column="0" Grid.Row="0" x:Name="WindowSelector" SelectionChanged="WindowSelectionChanged">
|
||||
<SelectorBarItem x:Name="WindowsSelectorBarItem" Text="Windows">
|
||||
<SelectorBarItem.Icon>
|
||||
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph="" />
|
||||
</SelectorBarItem.Icon>
|
||||
</SelectorBarItem>
|
||||
<SelectorBarItem Text="PowerToys" x:Name="PowerToysSelectorBarItem" />
|
||||
<SelectorBarItem Text="Current window" x:Name="CurrentWindowSelectorBarItem">
|
||||
<SelectorBarItem.Icon>
|
||||
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph="" />
|
||||
</SelectorBarItem.Icon>
|
||||
</SelectorBarItem>
|
||||
</SelectorBar>
|
||||
<TextBox Grid.Column="1" Grid.Row="0" Height="32" Width="300" HorizontalAlignment="Right" VerticalContentAlignment="Center" x:Name="SearchBox" HorizontalContentAlignment="Right" TextChanged="SearchBox_TextChanged"></TextBox>
|
||||
<Button Grid.Column="2" Grid.Row="0" HorizontalContentAlignment="Center">
|
||||
|
@ -28,8 +28,12 @@ namespace ShortcutGuide
|
||||
{
|
||||
private AppWindow _appWindow;
|
||||
|
||||
private string[] _currentApplicationIds;
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
_currentApplicationIds = YmlInterpreter.GetAllCurrentApplicationIds();
|
||||
|
||||
InitializeComponent();
|
||||
|
||||
Title = Resource.ResourceManager.GetString("Title", CultureInfo.InvariantCulture)!;
|
||||
@ -37,8 +41,7 @@ namespace ShortcutGuide
|
||||
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
|
||||
WindowId windowId = Win32Interop.GetWindowIdFromWindow(hwnd);
|
||||
_appWindow = AppWindow.GetFromWindowId(windowId);
|
||||
#if DEBUG
|
||||
#else
|
||||
#if !DEBUG
|
||||
this.SetIsAlwaysOnTop(true);
|
||||
this.SetIsShownInSwitchers(false);
|
||||
#endif
|
||||
@ -51,8 +54,7 @@ namespace ShortcutGuide
|
||||
var windowStyle = GetWindowLongW(hwnd, GWL_STYLE);
|
||||
windowStyle &= ~WS_CAPTION;
|
||||
_ = SetWindowLongW(hwnd, GWL_STYLE, windowStyle);
|
||||
#if DEBUG
|
||||
#else
|
||||
#if !DEBUG
|
||||
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
|
||||
#endif
|
||||
|
||||
@ -89,20 +91,38 @@ namespace ShortcutGuide
|
||||
_setPosition = true;
|
||||
}
|
||||
|
||||
WindowSelector.SelectedItem = WindowsSelectorBarItem;
|
||||
if (WindowSelector.Items.Count == 0)
|
||||
{
|
||||
foreach (var item in _currentApplicationIds)
|
||||
{
|
||||
if (item == YmlInterpreter.GetIndexYamlFile().DefaultShellName)
|
||||
{
|
||||
WindowSelector.Items.Insert(0, new SelectorBarItem { Name = item, Text = "Windows", Icon = new FontIcon() { Glyph = "\xE770" } });
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
WindowSelector.Items.Add(new SelectorBarItem { Name = item, Text = YmlInterpreter.GetShortcutsOfApplication(item).Name });
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void WindowSelectionChanged(object sender, SelectorBarSelectionChangedEventArgs e)
|
||||
{
|
||||
ShortcutPageParameters.CurrentPageName = ((SelectorBar)sender).SelectedItem.Name switch {
|
||||
"WindowsSelectorBarItem" => YmlInterpreter.GetIndexYamlFile().DefaultShellName,
|
||||
"PowerToysSelectorBarItem" => "Microsoft.PowerToys",
|
||||
_ => throw new NotImplementedException(),
|
||||
};
|
||||
ShortcutPageParameters.CurrentPageName = ((SelectorBar)sender).SelectedItem.Name;
|
||||
|
||||
ContentFrame.Loaded += (s, e) => ShortcutPageParameters.FrameHeight.OnFrameHeightChanged(ContentFrame.ActualHeight);
|
||||
ContentFrame.Loaded += (_, _) => ShortcutPageParameters.FrameHeight.OnFrameHeightChanged(ContentFrame.ActualHeight);
|
||||
|
||||
ContentFrame.Navigate(typeof(ShortcutView));
|
||||
|
||||
// I don't know why this has to be called again, but it does.
|
||||
ShortcutPageParameters.FrameHeight.OnFrameHeightChanged(ContentFrame.ActualHeight);
|
||||
}
|
||||
|
||||
private bool _setPosition;
|
||||
|
@ -40,10 +40,10 @@
|
||||
<RowDefinition Height="*" />
|
||||
</Grid.RowDefinitions>
|
||||
<SelectorBar Grid.Row="0" x:Name="CategorySelector" />
|
||||
<TextBlock x:Name="ErrorMessage" Style="{StaticResource SubheaderTextBlockStyle}" Grid.Row="1" Margin="6,0,6,0"></TextBlock>
|
||||
<ScrollViewer Grid.Row="1" HorizontalScrollMode="Enabled" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" VerticalScrollMode="Disabled">
|
||||
<Grid>
|
||||
<StackPanel Height="{Binding ContentHeight}" Orientation="Horizontal" Grid.Row="1" x:Name="OverviewStackPanel">
|
||||
<TextBlock x:Name="ErrorMessage" Style="{StaticResource SubheaderTextBlockStyle}" Grid.Row="1" Margin="6,0,6,0"></TextBlock>
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
|
@ -209,15 +209,23 @@ namespace ShortcutGuide
|
||||
OverviewStackPanel.Visibility = Visibility.Collapsed;
|
||||
ShortcutListElement.Visibility = Visibility.Visible;
|
||||
|
||||
if (int.Parse(sender.SelectedItem.Name, CultureInfo.InvariantCulture) == -1)
|
||||
try
|
||||
{
|
||||
OpenOverview();
|
||||
return;
|
||||
}
|
||||
if (int.Parse(sender.SelectedItem.Name, CultureInfo.InvariantCulture) == -1)
|
||||
{
|
||||
OpenOverview();
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var shortcut in shortcutList.Shortcuts[int.Parse(sender.SelectedItem.Name, CultureInfo.InvariantCulture)].Properties)
|
||||
foreach (var shortcut in shortcutList.Shortcuts[int.Parse(sender.SelectedItem.Name, CultureInfo.InvariantCulture)].Properties)
|
||||
{
|
||||
ShortcutListElement.Items.Add((ShortcutTemplateDataObject)shortcut);
|
||||
}
|
||||
}
|
||||
catch (NullReferenceException)
|
||||
{
|
||||
ShortcutListElement.Items.Add((ShortcutTemplateDataObject)shortcut);
|
||||
ErrorMessage.Visibility = Visibility.Visible;
|
||||
ErrorMessage.Text = "Error displaying category";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,8 +3,17 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using ShortcutGuide.Models;
|
||||
using Windows.Devices.SmartCards;
|
||||
using WinUIEx;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace ShortcutGuide
|
||||
@ -37,6 +46,69 @@ namespace ShortcutGuide
|
||||
return deserializer.Deserialize<IndexFile>(content);
|
||||
}
|
||||
|
||||
public static string[] GetAllCurrentApplicationIds()
|
||||
{
|
||||
IntPtr handle = NativeMethods.GetForegroundWindow();
|
||||
|
||||
List<string> applicationIds = [];
|
||||
|
||||
static bool IsMatch(string input, string filter)
|
||||
{
|
||||
string regexPattern = "^" + Regex.Escape(filter).Replace("\\*", ".*") + "$";
|
||||
return Regex.IsMatch(input, regexPattern);
|
||||
}
|
||||
|
||||
var processes = Process.GetProcesses();
|
||||
|
||||
foreach (var item in GetIndexYamlFile().Index.Where((s) => s.BackgroundProcess))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (processes.Any((p) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
return IsMatch(p.MainModule!.ModuleName, item.WindowFilter);
|
||||
}
|
||||
catch (Win32Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}))
|
||||
{
|
||||
foreach (var app in item.Apps)
|
||||
{
|
||||
applicationIds.Add(app);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
if (NativeMethods.GetWindowThreadProcessId(handle, out uint processId) > 0)
|
||||
{
|
||||
string? name = Process.GetProcessById((int)processId).MainModule?.ModuleName;
|
||||
|
||||
if (name is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var item in GetIndexYamlFile().Index.First((s) => !s.BackgroundProcess && IsMatch(name, s.WindowFilter)).Apps)
|
||||
{
|
||||
applicationIds.Add(item);
|
||||
}
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [.. applicationIds];
|
||||
}
|
||||
|
||||
public static ShortcutList GetShortcutsOfDefaultShell()
|
||||
{
|
||||
return GetShortcutsOfApplication(GetIndexYamlFile().DefaultShellName);
|
||||
|
Loading…
Reference in New Issue
Block a user