Use revealer to toggle auth

This commit is contained in:
asonix 2021-05-06 17:46:07 -05:00
parent 3c587e38e1
commit 7e44a22ab7

View file

@ -1,46 +1,15 @@
namespace Streamdeck.Views { namespace Streamdeck.Views {
public class ObsView : Gtk.Stack { public class ObsView : Gtk.Grid {
private Gtk.Button connect_button;
private Gtk.Revealer auth_revealer;
private Gtk.Entry auth_entry; private Gtk.Entry auth_entry;
construct { construct {
var base_grid = build_grid (); column_spacing = 12;
build_inputs (base_grid); row_spacing = 12;
base_grid.attach (build_button (), 1, 2); halign = Gtk.Align.CENTER;
margin_top = 64;
var auth_grid = build_grid ();
build_inputs (auth_grid);
build_auth (auth_grid);
auth_grid.attach (build_button (), 1, 3);
add_named (base_grid, "base-grid");
add_named (auth_grid, "auth-grid");
show_all ();
}
private Gtk.Grid build_grid () {
var grid = new Gtk.Grid ();
grid.column_spacing = 12;
grid.row_spacing = 12;
grid.halign = Gtk.Align.CENTER;
grid.margin_top = 64;
return grid;
}
private void build_auth (Gtk.Grid grid) {
var auth_label = new Gtk.Label (_("Password:"));
auth_entry = new Gtk.Entry ();
auth_entry.valign = Gtk.Align.CENTER;
auth_entry.set_visibility (false);
auth_entry.set_input_purpose (Gtk.InputPurpose.PASSWORD);
auth_entry.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL);
grid.attach (auth_label, 0, 2);
grid.attach (auth_entry, 1, 2);
}
private void build_inputs (Gtk.Grid grid) {
var host_label = new Gtk.Label (_("Hostname:")); var host_label = new Gtk.Label (_("Hostname:"));
var host_entry = new Gtk.Entry (); var host_entry = new Gtk.Entry ();
host_entry.valign = Gtk.Align.CENTER; host_entry.valign = Gtk.Align.CENTER;
@ -65,71 +34,82 @@ namespace Streamdeck.Views {
GLib.SettingsBindFlags.DEFAULT GLib.SettingsBindFlags.DEFAULT
); );
grid.attach (host_label, 0, 0); connect_button = new Gtk.Button ();
grid.attach (host_entry, 1, 0);
grid.attach (port_label, 0, 1); var auth_label = new Gtk.Label (_("Password:"));
grid.attach (port_entry, 1, 1); auth_entry = new Gtk.Entry ();
} auth_entry.valign = Gtk.Align.CENTER;
auth_entry.set_visibility (false);
auth_entry.set_input_purpose (Gtk.InputPurpose.PASSWORD);
auth_entry.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL);
var auth_grid = new Gtk.Grid ();
auth_grid.column_spacing = 12;
auth_grid.row_spacing = 12;
auth_grid.attach (auth_label, 0, 2);
auth_grid.attach (auth_entry, 1, 2);
auth_revealer = new Gtk.Revealer ();
auth_revealer.child = auth_grid;
auth_revealer.reveal_child = false;
attach (host_label, 0, 0);
attach (host_entry, 1, 0);
attach (port_label, 0, 1);
attach (port_entry, 1, 1);
attach (auth_revealer, 0, 2, 2);
attach (connect_button, 1, 3);
show_all ();
Daemon.instance.obs_state_signal.connect (handle_state_change);
connect_button.clicked.connect (handle_button_click);
private Gtk.Button build_button () {
var connect_button = new Gtk.Button ();
switch (Daemon.instance.get_obs_state ()) { switch (Daemon.instance.get_obs_state ()) {
case "Disconnected": case "Disconnected":
connect_button.label = _("Connect"); connect_button.label = _("Connect");
break; break;
case "Unauthenticated": case "Unauthenticated":
connect_button.label = _("Login"); connect_button.label = _("Login");
auth_revealer.reveal_child = true;
break; break;
default: default:
connect_button.label = _("Reconnect"); connect_button.label = _("Reconnect");
break; break;
} }
}
Daemon.instance.obs_state_signal.connect ((_obj, state) => { private void handle_state_change (string state) {
if (!connect_button.sensitive) { if (!connect_button.sensitive) {
connect_button.sensitive = true; connect_button.sensitive = true;
} }
switch (state) { switch (state) {
case "Disconnected": case "Disconnected":
connect_button.label = _("Connect"); connect_button.label = _("Connect");
if (auth_entry.text.length > 0) { break;
set_visible_child_name ("auth-grid"); case "Unauthenticated":
} else { auth_revealer.reveal_child = true;
set_visible_child_name ("base-grid"); break;
} default:
break; connect_button.label = _("Reconnect");
case "Unauthenticated": break;
connect_button.label = _("Login"); }
set_visible_child_name ("auth-grid"); }
break;
default:
if (auth_entry.text.length > 0) {
set_visible_child_name ("auth-grid");
} else {
set_visible_child_name ("base-grid");
}
connect_button.label = _("Reconnect");
break;
}
});
connect_button.clicked.connect (() => { private void handle_button_click () {
connect_button.sensitive = false; connect_button.sensitive = false;
switch (Daemon.instance.get_obs_state ()) { switch (Daemon.instance.get_obs_state ()) {
case "Disconnected": case "Disconnected":
Daemon.instance.connect_obs.begin (); Daemon.instance.connect_obs.begin ();
break; break;
case "Unauthenticated": case "Unauthenticated":
Daemon.instance.authenticate_obs.begin (auth_entry.text); Daemon.instance.authenticate_obs.begin (auth_entry.text);
break; break;
default: default:
Daemon.instance.disconnect_obs.begin (); Daemon.instance.disconnect_obs.begin ();
break; break;
} }
});
return connect_button;
} }
} }
} }