diff options
| author | MaxVerevkin <maxxverrr@gmail.com> | 2023-09-07 16:31:06 +0300 |
|---|---|---|
| committer | MaxVerevkin <maxxverrr@gmail.com> | 2023-09-07 16:31:06 +0300 |
| commit | 87673d7ecce661f3b8c5db949e70640934a55de9 (patch) | |
| tree | b2ac86792f42a0cf6bd7defc4f8bc86cddfc7988 /src/wm_info_provider | |
| parent | 74cc46a2383db5538d7e0392e4b79b7287775e9b (diff) | |
| download | i3bar-mango-87673d7ecce661f3b8c5db949e70640934a55de9.tar.gz | |
some refactoring
Diffstat (limited to 'src/wm_info_provider')
| -rw-r--r-- | src/wm_info_provider/hyprland.rs | 58 | ||||
| -rw-r--r-- | src/wm_info_provider/river.rs | 10 |
2 files changed, 29 insertions, 39 deletions
diff --git a/src/wm_info_provider/hyprland.rs b/src/wm_info_provider/hyprland.rs index 52d8325..1233e66 100644 --- a/src/wm_info_provider/hyprland.rs +++ b/src/wm_info_provider/hyprland.rs @@ -17,41 +17,31 @@ pub struct Hyprland { } impl Hyprland { - pub fn new(event_loop: &mut EventLoop<(Connection<State>, State)>) -> Option<Self> { + pub fn new() -> Option<Self> { let his = std::env::var("HYPRLAND_INSTANCE_SIGNATURE").ok()?; let ipc = Ipc::new(&his).ok()?; - let active_id = ipc.query_json::<IpcWorkspace>("j/activeworkspace").ok()?.id; - let mut this = Self { + Some(Self { + workspaces: ipc.query_sorted_workspaces().ok()?, + active_id: ipc.query_json::<IpcWorkspace>("j/activeworkspace").ok()?.id, ipc, - workspaces: Vec::new(), - active_id, - }; - this.fetch_workspaces().ok()?; - event_loop.register_with_fd(this.ipc.sock2.as_raw_fd(), |state| { - match hyprland_cb(&mut state.0, &mut state.1) { + }) + } +} + +impl WmInfoProvider for Hyprland { + fn register(&self, event_loop: &mut EventLoop) { + event_loop.register_with_fd(self.ipc.sock2.as_raw_fd(), |ctx| { + match hyprland_cb(ctx.conn, ctx.state) { Ok(()) => Ok(event_loop::Action::Keep), Err(e) => { - state.1.set_error(&mut state.0, e); - state.0.flush(IoMode::Blocking)?; + ctx.state.set_error(ctx.conn, e); + ctx.conn.flush(IoMode::Blocking)?; Ok(event_loop::Action::Unregister) } } }); - Some(this) } - fn fetch_workspaces(&mut self) -> io::Result<()> { - self.workspaces = self.ipc.query_json::<Vec<IpcWorkspace>>("j/workspaces")?; - self.workspaces.sort_unstable_by_key(|x| x.id); - Ok(()) - } -} - -impl WmInfoProvider for Hyprland { - fn new_ouput(&mut self, _: &mut Connection<State>, _: WlOutput) {} - - fn output_removed(&mut self, _: &mut Connection<State>, _: WlOutput) {} - fn get_tags(&self, output: &Output) -> Vec<Tag> { self.workspaces .iter() @@ -66,14 +56,6 @@ impl WmInfoProvider for Hyprland { .collect() } - fn get_layout_name(&self, _: &Output) -> Option<String> { - None - } - - fn get_mode_name(&self, _: &Output) -> Option<String> { - None - } - fn click_on_tag( &mut self, _: &mut Connection<State>, @@ -99,10 +81,12 @@ fn hyprland_cb(conn: &mut Connection<State>, state: &mut State) -> io::Result<() match hyprland.ipc.next_event() { Ok(event) => { if let Some(active_ws) = event.strip_prefix("workspace>>") { - hyprland.active_id = active_ws.parse().unwrap(); + hyprland.active_id = active_ws + .parse() + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; updated = true; } else if event.contains("workspace>>") { - hyprland.fetch_workspaces()?; + hyprland.workspaces = hyprland.ipc.query_sorted_workspaces()?; updated = true; } } @@ -149,6 +133,12 @@ impl Ipc { serde_json::from_reader(&mut sock).map_err(Into::into) } + fn query_sorted_workspaces(&self) -> io::Result<Vec<IpcWorkspace>> { + let mut workspaces = self.query_json::<Vec<IpcWorkspace>>("j/workspaces")?; + workspaces.sort_unstable_by_key(|x| x.id); + Ok(workspaces) + } + fn next_event(&mut self) -> io::Result<String> { loop { if let Some(i) = memchr::memchr(b'\n', &self.sock2_buf) { diff --git a/src/wm_info_provider/river.rs b/src/wm_info_provider/river.rs index d8e9c3b..6c0993b 100644 --- a/src/wm_info_provider/river.rs +++ b/src/wm_info_provider/river.rs @@ -54,12 +54,12 @@ impl RiverInfoProvider { } impl WmInfoProvider for RiverInfoProvider { - fn new_ouput(&mut self, conn: &mut Connection<State>, output: WlOutput) { + fn new_ouput(&mut self, conn: &mut Connection<State>, output: &Output) { let status = self.status_manager - .get_river_output_status_with_cb(conn, output, output_status_cb); + .get_river_output_status_with_cb(conn, output.wl, output_status_cb); self.output_statuses.push(OutputStatus { - output, + output: output.wl, status, focused_tags: 0, urgent_tags: 0, @@ -68,11 +68,11 @@ impl WmInfoProvider for RiverInfoProvider { }); } - fn output_removed(&mut self, conn: &mut Connection<State>, output: WlOutput) { + fn output_removed(&mut self, conn: &mut Connection<State>, output: &Output) { let index = self .output_statuses .iter() - .position(|s| s.output == output) + .position(|s| s.output == output.wl) .unwrap(); let output_status = self.output_statuses.swap_remove(index); output_status.status.destroy(conn); |
