diff --git a/crates/popcorn/src/main.rs b/crates/popcorn/src/main.rs index e7a11a9..22e945a 100644 --- a/crates/popcorn/src/main.rs +++ b/crates/popcorn/src/main.rs @@ -1,3 +1,6 @@ -fn main() { - println!("Hello, world!"); +mod show_popup; + +fn main() -> Result<(), Box> { + show_popup::show_popup()?; + Ok(()) } diff --git a/crates/popcorn/src/show_popup.rs b/crates/popcorn/src/show_popup.rs new file mode 100644 index 0000000..f04cb0a --- /dev/null +++ b/crates/popcorn/src/show_popup.rs @@ -0,0 +1,97 @@ +use i_slint_backend_winit::{ + Backend, + winit::{ + platform::x11::{WindowAttributesExtX11, WindowType}, + window::WindowAttributes, + }, +}; +use slint::{Color, LogicalPosition, LogicalSize, SharedString}; +slint::include_modules!(); + +// Helper function to simplify color values (A, R, G, B) +fn argb_col(color_hex: u32) -> Color { + Color::from_argb_encoded(color_hex) +} + +/// Set Slint properties from config data +fn set_ui_props(ui: &OSDpopup) { + // Config values + let popup_width = 200; + let popup_height = 50; + let popup_border_radius = 5; + let popup_padding = 7; + let window_position_x = 1146; + let window_position_y = 35; + + let osd_bg_color = argb_col(0x_67_5f_5f_5f); + let fill_color = argb_col(0x_ff_12_7a_9b); + + let value_font_size = 35; + let value_font_color = argb_col(0x_ff_ff_ff_ff); + let value_font = "Iosevka Extrabold"; + let percent_value = 35; + + let icon_size = value_font_size; + let icon_font_color = value_font_color; + let icon_font = "IosevkaTermSlab Nerd Font Mono"; + let icon_glyph = ""; + + // Config values -> Slint component properties + ui.set_popup_width(popup_width); + ui.set_popup_height(popup_height); + ui.set_popup_border_radius(popup_border_radius); + ui.set_popup_padding(popup_padding); + + ui.set_osd_bg_color(osd_bg_color); + ui.set_fill_color(fill_color); + + ui.set_value_font_size(value_font_size); + ui.set_value_font_color(value_font_color); + ui.set_value_font(SharedString::from(value_font)); + ui.set_percent_value(percent_value); + + ui.set_icon_size(icon_size); + ui.set_icon_font_color(icon_font_color); + ui.set_icon_font(SharedString::from(icon_font)); + ui.set_icon_glyph(SharedString::from(icon_glyph)); + + // Window size (width, height): pixels + #[allow(clippy::cast_precision_loss)] + ui.window() + .set_size(LogicalSize::new(popup_width as f32, popup_height as f32)); + + // Window position (x,y): pixels + #[allow(clippy::cast_precision_loss)] + ui.window().set_position(LogicalPosition::new( + window_position_x as f32, + window_position_y as f32, + )); +} + +/// Create and show the Window UI +pub fn show_popup() -> Result<(), Box> { + // 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 doesn't treat it as a normal window. Window type `Notification` didn't work. Fix later. + 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))?; + + // Create window + let ui = OSDpopup::new()?; + + // Send the data to the UI as properties + set_ui_props(&ui); + + // Run the UI + ui.run()?; + + Ok(()) +} diff --git a/crates/popcorn/ui/osd-popup.slint b/crates/popcorn/ui/osd-popup.slint index c8a2ec8..18aa436 100644 --- a/crates/popcorn/ui/osd-popup.slint +++ b/crates/popcorn/ui/osd-popup.slint @@ -1,21 +1,21 @@ export component OSDpopup inherits Window { - in property popup_width: 200; - in property popup_height: 50; - in property popup_border_radius: 5; - in property popup_padding: 7; + in property popup_width; + in property popup_height; + in property popup_border_radius; + in property popup_padding; - in property osd_bg_color: #5f5f5f67; - in property fill_color: #127a9bff; + in property osd_bg_color; + in property fill_color; - in property value_font_size: 35; - in property value_font_color: #ffffffff; - in property value_font: "Iosevka Extrabold"; - in property percent_value: 0; + in property value_font_size; + in property value_font_color; + in property value_font; + in property percent_value; - in property icon_size: self.value_font_size; - in property icon_font_color: self.value_font_color; - in property icon_font: "IosevkaTermSlab Nerd Font Mono"; - in property icon_glyph: ""; + in property icon_size; + in property icon_font_color; + in property icon_font; + in property icon_glyph; no-frame: true; always-on-top: true;