Refactor main window setup for clarity

- Extract WindowAttributes modification into a named closure
- Simplify and clean up winit/Slint imports
- Explanatory comments
- rustfmt and clippy cleanup
This commit is contained in:
Candifloss 2025-11-18 23:21:18 +05:30
parent 7b82e65493
commit 798f9593b2

View File

@ -1,35 +1,48 @@
use i_slint_backend_winit::{ use i_slint_backend_winit::{
Backend, Backend,
winit::platform::x11::{WindowAttributesExtX11, WindowType}, winit::{
platform::x11::{WindowAttributesExtX11, WindowType},
window::WindowAttributes,
},
}; };
use slint::{LogicalPosition, LogicalSize}; use slint::{LogicalPosition, LogicalSize};
slint::include_modules!(); slint::include_modules!();
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
// Configure winit attributes before any Slint window is created. // Closure that adjusts winit WindowAttributes before Slint creates the window.
let backend = Backend::builder() let window_attrs = |attrs: WindowAttributes| {
.with_window_attributes_hook(|attrs| { // Mark the X11 window as a DOCK so the WM treats it as a top bar/panel.
// Mark the X11 window as a dock so WMs treat it as a panel/topbar. attrs.with_x11_window_type(vec![WindowType::Dock])
attrs.with_x11_window_type(vec![WindowType::Dock]) };
// hide from taskbar / pagers (useful for panels)
//.with_skip_taskbar(true)
})
.build()?;
// Build a Slint backend that applies this attribute hook to all windows.
let backend = Backend::builder()
.with_window_attributes_hook(window_attrs) // Register the hook
.build()?; // Construct backend
// Activate this customized backend for all Slint window creation and events.
slint::platform::set_platform(Box::new(backend))?; slint::platform::set_platform(Box::new(backend))?;
// Bar dimensions.
let bar_height = 25; let bar_height = 25;
let bar_width = 1366; let bar_width = 1366;
// Create the Slint UI component.
let ui = TopBar::new()?; let ui = TopBar::new()?;
// Set component properties.
ui.set_bar_width(bar_width); ui.set_bar_width(bar_width);
ui.set_bar_height(bar_height); ui.set_bar_height(bar_height);
// Size the window to match the bar.
#[allow(clippy::cast_precision_loss)] #[allow(clippy::cast_precision_loss)]
ui.window() ui.window()
.set_size(LogicalSize::new(bar_width as f32, bar_height as f32)); .set_size(LogicalSize::new(bar_width as f32, bar_height as f32));
// Position the window (bar) at the top-left.
ui.window().set_position(LogicalPosition::new(0.0, 0.0)); ui.window().set_position(LogicalPosition::new(0.0, 0.0));
// Run the Slint event loop.
ui.run()?; ui.run()?;
Ok(()) Ok(())
} }