Use clone!() macro instead of Rc

This commit is contained in:
Brian Tarricone 2022-05-30 17:30:29 -07:00
parent f0bbc9d982
commit 762fdc3912

View File

@ -4,7 +4,7 @@ use gethostname::gethostname;
use glib::{GString, clone, SourceId}; use glib::{GString, clone, SourceId};
use gtk::{prelude::*, Button, Entry, Label, Plug, Window}; use gtk::{prelude::*, Button, Entry, Label, Plug, Window};
use log::{debug, error, warn}; use log::{debug, error, warn};
use std::{io::{self, Write}, process::exit, rc::Rc, thread, time::Duration}; use std::{io::{self, Write}, process::exit, thread, time::Duration};
use bscreensaver_util::{init_logging, settings::Configuration, desktop::NewLoginCommand}; use bscreensaver_util::{init_logging, settings::Configuration, desktop::NewLoginCommand};
@ -296,36 +296,30 @@ fn main() -> anyhow::Result<()> {
unlock_button.set_can_default(true); unlock_button.set_can_default(true);
unlock_button.set_has_default(true); unlock_button.set_has_default(true);
let timer = Rc::new(gtk::ProgressBar::builder() let timer = gtk::ProgressBar::builder()
.orientation(gtk::Orientation::Horizontal) .orientation(gtk::Orientation::Horizontal)
.fraction(1.0) .fraction(1.0)
.show_text(false) .show_text(false)
.can_focus(false) .can_focus(false)
.margin(2) .margin(2)
.build()); .build();
top_vbox.pack_end(&*timer, false, false, 0); top_vbox.pack_end(&timer, false, false, 0);
{
let timer = Rc::clone(&timer);
let delta = (DIALOG_UPDATE_INTERVAL.as_millis() as f64) / (DIALOG_TIMEOUT.as_millis() as f64); let delta = (DIALOG_UPDATE_INTERVAL.as_millis() as f64) / (DIALOG_TIMEOUT.as_millis() as f64);
glib::timeout_add_local(DIALOG_UPDATE_INTERVAL, move || { glib::timeout_add_local(DIALOG_UPDATE_INTERVAL, clone!(@strong timer => move || {
let new_fraction = timer.fraction() - delta; let new_fraction = timer.fraction() - delta;
if new_fraction <= 0.0 { if new_fraction <= 0.0 {
exit(1); exit(1);
} }
timer.set_fraction(new_fraction); timer.set_fraction(new_fraction);
Continue(true) Continue(true)
}); }));
}
{ password_box.connect_key_press_event(clone!(@strong timer => move |_, _| {
let timer = Rc::clone(&timer);
password_box.connect_key_press_event(move |_, _| {
let new_fraction = timer.fraction() + 0.05; let new_fraction = timer.fraction() + 0.05;
timer.set_fraction(if new_fraction >= 1.0 { 1.0 } else { new_fraction }); timer.set_fraction(if new_fraction >= 1.0 { 1.0 } else { new_fraction });
Inhibit(false) Inhibit(false)
}); }));
}
dialog.show_all(); dialog.show_all();