-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartwizard.cpp
More file actions
1777 lines (1456 loc) · 49.3 KB
/
Copy pathstartwizard.cpp
File metadata and controls
1777 lines (1456 loc) · 49.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
#define _CRT_SECURE_NO_WARNINGS
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "helper_types.h"
#include <set>
#include <windows.h>
#include <iostream>
#include <unicode/unistr.h>
#include <unicode/ustream.h>
#include <filesystem>
#include <chrono>
#include <appmodel.h>
namespace fs = std::filesystem;
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h> // Required for glfwGetWin32Window
#include "text_renderer.h"
#include <shlobj.h>
#include <shobjidl.h>
#include <vector>
#include <algorithm>
#include <cwctype>
#include <shellapi.h>
#include <commctrl.h>
#include <strsafe.h>
#include <uxtheme.h>
#pragma comment(lib, "Shell32.lib")
#pragma comment(lib, "Ole32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "Comctl32.lib")
#pragma comment(lib, "uxtheme.lib")
const float M_PI = 3.141592653589793238;
std::set<UChar32> whitespace = {0x20, 0x09, 0x0A, 0x0D, 0x00A0, 0x2028, 0x2029};
std::set<UChar32> numeric = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};
std::set<UChar32> allowed_in_var_names = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x5F};
std::set<UChar32> punctuationset = {U'!', U'#', U'$', U'%', U'&', U'(', U')', U'*', U'+', U',', U'-', U'.', U'/', U':', U';', U'<', U'=', U'>', U'?', U'@', U'[', U'\\', U']', U'^', U'`', U'{', U'|', U'}', U'~'};
int FIT = 10;
std::wstring darkmode = L"true";
struct ListItem {
int x1;
int x2;
int y1;
int y2;
};
std::unordered_map<std::wstring, std::array<int, 3>> colorMap = {
{L"gray", {255, 255, 255}},
{L"light_blue", {120, 200, 255}},
{L"blue", {60, 100, 255}},
{L"light_orange", {255, 145, 70}},
{L"orange", {255, 105, 50}},
{L"light_green", {144, 255, 144}},
{L"green", {80, 220, 80}},
{L"light_pink", {255, 182, 193}},
{L"pink", {255, 100, 180}},
{L"light_purple", {200, 160, 255}},
{L"purple", {160, 80, 255}},
{L"light_teal", {150, 255, 255}},
{L"teal", {0, 255, 255}}
};
std::vector<ListItem> list_item_positions;
std::string EXE_FOLDER_PATH;
std::wstring CURRENT_EXE_PATH;
NOTIFYICONDATAA nid = { 0 };
#define WM_TRAYICON (WM_USER + 1)
#define WM_MY_TRIGGER (WM_USER + 2)
DWORD threadId;
HHOOK hhkLowLevelKybd = NULL;
bool win_used_in_combo = false;
bool win_down = false;
GLFWwindow* window;
int mouseX = 0;
int mouseY = 0;
bool clicked = false;
int WIN_WIDTH = 100;
int WIN_HEIGHT = 100;
int TRUE_HEIGHT = 0;
int WIN_X = 0;
int WIN_Y = 0;
int RAD_BIG = 1;
int RAD_SMALL = 5;
float FONT_SIZE = 30.0;
const char* FONT_PATH;
bool recalculating = false;
Theme theme;
int border_width = 1;
struct Cursor {
int anchor_char = 0;
int head_char = 0;
};
Cursor curs;
icu::UnicodeString current_search;
struct SubEntry {
HWND hwnd;
icu::UnicodeString name;
};
struct Entry {
icu::UnicodeString name;
std::string name_str;
GLuint tex = 0;
std::wstring exe;
std::wstring aumid;
HWND hwnd = NULL;
std::vector<SubEntry> children;
std::string special = "";
int keeptop = 0;
int matchrank = 0;
bool open = false;
};
struct App {
icu::UnicodeString name;
std::wstring exe;
std::string name_str;
std::wstring aumid;
GLuint textureID;
};
struct WindowInfo {
HWND hwnd;
std::wstring title;
std::wstring exe;
std::wstring aumid;
};
std::vector<Entry> entries;
int selected_id = 0;
int scroll_vert = 0;
double scroll_amnt = 0;
std::vector<App> apps;
bool SaveSetting(const std::wstring& valueName, const std::wstring& data) {
HKEY hKey;
LPCWSTR subkey = L"Software\\StartWizard";
LONG result = RegCreateKeyEx(HKEY_CURRENT_USER, subkey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
if (result == ERROR_SUCCESS) {
result = RegSetValueEx(hKey, valueName.c_str(), 0, REG_SZ, (BYTE*)data.c_str(), (data.length() + 1) * sizeof(wchar_t));
RegCloseKey(hKey);
}
return (result == ERROR_SUCCESS);
}
std::wstring LoadSetting(const std::wstring& valueName, const std::wstring def) {
HKEY hKey;
LPCWSTR subkey = L"Software\\StartWizard";
WCHAR buffer[255];
DWORD bufferSize = sizeof(buffer);
LONG result = RegOpenKeyEx(HKEY_CURRENT_USER, subkey, 0, KEY_READ, &hKey);
if (result == ERROR_SUCCESS) {
result = RegQueryValueEx(hKey, valueName.c_str(), NULL, NULL, (LPBYTE)buffer, &bufferSize);
RegCloseKey(hKey);
}
return (result == ERROR_SUCCESS) ? std::wstring(buffer) : def;
}
void setDarkmode(bool drk) {
if (!drk) {
SaveSetting(L"darkmode", L"false");
darkmode = L"false";
updateFromTintColor(&theme, false);
}else{
SaveSetting(L"darkmode", L"true");
darkmode = L"true";
updateFromTintColor(&theme, true);
}
}
GLuint HBitmapToTexture(HBITMAP hBitmap) {
if (!hBitmap) return 0;
BITMAP bm;
GetObject(hBitmap, sizeof(bm), &bm);
BITMAPINFOHEADER bi = { sizeof(bi), bm.bmWidth, -bm.bmHeight, 1, 32, BI_RGB };
std::vector<uint32_t> pixels(bm.bmWidth * bm.bmHeight);
HDC hdc = GetDC(NULL);
GetDIBits(hdc, hBitmap, 0, bm.bmHeight, pixels.data(), (BITMAPINFO*)&bi, DIB_RGB_COLORS);
ReleaseDC(NULL, hdc);
// Convert BGR (Windows) to RGB (OpenGL) and handle Alpha
bool hasAlpha = false;
for (auto& pixel : pixels) {
uint32_t a = (pixel >> 24) & 0xFF;
uint32_t r = (pixel >> 16) & 0xFF;
uint32_t g = (pixel >> 8) & 0xFF;
uint32_t b = pixel & 0xFF;
if (a > 0) hasAlpha = true;
pixel = (a << 24) | (b << 16) | (g << 8) | r;
}
// If no alpha was found in any pixel, force all pixels to be opaque.
// This handles 24-bit or 32-bit bitmaps where the alpha channel is unused (all zero).
if (!hasAlpha) {
for (auto& pixel : pixels) {
pixel |= 0xFF000000;
}
}
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm.bmWidth, bm.bmHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return textureID;
}
bool launch_exe_detached(const std::wstring& exe) {
SHELLEXECUTEINFOW sei{};
sei.cbSize = sizeof(sei);
sei.fMask = SEE_MASK_NOASYNC;
sei.lpVerb = L"open";
sei.lpFile = exe.c_str();
sei.nShow = SW_SHOWNORMAL;
if (!ShellExecuteExW(&sei)) {
std::wcout << L"ShellExecuteEx failed: " << GetLastError() << L"\n";
return false;
}
return true;
}
bool launch_via_aumid(const std::wstring& aumid) {
IApplicationActivationManager* paam = nullptr;
HRESULT hr = CoCreateInstance(CLSID_ApplicationActivationManager, nullptr,
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&paam));
if (FAILED(hr)) return false;
DWORD pid = 0;
hr = paam->ActivateApplication(aumid.c_str(), nullptr, AO_NONE, &pid);
paam->Release();
if (FAILED(hr)) {
std::cout << "Failed to launch via AUMID\n";
return false;
}
std::cout << "Successfully launched via AUMID\n";
return true;
}
void setTint(std::wstring wstr) {
auto clr = colorMap[wstr];
SaveSetting(L"theme", wstr);
theme.tint_color->r = (float)(clr[0])/255.0;
theme.tint_color->g = (float)(clr[1])/255.0;
theme.tint_color->b = (float)(clr[2])/255.0;
updateFromTintColor(&theme, darkmode == L"true");
}
bool launch_app(const Entry& entry) {
std::cout << "Launch\n";
if (!entry.special.empty()) {
std::cout << "Special\n";
if (entry.special == "set_darkmode: true") {
setDarkmode(true);
}else if (entry.special == "set_darkmode: false") {
setDarkmode(false);
}else if (entry.special.substr(0, 11) == "set_theme: ") {
std::string color = entry.special.substr(11);
std::wstring wstr(color.begin(), color.end());
setTint(wstr);
}else{
SetClipboardText(entry.special);
current_search = icu::UnicodeString::fromUTF8(entry.special);
recalculating = true;
curs.head_char = current_search.length();
curs.anchor_char = curs.head_char;
}
return false;
} else if (entry.hwnd != NULL) {
std::cout << "Moving up\n";
if (IsIconic(entry.hwnd)) {
ShowWindow(entry.hwnd, SW_RESTORE);
}
SetForegroundWindow(entry.hwnd);
return true;
}
std::cout << "Launching: " << std::string(entry.exe.begin(), entry.exe.end()) << "\n";
if (!entry.exe.empty()) {
std::cout << "Launch exe " << std::string(entry.exe.begin(), entry.exe.end()) << "\n";
if (launch_exe_detached(entry.exe)) {
std::cout << "Successfully launched detached\n";
return true;
}
std::cout << "Exe launch failed, trying AUMID\n";
}
if (!entry.aumid.empty()) {
std::cout << "Launch aumid " << std::string(entry.aumid.begin(), entry.aumid.end()) << "\n";
return launch_via_aumid(entry.aumid);
}
return false;
}
std::wstring normalizePath(std::wstring path) {
if (path.empty()) return path;
// Trim leading/trailing whitespace
while (!path.empty() && std::iswspace(path.back())) path.pop_back();
size_t start = 0;
while (start < path.length() && std::iswspace(path[start])) start++;
if (start > 0) path = path.substr(start);
if (path.empty()) return path;
// 1. Standardize slashes
std::replace(path.begin(), path.end(), L'/', L'\\');
// 2. Remove \\?\ prefix if present
if (path.length() >= 4 && path.substr(0, 4) == L"\\\\?\\") {
path = path.substr(4);
}
// 3. Get Full Path
WCHAR fullPath[MAX_PATH];
DWORD ret = GetFullPathNameW(path.c_str(), MAX_PATH, fullPath, nullptr);
if (ret > 0 && ret < MAX_PATH) {
path = fullPath;
}
// 4. Get Long Path (handles 8.3 names)
WCHAR longPath[MAX_PATH];
ret = GetLongPathNameW(path.c_str(), longPath, MAX_PATH);
if (ret > 0 && ret < MAX_PATH) {
path = longPath;
}
return path;
}
std::wstring GetWindowAUMID(HWND hwnd) {
IPropertyStore* pps = nullptr;
if (FAILED(SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&pps)))) return L"";
PROPERTYKEY PKEY_AUMI = { {0x9F4C2855,0x9F79,0x4B39,{0xA8,0xD0,0xE1,0xD4,0x2D,0xE1,0xD5,0xF3}}, 5 };
PROPVARIANT pv;
PropVariantInit(&pv);
std::wstring result;
if (SUCCEEDED(pps->GetValue(PKEY_AUMI, &pv)) && pv.vt == VT_LPWSTR) {
result = pv.pwszVal;
}
PropVariantClear(&pv);
pps->Release();
return result;
}
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
if (!IsWindowVisible(hwnd)) return TRUE;
WCHAR title[256];
GetWindowTextW(hwnd, title, 256);
if (wcslen(title) == 0) return TRUE;
DWORD processId;
GetWindowThreadProcessId(hwnd, &processId);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId);
std::wstring exePath;
if (hProcess) {
WCHAR buffer[MAX_PATH];
DWORD size = MAX_PATH;
if (QueryFullProcessImageNameW(hProcess, 0, buffer, &size)) {
exePath = normalizePath(buffer);
}
CloseHandle(hProcess);
}
std::wstring aumid = GetWindowAUMID(hwnd);
if (!exePath.empty() || !aumid.empty()) {
std::vector<WindowInfo>* windows = reinterpret_cast<std::vector<WindowInfo>*>(lParam);
windows->push_back({ hwnd, title, exePath, aumid });
}
return TRUE;
}
std::vector<WindowInfo> EnumerateOpenWindows() {
std::vector<WindowInfo> windows;
EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(&windows));
return windows;
}
std::wstring ResolveAUMIDPath(const std::wstring& aumid) {
// Matches {GUID}\some\path.exe style AUMIDs
if (aumid.empty() || aumid[0] != L'{') return L"";
size_t close = aumid.find(L'}');
if (close == std::wstring::npos) return L"";
std::wstring guidStr = aumid.substr(1, close - 1);
std::wstring rest = aumid.substr(close + 2);
GUID guid;
if (FAILED(CLSIDFromString((L"{" + guidStr + L"}").c_str(), &guid))) return L"";
PWSTR folderPath = nullptr;
if (FAILED(SHGetKnownFolderPath(guid, 0, nullptr, &folderPath))) return L"";
std::wstring result = std::wstring(folderPath) + L"\\" + rest;
CoTaskMemFree(folderPath);
return normalizePath(result);
}
void GetAllApps() {
for (auto& app : apps) {
if (app.textureID != 0) {
glDeleteTextures(1, &app.textureID);
}
}
apps.clear();
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (FAILED(hr)) return;
PROPERTYKEY PKEY_AUMI = { {0x9F4C2855,0x9F79,0x4B39,{0xA8,0xD0,0xE1,0xD4,0x2D,0xE1,0xD5,0xF3}}, 5 };
std::set<std::string> seenNames = {"Windows Software Development Kit"};
// --- Pass 1: .lnk scan (reliable exe paths for Win32 apps) ---
std::vector<std::wstring> startMenuPaths;
PWSTR path = nullptr;
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_CommonPrograms, 0, NULL, &path))) {
startMenuPaths.push_back(path);
CoTaskMemFree(path);
}
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Programs, 0, NULL, &path))) {
startMenuPaths.push_back(path);
CoTaskMemFree(path);
}
for (const auto& basePath : startMenuPaths) {
if (!fs::exists(basePath)) continue;
std::error_code ec;
for (const auto& entry : fs::recursive_directory_iterator(basePath, ec)) {
if (ec) continue;
if (!entry.is_regular_file() || entry.path().extension() != ".lnk") continue;
std::wstring name_ws = entry.path().stem().wstring();
std::string name_str;
icu::UnicodeString(name_ws.c_str()).toUTF8String(name_str);
if (seenNames.count(name_str)) continue;
App a;
a.exe = normalizePath(entry.path().wstring());
a.name = icu::UnicodeString(name_ws.c_str());
a.name_str = name_str;
IShellLinkW* psl = nullptr;
if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&psl)))) {
IPersistFile* ppf = nullptr;
if (SUCCEEDED(psl->QueryInterface(IID_PPV_ARGS(&ppf)))) {
if (SUCCEEDED(ppf->Load(a.exe.c_str(), STGM_READ))) {
WCHAR szGotPath[MAX_PATH];
if (SUCCEEDED(psl->GetPath(szGotPath, MAX_PATH, NULL, SLGP_UNCPRIORITY | SLGP_RAWPATH))) {
WCHAR szExpandedPath[MAX_PATH];
if (ExpandEnvironmentStringsW(szGotPath, szExpandedPath, MAX_PATH) > 0) {
a.exe = normalizePath(szExpandedPath);
} else {
a.exe = normalizePath(szGotPath);
}
}
IPropertyStore* pps = nullptr;
if (SUCCEEDED(SHGetPropertyStoreFromParsingName(
entry.path().wstring().c_str(), nullptr, GPS_DEFAULT, IID_PPV_ARGS(&pps)))) {
PROPVARIANT pv;
PropVariantInit(&pv);
if (SUCCEEDED(pps->GetValue(PKEY_AUMI, &pv)) && pv.vt == VT_LPWSTR) {
a.aumid = pv.pwszVal;
}
PropVariantClear(&pv);
pps->Release();
}
}
ppf->Release();
}
psl->Release();
}
if (a.exe == CURRENT_EXE_PATH) {
seenNames.insert(name_str);
continue;
}
IShellItem* pItem = nullptr;
if (SUCCEEDED(SHCreateItemFromParsingName(a.exe.c_str(), NULL, IID_PPV_ARGS(&pItem)))) {
IShellItemImageFactory* pImageFactory = nullptr;
if (SUCCEEDED(pItem->QueryInterface(IID_PPV_ARGS(&pImageFactory)))) {
int iconSize = TextRenderer::get_text_height();
if (iconSize <= 0) iconSize = 32;
SIZE size = { iconSize, iconSize };
HBITMAP hBitmap;
if (SUCCEEDED(pImageFactory->GetImage(size, SIIGBF_ICONONLY, &hBitmap))) {
a.textureID = HBitmapToTexture(hBitmap);
DeleteObject(hBitmap);
} else {
a.textureID = 0;
}
pImageFactory->Release();
}
pItem->Release();
}
seenNames.insert(name_str);
apps.push_back(a);
}
}
// --- Pass 2: FOLDERID_AppsFolder (catches UWP/MSIX apps missing from .lnk scan) ---
IShellItem* pAppsFolder = nullptr;
if (SUCCEEDED(SHGetKnownFolderItem(FOLDERID_AppsFolder, KF_FLAG_DEFAULT, nullptr, IID_PPV_ARGS(&pAppsFolder)))) {
IEnumShellItems* pEnum = nullptr;
if (SUCCEEDED(pAppsFolder->BindToHandler(nullptr, BHID_EnumItems, IID_PPV_ARGS(&pEnum)))) {
IShellItem* pItem = nullptr;
while (pEnum->Next(1, &pItem, nullptr) == S_OK) {
App a;
LPWSTR pName = nullptr;
if (SUCCEEDED(pItem->GetDisplayName(SIGDN_NORMALDISPLAY, &pName))) {
a.name = icu::UnicodeString(pName);
a.name.toUTF8String(a.name_str);
CoTaskMemFree(pName);
}
if (a.name_str.empty() || seenNames.count(a.name_str)) {
pItem->Release();
continue;
}
IPropertyStore* pps = nullptr;
if (SUCCEEDED(pItem->BindToHandler(nullptr, BHID_PropertyStore, IID_PPV_ARGS(&pps)))) {
PROPVARIANT pv;
PropVariantInit(&pv);
if (SUCCEEDED(pps->GetValue(PKEY_AUMI, &pv)) && pv.vt == VT_LPWSTR) {
a.aumid = pv.pwszVal;
}
PropVariantClear(&pv);
pps->Release();
}
LPWSTR pPath = nullptr;
if (SUCCEEDED(pItem->GetDisplayName(SIGDN_FILESYSPATH, &pPath))) {
a.exe = normalizePath(pPath);
CoTaskMemFree(pPath);
}
if (a.exe.empty()) {
a.exe = ResolveAUMIDPath(a.aumid);
}
if (a.exe == CURRENT_EXE_PATH) {
seenNames.insert(a.name_str);
pItem->Release();
continue;
}
IShellItemImageFactory* pImageFactory = nullptr;
if (SUCCEEDED(pItem->QueryInterface(IID_PPV_ARGS(&pImageFactory)))) {
int iconSize = TextRenderer::get_text_height();
if (iconSize <= 0) iconSize = 32;
SIZE size = { iconSize, iconSize };
HBITMAP hBitmap;
if (SUCCEEDED(pImageFactory->GetImage(size, SIIGBF_ICONONLY, &hBitmap))) {
a.textureID = HBitmapToTexture(hBitmap);
DeleteObject(hBitmap);
}
pImageFactory->Release();
}
seenNames.insert(a.name_str);
apps.push_back(a);
pItem->Release();
}
pEnum->Release();
}
pAppsFolder->Release();
}
CoUninitialize();
}
bool fuzzySearch(std::string inraw, std::string find) {
std::string srch = "";
std::string in = toLower(inraw);
for (char c : find) {
if (c == ' ' || c == '.' || c == ',' || c == '_') {
if (in.find(srch) == std::string::npos) {
return false;
}
srch = "";
}else{
srch += c;
}
}
if (srch != "") {
if (in.find(srch) == std::string::npos) {
return false;
}
}
return true;
}
bool equalsIgnoreCase(const std::wstring& wa, const std::wstring& wb) {
std::string a(wa.begin(), wa.end());
std::string b(wb.begin(), wb.end());
return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin(), [](wchar_t charA, wchar_t charB) {
return towlower(charA) == towlower(charB);
});
}
std::wstring GetPackageFamilyFromExePath(const std::wstring& exePath) {
// Extract folder name from WindowsApps path
// Path looks like: C:\Program Files\WindowsApps\<PackageFullName>\foo.exe
const std::wstring marker = L"\\WindowsApps\\";
size_t start = exePath.find(marker);
if (start == std::wstring::npos) return L"";
start += marker.length();
size_t end = exePath.find(L'\\', start);
if (end == std::wstring::npos) return L"";
std::wstring packageFullName = exePath.substr(start, end - start);
WCHAR familyName[PACKAGE_FAMILY_NAME_MAX_LENGTH + 1] = {};
UINT32 len = PACKAGE_FAMILY_NAME_MAX_LENGTH + 1;
if (PackageFamilyNameFromFullName(packageFullName.c_str(), &len, familyName) == ERROR_SUCCESS) {
return familyName;
}
return L"";
}
std::wstring GetFamilyFromAUMID(const std::wstring& aumid) {
size_t bang = aumid.find(L'!');
if (bang == std::wstring::npos) return aumid;
return aumid.substr(0, bang);
}
void recalculate() {
entries.clear();
selected_id = 0;
scroll_vert = 0;
std::string find;
current_search.toUTF8String(find);
find = toLower(find);
auto res = calcExpression(current_search);
if (res.first){
Entry e;
e.name = doubleToUnicodeString_pretty(res.second);
e.name.toUTF8String(e.special);
e.keeptop = 2;
entries.push_back(e);
}
auto openWindows = EnumerateOpenWindows();
for (const auto& app : apps) {
bool matchfrst = fuzzySearch(app.name_str, find);
bool matchsecond = fuzzySearch(std::string(app.exe.begin(), app.exe.end()), find);
if (matchfrst || matchsecond) {
Entry e;
e.name = app.name;
e.name_str = app.name_str;
e.exe = app.exe;
e.tex = app.textureID;
e.aumid = app.aumid;
if (matchfrst) {
e.matchrank = 1;
}
for (auto win : openWindows) {
bool exeMatch = !win.exe.empty() && equalsIgnoreCase(win.exe, app.exe);
bool aumidMatch = !win.aumid.empty() && !app.aumid.empty() && equalsIgnoreCase(win.aumid, app.aumid);
bool pkgMatch = !app.aumid.empty() && !win.exe.empty()
&& equalsIgnoreCase(GetPackageFamilyFromExePath(win.exe),
GetFamilyFromAUMID(app.aumid));
bool exWinaumApp = !win.exe.empty() && equalsIgnoreCase(win.exe, app.aumid);
bool exAppaumWin = !win.aumid.empty()&& equalsIgnoreCase(win.aumid, app.exe);
if (exeMatch || aumidMatch || pkgMatch || exWinaumApp || exAppaumWin) {
e.children.push_back({win.hwnd, icu::UnicodeString::fromUTF8(std::string(win.title.begin(), win.title.end()))});
}
}
entries.push_back(e);
}
}
if (!find.empty() && find[0] == ':'){
find = find.substr(1);
std::vector<std::pair<std::string, std::string>> prs = {{":Set Dark Mode", "set_darkmode: true"}, {":Set Light Mode", "set_darkmode: false"}, {":Set Theme Gray", "set_theme: gray"}, {":Set Theme Light Blue", "set_theme: light_blue"}, {":Set Theme Blue", "set_theme: blue"}, {":Set Theme Light Orange", "set_theme: light_orange"}, {":Set Theme Orange", "set_theme: orange"}, {":Set Theme Light Green", "set_theme: light_green"}, {":Set Theme Green", "set_theme: green"}, {":Set Theme Light Pink", "set_theme: light_pink"}, {":Set Theme Pink", "set_theme: pink"}, {":Set Theme Light Purple", "set_theme: light_purple"}, {":Set Theme Purple", "set_theme: purple"}, {":Set Theme Light Teal", "set_theme: light_teal"}, {":Set Theme Teal", "set_theme: teal"}};
for (std::pair<std::string, std::string> pr : prs) {
if (fuzzySearch(pr.first, find)) {
Entry e;
e.name = icu::UnicodeString::fromUTF8(pr.first);
e.name_str = pr.first;
e.special = pr.second;
e.keeptop = 1;
entries.push_back(e);
}
}
}
std::sort(entries.begin(), entries.end(), [](const Entry& a, const Entry& b) {
if (a.keeptop != b.keeptop) {
return a.keeptop > b.keeptop;
}
if (a.children.size() != b.children.size()) {
return a.children.size() > b.children.size();
}
if (a.matchrank != b.matchrank) {
return a.matchrank>b.matchrank;
}
return a.name_str < b.name_str;
});
}
auto drawCornerEdge = [](float cx, float cy, float startAngle, float endAngle, int segments, double radius, int edgewidth) {
double smallerradius = radius-edgewidth;
glBegin(GL_QUAD_STRIP);
for (int i = 0; i <= segments; ++i) {
float t = (float)i / (float)segments;
float theta = startAngle + t * (endAngle - startAngle);
glVertex2f(cx + std::cos(theta) * radius, cy + std::sin(theta) * radius);
glVertex2f(cx + std::cos(theta) * smallerradius, cy + std::sin(theta) * smallerradius);
}
glEnd();
};
auto drawCorner = [](float cx, float cy, float startAngle, float endAngle, int segments, double radius) {
glBegin(GL_TRIANGLE_FAN);
glVertex2f(cx, cy);
for (int i = 0; i <= segments; ++i) {
float t = (float)i / (float)segments;
float theta = startAngle + t * (endAngle - startAngle);
glVertex2f(cx + std::cos(theta) * radius, cy + std::sin(theta) * radius);
}
glEnd();
};
void DrawRect(int x, int y, int w, int h, Color* color) {
glColor4f(color->r, color->g, color->b, color->a);
glBegin(GL_QUADS);
glVertex2f(x, y); // top-left
glVertex2f(x+w, y); // top-right
glVertex2f(x+w, y+h); // bottom-right
glVertex2f(x, y+h); // bottom-left
glEnd();
}
void DrawRoundBorder(int x, int y, int w, int h, Color* color, int segments, double radius) {
glColor4f(color->r, color->g, color->b, color->a);
drawCornerEdge(x + radius, y + radius, M_PI, 1.5f * M_PI, segments, radius, border_width); // BL
drawCornerEdge(x + w - radius, y + radius, 1.5f * M_PI, 2.0f * M_PI, segments, radius, border_width); // BR
drawCornerEdge(x + w - radius, y + h - radius, 0.0f, 0.5f * M_PI, segments, radius, border_width); // TR
drawCornerEdge(x + radius, y + h - radius, 0.5f * M_PI, M_PI, segments, radius, border_width); // TL
DrawRect(x+radius, y, w-radius*2, border_width, color);
DrawRect(x+radius, y+h-border_width, w-radius*2, border_width, color);
DrawRect(x, y+radius, border_width, h-radius*2, color);
DrawRect(x+w-border_width, y+radius, border_width, h-radius*2, color);
}
void DrawRoundedRect(float x, float y, float w, float h, float radius, Color* color, Color* bcolor, int segments) {
glColor4f(color->r, color->g, color->b, color->a);
glBegin(GL_QUADS);
// Center
glVertex2f(x + radius, y);
glVertex2f(x + w - radius, y);
glVertex2f(x + w - radius, y + h);
glVertex2f(x + radius, y + h);
// Left strip
glVertex2f(x, y + radius);
glVertex2f(x + radius, y + radius);
glVertex2f(x + radius, y + h - radius);
glVertex2f(x, y + h - radius);
// Right strip
glVertex2f(x + w - radius, y + radius);
glVertex2f(x + w, y + radius);
glVertex2f(x + w, y + h - radius);
glVertex2f(x + w - radius, y + h - radius);
glEnd();
// Draw the four quartercircles
drawCorner(x + radius, y + radius, M_PI, 1.5f * M_PI, segments, radius); // BL
drawCorner(x + w - radius, y + radius, 1.5f * M_PI, 2.0f * M_PI, segments, radius); // BR
drawCorner(x + w - radius, y + h - radius, 0.0f, 0.5f * M_PI, segments, radius); // TR
drawCorner(x + radius, y + h - radius, 0.5f * M_PI, M_PI, segments, radius); // TL
if (bcolor != nullptr) {
DrawRoundBorder(x, y, w, h, bcolor, segments, radius);
}
}
void DrawRoundBorder(int x, int y, int w, int h, Color* color, int segments,
float rTL, float rTR, float rBR, float rBL) {
glColor4f(color->r, color->g, color->b, color->a);
drawCornerEdge(x + w - rTR, y + rTR, 1.5f * M_PI, 2.0f * M_PI, segments, rTR, border_width); // TR
drawCornerEdge(x + w - rBR, y + h - rBR, 0.0f, 0.5f * M_PI, segments, rBR, border_width); // BR
drawCornerEdge(x + rBL, y + h - rBL, 0.5f * M_PI, 1.0f * M_PI, segments, rBL, border_width); // BL
drawCornerEdge(x + rTL, y + rTL, 1.0f * M_PI, 1.5f * M_PI, segments, rTL, border_width); // TL
DrawRect(x + rTL, y, w - rTL - rTR, border_width, color); // Top edge
DrawRect(x + rBL, y + h - border_width, w - rBL - rBR, border_width, color); // Bottom edge
DrawRect(x, y + rTL, border_width, h - rTL - rBL, color); // Left edge
DrawRect(x + w - border_width, y + rTR, border_width, h - rTR - rBR, color); // Right edge
}
void DrawRoundedRect(float x, float y, float w, float h, Color* color, Color* bcolor,
int segments, float rTL, float rTR, float rBR, float rBL) {
glColor4f(color->r, color->g, color->b, color->a);
glBegin(GL_QUADS);
float maxTop = (rTL > rTR) ? rTL : rTR;
float maxBottom = (rBL > rBR) ? rBL : rBR;
glVertex2f(x, y + maxTop);
glVertex2f(x + w, y + maxTop);
glVertex2f(x + w, y + h - maxBottom);
glVertex2f(x, y + h - maxBottom);
glVertex2f(x + rTL, y);
glVertex2f(x + w - rTR, y);
glVertex2f(x + w - rTR, y + maxTop);
glVertex2f(x + rTL, y + maxTop);
glVertex2f(x + rBL, y + h - maxBottom);
glVertex2f(x + w - rBR, y + h - maxBottom);
glVertex2f(x + w - rBR, y + h);
glVertex2f(x + rBL, y + h);
glEnd();
drawCorner(x + w - rTR, y + rTR, 1.5f * M_PI, 2.0f * M_PI, segments, rTR); // TR
drawCorner(x + w - rBR, y + h - rBR, 0.0f, 0.5f * M_PI, segments, rBR); // BR
drawCorner(x + rBL, y + h - rBL, 0.5f * M_PI, 1.0f * M_PI, segments, rBL); // BL
drawCorner(x + rTL, y + rTL, 1.0f * M_PI, 1.5f * M_PI, segments, rTL); // TL
if (bcolor != nullptr) {
DrawRoundBorder(x, y, w, h, bcolor, segments, rTL, rTR, rBR, rBL);
}
}
void DrawTexturedRect(float x, float y, float w, float h, GLuint textureID) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(x, y);
glTexCoord2f(1, 0); glVertex2f(x + w, y);
glTexCoord2f(1, 1); glVertex2f(x + w, y + h);
glTexCoord2f(0, 1); glVertex2f(x, y + h);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void render() {
int sep = RAD_SMALL/2;
int top_h = WIN_HEIGHT / 5 - sep*2;
int remaining = WIN_HEIGHT - top_h - sep*2;
int lstTotal = (remaining/FIT);
int indiv = lstTotal - sep;
int bottomRad = RAD_SMALL+sep;
int topRad = RAD_BIG+sep;
if (entries.size() == 0) {
bottomRad = topRad;
}
int HEIGHT = sep*2 + top_h + lstTotal*fmin(FIT, entries.size());
DrawRoundedRect(0, 0, WIN_WIDTH, HEIGHT, theme.extras_background_color, theme.border, 15, topRad, topRad, bottomRad, bottomRad);
DrawRoundedRect(sep, sep, WIN_WIDTH-sep*2, top_h, RAD_BIG, theme.main_background_color, theme.border, 15);
TRUE_HEIGHT = HEIGHT;
glEnable(GL_SCISSOR_TEST);
glScissor(sep, 0, WIN_WIDTH-2*sep, WIN_HEIGHT);
int TextH = TextRenderer::get_text_height();
int texty = (top_h - TextH) / 2 + sep;
int cursorWidth = TextRenderer::get_text_width(1) * 0.2;
int cursor_offset = TextRenderer::get_text_width(curs.head_char);
if (curs.anchor_char != curs.head_char) {
int anch_off = TextRenderer::get_text_width(curs.anchor_char);
DrawRect(texty+sep+fmin(cursor_offset, anch_off), texty, fabs(anch_off-cursor_offset), TextH, theme.hover_background_color);
}
if (current_search.length() == 0) {
auto now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
std::tm* local_tm = std::localtime(&now_c);
int hour = local_tm->tm_hour;
std::string greeting = "";
if (hour < 12) {
greeting = "Good Morning, Boss";
}else if (hour < 19) {
greeting = "Good Afternoon, Boss";
}else {
greeting = "Good Evening, Boss";
}
TextRenderer::draw_text(texty+sep, texty, icu::UnicodeString::fromUTF8(greeting), theme.lesser_text_color);
}else{
TextRenderer::draw_text(texty+sep, texty, current_search, theme.main_text_color);
}
DrawRect(texty+cursor_offset+sep, texty, cursorWidth, TextH, theme.main_text_color);
int offsety = (indiv-TextRenderer::get_text_height())/2;
int indent = 4*sep;
for (int i = scroll_vert; i < fmin(scroll_vert + FIT, entries.size()); i++) {
Color* back = theme.main_background_color;
Color* txt = theme.main_text_color;
if (i == selected_id) {
back = theme.main_text_color;
txt = theme.darker_background_color;
}
auto e = entries[i];
int y = top_h+sep*2 + lstTotal*(i-scroll_vert);
if (e.hwnd != NULL) {
int x1 = sep+indent;
int width = WIN_WIDTH-indent-sep*2;
DrawRoundedRect(x1, y, width, indiv, RAD_SMALL, back, theme.border, 5);
list_item_positions[i-scroll_vert].x1 = x1;
list_item_positions[i-scroll_vert].x2 = x1+width;
}else{
int width = WIN_WIDTH-sep*2;
DrawRoundedRect(sep, y, width, indiv, RAD_SMALL, back, theme.border, 5);
list_item_positions[i-scroll_vert].x1 = sep;
list_item_positions[i-scroll_vert].x2 = sep+width;
}
list_item_positions[i-scroll_vert].y1 = y - sep/2;
list_item_positions[i-scroll_vert].y2 = y+indiv + sep/2;
int textX;
if (e.hwnd != NULL) {