Move callbacks to separate module

- Modularity
- Redability
- Separation of logic
This commit is contained in:
Candifloss 2025-11-19 17:42:28 +05:30
parent 47efaa2d08
commit 34ccdb6ba0
2 changed files with 19 additions and 7 deletions

View File

@ -6,6 +6,7 @@ use i_slint_backend_winit::{
}, },
}; };
use slint::{LogicalPosition, LogicalSize}; use slint::{LogicalPosition, LogicalSize};
mod widgets;
slint::include_modules!(); slint::include_modules!();
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
@ -33,13 +34,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
ui.set_bar_width(bar_width); ui.set_bar_width(bar_width);
ui.set_bar_height(bar_height); ui.set_bar_height(bar_height);
let weak = ui.as_weak(); // Install all widget callbacks
ui.on_open_program(move || { widgets::install_callbacks(&ui);
if let Some(_ui) = weak.upgrade() {
// Launch a program on click
std::process::Command::new("xclock").spawn().ok();
}
});
// Size the window to match the bar. // Size the window to match the bar.
#[allow(clippy::cast_precision_loss)] #[allow(clippy::cast_precision_loss)]
ui.window() ui.window()

15
src/widgets.rs Normal file
View File

@ -0,0 +1,15 @@
use crate::TopBar;
use slint::ComponentHandle;
use std::process::Command;
/// Connect widget callbacks for the top bar.
pub fn install_callbacks(ui: &TopBar) {
let weak = ui.as_weak();
ui.on_open_program(move || {
if weak.upgrade().is_some() {
// Run program on click
Command::new("xclock").spawn().ok();
}
});
}