Add simple (if ugly) settings dialog
This commit is contained in:
11
settings/Cargo.toml
Normal file
11
settings/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "bscreensaver-settings"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
bscreensaver-util = { path = "../util" }
|
||||
glib = { version = "0.15", features = ["v2_68"] }
|
||||
gtk = { version = "0.15", features = ["v3_24"] }
|
||||
log = "0.4"
|
10
settings/bscreensaver-settings.desktop
Normal file
10
settings/bscreensaver-settings.desktop
Normal file
@ -0,0 +1,10 @@
|
||||
[Desktop Entry]
|
||||
Version=1.0
|
||||
Name=Screensaver Settings
|
||||
Comment=Customize screensaver settings
|
||||
Exec=bscreensaver-settings
|
||||
Icon=screensaver
|
||||
Terminal=false
|
||||
StartupNotify=true
|
||||
Type=Application
|
||||
Categories=GTK;Settings;DesktopSettings;
|
249
settings/src/main.rs
Normal file
249
settings/src/main.rs
Normal file
@ -0,0 +1,249 @@
|
||||
use gtk::{glib, prelude::*};
|
||||
use glib::clone;
|
||||
use std::{env, process::exit, time::Duration};
|
||||
|
||||
use bscreensaver_util::settings::Configuration;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Widgets {
|
||||
lock_timeout: gtk::SpinButton,
|
||||
blank_before_locking: gtk::SpinButton,
|
||||
new_login_command: gtk::Entry,
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let config = Configuration::load()?;
|
||||
|
||||
let app = gtk::Application::builder()
|
||||
.application_id("org.spurint.bscreensaver-settings")
|
||||
.build();
|
||||
app.connect_activate(move |app| show_ui(&app, &config));
|
||||
|
||||
exit(app.run_with_args(&env::args().into_iter().collect::<Vec<String>>()));
|
||||
}
|
||||
|
||||
fn show_ui(app: >k::Application, config: &Configuration) {
|
||||
let mainwin = gtk::ApplicationWindow::builder()
|
||||
.application(app)
|
||||
.name("BScreensaver Settings")
|
||||
.title("Screensaver Settings")
|
||||
.type_(gtk::WindowType::Toplevel)
|
||||
.resizable(false)
|
||||
.build();
|
||||
|
||||
let label_sg = gtk::SizeGroup::builder()
|
||||
.mode(gtk::SizeGroupMode::Horizontal)
|
||||
.build();
|
||||
|
||||
let topvbox = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.spacing(8)
|
||||
.margin(4)
|
||||
.build();
|
||||
mainwin.add(&topvbox);
|
||||
|
||||
let hbox = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.spacing(8)
|
||||
.build();
|
||||
topvbox.pack_start(&hbox, false, false, 0);
|
||||
|
||||
let label = gtk::Label::builder()
|
||||
.label("Lock screen after")
|
||||
.xalign(0.0)
|
||||
.build();
|
||||
label_sg.add_widget(&label);
|
||||
hbox.pack_start(&label, false, false, 0);
|
||||
|
||||
let lock_timeout_spinbutton = gtk::SpinButton::builder()
|
||||
.adjustment(>k::Adjustment::builder()
|
||||
.lower(1.0)
|
||||
.upper(f64::MAX as u32 as f64)
|
||||
.step_increment(1.0)
|
||||
.page_increment(5.0)
|
||||
.value((config.lock_timeout.as_secs() / 60) as f64)
|
||||
.build()
|
||||
)
|
||||
.tooltip_text("Minutes before the screen locks")
|
||||
.activates_default(true)
|
||||
.build();
|
||||
hbox.pack_start(&lock_timeout_spinbutton, false, false, 0);
|
||||
|
||||
let label = gtk::Label::builder()
|
||||
.label("minutes")
|
||||
.xalign(0.0)
|
||||
.build();
|
||||
hbox.pack_start(&label, false, false, 0);
|
||||
|
||||
let hbox = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.spacing(8)
|
||||
.build();
|
||||
topvbox.pack_start(&hbox, false, false, 0);
|
||||
|
||||
let label = gtk::Label::builder()
|
||||
.label("Blank screen")
|
||||
.xalign(0.0)
|
||||
.build();
|
||||
label_sg.add_widget(&label);
|
||||
hbox.pack_start(&label, false, false, 0);
|
||||
|
||||
let blank_before_locking_spinbutton = gtk::SpinButton::builder()
|
||||
.adjustment(>k::Adjustment::builder()
|
||||
.lower(0.0)
|
||||
.upper(f64::MAX as u32 as f64)
|
||||
.step_increment(1.0)
|
||||
.page_increment(5.0)
|
||||
.value((config.blank_before_locking.as_secs() / 60) as f64)
|
||||
.build()
|
||||
)
|
||||
.tooltip_text("Minutes before screen locks to blank the screen")
|
||||
.activates_default(true)
|
||||
.build();
|
||||
hbox.pack_start(&blank_before_locking_spinbutton, false, false, 0);
|
||||
|
||||
let label = gtk::Label::builder()
|
||||
.label("minutes before locking")
|
||||
.xalign(0.0)
|
||||
.build();
|
||||
hbox.pack_start(&label, false, false, 0);
|
||||
|
||||
let hbox = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.spacing(8)
|
||||
.build();
|
||||
topvbox.pack_start(&hbox, false, false, 0);
|
||||
|
||||
let label = gtk::Label::builder()
|
||||
.label("New Login Command:")
|
||||
.tooltip_text("Command to run when the 'New Login' button is clicked in the unlock dialog")
|
||||
.xalign(0.0)
|
||||
.build();
|
||||
label_sg.add_widget(&label);
|
||||
hbox.pack_start(&label, false, false, 0);
|
||||
|
||||
let new_login_command_entry = gtk::Entry::builder()
|
||||
.text(config.new_login_command.as_ref().unwrap_or(&"".to_string()))
|
||||
.width_chars(30)
|
||||
.activates_default(true)
|
||||
.build();
|
||||
hbox.pack_start(&new_login_command_entry, false, false, 0);
|
||||
|
||||
let widgets = Widgets {
|
||||
lock_timeout: lock_timeout_spinbutton.clone(),
|
||||
blank_before_locking: blank_before_locking_spinbutton.clone(),
|
||||
new_login_command: new_login_command_entry.clone(),
|
||||
};
|
||||
mainwin.connect_delete_event(clone!(@strong config, @strong widgets, @strong app, @strong mainwin => move |_,_| {
|
||||
Inhibit(!confirm_cancel(&config, &widgets, &mainwin))
|
||||
}));
|
||||
|
||||
let button = gtk::Button::from_icon_name(Some("folder-open"), gtk::IconSize::Button);
|
||||
button.connect_clicked(clone!(@strong mainwin, @strong widgets => move |_| {
|
||||
run_file_chooser(&mainwin, &widgets);
|
||||
}));
|
||||
hbox.pack_start(&button, false, false, 0);
|
||||
|
||||
let button_box = gtk::ButtonBox::builder()
|
||||
.spacing(8)
|
||||
.layout_style(gtk::ButtonBoxStyle::End)
|
||||
.build();
|
||||
topvbox.pack_end(&button_box, false, false, 0);
|
||||
|
||||
let close_button = gtk::Button::builder()
|
||||
.label("Close")
|
||||
.build();
|
||||
close_button.connect_clicked(clone!(@strong config, @strong widgets, @strong app, @strong mainwin => move |_| {
|
||||
if confirm_cancel(&config, &widgets, &mainwin) {
|
||||
app.quit();
|
||||
}
|
||||
}));
|
||||
button_box.pack_end(&close_button, false, false, 0);
|
||||
|
||||
let save_button = gtk::Button::builder()
|
||||
.label("Save")
|
||||
.build();
|
||||
save_button.connect_clicked(clone!(@strong config, @strong widgets, @strong app, @strong mainwin => move |_| {
|
||||
if save_config(&config, &widgets, &mainwin) {
|
||||
app.quit();
|
||||
}
|
||||
}));
|
||||
button_box.pack_end(&save_button, false, false, 0);
|
||||
save_button.set_can_default(true);
|
||||
save_button.set_has_default(true);
|
||||
|
||||
mainwin.show_all();
|
||||
}
|
||||
|
||||
fn run_file_chooser(mainwin: >k::ApplicationWindow, widgets: &Widgets) {
|
||||
let file_chooser = gtk::FileChooserNative::new(
|
||||
Some("New Login Command"),
|
||||
Some(mainwin),
|
||||
gtk::FileChooserAction::Open,
|
||||
Some("Select"),
|
||||
Some("Cancel"),
|
||||
);
|
||||
let response = file_chooser.run();
|
||||
file_chooser.hide();
|
||||
if response == gtk::ResponseType::Accept {
|
||||
if let Some(filename) = file_chooser.filename() {
|
||||
widgets.new_login_command.set_text(&filename.to_string_lossy());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn save_config(config: &Configuration, widgets: &Widgets, mainwin: >k::ApplicationWindow) -> bool{
|
||||
let (new_config, changed) = build_new_configuration(&config, &widgets);
|
||||
if changed {
|
||||
if let Err(err) = new_config.save() {
|
||||
let error_dialog = gtk::MessageDialog::new(
|
||||
Some(mainwin),
|
||||
gtk::DialogFlags::MODAL | gtk::DialogFlags::DESTROY_WITH_PARENT,
|
||||
gtk::MessageType::Error,
|
||||
gtk::ButtonsType::Close,
|
||||
&format!("Failed to save configuration: {}", err),
|
||||
);
|
||||
error_dialog.set_title("Screensaver Settings");
|
||||
error_dialog.run();
|
||||
error_dialog.hide();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn confirm_cancel(config: &Configuration, widgets: &Widgets, mainwin: >k::ApplicationWindow) -> bool {
|
||||
let (_, changed) = build_new_configuration(&config, &widgets);
|
||||
if changed {
|
||||
let dialog = gtk::MessageDialog::new(
|
||||
Some(mainwin),
|
||||
gtk::DialogFlags::MODAL | gtk::DialogFlags::DESTROY_WITH_PARENT,
|
||||
gtk::MessageType::Warning,
|
||||
gtk::ButtonsType::None,
|
||||
"You have unsaved changes. Are you sure you want to close?",
|
||||
);
|
||||
dialog.set_title("Screensaver Settings");
|
||||
dialog.add_buttons(&[
|
||||
("Close Window", gtk::ResponseType::Close),
|
||||
("Cancel", gtk::ResponseType::Cancel),
|
||||
]);
|
||||
dialog.set_default_response(gtk::ResponseType::Cancel);
|
||||
let response = dialog.run();
|
||||
dialog.hide();
|
||||
match response {
|
||||
gtk::ResponseType::Close => true,
|
||||
_ => false,
|
||||
}
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn build_new_configuration(old_config: &Configuration, widgets: &Widgets) -> (Configuration, bool) {
|
||||
let mut new_config = old_config.clone();
|
||||
new_config.lock_timeout = Duration::from_secs(widgets.lock_timeout.adjustment().value() as u64 * 60);
|
||||
new_config.blank_before_locking = Duration::from_secs(widgets.blank_before_locking.adjustment().value() as u64 * 60);
|
||||
new_config.new_login_command = Some(widgets.new_login_command.text()).filter(|s| !s.is_empty()).map(|s| s.to_string());
|
||||
let changed = old_config != &new_config;
|
||||
(new_config, changed)
|
||||
}
|
Reference in New Issue
Block a user