Skip to content

Commit da7b789

Browse files
committed
Fix error messages and read application titles out of index manifest
1 parent a14c458 commit da7b789

9 files changed

Lines changed: 135 additions & 30 deletions

File tree

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Models/IndexFile.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ public struct IndexFile
88
{
99
public struct IndexItem
1010
{
11-
public string Filter { get; set; }
11+
public string WindowFilter { get; set; }
12+
13+
public bool BackgroundProcess { get; set; }
1214

1315
public string[] Apps { get; set; }
1416
}

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Models/ShortcutList.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ public struct ShortcutList
1313
public string WindowFilter { get; set; }
1414

1515
public bool BackgroundProcess { get; set; }
16+
17+
public string Name { get; set; }
1618
}
1719
}

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Models/ShortcutPageParameters.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ internal sealed class FrameHeightObservable
3333

3434
public void OnFrameHeightChanged(double height)
3535
{
36+
if (height <= 0)
37+
{
38+
return;
39+
}
40+
3641
FrameHeightChanged?.Invoke(this, height);
3742
}
3843
}

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/NativeMethods.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Runtime.InteropServices;
7+
using System.Text;
78
using Windows.Graphics;
89

910
internal static partial class NativeMethods
@@ -40,6 +41,12 @@ internal static partial class NativeMethods
4041
[return: MarshalAs(UnmanagedType.Bool)]
4142
public static partial bool GetCursorPos(out POINT lpPoint);
4243

