Skip to content

Commit 8ae31d2

Browse files
committed
Improvements to ContentDialog generation
1 parent 3c538c5 commit 8ae31d2

3 files changed

Lines changed: 121 additions & 169 deletions

File tree

src/UniGetUI/Pages/DialogPages/DialogHelper_Generic.cs

Lines changed: 93 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using Windows.UI;
3+
using Microsoft.UI;
34
using Microsoft.UI.Text;
45
using Microsoft.UI.Xaml;
56
using Microsoft.UI.Xaml.Controls;
@@ -21,6 +22,52 @@ namespace UniGetUI.Pages.DialogPages;
2122

2223
public static partial class DialogHelper
2324
{
25+
private static class DialogFactory
26+
{
27+
public static ContentDialog Create()
28+
{
29+
return new ContentDialog()
30+
{
31+
XamlRoot = Window.MainContentGrid.XamlRoot,
32+
Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style
33+
};
34+
}
35+
public static ContentDialog Create(int maxWidth, int maxHeight)
36+
{
37+
var dialog = Create();
38+
// dialog.Margin = new Thickness(0, 30, 0, 0);
39+
dialog.Resources["ContentDialogMaxWidth"] = maxWidth;
40+
dialog.Resources["ContentDialogMaxHeight"] = maxHeight;
41+
return dialog;
42+
}
43+
44+
public static ContentDialog Create_AsWindow(bool hasTitle)
45+
{
46+
var dialog = Create();
47+
dialog.Resources["ContentDialogMaxWidth"] = 8192;
48+
dialog.Resources["ContentDialogMaxHeight"] = 4096;
49+
dialog.SizeChanged += (_, _) =>
50+
{
51+
if (dialog.Content is Page page)
52+
{
53+
double maxW, maxH;
54+
int tresholdW = 1300, tresholdH = 1300;
55+
if (Window.NavigationPage.ActualWidth < tresholdW) maxW = 100;
56+
else if (Window.NavigationPage.ActualWidth >= tresholdW + 200) maxW = 300;
57+
else maxW = Window.NavigationPage.ActualWidth - (tresholdW - 100);
58+
59+
if (Window.NavigationPage.ActualHeight < tresholdH) maxH = (hasTitle? 120: 80);
60+
else if (Window.NavigationPage.ActualHeight >= tresholdH + 200) maxH = (hasTitle ? 320 : 280);
61+
else maxH = Window.NavigationPage.ActualHeight - (tresholdH - (hasTitle ? 120 : 80));
62+
63+
page.Width = Math.Min(Math.Abs(Window.NavigationPage.ActualWidth - maxW), 8192);
64+
page.Height = Math.Min(Math.Abs(Window.NavigationPage.ActualHeight - maxH), 4096);
65+
}
66+
};
67+
return dialog;
68+
}
69+
}
70+
2471
public static MainWindow Window { private get; set; } = null!;
2572

2673
public static void ShowLoadingDialog(string text)
@@ -60,7 +107,6 @@ public static void HideLoadingDialog()
60107
public static async Task ShowMissingDependency(string dep_name, string exe_name, string exe_args,
61108
string fancy_command, int current, int total)
62109
{
63-
ContentDialog dialog = new();
64110

65111
if (Settings.GetDictionaryItem<string, string>("DependencyManagement", dep_name) == "skipped")
66112
{
@@ -72,8 +118,7 @@ public static async Task ShowMissingDependency(string dep_name, string exe_name,
72118
bool NotFirstTime = Settings.GetDictionaryItem<string, string>("DependencyManagement", dep_name) == "attempted";
73119
Settings.SetDictionaryItem("DependencyManagement", dep_name, "attempted");
74120

75-
dialog.XamlRoot = Window.MainContentGrid.XamlRoot;
76-
dialog.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
121+
var dialog = DialogFactory.Create();
77122
dialog.Title = CoreTools.Translate("Missing dependency") + (total > 1 ? $" ({current}/{total})" : "");
78123
dialog.SecondaryButtonText = CoreTools.Translate("Not right now");
79124
dialog.PrimaryButtonText = CoreTools.Translate("Install {0}", dep_name);
@@ -157,12 +202,12 @@ public static async Task ShowMissingDependency(string dep_name, string exe_name,
157202
CoreTools.Translate(
158203
"Please wait while {0} is being installed. A black window may show up. Please wait until it closes.",
159204
dep_name);
160-
Process p = new()
205+
Process install_dep_p = new()
161206
{
162207
StartInfo = new ProcessStartInfo { FileName = exe_name, Arguments = exe_args, },
163208
};
164-
p.Start();
165-
await p.WaitForExitAsync();
209+
install_dep_p.Start();
210+
await install_dep_p.WaitForExitAsync();
166211
dialog.IsPrimaryButtonEnabled = true;
167212
dialog.IsSecondaryButtonEnabled = true;
168213
if (current < total)
@@ -225,47 +270,32 @@ public static async Task ShowMissingDependency(string dep_name, string exe_name,
225270

226271
public static async Task ManageIgnoredUpdates()
227272
{
228-
ContentDialog? UpdatesDialog = new()
229-
{
230-
Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style, XamlRoot = Window.XamlRoot,
231-
Resources = {
232-
["ContentDialogMaxWidth"] = 1400,
233-
["ContentDialogMaxHeight"] = 1000,
234-
},
235-
SecondaryButtonText = CoreTools.Translate("Close"),
236-
//UpdatesDialog.PrimaryButtonText = CoreTools.Translate("Reset");
237-
DefaultButton = ContentDialogButton.None,
238-
Title = CoreTools.Translate("Manage ignored updates"),
239-
};
273+
ContentDialog dialog = DialogFactory.Create(1400, 1000);
274+
275+
dialog.SecondaryButtonText = CoreTools.Translate("Close");
276+
dialog.DefaultButton = ContentDialogButton.None;
277+
dialog.Title = CoreTools.Translate("Manage ignored updates");
240278

241279
IgnoredUpdatesManager IgnoredUpdatesPage = new();
242-
UpdatesDialog.Content = IgnoredUpdatesPage;
243-
IgnoredUpdatesPage.Close += (_, _) => UpdatesDialog.Hide();
244-
await Window.ShowDialogAsync(UpdatesDialog);
280+
dialog.Content = IgnoredUpdatesPage;
281+
IgnoredUpdatesPage.Close += (_, _) => dialog.Hide();
282+
await Window.ShowDialogAsync(dialog);
245283
}
246284

247285
public static async Task ManageDesktopShortcuts(List<string>? NewShortucts = null)
248286
{
249-
ContentDialog? ShortcutsDialog = new()
250-
{
251-
Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
252-
XamlRoot = Window.XamlRoot,
253-
Resources = {
254-
["ContentDialogMaxWidth"] = 1400,
255-
["ContentDialogMaxHeight"] = 1000,
256-
},
257-
Title = CoreTools.Translate("Automatic desktop shortcut remover"),
258-
};
287+
ContentDialog dialog = DialogFactory.Create(1400, 1000);
259288

260289
DesktopShortcutsManager DesktopShortcutsPage = new(NewShortucts);
261-
DesktopShortcutsPage.Close += (_, _) => ShortcutsDialog.Hide();
290+
DesktopShortcutsPage.Close += (_, _) => dialog.Hide();
262291

263-
ShortcutsDialog.Content = DesktopShortcutsPage;
264-
ShortcutsDialog.SecondaryButtonText = CoreTools.Translate("Save and close");
265-
ShortcutsDialog.DefaultButton = ContentDialogButton.None;
266-
ShortcutsDialog.SecondaryButtonClick += (_, _) => DesktopShortcutsPage.SaveChangesAndClose();
292+
dialog.Title = CoreTools.Translate("Automatic desktop shortcut remover");
293+
dialog.Content = DesktopShortcutsPage;
294+
dialog.SecondaryButtonText = CoreTools.Translate("Save and close");
295+
dialog.DefaultButton = ContentDialogButton.None;
296+
dialog.SecondaryButtonClick += (_, _) => DesktopShortcutsPage.SaveChangesAndClose();
267297

268-
await Window.ShowDialogAsync(ShortcutsDialog);
298+
await Window.ShowDialogAsync(dialog);
269299
}
270300

271301
public static async Task HandleNewDesktopShortcuts()
@@ -347,12 +377,8 @@ public static async void WarnAboutAdminRights()
347377

348378
public static async Task ShowAboutUniGetUI()
349379
{
350-
ContentDialog? AboutDialog = new();
380+
ContentDialog AboutDialog = DialogFactory.Create(1200, 1000);
351381
AboutUniGetUI AboutPage = new();
352-
AboutDialog.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
353-
AboutDialog.XamlRoot = Window.XamlRoot;
354-
AboutDialog.Resources["ContentDialogMaxWidth"] = 1200;
355-
AboutDialog.Resources["ContentDialogMaxHeight"] = 1000;
356382
AboutDialog.Content = AboutPage;
357383
AboutDialog.PrimaryButtonText = CoreTools.Translate("Close");
358384
AboutPage.Close += (_, _) => AboutDialog.Hide();
@@ -362,26 +388,13 @@ public static async Task ShowAboutUniGetUI()
362388

363389
public static async void ShowReleaseNotes()
364390
{
365-
ContentDialog? NotesDialog = new()
366-
{
367-
Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
368-
XamlRoot = Window.XamlRoot,
369-
Resources = {
370-
["ContentDialogMaxWidth"] = 12000,
371-
["ContentDialogMaxHeight"] = 10000,
372-
},
373-
CloseButtonText = CoreTools.Translate("Close"),
374-
Title = CoreTools.Translate("Release notes"),
375-
};
376-
ReleaseNotes? notes = new();
391+
ContentDialog NotesDialog = DialogFactory.Create_AsWindow(true);
392+
393+
// NotesDialog.CloseButtonText = CoreTools.Translate("Close");
394+
NotesDialog.Title = CoreTools.Translate("Release notes");
395+
ReleaseNotes notes = new();
377396
notes.Close += (_, _) => NotesDialog.Hide();
378397
NotesDialog.Content = notes;
379-
NotesDialog.SizeChanged += (_, _) =>
380-
{
381-
notes.MinWidth = Math.Abs(Window.NavigationPage.ActualWidth - 300);
382-
notes.MinHeight = Math.Abs(Window.NavigationPage.ActualHeight - 200);
383-
};
384-
385398
await Window.ShowDialogAsync(NotesDialog);
386399
}
387400

@@ -425,19 +438,15 @@ public static async void HandleBrokenWinGet()
425438
if (Settings.Get("ForceLegacyBundledWinGet"))
426439
Settings.Set("ForceLegacyBundledWinGet", false);
427440

428-
var c = new ContentDialog
429-
{
430-
Title = CoreTools.Translate("WinGet was repaired successfully"),
431-
Content = CoreTools.Translate("It is recommended to restart UniGetUI after WinGet has been repaired") +
432-
"\n\n" +
433-
CoreTools.Translate(
434-
"NOTE: This troubleshooter can be disabled from UniGetUI Settings, on the WinGet section"),
435-
PrimaryButtonText = CoreTools.Translate("Close"),
436-
SecondaryButtonText = CoreTools.Translate("Restart"),
437-
DefaultButton = ContentDialogButton.Secondary,
438-
XamlRoot = Window.XamlRoot,
439-
Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
440-
};
441+
var c = DialogFactory.Create();
442+
c.Title = CoreTools.Translate("WinGet was repaired successfully");
443+
c.Content = CoreTools.Translate("It is recommended to restart UniGetUI after WinGet has been repaired") +
444+
"\n\n" +
445+
CoreTools.Translate(
446+
"NOTE: This troubleshooter can be disabled from UniGetUI Settings, on the WinGet section");
447+
c.PrimaryButtonText = CoreTools.Translate("Close");
448+
c.SecondaryButtonText = CoreTools.Translate("Restart");
449+
c.DefaultButton = ContentDialogButton.Secondary;
441450

442451
// Restart UniGetUI or reload packages depending on the user's choice
443452
if (await Window.ShowDialogAsync(c) == ContentDialogResult.Secondary)
@@ -458,31 +467,23 @@ public static async void HandleBrokenWinGet()
458467
Logger.Error(ex);
459468
DialogHelper.HideLoadingDialog();
460469

461-
var c = new ContentDialog
462-
{
463-
Title = CoreTools.Translate("WinGet could not be repaired"),
464-
Content =
465-
CoreTools.Translate("An unexpected issue occurred while attempting to repair WinGet. Please try again later") +
466-
"\n\n" + ex.Message + "\n\n" +
467-
CoreTools.Translate("NOTE: This troubleshooter can be disabled from UniGetUI Settings, on the WinGet section"),
468-
PrimaryButtonText = CoreTools.Translate("Close"),
469-
DefaultButton = ContentDialogButton.None,
470-
XamlRoot = Window.XamlRoot
471-
};
472-
470+
var c = DialogFactory.Create();
471+
c.Title = CoreTools.Translate("WinGet could not be repaired");
472+
c.Content = CoreTools.Translate(
473+
"An unexpected issue occurred while attempting to repair WinGet. Please try again later") +
474+
"\n\n" + ex.Message + "\n\n" + CoreTools.Translate(
475+
"NOTE: This troubleshooter can be disabled from UniGetUI Settings, on the WinGet section");
476+
c.PrimaryButtonText = CoreTools.Translate("Close");
477+
c.DefaultButton = ContentDialogButton.None;
473478
await Window.ShowDialogAsync(c);
474479
}
475480

476481
}
477482

478483
public static async void ShowTelemetryDialog()
479484
{
480-
var dialog = new ContentDialog
481-
{
482-
Title = CoreTools.Translate("Share anonymous usage data"),
483-
XamlRoot = Window.XamlRoot,
484-
Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
485-
};
485+
var dialog = DialogFactory.Create();
486+
dialog.Title = CoreTools.Translate("Share anonymous usage data");
486487

487488
var MessageBlock = new RichTextBlock();
488489
dialog.Content = MessageBlock;
@@ -516,11 +517,10 @@ public static async void ShowTelemetryDialog()
516517
FontWeight = FontWeights.SemiBold
517518
});
518519

519-
520520
dialog.SecondaryButtonText = CoreTools.Translate("Decline");
521521
dialog.PrimaryButtonText = CoreTools.Translate("Accept");
522522
dialog.DefaultButton = ContentDialogButton.Primary;
523-
dialog.Closing += (s, e) =>
523+
dialog.Closing += (_, e) =>
524524
{
525525
if (e.Result == ContentDialogResult.None) e.Cancel = true;
526526
};

src/UniGetUI/Pages/DialogPages/DialogHelper_Operations.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,8 @@ public static partial class DialogHelper
1515
{
1616
public static async Task ShowOperationFailedDialog(AbstractOperation operation, OperationControl opControl)
1717
{
18-
ContentDialog dialog = new()
19-
{
20-
Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
21-
XamlRoot = Window.XamlRoot,
22-
Resources =
23-
{
24-
["ContentDialogMaxWidth"] = 850,
25-
["ContentDialogMaxHeight"] = 800,
26-
},
27-
Title = operation.Metadata.FailureTitle,
28-
};
18+
ContentDialog dialog = DialogFactory.Create(850, 800);
19+
dialog.Title = operation.Metadata.FailureTitle;
2920

3021
Grid grid = new()
3122
{
@@ -183,15 +174,7 @@ public static async Task ShowLiveLogDialog(AbstractOperation operation)
183174
{
184175
bool LastLineWasProgress = false;
185176

186-
ContentDialog OutputDialog = new ContentDialog
187-
{
188-
Style = (Style)Application.Current.Resources["DefaultContentDialogStyle"],
189-
XamlRoot = Window.XamlRoot,
190-
Resources = {
191-
["ContentDialogMaxWidth"] = 1200,
192-
["ContentDialogMaxHeight"] = 1000,
193-
},
194-
};
177+
ContentDialog OutputDialog = DialogFactory.Create(1200, 1000);
195178

196179
var LiveOutputTextBlock = new RichTextBlock
197180
{

0 commit comments

Comments
 (0)