opt gtk sudo ui, fix edit button show ()

Signed-off-by: 21pages <sunboeasy@gmail.com>
This commit is contained in:
21pages 2024-09-20 11:06:56 +08:00 committed by GitHub
parent cfd801c5d6
commit 3d5262c36f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -30,7 +30,7 @@ use std::{
const EXIT_CODE: i32 = -1; const EXIT_CODE: i32 = -1;
enum Message { enum Message {
PasswordPrompt(String), PasswordPrompt((String, bool)),
Password((String, String)), Password((String, String)),
ErrorDialog(String), ErrorDialog(String),
Cancel, Cancel,
@ -107,24 +107,20 @@ fn ui(args: Vec<String>) {
let username = Arc::new(Mutex::new(crate::platform::get_active_username())); let username = Arc::new(Mutex::new(crate::platform::get_active_username()));
let username_clone = username.clone(); let username_clone = username.clone();
let first_prompt = Arc::new(Mutex::new(true));
application.connect_activate(glib::clone!(@weak application =>move |_| { application.connect_activate(glib::clone!(@weak application =>move |_| {
let rx_to_ui = rx_to_ui_clone.clone(); let rx_to_ui = rx_to_ui_clone.clone();
let tx_from_ui = tx_from_ui_clone.clone(); let tx_from_ui = tx_from_ui_clone.clone();
let last_password = Arc::new(Mutex::new(String::new())); let last_password = Arc::new(Mutex::new(String::new()));
let username = username_clone.clone(); let username = username_clone.clone();
let first_prompt = first_prompt.clone();
glib::timeout_add_local(std::time::Duration::from_millis(50), move || { glib::timeout_add_local(std::time::Duration::from_millis(50), move || {
if let Ok(msg) = rx_to_ui.lock().unwrap().try_recv() { if let Ok(msg) = rx_to_ui.lock().unwrap().try_recv() {
match msg { match msg {
Message::PasswordPrompt(err_msg) => { Message::PasswordPrompt((err_msg, show_edit)) => {
let last_pwd = last_password.lock().unwrap().clone(); let last_pwd = last_password.lock().unwrap().clone();
let username = username.lock().unwrap().clone(); let username = username.lock().unwrap().clone();
let first = first_prompt.lock().unwrap().clone(); if let Some((username, password)) = password_prompt(&username, &last_pwd, &err_msg, show_edit) {
*first_prompt.lock().unwrap() = false;
if let Some((username, password)) = password_prompt(&username, &last_pwd, &err_msg, first) {
*last_password.lock().unwrap() = password.clone(); *last_password.lock().unwrap() = password.clone();
if let Err(e) = tx_from_ui if let Err(e) = tx_from_ui
.lock() .lock()
@ -157,7 +153,7 @@ fn ui(args: Vec<String>) {
let acitve_user = crate::platform::get_active_username(); let acitve_user = crate::platform::get_active_username();
let mut initial_password = None; let mut initial_password = None;
if acitve_user != "root" { if acitve_user != "root" {
if let Err(e) = tx_to_ui_clone.send(Message::PasswordPrompt("".to_string())) { if let Err(e) = tx_to_ui_clone.send(Message::PasswordPrompt(("".to_string(), true))) {
log::error!("Channel error: {e:?}"); log::error!("Channel error: {e:?}");
std::process::exit(EXIT_CODE); std::process::exit(EXIT_CODE);
} }
@ -385,7 +381,7 @@ fn ui_parent(
let err_msg = if first { "" } else { "Sorry, try again." }; let err_msg = if first { "" } else { "Sorry, try again." };
first = false; first = false;
if let Err(e) = if let Err(e) =
tx_to_ui.send(Message::PasswordPrompt(err_msg.to_string())) tx_to_ui.send(Message::PasswordPrompt((err_msg.to_string(), false)))
{ {
log::error!("Channel error: {e:?}"); log::error!("Channel error: {e:?}");
kill_child(child); kill_child(child);
@ -627,6 +623,7 @@ fn password_prompt(
user_box.add(&user_entry); user_box.add(&user_entry);
user_box.set_halign(gtk::Align::Center); user_box.set_halign(gtk::Align::Center);
user_box.set_valign(gtk::Align::Center); user_box.set_valign(gtk::Align::Center);
user_box.set_vexpand(true);
content_area.add(&user_box); content_area.add(&user_box);
edit_button.connect_clicked( edit_button.connect_clicked(
@ -690,21 +687,21 @@ fn password_prompt(
let cancel_button = gtk::Button::builder() let cancel_button = gtk::Button::builder()
.label(translate("Cancel".to_string())) .label(translate("Cancel".to_string()))
.expand(true) .hexpand(true)
.build(); .build();
cancel_button.connect_clicked(glib::clone!(@weak dialog => move |_| { cancel_button.connect_clicked(glib::clone!(@weak dialog => move |_| {
dialog.response(gtk::ResponseType::Cancel); dialog.response(gtk::ResponseType::Cancel);
})); }));
let authenticate_button = gtk::Button::builder() let authenticate_button = gtk::Button::builder()
.label(translate("Authenticate".to_string())) .label(translate("Authenticate".to_string()))
.expand(true) .hexpand(true)
.build(); .build();
authenticate_button.connect_clicked(glib::clone!(@weak dialog => move |_| { authenticate_button.connect_clicked(glib::clone!(@weak dialog => move |_| {
dialog.response(gtk::ResponseType::Ok); dialog.response(gtk::ResponseType::Ok);
})); }));
let button_box = gtk::Box::builder() let button_box = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal) .orientation(gtk::Orientation::Horizontal)
.expand(true) .hexpand(true)
.homogeneous(true) .homogeneous(true)
.spacing(10) .spacing(10)
.margin_top(10) .margin_top(10)