Pass all properties from Rust

- No more default values in Slint
This commit is contained in:
Candifloss 2026-01-03 13:53:04 +05:30
parent 52e68acb41
commit a321b03ba4
3 changed files with 129 additions and 51 deletions

View File

@ -5,8 +5,8 @@ use i_slint_backend_winit::{
window::WindowAttributes, window::WindowAttributes,
}, },
}; };
use slint::{LogicalPosition, LogicalSize};
slint::include_modules!(); mod powermenu;
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
// Configure X11 window attributes before Slint creates the window. // Configure X11 window attributes before Slint creates the window.
@ -22,17 +22,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let backend = Backend::builder() let backend = Backend::builder()
.with_window_attributes_hook(window_attrs) .with_window_attributes_hook(window_attrs)
.build()?; .build()?;
slint::platform::set_platform(Box::new(backend))?; slint::platform::set_platform(Box::new(backend))?;
let ui = PowerMenu::new()?; let screen_width = 1920;
let screen_height = 1080;
let screen_width = 1366; powermenu::run_power_menu(screen_width, screen_height)
let screen_height = 768;
ui.window().set_size(LogicalSize::new(screen_width as f32, screen_height as f32));
ui.window().set_position(LogicalPosition::new(0.0, 0.0));
ui.run();
Ok(())
} }

102
src/powermenu.rs Normal file
View File

@ -0,0 +1,102 @@
use slint::{Color, LogicalPosition, LogicalSize, SharedString};
slint::include_modules!();
pub fn run_power_menu(
screen_width: i32,
screen_height: i32,
) -> Result<(), Box<dyn std::error::Error>> {
let ui = PowerMenu::new()?;
let screen_bg = Color::from_argb_encoded(0x20A3A3A3);
let btn_width = 120;
let btn_height = 120;
let btn_border_radius = 20;
let btn_bg_normal = Color::from_argb_encoded(0x75919191);
let btn_bg_hover = Color::from_argb_encoded(0x84929292);
let btn_bg_clicked = Color::from_argb_encoded(0xAB9B9B9B);
let option_icon_height = btn_height * 75 / 100;
let icon_font_size = option_icon_height * 80 / 100;
let icon_font = SharedString::from("IosevkaTermSlab Nerd Font Mono");
let icon_font_color = Color::from_argb_encoded(0xFFFFFFFF);
let option_text_height = btn_height * 25 / 100;
let option_text_font_size = option_text_height * 45 / 100;
let option_text_font = SharedString::from("IosevkaTermSlab Nerd Font Mono");
let option_text_color = Color::from_argb_encoded(0xFFFFFFFF);
let button_style = ButtonStyle {
btn_width,
btn_height,
btn_border_radius,
btn_bg_normal,
btn_bg_hover,
btn_bg_clicked,
option_icon_height,
icon_font_size,
icon_font,
icon_font_color,
option_text_height,
option_text_font_size,
option_text_font,
option_text_color,
};
let menu_padding_x = 20;
let menu_padding_y = menu_padding_x;
let menu_spacing = menu_padding_x;
let menu_border_radius = menu_padding_x;
let menu_width = btn_width * 4 + menu_spacing * 3 + menu_padding_x * 2;
let menu_height = btn_height + menu_padding_y * 2;
let menu_pos_x = (screen_width - menu_width) / 2;
let menu_pos_y = (screen_height - menu_height) / 2;
let menu_bg = Color::from_argb_encoded(0x287A7A7A);
ui.set_screen_width(screen_width);
ui.set_screen_height(screen_height);
ui.set_screen_bg(screen_bg);
ui.set_menu_padding_x(menu_padding_x);
ui.set_menu_padding_y(menu_padding_y);
ui.set_menu_spacing(menu_spacing);
ui.set_menu_border_radius(menu_border_radius);
ui.set_menu_width(menu_width);
ui.set_menu_height(menu_height);
ui.set_menu_pos_x(menu_pos_x);
ui.set_menu_pos_y(menu_pos_y);
ui.set_menu_bg(menu_bg);
ui.set_ico_lockscreen(SharedString::from(""));
ui.set_ico_logout(SharedString::from(""));
ui.set_ico_reboot(SharedString::from(""));
ui.set_ico_poweroff(SharedString::from(""));
ui.set_text_lockscreen(SharedString::from("Lock screen"));
ui.set_text_logout(SharedString::from("Log Out"));
ui.set_text_reboot(SharedString::from("Reboot"));
ui.set_text_poweroff(SharedString::from("Power Off"));
ui.set_button_style(button_style);
ui.window()
.set_size(LogicalSize::new(screen_width as f32, screen_height as f32));
ui.window().set_position(LogicalPosition::new(0.0, 0.0));
ui.run();
Ok(())
}

View File

@ -71,51 +71,33 @@ component PowerMenuButton {
} }
export component PowerMenu inherits Window { export component PowerMenu inherits Window {
in property <int> screen_width: 1920; in property <int> screen_width;
in property <int> screen_height: 1080; in property <int> screen_height;
in property <color> screen_bg: #a3a3a320; in property <color> screen_bg;
in property <ButtonStyle> button_style: { in property <ButtonStyle> button_style;
btn_width: 120,
btn_height: 120,
btn_border_radius: 20,
btn_bg_normal: #91919175, in property <int> menu_padding_x;
btn_bg_hover: #92929284, in property <int> menu_padding_y;
btn_bg_clicked: #9b9b9bab, in property <int> menu_spacing;
in property <int> menu_border_radius;
option_icon_height: 90, in property <int> menu_width;
icon_font_size: 80, in property <int> menu_height;
icon_font: "IosevkaTermSlab Nerd Font Mono", in property <int> menu_pos_x;
icon_font_color: #ffffff, in property <int> menu_pos_y;
option_text_height: 30, in property <color> menu_bg;
option_text_font_size: 14,
option_text_font: "IosevkaTermSlab Nerd Font Mono",
option_text_color: #ffffff,
};
in property <int> menu_padding_x: 20; in property <string> ico_lockscreen;
in property <int> menu_padding_y: menu_padding_x; in property <string> ico_logout;
in property <int> menu_spacing: menu_padding_x; in property <string> ico_reboot;
in property <int> menu_border_radius: menu_padding_x; in property <string> ico_poweroff;
in property <int> menu_width: (button_style.btn_width*4 + menu_spacing*3 + menu_padding_x*2); in property <string> text_lockscreen;
in property <int> menu_height: (button_style.btn_height + menu_padding_y*2); in property <string> text_logout;
in property <int> menu_pos_x: (screen_width - menu_width)/2; in property <string> text_reboot;
in property <int> menu_pos_y: (screen_height - menu_height)/2; in property <string> text_poweroff;
in property <color> menu_bg: #7a7a7a28;
in property <string> ico_lockscreen: "";
in property <string> ico_logout: "";
in property <string> ico_reboot: "";
in property <string> ico_poweroff: "";
in property <string> text_lockscreen: "Lock screen";
in property <string> text_logout: "Log Out";
in property <string> text_reboot: "Reboot";
in property <string> text_poweroff: "Power Off";
no-frame: true; no-frame: true;
always-on-top: true; always-on-top: true;