Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(new-account): use dialog for settings #993

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 4 additions & 21 deletions data/ui/dialogs/new_account.ui
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@
<signal name="entry_activated" handler="on_next_clicked" swapped="no"/>
<signal name="changed" handler="clear_errors" swapped="no"/>
<child type="suffix">
<object class="GtkToggleButton" id="settings_toggle">
<object class="GtkButton" id="settings_toggle">
<property name="icon-name">tuba-settings-symbolic</property>
<property name="valign">center</property>
<!-- translators: this is a button in the 'new account' window -->
<property name="tooltip-text" translatable="yes">Settings</property>
<signal name="clicked" handler="on_settings_clicked" swapped="no"/>
<style>
<class name="circular" />
<class name="flat" />
Expand All @@ -85,24 +86,6 @@
</child>
</object>
</child>
<child>
<object class="AdwEntryRow" id="proxy_entry">
<property name="title" translatable="yes">Proxy</property>
<property name="visible">0</property>
<property name="show-apply-button">1</property>
<property name="input_purpose">url</property>
<signal name="apply" handler="on_proxy_apply" swapped="no"/>
</object>
</child>
<child>
<object class="AdwSwitchRow" id="admin_switch">
<property name="visible">0</property>
<!-- translators: Switch title in the new account window -->
<property name="title" translatable="yes">Admin</property>
<!-- translators: Switch description in the new account window -->
<property name="subtitle" translatable="yes">Enables the Admin Dashboard and requests the needed permissions</property>
</object>
</child>
</object>

