Skip to content

Commit d7d5f44

Browse files
committed
Add hidden form to receive application restart events
1 parent 23edcbd commit d7d5f44

File tree

12 files changed

+94
-50
lines changed

12 files changed

+94
-50
lines changed

scripts/installer.iss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ WizardStyle=modern
3535
CloseApplications=force
3636
LicenseFile=..\LICENSE
3737
OutputDir=..\dist
38+
RestartApplications=no
3839

3940
[Languages]
4041
Name: "english"; MessagesFile: "compiler:Default.isl"

scripts/tools/chocolateyInstall.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $packageArgs = @{
1818
checksum64 = '{{installerChecksum64}}'
1919
checksumType64= 'sha256'
2020

21-
silentArgs = '/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP- /RESTARTAPPLICATIONS'
21+
silentArgs = '/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-'
2222
validExitCodes= @(0)
2323
}
2424

src/AboutDialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public AboutDialog()
2626

2727
public static string GetVersionString()
2828
{
29-
string versionStr = Assembly.GetExecutingAssembly().GetName().Version.ToString();
29+
string versionStr = UpdateChecker.GetCurrentVersion();
3030
versionStr = versionStr.Remove(versionStr.Length - 2);
3131
versionStr += UwpDesktop.IsRunningAsUwp() ? " (UWP)" : string.Empty;
3232
return versionStr;

src/AppContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class AppContext : ApplicationContext
1717
public static NotifyIcon notifyIcon;
1818
public static EventScheduler scheduler = new EventScheduler();
1919

20-
public AppContext(string[] args)
20+
public AppContext(string[] args) : base(new HiddenForm())
2121
{
2222
JsonConfig.LoadConfig();
2323
Localization.Initialize();
@@ -96,14 +96,14 @@ private void OnArgumentsReceived(string[] args)
9696
{
9797
if (JsonConfig.settings.hideTrayIcon)
9898
{
99-
notifyIcon.ContextMenuStrip.BeginInvoke(ToggleTrayIcon);
99+
MainForm.BeginInvoke(ToggleTrayIcon);
100100
}
101101

102102
ThemeManager.importPaths.AddRange(args);
103103

104104
if (args.Length > 0 && !ThemeManager.importMode)
105105
{
106-
notifyIcon.ContextMenuStrip.BeginInvoke(ThemeManager.SelectTheme);
106+
MainForm.BeginInvoke(ThemeManager.SelectTheme);
107107
}
108108
}
109109

src/DarkUI.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,10 @@ public static void ThemeForm(Form form, bool onInit = true)
5656

5757
public static void UserDefaultAppThemeIsDarkChanged(object sender, bool isDark)
5858
{
59-
AppContext.notifyIcon.ContextMenuStrip.BeginInvoke(() =>
59+
foreach (Form form in Application.OpenForms)
6060
{
61-
foreach (Form form in Application.OpenForms)
62-
{
63-
ThemeForm(form, false);
64-
}
65-
});
61+
form.BeginInvoke(() => ThemeForm(form, false));
62+
}
6663
}
6764

6865
// Code from https://stackoverflow.com/a/664083/5504760

src/DownloadDialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private void OnFormClosing(object sender, FormClosingEventArgs e)
201201
{
202202
try
203203
{
204-
System.Threading.Thread.Sleep(100); // Wait for file to free up
204+
Thread.Sleep(100); // Wait for file to free up
205205
File.Delete(themeZipDest);
206206
}
207207
catch { /* Do nothing */ }

src/EventScheduler.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ private void HandleDisplayEvent(DisplayEvent e)
190190
catch (Exception exc)
191191
{
192192
LoggingHandler.LogMessage("Error setting wallpaper: {0}", exc.ToString());
193-
MessageDialog.ShowWarning(string.Format(Localization.GetTranslation(
194-
"Error occurred when setting wallpaper '{0}':\n\n{1}"),
195-
Path.GetRelativePath(Environment.CurrentDirectory, imagePath), exc.ToString()));
193+
LoggingHandler.LogError(UwpDesktop.GetHelper().GetLocalFolder(), exc);
196194
}
197195
}
198196

src/HiddenForm.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
using System;
6+
using System.Runtime.InteropServices;
7+
using System.Windows.Forms;
8+
9+
namespace WinDynamicDesktop
10+
{
11+
// https://github.com/specshell/specshell.software.winform.hiddenform/blob/main/src/Specshell.WinForm.HiddenForm/HiddenForm.cs
12+
internal class HiddenForm : Form
13+
{
14+
private const int ENDSESSION_CLOSEAPP = 0x1;
15+
private const int WM_QUERYENDSESSION = 0x11;
16+
private const int WM_ENDSESSION = 0x16;
17+
18+
[DllImport("user32.dll")]
19+
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hwndNewParent);
20+
21+
public HiddenForm()
22+
{
23+
this.Load += this.HiddenForm_Load;
24+
}
25+
26+
protected override CreateParams CreateParams
27+
{
28+
get
29+
{
30+
const int WS_POPUP = unchecked((int)0x80000000);
31+
const int WS_EX_TOOLWINDOW = 0x80;
32+
33+
CreateParams cp = base.CreateParams;
34+
cp.ExStyle = WS_EX_TOOLWINDOW;
35+
cp.Style = WS_POPUP;
36+
cp.Height = 0;
37+
cp.Width = 0;
38+
return cp;
39+
}
40+
}
41+
42+
protected override void WndProc(ref Message m)
43+
{
44+
// https://github.com/rocksdanister/lively/blob/9142f6a4cfc222cd494f205a5daaa1a0238282e3/src/Lively/Lively/Views/WindowMsg/WndProcMsgWindow.xaml.cs#L41
45+
switch (m.Msg)
46+
{
47+
case WM_QUERYENDSESSION:
48+
if ((m.LParam & ENDSESSION_CLOSEAPP) != 0)
49+
{
50+
UpdateChecker.RegisterApplicationRestart(null, (int)RestartFlags.RESTART_NO_CRASH |
51+
(int)RestartFlags.RESTART_NO_HANG | (int)RestartFlags.RESTART_NO_REBOOT);
52+
}
53+
m.Result = new IntPtr(1);
54+
break;
55+
case WM_ENDSESSION:
56+
if (m.WParam != IntPtr.Zero)
57+
{
58+
Application.Exit();
59+
}
60+
m.Result = IntPtr.Zero;
61+
break;
62+
default:
63+
base.WndProc(ref m);
64+
break;
65+
}
66+
}
67+
68+
private void HiddenForm_Load(object source, EventArgs e)
69+
{
70+
const int HWND_MESSAGE = -1;
71+
SetParent(this.Handle, new IntPtr(HWND_MESSAGE));
72+
}
73+
}
74+
}

src/ScheduleDialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private void OnFormClosing(object sender, FormClosingEventArgs e)
216216

217217
if (result == DialogResult.Yes)
218218
{
219-
Application.Exit();
219+
Environment.Exit(0);
220220
}
221221
else
222222
{

src/ThemeThumbLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public static void CacheThumbnails(System.Windows.Forms.ListView listView)
152152

153153
private static string CreateBlankWallpaper()
154154
{
155-
string wallpaperPath = Path.Combine(Environment.CurrentDirectory, "wallpaper_blank.jpg");
155+
string wallpaperPath = Path.Combine(Path.GetTempPath(), "WinDynamicDesktop_blank_preview.jpg");
156156
if (!File.Exists(wallpaperPath))
157157
{
158158
(new Bitmap(1, 1)).Save(wallpaperPath, ImageFormat.Jpeg);

0 commit comments

Comments
 (0)