photomator/Photomator/Views/StatusDialog.cs

202 lines
6.6 KiB
C#
Raw Permalink Normal View History

2024-09-08 22:37:06 +02:00
using Gtk;
using Adw;
using System.Text;
2024-09-14 23:22:12 +02:00
using System.Runtime.InteropServices;
using System.Diagnostics;
2024-09-08 22:37:06 +02:00
namespace Photomator.Views;
2024-09-14 17:49:20 +02:00
public partial class StatusDialog : Adw.Window {
2024-09-08 22:37:06 +02:00
private readonly Adw.Application _application;
private readonly Adw.ApplicationWindow _mainWindow;
private readonly ProgressBar _progress;
2024-09-14 17:49:20 +02:00
public StatusDialog(Adw.Application application, Adw.ApplicationWindow parent) : base() {
2024-09-08 22:37:06 +02:00
_application = application;
_mainWindow = parent;
_progress = InitProgressBar();
SetModal(true);
SetTransientFor(parent);
SetDeletable(true);
SetDefaultSize(400, 500);
StatusPage pageStatus = InitPageProgress();
SetContent(pageStatus);
2024-09-08 22:37:06 +02:00
}
2024-09-14 17:49:20 +02:00
public void Start() {
2024-09-08 22:37:06 +02:00
_application.AddWindow(this);
Present();
}
2024-09-14 17:49:20 +02:00
private StatusPage InitPageProgress() {
2024-09-08 22:37:06 +02:00
StatusPage pageStatus = StatusPage.New();
pageStatus.SetTitle("Konvertierung wird ausgeführt...");
pageStatus.SetDescription("Das kann eine Weile dauern.");
pageStatus.SetChild(_progress);
return pageStatus;
}
2024-09-14 17:49:20 +02:00
private static ProgressBar InitProgressBar() {
2024-09-08 22:37:06 +02:00
ProgressBar progressBar = ProgressBar.New();
progressBar.WidthRequest = 300;
progressBar.SetHalign(Align.Center);
progressBar.SetMarginTop(10);
progressBar.SetMarginBottom(12);
progressBar.SetShowText(true);
progressBar.AddCssClass("installer");
progressBar.SetFraction(0);
progressBar.SetText("");
return progressBar;
}
2024-09-14 17:49:20 +02:00
public void SetProgress(double progress) {
2024-09-08 22:37:06 +02:00
_progress.SetFraction(progress);
_progress.SetText(string.Format("{0:p0}", progress));
}
2024-09-14 17:49:20 +02:00
private StatusPage InitPageSuccess(int number, string dest) {
2024-09-08 22:37:06 +02:00
StatusPage pageStatus = StatusPage.New();
pageStatus.SetTitle("Die Konvertierung wurde erfolgreich abgeschlossen");
pageStatus.SetDescription($"{number} Dateien wurden erfolgreich konvertiert.");
pageStatus.SetIconName("selection-mode-symbolic");
Button btnOpen = Button.NewWithLabel("Ordner öffnen");
2024-09-14 17:49:20 +02:00
btnOpen.OnClicked += async (_, _) => {
2024-09-14 23:22:12 +02:00
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
FileLauncher fileLauncher = FileLauncher.New(Gio.FileHelper.NewForPath(dest));
await fileLauncher.LaunchAsync(_mainWindow);
} else {
Process.Start("explorer.exe", @$"{dest}");
}
2024-09-08 22:37:06 +02:00
Close();
};
btnOpen.SetCssClasses(["pill", "suggested-action"]);
btnOpen.SetHalign(Align.Center);
pageStatus.SetChild(btnOpen);
return pageStatus;
}
2024-09-14 17:49:20 +02:00
public void Success(int number, string dest) {
2024-09-08 22:37:06 +02:00
StatusPage pageSuccess = InitPageSuccess(number, dest);
SetContent(pageSuccess);
2024-09-08 22:37:06 +02:00
}
2024-09-14 17:49:20 +02:00
private StatusPage InitPagePartial(int number, string dest, ConvertError[] convertErrors) {
2024-09-08 22:37:06 +02:00
StatusPage pageStatus = StatusPage.New();
pageStatus.SetTitle("Bei der Konvertierung sind Fehler aufgetreten");
pageStatus.SetDescription($"{number - convertErrors.Length} von {number} Dateien wurden erfolgreich konvertiert.");
pageStatus.SetIconName("dialog-warning-symbolic");
Box box = Box.New(Orientation.Vertical, 36);
box.SetHalign(Align.Center);
2024-09-14 17:49:20 +02:00
if (convertErrors.Length < number) {
2024-09-08 22:37:06 +02:00
Button btnOpen = Button.NewWithLabel("Ordner öffnen");
2024-09-14 17:49:20 +02:00
btnOpen.OnClicked += async (_, _) => {
2024-09-14 23:22:12 +02:00
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
FileLauncher fileLauncher = FileLauncher.New(Gio.FileHelper.NewForPath(dest));
await fileLauncher.LaunchAsync(_mainWindow);
} else {
Process.Start("explorer.exe", @$"{dest}");
}
2024-09-08 22:37:06 +02:00
Close();
};
btnOpen.SetCssClasses(["pill", "suggested-action"]);
btnOpen.SetHalign(Align.Center);
box.Append(btnOpen);
}
ScrolledWindow scrolled = ScrolledWindow.New();
scrolled.SetMinContentWidth(328);
scrolled.SetMinContentHeight(200);
scrolled.SetOverflow(Overflow.Hidden);
StringBuilder sb = new StringBuilder();
2024-09-14 17:49:20 +02:00
foreach (ConvertError err in convertErrors) {
2024-09-08 22:37:06 +02:00
sb.AppendLine($"{err.File}: {err.Message}");
}
Label label = Label.New(sb.ToString());
label.SetHexpand(true);
label.SetValign(Align.Fill);
label.SetWrap(false);
label.SetSelectable(true);
label.SetXalign(0);
label.SetYalign(0);
label.SetMarginStart(10);
label.SetMarginEnd(10);
label.SetMarginTop(5);
label.SetMarginBottom(5);
label.SetCssClasses(["monospace", "terminal"]);
scrolled.SetChild(label);
scrolled.AddCssClass("card");
box.Append(scrolled);
pageStatus.SetChild(box);
return pageStatus;
}
2024-09-14 17:49:20 +02:00
public void Partial(int number, string dest, ConvertError[] convertErrors) {
2024-09-08 22:37:06 +02:00
StatusPage pagePartial = InitPagePartial(number, dest, convertErrors);
SetContent(pagePartial);
2024-09-08 22:37:06 +02:00
}
2024-09-14 17:49:20 +02:00
private StatusPage InitPageError(string error) {
2024-09-08 22:37:06 +02:00
StatusPage pageStatus = StatusPage.New();
pageStatus.SetTitle("Konvertierung fehlgeschlagen");
pageStatus.SetDescription("Es ist ein Fehler aufgetreten.");
pageStatus.SetIconName("dialog-error-symbolic");
Box box = Box.New(Orientation.Vertical, 50);
box.SetHalign(Align.Center);
ScrolledWindow scrolled = ScrolledWindow.New();
scrolled.SetMinContentWidth(328);
scrolled.SetMinContentHeight(200);
scrolled.SetOverflow(Overflow.Hidden);
Label label = Label.New(error);
label.SetHexpand(true);
label.SetValign(Align.Fill);
label.SetWrap(true);
label.SetSelectable(true);
label.SetXalign(0);
label.SetYalign(0);
label.SetMarginStart(10);
label.SetMarginEnd(10);
label.SetMarginTop(5);
label.SetMarginBottom(5);
label.SetCssClasses(["monospace", "terminal"]);
scrolled.SetChild(label);
scrolled.AddCssClass("card");
Button button = Button.NewWithLabel("Schließen");
button.OnClicked += (_, _) => Close();
button.SetCssClasses(["pill"]);
button.SetHalign(Align.Center);
box.Append(scrolled);
box.Append(button);
pageStatus.SetChild(box);
return pageStatus;
}
2024-09-14 17:49:20 +02:00
public void Error(string error) {
2024-09-08 22:37:06 +02:00
StatusPage pageError = InitPageError(error);
SetContent(pageError);
2024-09-08 22:37:06 +02:00
}
}