44+
[LibraryImport("user32.dll")]
45+
public static partial IntPtr GetForegroundWindow();
46+
47+
[DllImport("user32.dll", SetLastError = true)]
48+
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
49+
4350
public struct POINT
4451
{
4552
public int X;

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/MainWindow.xaml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,6 @@
2121
<RowDefinition Height="*" />
2222
</Grid.RowDefinitions>
2323
<SelectorBar Grid.Column="0" Grid.Row="0" x:Name="WindowSelector" SelectionChanged="WindowSelectionChanged">
24-
<SelectorBarItem x:Name="WindowsSelectorBarItem" Text="Windows">
25-
<SelectorBarItem.Icon>
26-
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE770;" />
27-
</SelectorBarItem.Icon>
28-
</SelectorBarItem>
29-
<SelectorBarItem Text="PowerToys" x:Name="PowerToysSelectorBarItem" />
30-
<SelectorBarItem Text="Current window" x:Name="CurrentWindowSelectorBarItem">
31-
<SelectorBarItem.Icon>
32-
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xe923;" />
33-
</SelectorBarItem.Icon>
34-
</SelectorBarItem>
3524
</SelectorBar>
3625
<TextBox Grid.Column="1" Grid.Row="0" Height="32" Width="300" HorizontalAlignment="Right" VerticalContentAlignment="Center" x:Name="SearchBox" HorizontalContentAlignment="Right" TextChanged="SearchBox_TextChanged"></TextBox>
3726
<Button Grid.Column="2" Grid.Row="0" HorizontalContentAlignment="Center">

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/MainWindow.xaml.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,20 @@ public sealed partial class MainWindow : WindowEx
2828
{
2929
private AppWindow _appWindow;
3030

31+
private string[] _currentApplicationIds;
32+
3133
public MainWindow()
3234
{
35+
_currentApplicationIds = YmlInterpreter.GetAllCurrentApplicationIds();
36+
3337
InitializeComponent();
3438

3539
Title = Resource.ResourceManager.GetString("Title", CultureInfo.InvariantCulture)!;
3640

3741
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
3842
WindowId windowId = Win32Interop.GetWindowIdFromWindow(hwnd);
3943
_appWindow = AppWindow.GetFromWindowId(windowId);
40-
#if DEBUG
41-
#else
44+
#if !DEBUG
4245
this.SetIsAlwaysOnTop(true);
4346
this.SetIsShownInSwitchers(false);
4447
#endif
@@ -51,8 +54,7 @@ public MainWindow()
5154
var windowStyle = GetWindowLongW(hwnd, GWL_STYLE);
5255
windowStyle &= ~WS_CAPTION;
5356
_ = SetWindowLongW(hwnd, GWL_STYLE, windowStyle);
54-
#if DEBUG
55-
#else
57+
#if !DEBUG
5658
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
5759
#endif
5860

@@ -89,20 +91,38 @@ private void OnLauched(object sender, WindowActivatedEventArgs e)
8991
_setPosition = true;
9092
}
9193

92-
WindowSelector.SelectedItem = WindowsSelectorBarItem;
94+
if (WindowSelector.Items.Count == 0)
95+
{
96+
foreach (var item in _currentApplicationIds)
97+
{
98+
if (item == YmlInterpreter.GetIndexYamlFile().DefaultShellName)
99+
{
100+
WindowSelector.Items.Insert(0, new SelectorBarItem { Name = item, Text = "Windows", Icon = new FontIcon() { Glyph = "\xE770" } });
101+
}
102+
else
103+
{
104+
try
105+
{
106+
WindowSelector.Items.Add(new SelectorBarItem { Name = item, Text = YmlInterpreter.GetShortcutsOfApplication(item).Name });
107+
}
108+
catch (IOException)
109+
{
110+
}
111+
}
112+
}
113+
}
93114
}
94115

95116
public void WindowSelectionChanged(object sender, SelectorBarSelectionChangedEventArgs e)
96117
{
97-
ShortcutPageParameters.CurrentPageName = ((SelectorBar)sender).SelectedItem.Name switch {
98-
"WindowsSelectorBarItem" => YmlInterpreter.GetIndexYamlFile().DefaultShellName,
99-
"PowerToysSelectorBarItem" => "Microsoft.PowerToys",
100-
_ => throw new NotImplementedException(),
101-
};
118+
ShortcutPageParameters.CurrentPageName = ((SelectorBar)sender).SelectedItem.Name;
102119

103-
ContentFrame.Loaded += (s, e) => ShortcutPageParameters.FrameHeight.OnFrameHeightChanged(ContentFrame.ActualHeight);
120+
ContentFrame.Loaded += (_, _) => ShortcutPageParameters.FrameHeight.OnFrameHeightChanged(ContentFrame.ActualHeight);
104121

105122
ContentFrame.Navigate(typeof(ShortcutView));
123+
124+
// I don't know why this has to be called again, but it does.
125+
ShortcutPageParameters.FrameHeight.OnFrameHeightChanged(ContentFrame.ActualHeight);
106126
}
107127

108128
private bool _setPosition;

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/ShortcutView.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
<RowDefinition Height="*" />
4141
</Grid.RowDefinitions>
4242
<SelectorBar Grid.Row="0" x:Name="CategorySelector" />
43+
<TextBlock x:Name="ErrorMessage" Style="{StaticResource SubheaderTextBlockStyle}" Grid.Row="1" Margin="6,0,6,0"></TextBlock>
4344
<ScrollViewer Grid.Row="1" HorizontalScrollMode="Enabled" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" VerticalScrollMode="Disabled">
4445
<Grid>
4546
<StackPanel Height="{Binding ContentHeight}" Orientation="Horizontal" Grid.Row="1" x:Name="OverviewStackPanel">
46-
<TextBlock x:Name="ErrorMessage" Style="{StaticResource SubheaderTextBlockStyle}" Grid.Row="1" Margin="6,0,6,0"></TextBlock>
4747
<Grid>
4848
<Grid.RowDefinitions>
4949
<RowDefinition Height="Auto" />

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/ShortcutView.xaml.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,23 @@ public void CategorySelector_SelectionChanged(SelectorBar sender, SelectorBarSel
209209
OverviewStackPanel.Visibility = Visibility.Collapsed;
210210
ShortcutListElement.Visibility = Visibility.Visible;
211211

212-
if (int.Parse(sender.SelectedItem.Name, CultureInfo.InvariantCulture) == -1)
212+
try
213213
{
214-
OpenOverview();
215-
return;
216-
}
214+
if (int.Parse(sender.SelectedItem.Name, CultureInfo.InvariantCulture) == -1)
215+
{
216+
OpenOverview();
217+
return;
218+
}
217219

218-
foreach (var shortcut in shortcutList.Shortcuts[int.Parse(sender.SelectedItem.Name, CultureInfo.InvariantCulture)].Properties)
220+
foreach (var shortcut in shortcutList.Shortcuts[int.Parse(sender.SelectedItem.Name, CultureInfo.InvariantCulture)].Properties)
221+
{
222+
ShortcutListElement.Items.Add((ShortcutTemplateDataObject)shortcut);
223+
}
224+
}
225+
catch (NullReferenceException)
219226
{
220-
ShortcutListElement.Items.Add((ShortcutTemplateDataObject)shortcut);
227+
ErrorMessage.Visibility = Visibility.Visible;
228+
ErrorMessage.Text = "Error displaying category";
221229
}
222230
}
223231

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/YmlInterpreter.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Collections.Generic;
7+
using System.ComponentModel;
8+
using System.Diagnostics;
69
using System.IO;
10+
using System.Linq;
11+
using System.Runtime.InteropServices;
12+
using System.Text;
13+
using System.Text.RegularExpressions;
714
using ShortcutGuide.Models;
15+
using Windows.Devices.SmartCards;
16+
using WinUIEx;
817
using YamlDotNet.Serialization;
918

