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::{
Backend,
winit::platform::x11::{WindowAttributesExtX11, WindowType},
winit::{
platform::x11::{WindowAttributesExtX11, WindowType},
window::WindowAttributes,
},
};
use slint::{LogicalPosition, LogicalSize};
slint::include_modules!();
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Configure winit attributes before any Slint window is created.
let backend = Backend::builder()
.with_window_attributes_hook(|attrs| {
// Mark the X11 window as a dock so WMs treat it as a panel/topbar.
attrs.with_x11_window_type(vec![WindowType::Dock])
// hide from taskbar / pagers (useful for panels)
//.with_skip_taskbar(true)
})
.build()?;
// Closure that adjusts winit WindowAttributes before Slint creates the window.
let window_attrs = |attrs: WindowAttributes| {
// Mark the X11 window as a DOCK so the WM treats it as a top bar/panel.
attrs.with_x11_window_type(vec![WindowType::Dock])
};
// 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))?;
// Bar dimensions.
let bar_height = 25;
let bar_width = 1366;
// Create the Slint UI component.
let ui = TopBar::new()?;
// Set component properties.
ui.set_bar_width(bar_width);
ui.set_bar_height(bar_height);
// Size the window to match the bar.
#[allow(clippy::cast_precision_loss)]
ui.window()
.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));
// Run the Slint event loop.
ui.run()?;
Ok(())
}