photomator/Photomator/Views/StatusDialog.cs

215 lines
6.6 KiB
C#

using Gtk;
using Adw;
using System.Text;
namespace Photomator.Views;
public partial class StatusDialog : Adw.Window
{
private readonly Adw.Application _application;
private readonly Adw.ApplicationWindow _mainWindow;
private readonly ViewStack _stack;
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);
_stack = ViewStack.New();
_stack.SetVexpand(true);
StatusPage pageStatus = InitPageProgress();
_stack.AddNamed(pageStatus, "page-status");
SetContent(_stack);
}
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 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 (_, _) =>
{
FileLauncher fileLauncher = FileLauncher.New(Gio.FileHelper.NewForPath(dest));
await fileLauncher.LaunchAsync(_mainWindow);
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);
_stack.AddNamed(pageSuccess, "page-success");
_stack.SetVisibleChildName("page-success");
}
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 (_, _) =>
{
FileLauncher fileLauncher = FileLauncher.New(Gio.FileHelper.NewForPath(dest));
await fileLauncher.LaunchAsync(_mainWindow);
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);
_stack.AddNamed(pagePartial, "page-partial");
_stack.SetVisibleChildName("page-partial");
}
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);
_stack.AddNamed(pageError, "page-error");
_stack.SetVisibleChildName("page-error");
}
}