Skip to content

Commit 46df486

Browse files
committed
Refactoring and localisation
1 parent 7596e96 commit 46df486

File tree

18 files changed

+296
-353
lines changed

18 files changed

+296
-353
lines changed

src/modules/ShortcutGuide/ShortcutGuide/pch.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#pragma once
22
#define NOMINMAX
3+
#include <vector>
34
#include <winrt/base.h>
5+
#include <UIAutomation.h>
6+
#include <windows.h>
47
#include <winrt/Windows.Foundation.h>
58
#include <winrt/Windows.Foundation.Collections.h>
6-
#include <Windows.h>
79
#include <dxgi1_3.h>
810
#include <d3d11_2.h>
911
#include <d2d1_3.h>
@@ -24,5 +26,4 @@
2426
#include <stdexcept>
2527
#include <tuple>
2628
#include <unordered_set>
27-
#include <string>
2829
#include <filesystem>

src/modules/ShortcutGuide/ShortcutGuide/tasklist_positions.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
#include "pch.h"
22
#include "tasklist_positions.h"
3-
#include <windows.h>
4-
#include <vector>
5-
#include <winrt/base.h>
6-
#include <UIAutomation.h>
3+
4+
// Tried my hardest adapting this to C#, but FindWindowW didn't work properly in C#. ~Noraa Junker
75

