Skip to content

Commit a6d881e

Browse files
committed
Refactored settings dialog to position certificate auth controls on general tab
1 parent 8e760cf commit a6d881e

File tree

1 file changed

+66
-24
lines changed

1 file changed

+66
-24
lines changed

snx-rs-gui/src/settings.rs

+66-24
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use async_channel::Sender;
33
use gtk::{
44
glib::{self, clone},
55
prelude::*,
6-
Align, ButtonsType, DialogFlags, MessageType, Orientation, ResponseType, WindowPosition,
6+
Align, ButtonsType, DialogFlags, MessageType, Orientation, ResponseType, Widget, WindowPosition,
77
};
88
use ipnet::Ipv4Net;
99
use std::net::Ipv4Addr;
@@ -27,6 +27,18 @@ const CSS_ERROR: &str = r"label {
2727
}
2828
";
2929

30+
fn set_container_visible(widget: &Widget, flag: bool) {
31+
if let Some(parent) = widget.parent() {
32+
if let Some(parent) = parent.parent() {
33+
if flag {
34+
parent.show_all();
35+
} else {
36+
parent.hide();
37+
}
38+
}
39+
}
40+
}
41+
3042
struct SettingsDialog {
3143
params: Arc<TunnelParams>,
3244
dialog: gtk::Dialog,
@@ -136,6 +148,9 @@ impl MyWidgets {
136148
}
137149

138150
impl SettingsDialog {
151+
const DEFAULT_WIDTH: i32 = 700;
152+
const DEFAULT_HEIGHT: i32 = 370;
153+
139154
pub fn new(params: Arc<TunnelParams>) -> Self {
140155
let dialog = gtk::Dialog::with_buttons(
141156
Some("VPN settings"),
@@ -148,8 +163,8 @@ impl SettingsDialog {
148163
],
149164
);
150165

151-
dialog.set_default_width(700);
152-
dialog.set_default_height(370);
166+
dialog.set_default_width(Self::DEFAULT_WIDTH);
167+
dialog.set_default_height(Self::DEFAULT_HEIGHT);
153168
dialog.set_position(WindowPosition::CenterAlways);
154169

155170
let server_name = gtk::Entry::builder().text(&params.server_name).hexpand(true).build();
@@ -273,14 +288,15 @@ impl SettingsDialog {
273288
error.style_context().add_provider(&provider, 100);
274289

275290
auth_type.connect_active_notify(
276-
clone!(@weak auth_type, @weak user_name, @weak password, @weak tunnel_type => move |widget| {
291+
clone!(@weak dialog, @weak auth_type, @weak user_name, @weak password, @weak tunnel_type, @weak cert_path => move |widget| {
277292
if let Some(id) = widget.active_id() {
278293
let factors = unsafe { auth_type.data::<Vec<String>>(&id).map(|p| p.as_ref()) };
279294
if let Some(factors) = factors {
280295
let is_saml = factors.iter().any(|f| f == "identity_provider");
281296
let is_cert = factors.iter().any(|f| f == "certificate");
282-
user_name.set_sensitive(!is_saml && !is_cert);
283-
password.set_sensitive(!is_saml && !is_cert);
297+
set_container_visible(user_name.as_ref(), !is_saml && !is_cert);
298+
set_container_visible(cert_path.as_ref(), is_cert);
299+
dialog.resize(SettingsDialog::DEFAULT_WIDTH, SettingsDialog::DEFAULT_HEIGHT);
284300
if is_saml {
285301
tunnel_type.set_active(Some(0));
286302
tunnel_type.set_sensitive(false);
@@ -663,21 +679,6 @@ impl SettingsDialog {
663679
.margin_end(16)
664680
.build();
665681

666-
let cert_type_box = self.cert_type_box();
667-
certs_box.pack_start(&cert_type_box, false, true, 6);
668-
669-
let cert_path = self.form_box("Client certificate or driver path (.pem, .pfx/.p12, .so)");
670-
cert_path.pack_start(&self.widgets.cert_path, false, true, 0);
671-
certs_box.pack_start(&cert_path, false, true, 6);
672-
673-
let cert_password = self.form_box("PFX password or PKCS11 pin");
674-
cert_password.pack_start(&self.widgets.cert_password, false, true, 0);
675-
certs_box.pack_start(&cert_password, false, true, 6);
676-
677-
let cert_id = self.form_box("Hex ID of PKCS11 certificate");
678-
cert_id.pack_start(&self.widgets.cert_id, false, true, 0);
679-
certs_box.pack_start(&cert_id, false, true, 6);
680-
681682
let ca_cert = self.form_box("Server CA root certificates");
682683
ca_cert.pack_start(&self.widgets.ca_cert, false, true, 0);
683684
certs_box.pack_start(&ca_cert, false, true, 6);
@@ -766,13 +767,53 @@ impl SettingsDialog {
766767
routing_box
767768
}
768769

770+
fn user_auth_box(&self) -> gtk::Box {
771+
let user_auth_box = gtk::Box::builder()
772+
.orientation(Orientation::Vertical)
773+
.margin(0)
774+
.margin_start(0)
775+
.margin_end(0)
776+
.build();
777+
user_auth_box.pack_start(&self.user_box(), false, true, 6);
778+
user_auth_box.pack_start(&self.password_box(), false, true, 6);
779+
780+
user_auth_box
781+
}
782+
783+
fn cert_auth_box(&self) -> gtk::Box {
784+
let certs_box = gtk::Box::builder()
785+
.orientation(Orientation::Vertical)
786+
.margin(0)
787+
.margin_start(0)
788+
.margin_end(0)
789+
.build();
790+
791+
let cert_type_box = self.cert_type_box();
792+
certs_box.pack_start(&cert_type_box, false, true, 6);
793+
794+
let cert_path = self.form_box("Client certificate or driver path (.pem, .pfx/.p12, .so)");
795+
cert_path.pack_start(&self.widgets.cert_path, false, true, 0);
796+
certs_box.pack_start(&cert_path, false, true, 6);
797+
798+
let cert_password = self.form_box("PFX password or PKCS11 pin");
799+
cert_password.pack_start(&self.widgets.cert_password, false, true, 0);
800+
certs_box.pack_start(&cert_password, false, true, 6);
801+
802+
let cert_id = self.form_box("Hex ID of PKCS11 certificate");
803+
cert_id.pack_start(&self.widgets.cert_id, false, true, 0);
804+
certs_box.pack_start(&cert_id, false, true, 6);
805+
806+
certs_box
807+
}
808+
769809
fn general_tab(&self) -> gtk::Box {
770810
let tab = gtk::Box::builder().orientation(Orientation::Vertical).margin(6).build();
771811
tab.pack_start(&self.server_box(), false, true, 6);
772812
tab.pack_start(&self.auth_box(), false, true, 6);
773813
tab.pack_start(&self.tunnel_box(), false, true, 6);
774-
tab.pack_start(&self.user_box(), false, true, 6);
775-
tab.pack_start(&self.password_box(), false, true, 6);
814+
tab.show_all();
815+
tab.pack_start(&self.user_auth_box(), false, true, 6);
816+
tab.pack_start(&self.cert_auth_box(), false, true, 6);
776817
tab
777818
}
778819

@@ -800,6 +841,7 @@ impl SettingsDialog {
800841

801842
let scrolled_win = gtk::ScrolledWindow::builder().build();
802843
scrolled_win.add(&viewport);
844+
scrolled_win.show_all();
803845
scrolled_win
804846
}
805847

@@ -812,7 +854,7 @@ impl SettingsDialog {
812854
notebook.append_page(&self.general_tab(), Some(&gtk::Label::new(Some("General"))));
813855
notebook.append_page(&self.advanced_tab(), Some(&gtk::Label::new(Some("Advanced"))));
814856

815-
notebook.show_all();
857+
notebook.show();
816858
}
817859
}
818860

0 commit comments

Comments
 (0)