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 {
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;
construct {
var base_grid = build_grid ();
build_inputs (base_grid);
base_grid.attach (build_button (), 1, 2);
column_spacing = 12;
row_spacing = 12;
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_entry = new Gtk.Entry ();
host_entry.valign = Gtk.Align.CENTER;
@ -65,71 +34,82 @@ namespace Streamdeck.Views {
GLib.SettingsBindFlags.DEFAULT
);
grid.attach (host_label, 0, 0);
grid.attach (host_entry, 1, 0);
grid.attach (port_label, 0, 1);
grid.attach (port_entry, 1, 1);
}
connect_button = new Gtk.Button ();
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);
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 ()) {
case "Disconnected":
connect_button.label = _("Connect");
break;
case "Unauthenticated":
connect_button.label = _("Login");
auth_revealer.reveal_child = true;
break;
default:
connect_button.label = _("Reconnect");
break;
}
}
Daemon.instance.obs_state_signal.connect ((_obj, state) => {
if (!connect_button.sensitive) {
connect_button.sensitive = true;
}
private void handle_state_change (string state) {
if (!connect_button.sensitive) {
connect_button.sensitive = true;
}
switch (state) {
case "Disconnected":
connect_button.label = _("Connect");
if (auth_entry.text.length > 0) {
set_visible_child_name ("auth-grid");
} else {
set_visible_child_name ("base-grid");
}
break;
case "Unauthenticated":
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;
}
});
switch (state) {
case "Disconnected":
connect_button.label = _("Connect");
break;
case "Unauthenticated":
auth_revealer.reveal_child = true;
break;
default:
connect_button.label = _("Reconnect");
break;
}
}
connect_button.clicked.connect (() => {
connect_button.sensitive = false;
switch (Daemon.instance.get_obs_state ()) {
case "Disconnected":
Daemon.instance.connect_obs.begin ();
break;
case "Unauthenticated":
Daemon.instance.authenticate_obs.begin (auth_entry.text);
break;
default:
Daemon.instance.disconnect_obs.begin ();
break;
}
});
return connect_button;
private void handle_button_click () {
connect_button.sensitive = false;
switch (Daemon.instance.get_obs_state ()) {
case "Disconnected":
Daemon.instance.connect_obs.begin ();
break;
case "Unauthenticated":
Daemon.instance.authenticate_obs.begin (auth_entry.text);
break;
default:
Daemon.instance.disconnect_obs.begin ();
break;
}
}
}
}