86
extern "C"
97
{
10-
winrt::com_ptr<IUIAutomation> automation;
11-
winrt::com_ptr<IUIAutomationElement> element;
12-
winrt::com_ptr<IUIAutomationCondition> true_condition;
13-
14-
// Helper to get the taskbar HWND for the monitor under the cursor
158
HWND GetTaskbarHwndForCursorMonitor(HMONITOR monitor)
169
{
1710
POINT pt;
@@ -124,7 +117,7 @@ extern "C"
124117
child = nullptr;
125118
if (elements->GetElement(i, child.put()) < 0)
126119
return false;
127-
TasklistButton button;
120+
TasklistButton button = {};
128121
if (VARIANT var_rect; child->GetCurrentPropertyValue(UIA_BoundingRectanglePropertyId, &var_rect) >= 0)
129122
{
130123
if (var_rect.vt == (VT_R8 | VT_ARRAY))
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
#pragma once
2-
#include <vector>
3-
#include <unordered_set>
4-
#include <string>
5-
#include <Windows.h>
6-
#include <UIAutomationClient.h>
72

83
struct TasklistButton
94
{
@@ -14,3 +9,15 @@ struct TasklistButton
149
int height;
1510
int keynum;
1611
};
12+
13+
extern "C"
14+
{
15+
winrt::com_ptr<IUIAutomation> automation;
16+
winrt::com_ptr<IUIAutomationElement> element;
17+
winrt::com_ptr<IUIAutomationCondition> true_condition;
18+
19+
// Helper to get the taskbar HWND for the monitor under the cursor
20+
HWND GetTaskbarHwndForCursorMonitor(HMONITOR monitor);
21+
__declspec(dllexport) bool update_buttons(std::vector<TasklistButton>& buttons);
22+
__declspec(dllexport) TasklistButton* get_buttons(HMONITOR monitor, int* size);
23+
}

src/modules/ShortcutGuideV2/ShortcutGuide.IndexYmlGenerator/ShortcutGuide.IndexYmlGenerator/IndexYmlGenerator.cs

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

55
using System.Collections.Generic;
66
using System.IO;
7+
using ShortcutGuide.Helpers;
78
using ShortcutGuide.Models;
89
using YamlDotNet.Serialization;
910

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

Lines changed: 0 additions & 88 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using Windows.Foundation;
7+
using WinUIEx;
8+
9+
namespace ShortcutGuide.Helpers
10+
{
11+
public static partial class DisplayHelper
12+
{
13+
private enum MonitorFromWindowDwFlags : int
14+
{
15+
MONITOR_DEFAULTTONEAREST = 2,
16+
}
17+
18+
public static Rect GetWorkAreaForDisplayWithWindow(nint hwnd)
19+
{
20+
foundMonitorIndex = -1;
21+
monitorIndex = 0;
22+
var monitor = NativeMethods.MonitorFromWindow(hwnd, (int)MonitorFromWindowDwFlags.MONITOR_DEFAULTTONEAREST);
23+
NativeMethods.EnumDisplayMonitors(nint.Zero, nint.Zero, MonitorEnumProc, new NativeMethods.LPARAM(monitor));
24+
return MonitorInfo.GetDisplayMonitors()[foundMonitorIndex].RectWork;
25+
}
26+
27+
private static int foundMonitorIndex = -1;
28+
private static int monitorIndex;
29+
30+
private static bool MonitorEnumProc(nint hMonitor, nint hdcMonitor, ref NativeMethods.RECT lprcMonitor, nint dwData)
31+
{
32+
nint targetMonitor = dwData;
33+
34+
if (hMonitor == targetMonitor)
35+
{
36+
foundMonitorIndex = monitorIndex;
37+
return false;
38+
}
39+
40+
monitorIndex++;
41+
return true;
42+
}
43+
}
44+
}

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/DpiHelper.cs renamed to src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/DpiHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// The Microsoft Corporation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
namespace ShortcutGuide
5+
namespace ShortcutGuide.Helpers
66
{
77
// This class is rewritten from C++ to C# from the measure tool project
88
internal static partial class DpiHelper

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ManifestInterpreter.cs renamed to src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/ManifestInterpreter.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using ShortcutGuide.Models;
1414
using YamlDotNet.Serialization;
1515

16-
namespace ShortcutGuide
16+
namespace ShortcutGuide.Helpers
1717
{
1818
public class ManifestInterpreter
1919
{
@@ -26,17 +26,11 @@ public static ShortcutFile GetShortcutsOfApplication(string applicationName)
2626
IEnumerable<string> files = Directory.EnumerateFiles(path, applicationName + ".*.yml") ??
2727
throw new FileNotFoundException($"The file for the application '{applicationName}' was not found in '{path}'.");
2828

29-
if (files.Any(f => f.EndsWith($".{Language}.yml", StringComparison.InvariantCulture)))
30-
{
31-
return YamlToShortcutList(File.ReadAllText(Path.Combine(path, applicationName + $".{Language}.yml")));
32-
}
33-
34-
if (files.Any(f => f.EndsWith(".en-US.yml", StringComparison.InvariantCulture)))
35-
{
36-
return YamlToShortcutList(File.ReadAllText(files.First(f => f.EndsWith(".en-US.yml", StringComparison.InvariantCulture))));
37-
}
38-
39-
throw new FileNotFoundException($"The file for the application '{applicationName}' was not found in '{path}' with the language '{Language}' or 'en-US'.");
29+
return files.Any(f => f.EndsWith($".{Language}.yml", StringComparison.InvariantCulture))
30+
? YamlToShortcutList(File.ReadAllText(Path.Combine(path, applicationName + $".{Language}.yml")))
31+
: files.Any(f => f.EndsWith(".en-US.yml", StringComparison.InvariantCulture))
32+
? YamlToShortcutList(File.ReadAllText(files.First(f => f.EndsWith(".en-US.yml", StringComparison.InvariantCulture))))
33+
: throw new FileNotFoundException($"The file for the application '{applicationName}' was not found in '{path}' with the language '{Language}' or 'en-US'.");
4034
}
4135

4236
public static ShortcutFile YamlToShortcutList(string content)
@@ -60,7 +54,7 @@ public static IndexFile GetIndexYamlFile()
6054

6155
public static string[] GetAllCurrentApplicationIds()
6256
{
63-
IntPtr handle = NativeMethods.GetForegroundWindow();
57+
nint handle = NativeMethods.GetForegroundWindow();
6458

6559
List<string> applicationIds = [];
6660

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/PowerToysShortcutsPopulator.cs renamed to src/modules/ShortcutGuideV2/ShortcutGuide.Ui/Helpers/PowerToysShortcutsPopulator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.IO;
88
using System.Text.RegularExpressions;
99
using Microsoft.PowerToys.Settings.UI.Library;
10+
using ShortcutGuide.Helpers;
1011

1112
namespace ShortcutGuide
1213
{
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.InteropServices;
7+
using TasklistButton = NativeMethods.TasklistButton;
8+
9+
namespace ShortcutGuide.Helpers
10+
{
11+
internal sealed partial class TasklistPositions
12+
{
13+
public static TasklistButton[] GetButtons()
14+
{
15+
var monitor = NativeMethods.MonitorFromWindow(MainWindow.WindowHwnd, 0);
16+
nint ptr = NativeMethods.GetTasklistButtons(monitor, out int size);
17+
if (ptr == nint.Zero)
18+
{
19+
return [];
20+
}
21+
22+
if (size <= 0)
23+
{
24+
return [];
25+
}
26+
27+
TasklistButton[] buttons = new TasklistButton[size];
28+
nint currentPtr = ptr;
29+
for (int i = 0; i < size; i++)
30+
{
31+
buttons[i] = Marshal.PtrToStructure<TasklistButton>(currentPtr);
32+
currentPtr += Marshal.SizeOf<TasklistButton>();
33+
}
34+
35+
return buttons;
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)