</child>
Expand Down Expand Up @@ -141,9 +124,9 @@
<object class="GtkButton">
<property name="receives_default">1</property>
<property name="label" translatable="yes">Next</property>
<property name="visible" bind-source="TubaDialogsNewAccount" bind-property="use-auto-auth" bind-flags="sync-create|invert-boolean"/>"
<property name="visible" bind-source="TubaDialogsNewAccount" bind-property="use-auto-auth" bind-flags="sync-create|invert-boolean"/>
<property name="sensitive" bind-source="TubaDialogsNewAccount" bind-property="is_working" bind-flags="sync-create|invert-boolean"/>
<signal name="clicked" handler="on_next_clicked" swapped="no"/>"
<signal name="clicked" handler="on_next_clicked" swapped="no"/>
<style>
<class name="suggested-action"/>
</style>
Expand Down
121 changes: 106 additions & 15 deletions src/Dialogs/NewAccount.vala
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ public class Tuba.Dialogs.NewAccount: Adw.Window {
protected string? redirect_uri { get; set; }
protected bool use_auto_auth { get; set; default = SHOULD_AUTO_AUTH; }
protected InstanceAccount account { get; set; default = new InstanceAccount.empty (""); }
protected bool can_access_settings { get; set; default=false; }
protected bool admin_mode { get; set; default=false; }

[GtkChild] unowned Adw.ToastOverlay toast_overlay;
[GtkChild] unowned Adw.NavigationView deck;
[GtkChild] unowned Adw.NavigationPage instance_step;
[GtkChild] unowned Adw.NavigationPage code_step;
[GtkChild] unowned Adw.NavigationPage done_step;

[GtkChild] unowned Gtk.ToggleButton settings_toggle;
[GtkChild] unowned Adw.EntryRow proxy_entry;
[GtkChild] unowned Adw.SwitchRow admin_switch;

[GtkChild] unowned Adw.EntryRow instance_entry;
[GtkChild] unowned Gtk.Label instance_entry_error;

Expand All @@ -39,13 +37,14 @@ public class Tuba.Dialogs.NewAccount: Adw.Window {
[GtkChild] unowned Gtk.Label manual_auth_label;

public string get_full_scopes () {
if (admin_switch.active) return @"$SCOPES $ADMIN_SCOPES";
if (this.admin_mode) return @"$SCOPES $ADMIN_SCOPES";

return SCOPES;
}

public NewAccount (bool can_access_settings = false) {
Object (transient_for: app.main_window);
this.can_access_settings = can_access_settings;
app.add_account_window = this;
app.add_window (this);

Expand All @@ -55,10 +54,8 @@ public class Tuba.Dialogs.NewAccount: Adw.Window {
});

if (!can_access_settings) {
settings_toggle.bind_property ("active", proxy_entry, "visible", BindingFlags.SYNC_CREATE);
app.toast.connect (add_toast);
}
settings_toggle.bind_property ("active", admin_switch, "visible", BindingFlags.SYNC_CREATE);

manual_auth_label.activate_link.connect (on_manual_auth);

Expand Down Expand Up @@ -96,8 +93,7 @@ public class Tuba.Dialogs.NewAccount: Adw.Window {
throw new Oopsie.INTERNAL ("Using manual auth method");

return "tuba://auth_code";
}
catch (Error e) {
} catch (Error e) {
warning (e.message);
use_auto_auth = false;
return "urn:ietf:wg:oauth:2.0:oob";
Expand Down Expand Up @@ -207,7 +203,7 @@ public class Tuba.Dialogs.NewAccount: Adw.Window {

yield account.verify_credentials ();

account.admin_mode = admin_switch.active;
account.admin_mode = this.admin_mode;
account = accounts.create_account (account.to_json ());

debug ("Saving account");
Expand Down Expand Up @@ -247,11 +243,6 @@ public class Tuba.Dialogs.NewAccount: Adw.Window {
code_entry_error.label = error_message;
}

[GtkCallback]
private void on_proxy_apply () {
settings.proxy = proxy_entry.text;
}

[GtkCallback]
public void clear_errors () {
instance_entry.remove_css_class ("error");
Expand Down Expand Up @@ -295,4 +286,104 @@ public class Tuba.Dialogs.NewAccount: Adw.Window {
void on_back_clicked () {
reset ();
}

class SettingsDialog : Adw.Dialog {
public signal void admin_changed (bool new_admin_val);

Adw.EntryRow proxy_entry;
Adw.SwitchRow admin_row;
Gtk.Button apply_button;
construct {
this.title = _("Settings");
this.content_width = 460;
this.content_height = 220;
this.can_close = false;

var cancel_button = new Gtk.Button.with_label (_("Cancel"));
cancel_button.clicked.connect (on_cancel);

apply_button = new Gtk.Button.with_label (_("Apply")) {
css_classes = {"suggested-action"},
sensitive = false
};
apply_button.clicked.connect (on_apply);

var page = new Adw.PreferencesPage () {
valign = Gtk.Align.CENTER
};
var headerbar = new Adw.HeaderBar () {
show_end_title_buttons = false,
show_start_title_buttons = false
};

headerbar.pack_start (cancel_button);
headerbar.pack_end (apply_button);

var toolbar_view = new Adw.ToolbarView () {
content = page
};
toolbar_view.add_top_bar (headerbar);

var group = new Adw.PreferencesGroup ();
proxy_entry = new Adw.EntryRow () {
title = _("Proxy"),
visible = false,
input_purpose = Gtk.InputPurpose.URL,
show_apply_button = false
};
group.add (proxy_entry);

admin_row = new Adw.SwitchRow () {
// translators: Switch title in the new account window
title = _("Admin"),
// translators: Switch description in the new account window
subtitle = _("Enables the Admin Dashboard and requests the needed permissions")
};
group.add (admin_row);
page.add (group);

this.child = toolbar_view;
}

bool original_admin_val = false;
public SettingsDialog (bool can_access_settings = true, string proxy_val = "", bool admin_val = false) {
if (!can_access_settings) {
proxy_entry.visible = true;
proxy_entry.text = proxy_val;
}
original_admin_val = admin_val;
admin_row.active = admin_val;

admin_row.notify["active"].connect (modified);
proxy_entry.changed.connect (modified);
}

void modified () {
apply_button.sensitive = admin_row.active != original_admin_val || settings.proxy != proxy_entry.text;
}

void on_cancel () {
this.force_close ();
}

void on_apply () {
if (proxy_entry.visible && settings.proxy != proxy_entry.text)
settings.proxy = proxy_entry.text;

if (admin_row.active != original_admin_val)
admin_changed (admin_row.active);

on_cancel ();
}
}

[GtkCallback] void on_settings_clicked () {
var settings_dialog = new SettingsDialog (this.can_access_settings, settings.proxy, this.admin_mode);
settings_dialog.admin_changed.connect (on_admin_change);
settings_dialog.present (this);
}

void on_admin_change (bool new_val) {
this.admin_mode = new_val;
}
}
Loading