From 4895333ff1e6b2b9442a58a3c69dad96d7f3b8f3 Mon Sep 17 00:00:00 2001 From: benstrb Date: Sat, 21 Mar 2026 22:00:34 +0100 Subject: [PATCH] =?UTF-8?q?=CE=A3:3=20v2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 11 ++++++ Cargo.toml | 1 + flake.lock | 12 +++---- src/widget.rs | 1 + src/widgets/focused_window.rs | 64 ++++++++++++++++++++++++++++++----- 5 files changed, 75 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f563be..8f9ee3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1787,6 +1787,16 @@ dependencies = [ "jni-sys", ] +[[package]] +name = "niri-ipc" +version = "25.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12bd9eb4e437f5282ce853cf66837658379cb537b4b6bae687da59246005329a" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -3310,6 +3320,7 @@ dependencies = [ "chrono", "iced", "iced_layershell", + "niri-ipc", "tokio", "winit", "zbus", diff --git a/Cargo.toml b/Cargo.toml index 539476c..ac9b81c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2024" chrono = "0.4.44" iced = { version = "0.14.0", default-features = false, features = ["wgpu", "wayland", "tokio"] } iced_layershell = { version = "0.15.0", default-features = false } +niri-ipc = "25.11.0" tokio = { version = "1.50.0", features = ["time"] } winit = { version = "0.30.12", default-features = false, features = ["wayland"] } zbus = "5.14.0" diff --git a/flake.lock b/flake.lock index fd1d531..beb223f 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1773579282, - "narHash": "sha256-LWvZj9Bvm1EuoO6zbX4yjZebwnZNfeTbmCJGS7RGQ3Y=", + "lastModified": 1773821835, + "narHash": "sha256-TJ3lSQtW0E2JrznGVm8hOQGVpXjJyXY2guAxku2O9A4=", "owner": "nixos", "repo": "nixpkgs", - "rev": "5a88de74db0e948139be4b46f9a94d64aa11391c", + "rev": "b40629efe5d6ec48dd1efba650c797ddbd39ace0", "type": "github" }, "original": { @@ -29,11 +29,11 @@ ] }, "locked": { - "lastModified": 1773630837, - "narHash": "sha256-zJhgAGnbVKeBMJOb9ctZm4BGS/Rnrz+5lfSXTVah4HQ=", + "lastModified": 1773975983, + "narHash": "sha256-zrRVwdfhDdohANqEhzY/ydeza6EXEi8AG6cyMRNYT9Q=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "f600ea449c7b5bb596fa1cf21c871cc5b9e31316", + "rev": "cc80954a95f6f356c303ed9f08d0b63ca86216ac", "type": "github" }, "original": { diff --git a/src/widget.rs b/src/widget.rs index 17beceb..9d89737 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -20,4 +20,5 @@ pub enum Message { Battery(Option), Time, ShutdownEvent(ShutdownEvents), + FocusChanged(Option), } diff --git a/src/widgets/focused_window.rs b/src/widgets/focused_window.rs index 53d4c37..e974cb9 100644 --- a/src/widgets/focused_window.rs +++ b/src/widgets/focused_window.rs @@ -1,7 +1,7 @@ -use iced::{Subscription, Task}; -use wayland_protocols_wlr::foreign_toplevel; +use iced::{Subscription, Task, futures::SinkExt}; +use niri_ipc::{Event, Request, socket::Socket}; -use crate::widget::PanelWidget; +use crate::widget::{Message, PanelWidget}; pub struct FocusedWindowWidget { focused_window: String, @@ -10,21 +10,69 @@ pub struct FocusedWindowWidget { impl FocusedWindowWidget { pub fn new() -> Self { Self { - focused_window: foreign_toplevel::v1::client::zwlr_foreign_toplevel_manager_v1::Event, + focused_window: "dsadsadsadas".into(), } } } impl PanelWidget for FocusedWindowWidget { - fn update(&mut self, _message: &crate::widget::Message) -> iced::Task { + fn update(&mut self, message: &Message) -> iced::Task { + let Message::FocusChanged(i) = message else { + return Task::none(); + }; + + self.focused_window = match i { + Some(id) => id.to_string(), + None => "None".into(), + }; + Task::none() } - fn subscribe(&self) -> iced::Subscription { - Subscription::none() + //ain't gonna lie, I got no idea wth is going on here, the issue was that, smth smth channel + //not filled, it was not working properly + //this is entirely vibecoded + fn subscribe(&self) -> Subscription { + Subscription::run_with("niri-focus-watcher", |_| { + iced::stream::channel(16, async |mut tx| { + let (btx, brx) = std::sync::mpsc::channel::(); + + std::thread::spawn(move || { + let mut socket = Socket::connect().expect("failed to connect"); + let _ = socket.send(Request::EventStream).expect("failed to send"); + let mut read_event = socket.read_events(); + loop { + match read_event() { + Ok(Event::WindowFocusChanged { id }) => { + if btx.send(Message::FocusChanged(id)).is_err() { + break; + } + } + Ok(_) => {} + Err(e) => { + eprintln!("{e}"); + break; + } + } + } + }); + + loop { + match brx.try_recv() { + Ok(msg) => { + let _ = tx.send(msg).await; + } + Err(std::sync::mpsc::TryRecvError::Empty) => { + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + } + Err(std::sync::mpsc::TryRecvError::Disconnected) => break, + } + } + }) + }) } - fn view(&self, _id: iced::window::Id) -> iced::Element<'_, crate::widget::Message> { + fn view(&self, _id: iced::window::Id) -> iced::Element<'_, Message> { iced::widget::text!("{}", self.focused_window).into() } }