photomator/Photomator/Views/StatusDialog.cs
Denys Konovalov a299ec043b Various fixes for Windows version
- new installer based on InnoSetup
- add MIT license
- produce WinExe output
- other small fixes
2024-09-15 14:38:59 +02:00

202 lines
6.6 KiB
C#

using Gtk;
using Adw;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace Photomator.Views;
public partial class StatusDialog : Adw.Window {
private readonly Adw.Application _application;
private readonly Adw.ApplicationWindow _mainWindow;
private readonly ProgressBar _progress;
public StatusDialog(Adw.Application application, Adw.ApplicationWindow parent) : base() {
_application = application;
_mainWindow = parent;
_progress = InitProgressBar();
SetModal(true);
SetTransientFor(parent);
SetDeletable(true);
SetDefaultSize(400, 500);
StatusPage pageStatus = InitPageProgress();
SetContent(pageStatus);
}
public void Start() {
_application.AddWindow(this);
Present();
}
private StatusPage InitPageProgress() {
StatusPage pageStatus = StatusPage.New();
pageStatus.SetTitle("Konvertierung wird ausgeführt...");
pageStatus.SetDescription("Das kann eine Weile dauern.");
pageStatus.SetChild(_progress);
return pageStatus;
}
private static ProgressBar InitProgressBar() {
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;
}
public void SetProgress(double progress) {
_progress.SetFraction(progress);
_progress.SetText(string.Format("{0:p0}", progress));
}
private StatusPage InitPageSuccess(int number, string dest) {
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");
btnOpen.OnClicked += async (_, _) => {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
FileLauncher fileLauncher = FileLauncher.New(Gio.FileHelper.NewForPath(dest));
await fileLauncher.LaunchAsync(_mainWindow);
} else {
Process.Start("explorer.exe", @$"{dest}");
}
Close();
};
btnOpen.SetCssClasses(["pill", "suggested-action"]);
btnOpen.SetHalign(Align.Center);
pageStatus.SetChild(btnOpen);
return pageStatus;
}
public void Success(int number, string dest) {
StatusPage pageSuccess = InitPageSuccess(number, dest);
SetContent(pageSuccess);
}
private StatusPage InitPagePartial(int number, string dest, ConvertError[] convertErrors) {
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);
if (convertErrors.Length < number) {
Button btnOpen = Button.NewWithLabel("Ordner öffnen");
btnOpen.OnClicked += async (_, _) => {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
FileLauncher fileLauncher = FileLauncher.New(Gio.FileHelper.NewForPath(dest));
await fileLauncher.LaunchAsync(_mainWindow);
} else {
Process.Start("explorer.exe", @$"{dest}");
}
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();
foreach (ConvertError err in convertErrors) {
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;
}
public void Partial(int number, string dest, ConvertError[] convertErrors) {
StatusPage pagePartial = InitPagePartial(number, dest, convertErrors);
SetContent(pagePartial);
}
private StatusPage InitPageError(string error) {
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;
}
public void Error(string error) {
StatusPage pageError = InitPageError(error);
SetContent(pageError);
}
}