-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppSetup.cpp
More file actions
784 lines (678 loc) · 27.2 KB
/
Copy pathAppSetup.cpp
File metadata and controls
784 lines (678 loc) · 27.2 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
#include "AppSetup.h"
#include "AppLog.h"
#include "CommonUtil.h"
#include "ImageCore/ImageDecodeDispatcher.h"
#include "IniStore.h"
#include "framework.h"
#include <filesystem>
#include <format>
#include <vector>
#include <shlobj.h>
#include <shellapi.h>
#include <winreg.h>
namespace
{
std::wstring BuildExtensionPromptLine(const std::vector<std::wstring>& exts)
{
std::wstring line {};
for (size_t i = 0; i < exts.size(); ++i)
{
if (i != 0)
{
line += L' ';
}
line += exts[i];
}
return line;
}
void WriteIniDefaults(const std::wstring& iniFile)
{
if (iniFile.empty())
{
return;
}
// General
IniStore::SetString(iniFile, L"General", L"IniVersion", L"1");
IniStore::SetString(iniFile, L"General", L"Initialized", L"1");
IniStore::SetString(iniFile, L"General", L"AskedAssociations", L"0");
IniStore::SetString(iniFile, L"General", L"AssociationsEnabled", L"0");
// Image / viewer behavior
IniStore::SetString(iniFile, L"Image", L"ZoomStiffness", L"80.0");
// Viewer defaults (reserved for future expansion)
IniStore::SetString(iniFile, L"Viewer", L"PaneCount", L"1");
IniStore::SetString(iniFile, L"Viewer", L"ShowNavItems", L"1");
// Thumbnail strip defaults (reserved for future expansion)
IniStore::SetString(iniFile, L"Thumbnails", L"MinSize", L"32");
IniStore::SetString(iniFile, L"Thumbnails", L"MaxSize", L"256");
IniStore::SetString(iniFile, L"Thumbnails", L"ItemSpacing", L"8");
IniStore::SetString(iniFile, L"Thumbnails", L"Padding", L"8");
IniStore::SetString(iniFile, L"Thumbnails", L"TileLabelSpacing", L"2");
// Window placement (will be populated on first exit).
IniStore::SetString(iniFile, L"Window", L"ShowCmd", L"1"); // SW_SHOWNORMAL
// Background color (R,G,B 0-255). Default matches previous clear: (0.09,0.09,0.10) ~ (23,23,26)
IniStore::SetString(iniFile, L"Window", L"BackgroundColor", L"20,23,23");
// Focused ImageBrowser background color (R,G,B 0-255). Default: dark yellow accent.
// (0.18,0.16,0.03) ~ (46,41,8)
IniStore::SetString(iniFile, L"Window", L"FocusedBackgroundColor", L"35,43,43");
}
void EnsureIniFileExists(const std::wstring& iniFile, bool associationsEnabled)
{
if (iniFile.empty())
{
return;
}
if (std::filesystem::exists(iniFile))
{
return;
}
try
{
std::filesystem::path p = iniFile;
std::filesystem::create_directories(p.parent_path());
}
catch (...)
{
// Best-effort.
}
// Create an INI with full default settings so the user can see/edit everything.
WriteIniDefaults(iniFile);
// First-run prompt result.
IniStore::SetString(iniFile, L"General", L"AskedAssociations", L"1");
IniStore::SetString(
iniFile,
L"General",
L"AssociationsEnabled",
associationsEnabled ? L"1" : L"0");
}
bool SetRegSzValue(HKEY root, const std::wstring& subKey, const wchar_t* valueNameOrNull, const std::wstring& value)
{
HKEY key = nullptr;
DWORD disp = 0;
const LONG rc = RegCreateKeyExW(
root,
subKey.c_str(),
0,
nullptr,
REG_OPTION_NON_VOLATILE,
KEY_SET_VALUE,
nullptr,
&key,
&disp);
if (rc != ERROR_SUCCESS)
{
return false;
}
const wchar_t* valueName = valueNameOrNull; // nullptr => (Default)
const DWORD bytes = static_cast<DWORD>((value.size() + 1) * sizeof(wchar_t));
const LONG rc2 = RegSetValueExW(
key,
valueName,
0,
REG_SZ,
reinterpret_cast<const BYTE*>(value.c_str()),
bytes);
RegCloseKey(key);
return rc2 == ERROR_SUCCESS;
}
void CleanupLegacyRegistrations(const std::wstring& exeName)
{
#if FICTURE2_BUILD_FLAVOR_STANDALONE
// For Standalone builds, clean up modern Capabilities registration (if exists from previous versions)
RegDeleteTreeW(HKEY_CURRENT_USER, L"Software\\FICture2\\Capabilities");
HKEY regAppsKey = nullptr;
if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\RegisteredApplications", 0, KEY_SET_VALUE, ®AppsKey) == ERROR_SUCCESS)
{
RegDeleteValueW(regAppsKey, L"FICture2");
RegCloseKey(regAppsKey);
}
#else
// For Store/Winget builds, clean up old Applications registration that causes duplicate entries
if (!exeName.empty())
{
RegDeleteTreeW(HKEY_CURRENT_USER, (L"Software\\Classes\\Applications\\" + exeName).c_str());
}
// Remove direct extension mappings (no longer used with Capabilities)
const std::vector<std::wstring> exts = ImageCore::ImageDecodeDispatcher::GetSupportedExtensions();
for (const auto& ext : exts)
{
HKEY key = nullptr;
if (RegOpenKeyExW(HKEY_CURRENT_USER, (L"Software\\Classes\\" + ext).c_str(), 0, KEY_READ | KEY_WRITE, &key) == ERROR_SUCCESS)
{
wchar_t value[256] = {};
DWORD size = sizeof(value);
if (RegQueryValueExW(key, nullptr, nullptr, nullptr, reinterpret_cast<BYTE*>(value), &size) == ERROR_SUCCESS)
{
if (std::wstring(value) == L"FICture2.Image")
{
// Only delete our own registration
RegDeleteValueW(key, nullptr);
}
}
RegCloseKey(key);
}
}
#endif
}
bool RegisterPerUserFileAssociations(const std::wstring& exePath, const std::vector<std::wstring>& extensions)
{
if (exePath.empty() || extensions.empty())
{
return false;
}
const std::wstring appName = L"FICture2";
std::wstring exeName = std::filesystem::path(exePath).filename().wstring();
// Clean up legacy registrations that cause duplicate entries in Default Apps
CleanupLegacyRegistrations(exeName);
bool ok = true;
const std::wstring progId = L"FICture2.Image";
const std::wstring cmd = L"\"" + exePath + L"\" \"%1\"";
const std::wstring icon = L"\"" + exePath + L"\",0";
// Always register ProgID (HKCU only)
ok = ok && SetRegSzValue(HKEY_CURRENT_USER, L"Software\\Classes\\" + progId, nullptr, L"FICture2 Image");
ok = ok && SetRegSzValue(HKEY_CURRENT_USER, L"Software\\Classes\\" + progId + L"\\DefaultIcon", nullptr, icon);
ok = ok && SetRegSzValue(HKEY_CURRENT_USER, L"Software\\Classes\\" + progId + L"\\shell\\open\\command", nullptr, cmd);
// Register modern Capabilities model for all flavors (HKCU).
// This is required for Windows Default Apps UI integration.
const std::wstring capabilitiesKey = L"Software\\" + appName + L"\\Capabilities";
ok = ok && SetRegSzValue(HKEY_CURRENT_USER, capabilitiesKey, L"ApplicationName", L"FICture2");
ok = ok && SetRegSzValue(HKEY_CURRENT_USER, capabilitiesKey, L"ApplicationDescription", L"FICture2 Image Viewer");
ok = ok && SetRegSzValue(HKEY_CURRENT_USER, L"Software\\RegisteredApplications", appName.c_str(), L"Software\\" + appName + L"\\Capabilities");
for (const auto& ext : extensions)
{
if (ext.empty() || ext[0] != L'.')
{
continue;
}
ok = ok && SetRegSzValue(
HKEY_CURRENT_USER,
capabilitiesKey + L"\\FileAssociations",
ext.c_str(),
progId);
}
// Also register legacy Application + OpenWith metadata for broader shell compatibility.
if (!exeName.empty())
{
ok = ok && SetRegSzValue(HKEY_CURRENT_USER, L"Software\\Classes\\Applications\\" + exeName + L"\\shell\\open\\command", nullptr, cmd);
ok = ok && SetRegSzValue(HKEY_CURRENT_USER, L"Software\\Classes\\Applications\\" + exeName + L"\\DefaultIcon", nullptr, icon);
ok = ok && SetRegSzValue(HKEY_CURRENT_USER, L"Software\\Classes\\Applications\\" + exeName, L"FriendlyAppName", L"FICture2");
for (const auto& ext : extensions)
{
if (ext.empty() || ext[0] != L'.')
{
continue;
}
(void)SetRegSzValue(HKEY_CURRENT_USER, L"Software\\Classes\\Applications\\" + exeName + L"\\SupportedTypes\\" + ext, nullptr, L"");
(void)SetRegSzValue(HKEY_CURRENT_USER, L"Software\\Classes\\" + ext + L"\\OpenWithProgids\\" + progId, nullptr, L"");
}
}
#if FICTURE2_BUILD_FLAVOR_STANDALONE
// Best-effort direct mapping (may be ignored when UserChoice policy is active).
for (const auto& ext : extensions)
{
if (ext.empty() || ext[0] != L'.')
{
continue;
}
ok = ok && SetRegSzValue(HKEY_CURRENT_USER, L"Software\\Classes\\" + ext, nullptr, progId);
}
#else
// Store/Winget builds generally rely on installer/MSIX pathways for defaults.
#endif
// Notify Explorer that associations changed.
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr);
return ok;
}
// Deleting something that is already gone still counts as success.
bool DeleteRegTree(HKEY root, const std::wstring& subKey)
{
const LONG rc = RegDeleteTreeW(root, subKey.c_str());
return rc == ERROR_SUCCESS || rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND;
}
bool DeleteRegValue(HKEY root, const std::wstring& subKey, const wchar_t* valueName)
{
const LONG rc = RegDeleteKeyValueW(root, subKey.c_str(), valueName);
return rc == ERROR_SUCCESS || rc == ERROR_FILE_NOT_FOUND || rc == ERROR_PATH_NOT_FOUND;
}
std::wstring ReadRegSzValue(HKEY root, const std::wstring& subKey, const wchar_t* valueNameOrNull)
{
wchar_t buffer[512] {};
DWORD bytes = sizeof(buffer) - sizeof(wchar_t);
DWORD type = 0;
const LONG rc = RegGetValueW(
root,
subKey.c_str(),
valueNameOrNull,
RRF_RT_REG_SZ,
&type,
buffer,
&bytes);
if (rc != ERROR_SUCCESS)
{
return {};
}
return buffer;
}
bool UnregisterPerUserFileAssociations(const std::wstring& exePath, const std::vector<std::wstring>& extensions)
{
const std::wstring appName = L"FICture2";
const std::wstring progId = L"FICture2.Image";
const std::wstring exeName = std::filesystem::path(exePath).filename().wstring();
bool ok = true;
// Direct extension mapping: only clear (Default) when it still points at us.
for (const auto& ext : extensions)
{
if (ext.empty() || ext[0] != L'.')
{
continue;
}
const std::wstring extKey = L"Software\\Classes\\" + ext;
if (_wcsicmp(ReadRegSzValue(HKEY_CURRENT_USER, extKey, nullptr).c_str(), progId.c_str()) == 0)
{
ok = ok && DeleteRegValue(HKEY_CURRENT_USER, extKey, nullptr);
}
ok = ok && DeleteRegTree(HKEY_CURRENT_USER, extKey + L"\\OpenWithProgids\\" + progId);
}
if (!exeName.empty())
{
ok = ok && DeleteRegTree(HKEY_CURRENT_USER, L"Software\\Classes\\Applications\\" + exeName);
}
ok = ok && DeleteRegValue(HKEY_CURRENT_USER, L"Software\\RegisteredApplications", appName.c_str());
ok = ok && DeleteRegTree(HKEY_CURRENT_USER, L"Software\\" + appName + L"\\Capabilities");
// Only remove the Capabilities subtree; leave any future app keys intact.
// If Capabilities was the only child, prune the empty FICture2 key best-effort.
(void)RegDeleteKeyW(HKEY_CURRENT_USER, (L"Software\\" + appName).c_str());
ok = ok && DeleteRegTree(HKEY_CURRENT_USER, L"Software\\Classes\\" + progId);
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr);
return ok;
}
void WriteIniInt(const std::wstring& iniFile, const wchar_t* section, const wchar_t* key, int value)
{
if (iniFile.empty() || section == nullptr || key == nullptr)
{
return;
}
IniStore::SetInt(iniFile, section, key, value);
}
void ClampRectToMonitorWorkArea(RECT& rc, bool enableLog = false)
{
// If the rect is invalid, bail.
if (rc.right <= rc.left || rc.bottom <= rc.top)
{
return;
}
FIC2_TIMER_START(t_clamp);
const HMONITOR mon = MonitorFromRect(&rc, MONITOR_DEFAULTTONEAREST);
if (enableLog) FIC2_LOG_STEP(t_clamp, "[LoadWP] MonitorFromRect");
MONITORINFO mi {};
mi.cbSize = sizeof(mi);
if (!GetMonitorInfoW(mon, &mi))
{
return;
}
if (enableLog) FIC2_LOG_STEP(t_clamp, "[LoadWP] GetMonitorInfoW");
const RECT wa = mi.rcWork;
// Keep the whole window inside the nearest monitor work area (NIFDiff-style).
// Older logic only guaranteed a 64px grab sliver, which could leave most of
// the window unreachable after a monitor/layout change.
const int w = (std::max)(200, (std::min)(static_cast<int>(rc.right - rc.left), static_cast<int>(wa.right - wa.left)));
const int h = (std::max)(200, (std::min)(static_cast<int>(rc.bottom - rc.top), static_cast<int>(wa.bottom - wa.top)));
const int x = (std::min)((std::max)(static_cast<int>(rc.left), static_cast<int>(wa.left)), static_cast<int>(wa.right) - w);
const int y = (std::min)((std::max)(static_cast<int>(rc.top), static_cast<int>(wa.top)), static_cast<int>(wa.bottom) - h);
rc.left = x;
rc.top = y;
rc.right = x + w;
rc.bottom = y + h;
if (enableLog) FIC2_LOG_STEP(t_clamp, "[LoadWP] ClampRectToMonitorWorkArea (full window)");
}
}
namespace FICture2App
{
std::wstring GetIniFilePath()
{
wchar_t iniPath[MAX_PATH] {};
if (FAILED(SHGetFolderPathW(nullptr, CSIDL_LOCAL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, iniPath)))
{
return {};
}
return std::wstring(iniPath) + L"\\FICture2\\FICture2.ini";
}
void RunFirstRunAssociationPromptIfNeeded()
{
const std::wstring iniFile = GetIniFilePath();
const bool firstRun = !iniFile.empty() && !std::filesystem::exists(iniFile);
if (!firstRun)
{
return;
}
#if !FICTURE2_ENABLE_FIRST_RUN_PROMPTS
// For winget/Store builds: just create INI file and skip all prompts
EnsureIniFileExists(iniFile, false);
return;
#else
// For standalone builds: show prompts on first run
// Thumbnail provider prompt
const int thumbChoice = MessageBoxW(
nullptr,
L"Register the DDS thumbnail provider for Windows Explorer?\n"
L"(Requires Administrator approval.)\n\n"
L"Do you want to apply this now?",
L"FICture2 - Thumbnail Provider",
MB_ICONQUESTION | MB_YESNO);
if (thumbChoice == IDYES)
{
RegisterThumbnailProvider(nullptr, false);
}
// Create the INI so we don't ask again on the next run
EnsureIniFileExists(iniFile, false);
#endif
}
bool RegisterSupportedFileAssociations(HWND owner)
{
#if !FICTURE2_BUILD_FLAVOR_STANDALONE
// For winget/Store builds, check if already registered by installer (HKLM)
HKEY key = nullptr;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\RegisteredApplications", 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
{
wchar_t value[512] = {};
DWORD size = sizeof(value);
if (RegQueryValueExW(key, L"FICture2", nullptr, nullptr, reinterpret_cast<BYTE*>(value), &size) == ERROR_SUCCESS)
{
RegCloseKey(key);
// Already registered system-wide by installer
MessageBoxW(
owner,
L"FICture2 is already registered with Windows.\n\n"
L"To set FICture2 as your default image viewer:\n"
L"1. Open Windows Settings\n"
L"2. Go to Apps > Default apps\n"
L"3. Search for 'FICture2'\n"
L"4. Set it as default for image file types",
L"FICture2 - File Associations",
MB_OK | MB_ICONINFORMATION);
return true;
}
RegCloseKey(key);
}
#endif
#if FICTURE2_BUILD_FLAVOR_STANDALONE
const std::vector<std::wstring> extsForPrompt = ImageCore::ImageDecodeDispatcher::GetSupportedExtensions();
const std::wstring extLine = BuildExtensionPromptLine(extsForPrompt);
const std::wstring msg =
L"Register FICture2 as the default image viewer for supported types?\n"
L"(This will configure per-user associations.)\n\n"
L"Extensions:\n" +
extLine +
L"\n\nDo you want to apply this now?";
#else
const std::vector<std::wstring> extsForPrompt = ImageCore::ImageDecodeDispatcher::GetSupportedExtensions();
const std::wstring extLine = BuildExtensionPromptLine(extsForPrompt);
const std::wstring msg =
L"Register FICture2 capabilities with Windows.\n\n"
L"After registration, you can set FICture2 as default for image files in:\n"
L"Windows Settings > Apps > Default apps\n\n"
L"Extensions:\n" +
extLine +
L"\n\nDo you want to register now?";
#endif
const int choice = MessageBoxW(
owner,
msg.c_str(),
L"FICture2 - File Associations",
MB_ICONQUESTION | MB_YESNO);
if (choice != IDYES)
{
return false;
}
wchar_t exePath[MAX_PATH] {};
GetModuleFileNameW(nullptr, exePath, static_cast<DWORD>(std::size(exePath)));
const std::vector<std::wstring> exts = ImageCore::ImageDecodeDispatcher::GetSupportedExtensions();
const bool enabled = RegisterPerUserFileAssociations(exePath, exts);
const std::wstring iniFile = GetIniFilePath();
if (!iniFile.empty())
{
WriteIniInt(iniFile, L"General", L"AskedAssociations", 1);
WriteIniInt(iniFile, L"General", L"AssociationsEnabled", enabled ? 1 : 0);
}
if (!enabled)
{
MessageBoxW(
owner,
L"Failed to configure file associations.\n\n"
L"Please check Windows Settings > Apps > Default apps to set FICture2 manually.",
L"FICture2",
MB_OK | MB_ICONWARNING);
return false;
}
#if FICTURE2_BUILD_FLAVOR_STANDALONE
// Standalone/Nexus: registration is complete, but Windows may still require explicit default selection.
MessageBoxW(
owner,
L"File associations were registered successfully.\n\n"
L"If some extensions are still not opening with FICture2,\n"
L"please set FICture2 in Windows Settings > Apps > Default apps.",
L"FICture2",
MB_OK | MB_ICONINFORMATION);
(void)ShellExecuteW(
owner,
L"open",
L"ms-settings:defaultapps",
nullptr,
nullptr,
SW_SHOWNORMAL);
#else
// Store/Winget: User needs to set manually in Windows Settings
MessageBoxW(
owner,
L"File associations registered successfully.\n\n"
L"To set FICture2 as your default image viewer:\n"
L"1. Open Windows Settings\n"
L"2. Go to Apps > Default apps\n"
L"3. Search for 'FICture2'\n"
L"4. Set it as default for image file types",
L"FICture2",
MB_OK | MB_ICONINFORMATION);
#endif
return enabled;
}
bool UnregisterSupportedFileAssociations(HWND owner)
{
const int choice = MessageBoxW(
owner,
L"Remove the per-user file associations for FICture2?\n"
L"(Explorer will fall back to whatever handler is registered next.)\n\n"
L"Do you want to remove them now?",
L"FICture2 - File Associations",
MB_ICONQUESTION | MB_YESNO);
if (choice != IDYES)
{
return false;
}
wchar_t exePath[MAX_PATH] {};
GetModuleFileNameW(nullptr, exePath, static_cast<DWORD>(std::size(exePath)));
const std::vector<std::wstring> exts = ImageCore::ImageDecodeDispatcher::GetSupportedExtensions();
const bool removed = UnregisterPerUserFileAssociations(exePath, exts);
const std::wstring iniFile = GetIniFilePath();
if (!iniFile.empty())
{
WriteIniInt(iniFile, L"General", L"AssociationsEnabled", 0);
}
if (!removed)
{
MessageBoxW(
owner,
L"Failed to fully remove file associations.\n\n"
L"Some entries may remain; you can clear them in Windows Settings > Apps > Default apps.",
L"FICture2",
MB_OK | MB_ICONWARNING);
return false;
}
MessageBoxW(
owner,
L"Per-user file associations were removed.",
L"FICture2",
MB_OK | MB_ICONINFORMATION);
return true;
}
bool AreFileAssociationsRegistered()
{
HKEY key = nullptr;
const std::wstring progIdKey = L"Software\\Classes\\FICture2.Image";
if (RegOpenKeyExW(HKEY_CURRENT_USER, progIdKey.c_str(), 0, KEY_READ, &key) != ERROR_SUCCESS)
{
return false;
}
RegCloseKey(key);
return true;
}
void RegisterThumbnailProvider(HWND owner, bool unregister)
{
wchar_t exePath[MAX_PATH] {};
GetModuleFileNameW(nullptr, exePath, static_cast<DWORD>(std::size(exePath)));
std::filesystem::path dllPath = std::filesystem::path(exePath).parent_path() / L"ThumbnailProvider.dll";
if (!std::filesystem::exists(dllPath))
{
std::wstring msg = L"ThumbnailProvider.dll not found.\n\nExpected location:\n";
msg += dllPath.wstring();
MessageBoxW(owner, msg.c_str(), L"FICture2", MB_OK | MB_ICONWARNING);
return;
}
std::wstring args;
if (unregister)
{
args = L"/u \"" + dllPath.wstring() + L"\"";
}
else
{
args = L"\"" + dllPath.wstring() + L"\"";
}
const HINSTANCE result = ShellExecuteW(
owner,
L"runas",
L"regsvr32.exe",
args.c_str(),
nullptr,
SW_SHOWNORMAL);
if (reinterpret_cast<intptr_t>(result) <= 32)
{
MessageBoxW(
owner,
L"Failed to launch regsvr32 with elevation.\n\nPlease run as Administrator.",
L"FICture2",
MB_OK | MB_ICONWARNING);
}
}
bool IsThumbnailProviderRegistered()
{
const wchar_t* kShellExtKey = L"Software\\Classes\\.dds\\ShellEx\\{b824b49d-22ac-4161-ac8a-9916e8fa3f7f}";
const wchar_t* kProviderClsid = L"{8b0a3d42-7022-4e35-b45f-7321b3e93c16}";
auto hasProvider = [kShellExtKey, kProviderClsid](HKEY root) -> bool
{
wchar_t value[128] {};
DWORD valueBytes = sizeof(value);
const LONG rc = RegGetValueW(
root,
kShellExtKey,
nullptr,
RRF_RT_REG_SZ,
nullptr,
value,
&valueBytes);
if (rc != ERROR_SUCCESS)
{
return false;
}
return _wcsicmp(value, kProviderClsid) == 0;
};
return hasProvider(HKEY_CURRENT_USER) || hasProvider(HKEY_LOCAL_MACHINE);
}
bool ReadWindowPlacement(RECT& outRect, int& outShowCmd)
{
const std::wstring iniFile = GetIniFilePath();
if (iniFile.empty())
{
return false;
}
const auto ini = IniStore::Load(iniFile);
if (!ini.IsLoaded())
{
return false;
}
const std::wstring leftStr = ini.GetString(L"Window", L"Left");
const std::wstring topStr = ini.GetString(L"Window", L"Top");
const std::wstring rightStr = ini.GetString(L"Window", L"Right");
const std::wstring bottomStr = ini.GetString(L"Window", L"Bottom");
const std::wstring showStr = ini.GetString(L"Window", L"ShowCmd");
if (leftStr.empty() || topStr.empty() || rightStr.empty() ||
bottomStr.empty() || showStr.empty())
{
return false;
}
const auto left = CommonUtil::TryParseInt(leftStr);
const auto top = CommonUtil::TryParseInt(topStr);
const auto right = CommonUtil::TryParseInt(rightStr);
const auto bottom = CommonUtil::TryParseInt(bottomStr);
const auto show = CommonUtil::TryParseInt(showStr);
if (!left || !top || !right || !bottom || !show)
{
return false;
}
RECT rc { *left, *top, *right, *bottom };
if (rc.right <= rc.left || rc.bottom <= rc.top)
{
return false;
}
ClampRectToMonitorWorkArea(rc);
outRect = rc;
outShowCmd = *show;
return true;
}
void LoadWindowPlacement(HWND hwnd)
{
// This function is kept for fallback use only.
// Normal startup applies placement via CreateWindowed + Show(showCmd)
// to avoid the ~50 ms SetWindowPlacement DWM IPC cost.
if (hwnd == nullptr)
{
return;
}
RECT rc {};
int showCmd = 0;
if (!ReadWindowPlacement(rc, showCmd))
{
return;
}
WINDOWPLACEMENT wp {};
wp.length = sizeof(wp);
if (!GetWindowPlacement(hwnd, &wp))
{
return;
}
wp.rcNormalPosition = rc;
wp.showCmd = (showCmd == SW_SHOWMAXIMIZED) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL;
(void)SetWindowPlacement(hwnd, &wp);
}
void SaveWindowPlacement(HWND hwnd)
{
if (hwnd == nullptr)
{
return;
}
const std::wstring iniFile = GetIniFilePath();
if (iniFile.empty())
{
return;
}
WINDOWPLACEMENT wp {};
wp.length = sizeof(wp);
if (!GetWindowPlacement(hwnd, &wp))
{
return;
}
const RECT rc = wp.rcNormalPosition;
WriteIniInt(iniFile, L"Window", L"Left", rc.left);
WriteIniInt(iniFile, L"Window", L"Top", rc.top);
WriteIniInt(iniFile, L"Window", L"Right", rc.right);
WriteIniInt(iniFile, L"Window", L"Bottom", rc.bottom);
WriteIniInt(iniFile, L"Window", L"ShowCmd", static_cast<int>(wp.showCmd));
}
}