Add widget: LeftWM Window Title

- Display the title of the current window
- Only for LeftWM
- Use `leftwm-state -q`
This commit is contained in:
Candifloss 2025-11-23 22:57:35 +05:30
parent 6ac513f301
commit c284df88a1
3 changed files with 69 additions and 2 deletions

49
src/widgets/leftwmdata.rs Normal file
View File

@ -0,0 +1,49 @@
use crate::TopBar;
use slint::{ComponentHandle, Timer, TimerMode, SharedString};
use std::process::Command;
use std::time::Duration;
use serde::Deserialize;
// Minimal JSON structure
#[derive(Deserialize)]
struct LeftState {
window_title: String,
}
// Run leftwm-state and return JSON output (or empty string)
fn get_leftwm_state() -> String {
Command::new("leftwm-state")
.arg("-q")
.output()
.map(|o| String::from_utf8_lossy(&o.stdout).to_string())
.unwrap_or_default()
}
// Parse only the window title
fn parse_window_title(json: &str) -> String {
serde_json::from_str::<LeftState>(json)
.map(|v| v.window_title)
.unwrap_or_default()
}
// Main updater loop
fn start_leftwm_updater(ui: &TopBar) {
let weak = ui.as_weak();
let timer = Box::leak(Box::new(Timer::default()));
// Poll every ~300 ms (fast enough for responsiveness)
timer.start(TimerMode::Repeated, Duration::from_millis(300), move || {
if let Some(ui) = weak.upgrade() {
let state = get_leftwm_state();
let title = parse_window_title(&state);
// Send to UI
ui.set_current_window_title(SharedString::from(title));
}
});
}
// Public entry
pub fn install(ui: &TopBar) {
start_leftwm_updater(ui);
}

View File

@ -6,6 +6,7 @@ mod timewidget;
mod volumewidget;
mod weatherwidget;
mod wifiwidget;
mod leftwmdata;
use crate::TopBar;
@ -17,6 +18,7 @@ pub fn install_callbacks(ui: &TopBar) {
brightnesswidget::install(ui);
wifiwidget::install(ui);
weatherwidget::install(ui);
leftwmdata::install(ui);
// In the future:
// notifwidget::install(ui);

View File

@ -9,7 +9,9 @@ export component TopBar inherits Window {
in property<int> bar_height;
in property<int> def_font_size;
in property<string> def_font_fam;
background: #00000000;
// Right side
in property<string> current_window_title;
// Time widget
in-out property <string> time_text;
@ -92,6 +94,7 @@ export component TopBar inherits Window {
y: 0px;
default-font-family: def_font_fam;
default-font-size: def_font_size *1px;
background: #00000000;
Rectangle {
x: 0px;
@ -110,8 +113,21 @@ export component TopBar inherits Window {
// Left side
Rectangle {
background: #79a9af7b;
width: parent.width/3;
//background: #46adbb79;
HorizontalLayout {
alignment: start; // Left-align
padding-left: 4px;
padding-right: 4px;
// CUrrent Window Title
Text {
text: root.current_window_title;
vertical-alignment: center;
wrap: no-wrap;
overflow: elide;
}
}
}
// Middle