i3status_rs/blocks/focused_window/
sway_ipc.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use super::{Backend, Info};
use crate::blocks::prelude::*;
use swayipc_async::{Connection, Event, EventStream, EventType, WindowChange, WorkspaceChange};

pub(super) struct SwayIpc {
    events: EventStream,
    info: Info,
}

impl SwayIpc {
    pub(super) async fn new() -> Result<Self> {
        Ok(Self {
            events: Connection::new()
                .await
                .error("failed to open connection with swayipc")?
                .subscribe(&[EventType::Window, EventType::Workspace])
                .await
                .error("could not subscribe to window events")?,
            info: default(),
        })
    }
}

#[async_trait]
impl Backend for SwayIpc {
    async fn get_info(&mut self) -> Result<Info> {
        loop {
            let event = self
                .events
                .next()
                .await
                .error("swayipc channel closed")?
                .error("bad event")?;
            match event {
                Event::Window(e) => match e.change {
                    WindowChange::Mark => {
                        self.info.marks = e.container.marks;
                    }
                    WindowChange::Focus => {
                        self.info.title.clear();
                        if let Some(new_title) = &e.container.name {
                            self.info.title.push_str(new_title);
                        }
                        self.info.marks = e.container.marks;
                    }
                    WindowChange::Title => {
                        if e.container.focused {
                            self.info.title.clear();
                            if let Some(new_title) = &e.container.name {
                                self.info.title.push_str(new_title);
                            }
                        } else {
                            continue;
                        }
                    }
                    WindowChange::Close => {
                        self.info.title.clear();
                        self.info.marks.clear();
                    }
                    _ => continue,
                },
                Event::Workspace(e) if e.change == WorkspaceChange::Init => {
                    self.info.title.clear();
                    self.info.marks.clear();
                }
                _ => continue,
            }

            return Ok(self.info.clone());
        }
    }
}