From d339a7d339b9eecee57170946eb61b06aa7cab4d Mon Sep 17 00:00:00 2001 From: DaPigGuy Date: Fri, 3 Mar 2023 09:32:06 -0800 Subject: [PATCH] GNOME - Port account settings to Blueprint --- .../Blueprints/account_settings_dialog.blp | 120 ++++++++ .../Views/AccountSettingsDialog.cs | 263 ++++++------------ 2 files changed, 199 insertions(+), 184 deletions(-) create mode 100644 NickvisionMoney.GNOME/Blueprints/account_settings_dialog.blp diff --git a/NickvisionMoney.GNOME/Blueprints/account_settings_dialog.blp b/NickvisionMoney.GNOME/Blueprints/account_settings_dialog.blp new file mode 100644 index 000000000..17a0796d9 --- /dev/null +++ b/NickvisionMoney.GNOME/Blueprints/account_settings_dialog.blp @@ -0,0 +1,120 @@ +using Gtk 4.0; +using Adw 1; + +Adw.MessageDialog _root { + heading: _("AccountSettings"); + default-width: 450; + hide-on-close: true; + modal: true; + + Gtk.Box { + orientation: vertical; + spacing: 16; + margin-top: 4; + + Adw.PreferencesGroup { + Adw.EntryRow _nameRow { + title: _("Name.Field"); + } + + Adw.ComboRow _accountTypeRow { + title: _("AccountType.Field"); + subtitle: _("AccountType.Description"); + subtitle-lines: 4; + model: Gtk.StringList { + strings [_("AccountType.Checking"), _("AccountType.Savings"), _("AccountType.Business")] + }; + } + + Adw.ActionRow { + title: _("DefaultTransactionType.Field"); + + [suffix] + Gtk.Box { + orientation: horizontal; + valign: center; + + Gtk.ToggleButton _incomeButton { + label: _("Income"); + active: bind _expenseButton.active inverted bidirectional; + } + + Gtk.ToggleButton _expenseButton { + label: _("Expense"); + } + + styles ["linked"] + } + } + } + + Gtk.Label _reportedCurrencyLabel { + use-markup: true; + justify: center; + } + + Adw.PreferencesGroup { + Adw.ExpanderRow _customCurrencyRow { + title: _("UseCustomCurrency.Field"); + show-enable-switch: true; + enable-expansion: false; + + Adw.ActionRow _customSymbolRow { + title: _("CustomCurrencySymbol.Field"); + + [suffix] + Gtk.Entry _customSymbolText { + valign: center; + max-length: 3; + placeholder-text: _("CustomCurrencySymbol.Placeholder"); + activates-default: true; + } + } + + Adw.ActionRow _customCodeRow { + title: _("CustomCurrencyCode.Field"); + + [suffix] + Gtk.Entry _customCodeText { + valign: center; + max-length: 3; + placeholder-text: _("CustomCurrencyCode.Placeholder"); + activates-default: true; + } + } + } + } + + Adw.PreferencesGroup { + Adw.ExpanderRow _passwordRow { + title: _("ManagePassword"); + subtitle: _("ManagePassword.Description"); + icon-name: "dialog-password-symbolic"; + show-enable-switch: true; + enable-expansion: false; + + Adw.PasswordEntryRow _newPasswordRow { + title: _("NewPassword.Field"); + } + + Adw.PasswordEntryRow _newPasswordConfirmRow { + title: _("ConfirmPassword.Field"); + } + + Adw.ActionRow { + subtitle: _("ManagePassword.Warning"); + + [suffix] + Gtk.Button _removePasswordButton { + label: _("Remove"); + valign: center; + + styles ["destructive-action"] + } + + styles ["warning"] + } + } + } + } +} \ No newline at end of file diff --git a/NickvisionMoney.GNOME/Views/AccountSettingsDialog.cs b/NickvisionMoney.GNOME/Views/AccountSettingsDialog.cs index 2f8e3bb4f..645e482e7 100644 --- a/NickvisionMoney.GNOME/Views/AccountSettingsDialog.cs +++ b/NickvisionMoney.GNOME/Views/AccountSettingsDialog.cs @@ -1,3 +1,4 @@ +using NickvisionMoney.GNOME.Helpers; using NickvisionMoney.Shared.Controllers; using NickvisionMoney.Shared.Models; using System; @@ -8,69 +9,48 @@ namespace NickvisionMoney.GNOME.Views; /// /// A dialog to configure account /// -public partial class AccountSettingsDialog +public partial class AccountSettingsDialog : Adw.MessageDialog { [LibraryImport("libadwaita-1.so.0", StringMarshalling = StringMarshalling.Utf8)] private static partial void gtk_css_provider_load_from_data(nint provider, string data, int length); private bool _constructing; private readonly AccountSettingsDialogController _controller; - private readonly Adw.MessageDialog _dialog; - private readonly Gtk.Box _boxMain; - private readonly Adw.PreferencesGroup _grpAccount; - private readonly Adw.EntryRow _rowName; - private readonly Adw.ComboRow _rowAccountType; - private readonly Gtk.ToggleButton _btnIncome; - private readonly Gtk.ToggleButton _btnExpense; - private readonly Gtk.Box _boxTypeButtons; - private readonly Adw.ActionRow _rowTransactionType; - private readonly Gtk.Label _lblReportedCurrency; - private readonly Adw.PreferencesGroup _grpCurrency; - private readonly Adw.ExpanderRow _rowCustomCurrency; - private readonly Gtk.Entry _txtCustomSymbol; - private readonly Adw.ActionRow _rowCustomSymbol; - private readonly Gtk.Entry _txtCustomCode; - private readonly Adw.ActionRow _rowCustomCode; - private readonly Adw.PreferencesGroup _grpPassword; - private readonly Adw.ExpanderRow _rowPassword; - private readonly Adw.PasswordEntryRow _rowNewPassword; - private readonly Adw.PasswordEntryRow _rowNewPasswordConfirm; - private readonly Adw.ActionRow _rowRemovePassword; - private readonly Gtk.Button _btnRemovePassword; - /// - /// Constructs an AccountSettingsDialog - /// - /// AccountSettingsDialogController - /// Gtk.Window - public AccountSettingsDialog(AccountSettingsDialogController controller, Gtk.Window parentWindow) + [Gtk.Connect] private readonly Adw.EntryRow _nameRow; + [Gtk.Connect] private readonly Adw.ComboRow _accountTypeRow; + [Gtk.Connect] private readonly Gtk.ToggleButton _incomeButton; + [Gtk.Connect] private readonly Gtk.ToggleButton _expenseButton; + [Gtk.Connect] private readonly Gtk.Label _reportedCurrencyLabel; + [Gtk.Connect] private readonly Adw.ExpanderRow _customCurrencyRow; + [Gtk.Connect] private readonly Gtk.Entry _customSymbolText; + [Gtk.Connect] private readonly Adw.ActionRow _customSymbolRow; + [Gtk.Connect] private readonly Gtk.Entry _customCodeText; + [Gtk.Connect] private readonly Adw.ActionRow _customCodeRow; + [Gtk.Connect] private readonly Adw.ExpanderRow _passwordRow; + [Gtk.Connect] private readonly Adw.PasswordEntryRow _newPasswordRow; + [Gtk.Connect] private readonly Adw.PasswordEntryRow _newPasswordConfirmRow; + [Gtk.Connect] private readonly Gtk.Button _removePasswordButton; + + private AccountSettingsDialog(Gtk.Builder builder, AccountSettingsDialogController controller, Gtk.Window parent) : base(builder.GetPointer("_root"), false) { _constructing = true; _controller = controller; //Dialog Settings - _dialog = Adw.MessageDialog.New(parentWindow, _controller.Localizer["AccountSettings"], ""); - _dialog.SetDefaultSize(450, -1); - _dialog.SetHideOnClose(true); - _dialog.SetModal(true); + SetTransientFor(parent); if (!_controller.NeedsSetup) { - _dialog.AddResponse("cancel", _controller.Localizer["Cancel"]); - _dialog.SetCloseResponse("cancel"); + AddResponse("cancel", _controller.Localizer["Cancel"]); + SetCloseResponse("cancel"); } - _dialog.AddResponse("ok", _controller.Localizer["Apply"]); - _dialog.SetDefaultResponse("ok"); - _dialog.SetResponseAppearance("ok", Adw.ResponseAppearance.Suggested); - _dialog.OnResponse += (sender, e) => _controller.Accepted = e.Response == "ok"; - //Main Box - _boxMain = Gtk.Box.New(Gtk.Orientation.Vertical, 16); - _boxMain.SetMarginTop(4); - //Preferences Group - _grpAccount = Adw.PreferencesGroup.New(); - _boxMain.Append(_grpAccount); + AddResponse("ok", _controller.Localizer["Apply"]); + SetDefaultResponse("ok"); + SetResponseAppearance("ok", Adw.ResponseAppearance.Suggested); + OnResponse += (sender, e) => _controller.Accepted = e.Response == "ok"; + //Build UI + builder.Connect(this); //Account Name - _rowName = Adw.EntryRow.New(); - _rowName.SetTitle(_controller.Localizer["Name", "Field"]); - _rowName.OnNotify += (sender, e) => + _nameRow.OnNotify += (sender, e) => { if (e.Pspec.GetName() == "text") { @@ -80,49 +60,21 @@ public AccountSettingsDialog(AccountSettingsDialogController controller, Gtk.Win } } }; - _grpAccount.Add(_rowName); //Account Type - _rowAccountType = Adw.ComboRow.New(); - _rowAccountType.SetModel(Gtk.StringList.New(new string[3] { _controller.Localizer["AccountType", "Checking"], _controller.Localizer["AccountType", "Savings"], _controller.Localizer["AccountType", "Business"] })); - _rowAccountType.OnNotify += (sender, e) => + _accountTypeRow.OnNotify += (sender, e) => { if (e.Pspec.GetName() == "selected-item") { Validate(); } }; - _rowAccountType.SetTitle(_controller.Localizer["AccountType", "Field"]); - _rowAccountType.SetSubtitle(_controller.Localizer["AccountType", "Description"]); - _rowAccountType.SetSubtitleLines(4); - _grpAccount.Add(_rowAccountType); //Default Transaction Type - _btnIncome = Gtk.ToggleButton.NewWithLabel(_controller.Localizer["Income"]); - _btnIncome.OnToggled += OnTransactionTypeChanged; - _btnExpense = Gtk.ToggleButton.NewWithLabel(_controller.Localizer["Expense"]); - _btnExpense.OnToggled += OnTransactionTypeChanged; - _btnExpense.BindProperty("active", _btnIncome, "active", (GObject.BindingFlags.Bidirectional | GObject.BindingFlags.SyncCreate | GObject.BindingFlags.InvertBoolean)); - _boxTypeButtons = Gtk.Box.New(Gtk.Orientation.Horizontal, 0); - _boxTypeButtons.SetValign(Gtk.Align.Center); - _boxTypeButtons.AddCssClass("linked"); - _boxTypeButtons.Append(_btnIncome); - _boxTypeButtons.Append(_btnExpense); - _rowTransactionType = Adw.ActionRow.New(); - _rowTransactionType.SetTitle(_controller.Localizer["DefaultTransactionType", "Field"]); - _rowTransactionType.AddSuffix(_boxTypeButtons); - _grpAccount.Add(_rowTransactionType); + _incomeButton.OnToggled += OnTransactionTypeChanged; + _expenseButton.OnToggled += OnTransactionTypeChanged; //Reported Currency - _lblReportedCurrency = Gtk.Label.New($"{_controller.Localizer["ReportedCurrency"]}\n{_controller.ReportedCurrencyString}"); - _lblReportedCurrency.SetUseMarkup(true); - _lblReportedCurrency.SetJustify(Gtk.Justification.Center); - _boxMain.Append(_lblReportedCurrency); + _reportedCurrencyLabel.SetLabel($"{_controller.Localizer["ReportedCurrency"]}\n{_controller.ReportedCurrencyString}"); //Custom Currency - _grpCurrency = Adw.PreferencesGroup.New(); - _boxMain.Append(_grpCurrency); - _rowCustomCurrency = Adw.ExpanderRow.New(); - _rowCustomCurrency.SetTitle(_controller.Localizer["UseCustomCurrency", "Field"]); - _rowCustomCurrency.SetShowEnableSwitch(true); - _rowCustomCurrency.SetEnableExpansion(false); - _rowCustomCurrency.OnNotify += (sender, e) => + _customCurrencyRow.OnNotify += (sender, e) => { if (e.Pspec.GetName() == "enable-expansion") { @@ -132,13 +84,7 @@ public AccountSettingsDialog(AccountSettingsDialogController controller, Gtk.Win } } }; - _grpCurrency.Add(_rowCustomCurrency); - _txtCustomSymbol = Gtk.Entry.New(); - _txtCustomSymbol.SetValign(Gtk.Align.Center); - _txtCustomSymbol.SetMaxLength(3); - _txtCustomSymbol.SetPlaceholderText(_controller.Localizer["CustomCurrencySymbol", "Placeholder"]); - _txtCustomSymbol.SetActivatesDefault(true); - _txtCustomSymbol.OnNotify += (sender, e) => + _customSymbolText.OnNotify += (sender, e) => { if (e.Pspec.GetName() == "text") { @@ -148,16 +94,7 @@ public AccountSettingsDialog(AccountSettingsDialogController controller, Gtk.Win } } }; - _rowCustomSymbol = Adw.ActionRow.New(); - _rowCustomSymbol.SetTitle(_controller.Localizer["CustomCurrencySymbol", "Field"]); - _rowCustomSymbol.AddSuffix(_txtCustomSymbol); - _rowCustomCurrency.AddRow(_rowCustomSymbol); - _txtCustomCode = Gtk.Entry.New(); - _txtCustomCode.SetValign(Gtk.Align.Center); - _txtCustomCode.SetMaxLength(3); - _txtCustomCode.SetPlaceholderText(_controller.Localizer["CustomCurrencyCode", "Placeholder"]); - _txtCustomCode.SetActivatesDefault(true); - _txtCustomCode.OnNotify += (sender, e) => + _customCodeText.OnNotify += (sender, e) => { if (e.Pspec.GetName() == "text") { @@ -167,20 +104,8 @@ public AccountSettingsDialog(AccountSettingsDialogController controller, Gtk.Win } } }; - _rowCustomCode = Adw.ActionRow.New(); - _rowCustomCode.SetTitle(_controller.Localizer["CustomCurrencyCode", "Field"]); - _rowCustomCode.AddSuffix(_txtCustomCode); - _rowCustomCurrency.AddRow(_rowCustomCode); //Password Row - _grpPassword = Adw.PreferencesGroup.New(); - _boxMain.Append(_grpPassword); - _rowPassword = Adw.ExpanderRow.New(); - _rowPassword.SetTitle(_controller.Localizer["ManagePassword"]); - _rowPassword.SetSubtitle(_controller.Localizer["ManagePassword", "Description"]); - _rowPassword.SetIconName("dialog-password-symbolic"); - _rowPassword.SetShowEnableSwitch(true); - _rowPassword.SetEnableExpansion(false); - _rowPassword.OnNotify += (sender, e) => + _passwordRow.OnNotify += (sender, e) => { if (e.Pspec.GetName() == "enable-expansion") { @@ -190,10 +115,7 @@ public AccountSettingsDialog(AccountSettingsDialogController controller, Gtk.Win } } }; - _grpPassword.Add(_rowPassword); - _rowNewPassword = Adw.PasswordEntryRow.New(); - _rowNewPassword.SetTitle(_controller.Localizer["NewPassword", "Field"]); - _rowNewPassword.OnNotify += (sender, e) => + _newPasswordRow.OnNotify += (sender, e) => { if (e.Pspec.GetName() == "text") { @@ -203,10 +125,7 @@ public AccountSettingsDialog(AccountSettingsDialogController controller, Gtk.Win } } }; - _rowPassword.AddRow(_rowNewPassword); - _rowNewPasswordConfirm = Adw.PasswordEntryRow.New(); - _rowNewPasswordConfirm.SetTitle(_controller.Localizer["ConfirmPassword", "Field"]); - _rowNewPasswordConfirm.OnNotify += (sender, e) => + _newPasswordConfirmRow.OnNotify += (sender, e) => { if (e.Pspec.GetName() == "text") { @@ -216,94 +135,70 @@ public AccountSettingsDialog(AccountSettingsDialogController controller, Gtk.Win } } }; - _rowPassword.AddRow(_rowNewPasswordConfirm); - _rowRemovePassword = Adw.ActionRow.New(); - _rowRemovePassword.SetSubtitle(_controller.Localizer["ManagePassword", "Warning"]); - _rowRemovePassword.AddCssClass("warning"); - _rowPassword.AddRow(_rowRemovePassword); - _btnRemovePassword = Gtk.Button.NewWithLabel(_controller.Localizer["Remove"]); - _btnRemovePassword.AddCssClass("destructive-action"); - _btnRemovePassword.SetValign(Gtk.Align.Center); - _btnRemovePassword.SetVisible(_controller.IsEncrypted); - _btnRemovePassword.OnClicked += OnRemovePassword; - _rowRemovePassword.AddSuffix(_btnRemovePassword); - //Layout - _dialog.SetExtraChild(_boxMain); + _removePasswordButton.OnClicked += OnRemovePassword; //Load - _rowName.SetText(_controller.Metadata.Name); - _rowAccountType.SetSelected((uint)_controller.Metadata.AccountType); - _btnIncome.SetActive(_controller.Metadata.DefaultTransactionType == TransactionType.Income); - _rowCustomCurrency.SetEnableExpansion(_controller.Metadata.UseCustomCurrency); - _txtCustomSymbol.SetText(_controller.Metadata.CustomCurrencySymbol ?? ""); - _txtCustomCode.SetText(_controller.Metadata.CustomCurrencyCode ?? ""); + _nameRow.SetText(_controller.Metadata.Name); + _accountTypeRow.SetSelected((uint)_controller.Metadata.AccountType); + _incomeButton.SetActive(_controller.Metadata.DefaultTransactionType == TransactionType.Income); + _customCurrencyRow.SetEnableExpansion(_controller.Metadata.UseCustomCurrency); + _customSymbolText.SetText(_controller.Metadata.CustomCurrencySymbol ?? ""); + _customCodeText.SetText(_controller.Metadata.CustomCurrencyCode ?? ""); Validate(); _constructing = false; } - public event GObject.SignalHandler OnResponse - { - add - { - _dialog.OnResponse += value; - } - remove - { - _dialog.OnResponse -= value; - } - } - - /// - /// Shows the dialog - /// - public void Show() => _dialog.Show(); /// - /// Destroys the dialog + /// Constructs an AccountSettingsDialog /// - public void Destroy() => _dialog.Destroy(); + /// AccountSettingsDialogController + /// Gtk.Window + public AccountSettingsDialog(AccountSettingsDialogController controller, Gtk.Window parent) : this(Builder.FromFile("account_settings_dialog.ui", controller.Localizer), controller, parent) + { + } /// /// Validates the dialog's input /// private void Validate() { - var transactionType = _btnIncome.GetActive() ? TransactionType.Income : TransactionType.Expense; + var transactionType = _incomeButton.GetActive() ? TransactionType.Income : TransactionType.Expense; var newPassword = ""; var newPasswordConfirm = ""; - if (_rowPassword.GetEnableExpansion()) + if (_passwordRow.GetEnableExpansion()) { - newPassword = _rowNewPassword.GetText(); - newPasswordConfirm = _rowNewPasswordConfirm.GetText(); + newPassword = _newPasswordRow.GetText(); + newPasswordConfirm = _newPasswordConfirmRow.GetText(); } - var checkStatus = _controller.UpdateMetadata(_rowName.GetText(), (AccountType)_rowAccountType.GetSelected(), _rowCustomCurrency.GetEnableExpansion(), _txtCustomSymbol.GetText(), _txtCustomCode.GetText(), transactionType, newPassword, newPasswordConfirm); - _rowName.RemoveCssClass("error"); - _rowName.SetTitle(_controller.Localizer["Name", "Field"]); - _rowCustomSymbol.RemoveCssClass("error"); - _rowCustomSymbol.SetTitle(_controller.Localizer["CustomCurrencySymbol", "Field"]); - _rowCustomCode.RemoveCssClass("error"); - _rowCustomCode.SetTitle(_controller.Localizer["CustomCurrencyCode", "Field"]); + var checkStatus = _controller.UpdateMetadata(_nameRow.GetText(), (AccountType)_accountTypeRow.GetSelected(), _customCurrencyRow.GetEnableExpansion(), _customSymbolText.GetText(), _customCodeText.GetText(), transactionType, newPassword, newPasswordConfirm); + _nameRow.RemoveCssClass("error"); + _nameRow.SetTitle(_controller.Localizer["Name", "Field"]); + _customSymbolRow.RemoveCssClass("error"); + _customSymbolRow.SetTitle(_controller.Localizer["CustomCurrencySymbol", "Field"]); + _customCodeRow.RemoveCssClass("error"); + _customCodeRow.SetTitle(_controller.Localizer["CustomCurrencyCode", "Field"]); if (checkStatus == AccountMetadataCheckStatus.Valid) { - _dialog.SetResponseEnabled("ok", true); + SetResponseEnabled("ok", true); } else { if (checkStatus.HasFlag(AccountMetadataCheckStatus.EmptyName)) { - _rowName.AddCssClass("error"); - _rowName.SetTitle(_controller.Localizer["Name", "Empty"]); + _nameRow.AddCssClass("error"); + _nameRow.SetTitle(_controller.Localizer["Name", "Empty"]); } if (checkStatus.HasFlag(AccountMetadataCheckStatus.EmptyCurrencySymbol)) { - _rowCustomSymbol.AddCssClass("error"); - _rowCustomSymbol.SetTitle(_controller.Localizer["CustomCurrencySymbol", "Empty"]); + _customSymbolRow.AddCssClass("error"); + _customSymbolRow.SetTitle(_controller.Localizer["CustomCurrencySymbol", "Empty"]); } if (checkStatus.HasFlag(AccountMetadataCheckStatus.EmptyCurrencyCode)) { - _rowCustomCode.AddCssClass("error"); - _rowCustomCode.SetTitle(_controller.Localizer["CustomCurrencyCode", "Empty"]); + _customCodeRow.AddCssClass("error"); + _customCodeRow.SetTitle(_controller.Localizer["CustomCurrencyCode", "Empty"]); } - _dialog.SetResponseEnabled("ok", false); + SetResponseEnabled("ok", false); } } @@ -314,15 +209,15 @@ private void Validate() /// EventArgs private void OnTransactionTypeChanged(Gtk.ToggleButton sender, EventArgs e) { - if (_btnIncome.GetActive()) + if (_incomeButton.GetActive()) { - _btnIncome.AddCssClass("denaro-income"); - _btnExpense.RemoveCssClass("denaro-expense"); + _incomeButton.AddCssClass("denaro-income"); + _expenseButton.RemoveCssClass("denaro-expense"); } else { - _btnIncome.RemoveCssClass("denaro-income"); - _btnExpense.AddCssClass("denaro-expense"); + _incomeButton.RemoveCssClass("denaro-income"); + _expenseButton.AddCssClass("denaro-expense"); } if (!_constructing) { @@ -338,9 +233,9 @@ private void OnTransactionTypeChanged(Gtk.ToggleButton sender, EventArgs e) private void OnRemovePassword(Gtk.Button sender, EventArgs e) { _controller.SetRemovePassword(); - _rowPassword.SetEnableExpansion(false); - _rowPassword.SetSensitive(false); - _rowPassword.SetTitle(_controller.Localizer["PasswordRemoveRequest.GTK"]); - _rowPassword.SetSubtitle(""); + _passwordRow.SetEnableExpansion(false); + _passwordRow.SetSensitive(false); + _passwordRow.SetTitle(_controller.Localizer["PasswordRemoveRequest.GTK"]); + _passwordRow.SetSubtitle(""); } }