Skip to content

Commit 4c29b10

Browse files
committed
Add post-init form handling to WinForms UI
1 parent 7d9169c commit 4c29b10

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/NetSparkle.UI.WinForms.NetCore/UIFactory.cs

+23-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NetSparkleUpdater.Enums;
77
using System.Threading;
88
using System.Collections.Generic;
9+
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
910

1011
namespace NetSparkleUpdater.UI.WinForms
1112
{
@@ -67,6 +68,19 @@ public UIFactory(Icon applicationIcon) : this()
6768
/// </summary>
6869
public ReleaseNotesGrabber ReleaseNotesGrabberOverride { get; set; } = null;
6970

71+
/// <summary>
72+
/// A delegate for handling forms that are created by a <see cref="UIFactory"/>
73+
/// </summary>
74+
/// <param name="form"><see cref="Form"/> that has been created and initialized</param>
75+
/// <param name="factory"><see cref="UIFactory"/> that created the given <see cref="Form"/></param>
76+
public delegate void FormHandler(Form form, UIFactory factory);
77+
78+
/// <summary>
79+
/// Set this property to manually do any other setup on a <see cref="Form"/> after it has been created by the <see cref="UIFactory"/>.
80+
/// Can be used to tweak styles or perform other operations on the <see cref="Form"/>, etc.
81+
/// </summary>
82+
public FormHandler ProcessFormAfterInit { get; set; }
83+
7084
/// <inheritdoc/>
7185
public virtual IUpdateAvailable CreateUpdateAvailableWindow(SparkleUpdater sparkle, List<AppCastItem> updates, bool isUpdateAlreadyDownloaded = false)
7286
{
@@ -89,22 +103,27 @@ public virtual IUpdateAvailable CreateUpdateAvailableWindow(SparkleUpdater spark
89103
window.ReleaseNotesGrabber = ReleaseNotesGrabberOverride;
90104
}
91105
window.Initialize();
106+
ProcessFormAfterInit?.Invoke(window, this);
92107
return window;
93108
}
94109

95110
/// <inheritdoc/>
96111
public virtual IDownloadProgress CreateProgressWindow(SparkleUpdater sparkle, AppCastItem item)
97112
{
98-
return new DownloadProgressWindow(item, _applicationIcon)
113+
var window = new DownloadProgressWindow(item, _applicationIcon)
99114
{
100115
SoftwareWillRelaunchAfterUpdateInstalled = sparkle.RelaunchAfterUpdate
101116
};
117+
ProcessFormAfterInit?.Invoke(window, this);
118+
return window;
102119
}
103120

104121
/// <inheritdoc/>
105122
public virtual ICheckingForUpdates ShowCheckingForUpdates(SparkleUpdater sparkle)
106123
{
107-
return new CheckingForUpdatesWindow(_applicationIcon);
124+
var window = new CheckingForUpdatesWindow(_applicationIcon);
125+
ProcessFormAfterInit?.Invoke(window, this);
126+
return window;
108127
}
109128

110129
/// <inheritdoc/>
@@ -156,6 +175,7 @@ public virtual void ShowToast(SparkleUpdater sparkle, List<AppCastItem> updates,
156175
ClickAction = clickHandler,
157176
Updates = updates
158177
};
178+
ProcessFormAfterInit?.Invoke(toast, this);
159179
toast.Show(Resources.DefaultUIFactory_ToastMessage, Resources.DefaultUIFactory_ToastCallToAction, 5);
160180
Application.Run(toast);
161181
});
@@ -173,6 +193,7 @@ private void ShowMessage(string title, string message)
173193
{
174194
var messageWindow = new MessageNotificationWindow(title, message, _applicationIcon);
175195
messageWindow.StartPosition = FormStartPosition.CenterScreen;
196+
ProcessFormAfterInit?.Invoke(messageWindow, this);
176197
messageWindow.ShowDialog();
177198
}
178199

src/NetSparkle.UI.WinForms.NetFramework/UIFactory.cs

+23-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NetSparkleUpdater.Enums;
77
using System.Threading;
88
using System.Collections.Generic;
9+
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
910

1011
namespace NetSparkleUpdater.UI.WinForms
1112
{
@@ -67,6 +68,19 @@ public UIFactory(Icon applicationIcon) : this()
6768
/// </summary>
6869
public ReleaseNotesGrabber ReleaseNotesGrabberOverride { get; set; } = null;
6970

71+
/// <summary>
72+
/// A delegate for handling forms that are created by a <see cref="UIFactory"/>
73+
/// </summary>
74+
/// <param name="form"><see cref="Form"/> that has been created and initialized</param>
75+
/// <param name="factory"><see cref="UIFactory"/> that created the given <see cref="Form"/></param>
76+
public delegate void FormHandler(Form form, UIFactory factory);
77+
78+
/// <summary>
79+
/// Set this property to manually do any other setup on a <see cref="Form"/> after it has been created by the <see cref="UIFactory"/>.
80+
/// Can be used to tweak styles or perform other operations on the <see cref="Form"/>, etc.
81+
/// </summary>
82+
public FormHandler ProcessFormAfterInit { get; set; }
83+
7084
/// <inheritdoc/>
7185
public virtual IUpdateAvailable CreateUpdateAvailableWindow(SparkleUpdater sparkle, List<AppCastItem> updates, bool isUpdateAlreadyDownloaded = false)
7286
{
@@ -89,22 +103,27 @@ public virtual IUpdateAvailable CreateUpdateAvailableWindow(SparkleUpdater spark
89103
window.ReleaseNotesGrabber = ReleaseNotesGrabberOverride;
90104
}
91105
window.Initialize();
106+
ProcessFormAfterInit?.Invoke(window, this);
92107
return window;
93108
}
94109

95110
/// <inheritdoc/>
96111
public virtual IDownloadProgress CreateProgressWindow(SparkleUpdater sparkle, AppCastItem item)
97112
{
98-
return new DownloadProgressWindow(item, _applicationIcon)
113+
var window = new DownloadProgressWindow(item, _applicationIcon)
99114
{
100115
SoftwareWillRelaunchAfterUpdateInstalled = sparkle.RelaunchAfterUpdate
101116
};
117+
ProcessFormAfterInit?.Invoke(window, this);
118+
return window;
102119
}
103120

104121
/// <inheritdoc/>
105122
public virtual ICheckingForUpdates ShowCheckingForUpdates(SparkleUpdater sparkle)
106123
{
107-
return new CheckingForUpdatesWindow(_applicationIcon);
124+
var window = new CheckingForUpdatesWindow(_applicationIcon);
125+
ProcessFormAfterInit?.Invoke(window, this);
126+
return window;
108127
}
109128

110129
/// <inheritdoc/>
@@ -156,6 +175,7 @@ public virtual void ShowToast(SparkleUpdater sparkle, List<AppCastItem> updates,
156175
ClickAction = clickHandler,
157176
Updates = updates
158177
};
178+
ProcessFormAfterInit?.Invoke(toast, this);
159179
toast.Show(Resources.DefaultUIFactory_ToastMessage, Resources.DefaultUIFactory_ToastCallToAction, 5);
160180
Application.Run(toast);
161181
});
@@ -173,6 +193,7 @@ private void ShowMessage(string title, string message)
173193
{
174194
var messageWindow = new MessageNotificationWindow(title, message, _applicationIcon);
175195
messageWindow.StartPosition = FormStartPosition.CenterScreen;
196+
ProcessFormAfterInit?.Invoke(messageWindow, this);
176197
messageWindow.ShowDialog();
177198
}
178199

0 commit comments

Comments
 (0)