1019
namespace ShortcutGuide
@@ -37,6 +46,69 @@ public static IndexFile GetIndexYamlFile()
3746
return deserializer.Deserialize<IndexFile>(content);
3847
}
3948

49+
public static string[] GetAllCurrentApplicationIds()
50+
{
51+
IntPtr handle = NativeMethods.GetForegroundWindow();
52+
53+
List<string> applicationIds = [];
54+
55+
static bool IsMatch(string input, string filter)
56+
{
57+
string regexPattern = "^" + Regex.Escape(filter).Replace("\\*", ".*") + "$";
58+
return Regex.IsMatch(input, regexPattern);
59+
}
60+
61+
var processes = Process.GetProcesses();
62+
63+
foreach (var item in GetIndexYamlFile().Index.Where((s) => s.BackgroundProcess))
64+
{
65+
try
66+
{
67+
if (processes.Any((p) =>
68+
{
69+
try
70+
{
71+
return IsMatch(p.MainModule!.ModuleName, item.WindowFilter);
72+
}
73+
catch (Win32Exception)
74+
{
75+
return false;
76+
}
77+
}))
78+
{
79+
foreach (var app in item.Apps)
80+
{
81+
applicationIds.Add(app);
82+
}
83+
}
84+
}
85+
catch (InvalidOperationException)
86+
{
87+
}
88+
}
89+
90+
if (NativeMethods.GetWindowThreadProcessId(handle, out uint processId) > 0)
91+
{
92+
string? name = Process.GetProcessById((int)processId).MainModule?.ModuleName;
93+
94+
if (name is not null)
95+
{
96+
try
97+
{
98+
foreach (var item in GetIndexYamlFile().Index.First((s) => !s.BackgroundProcess && IsMatch(name, s.WindowFilter)).Apps)
99+
{
100+
applicationIds.Add(item);
101+
}
102+
}
103+
catch (InvalidOperationException)
104+
{
105+
}
106+
}
107+
}
108+
109+
return [.. applicationIds];
110+
}
111+
40112
public static ShortcutList GetShortcutsOfDefaultShell()
41113
{
42114
return GetShortcutsOfApplication(GetIndexYamlFile().DefaultShellName);

0 commit comments

Comments
 (0)