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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Simple text based widgets built on top of Text
use crate::{
    bar::widgets::{Text, TextStyle, Widget},
    core::Context,
    Result,
};
use penrose::{
    core::State,
    pure::geometry::Rect,
    x::{event::PropertyEvent, Atom, XConn, XConnExt, XEvent},
};

/// A text widget that is set via updating the root window name a la dwm
#[derive(Clone, Debug, PartialEq)]
pub struct RootWindowName {
    inner: Text,
}

impl RootWindowName {
    /// Create a new RootWindowName widget
    pub fn new(style: TextStyle, is_greedy: bool, right_justified: bool) -> Self {
        Self {
            inner: Text::new("penrose", style, is_greedy, right_justified),
        }
    }
}

impl<X: XConn> Widget<X> for RootWindowName {
    fn draw(&mut self, ctx: &mut Context<'_>, s: usize, f: bool, w: u32, h: u32) -> Result<()> {
        Widget::<X>::draw(&mut self.inner, ctx, s, f, w, h)
    }

    fn current_extent(&mut self, ctx: &mut Context<'_>, h: u32) -> Result<(u32, u32)> {
        Widget::<X>::current_extent(&mut self.inner, ctx, h)
    }

    fn is_greedy(&self) -> bool {
        Widget::<X>::is_greedy(&self.inner)
    }

    fn require_draw(&self) -> bool {
        Widget::<X>::require_draw(&self.inner)
    }

    fn on_event(&mut self, event: &XEvent, _: &mut State<X>, x: &X) -> Result<()> {
        let name_props = [Atom::NetWmName.as_ref(), Atom::WmName.as_ref()];

        match event {
            XEvent::PropertyNotify(PropertyEvent {
                id, atom, is_root, ..
            }) if *is_root && name_props.contains(&atom.as_ref()) => {
                self.inner.set_text(x.window_title(*id)?)
            }

            _ => (),
        }

        Ok(())
    }
}

/// A text widget that shows the name of the currently focused window
#[derive(Clone, Debug, PartialEq)]
pub struct ActiveWindowName {
    inner: Text,
    max_chars: usize,
}

impl ActiveWindowName {
    /// Create a new ActiveWindowName widget with a maximum character count.
    ///
    /// max_chars can not be lower than 3.
    pub fn new(max_chars: usize, style: TextStyle, is_greedy: bool, right_justified: bool) -> Self {
        Self {
            inner: Text::new("", style, is_greedy, right_justified),
            max_chars: max_chars.max(3),
        }
    }

    fn set_text(&mut self, txt: &str) {
        if txt.chars().count() <= self.max_chars {
            self.inner.set_text(txt);
        } else {
            let s: String = txt.chars().take(self.max_chars - 3).collect();
            self.inner.set_text(format!("{}...", s));
        }
    }
}

impl<X: XConn> Widget<X> for ActiveWindowName {
    fn draw(&mut self, ctx: &mut Context<'_>, s: usize, f: bool, w: u32, h: u32) -> Result<()> {
        if f {
            Widget::<X>::draw(&mut self.inner, ctx, s, f, w, h)
        } else {
            ctx.fill_bg(Rect::new(0, 0, w, h))
        }
    }

    fn current_extent(&mut self, ctx: &mut Context<'_>, h: u32) -> Result<(u32, u32)> {
        Widget::<X>::current_extent(&mut self.inner, ctx, h)
    }

    fn is_greedy(&self) -> bool {
        Widget::<X>::is_greedy(&self.inner)
    }

    fn require_draw(&self) -> bool {
        Widget::<X>::require_draw(&self.inner)
    }

    fn on_refresh(&mut self, state: &mut State<X>, x: &X) -> Result<()> {
        if let Some(id) = state.client_set.current_client() {
            self.set_text(&x.window_title(*id)?)
        } else {
            self.set_text("")
        }

        Ok(())
    }

    fn on_event(&mut self, event: &XEvent, state: &mut State<X>, x: &X) -> Result<()> {
        let name_props = [Atom::NetWmName.as_ref(), Atom::WmName.as_ref()];

        if let Some(focused) = state.client_set.current_client() {
            match event {
                XEvent::PropertyNotify(PropertyEvent { id, atom, .. })
                    if id == focused && name_props.contains(&atom.as_ref()) =>
                {
                    self.set_text(&x.window_title(*id)?)
                }

                _ => (),
            }
        }

        Ok(())
    }
}

/// A text widget that shows the current layout name
#[derive(Clone, Debug, PartialEq)]
pub struct CurrentLayout {
    inner: Text,
}

impl CurrentLayout {
    /// Create a new CurrentLayout widget
    pub fn new(style: TextStyle) -> Self {
        Self {
            inner: Text::new("", style, false, false),
        }
    }
}

impl<X: XConn> Widget<X> for CurrentLayout {
    fn draw(&mut self, ctx: &mut Context<'_>, s: usize, f: bool, w: u32, h: u32) -> Result<()> {
        Widget::<X>::draw(&mut self.inner, ctx, s, f, w, h)
    }

    fn current_extent(&mut self, ctx: &mut Context<'_>, h: u32) -> Result<(u32, u32)> {
        Widget::<X>::current_extent(&mut self.inner, ctx, h)
    }

    fn is_greedy(&self) -> bool {
        Widget::<X>::is_greedy(&self.inner)
    }

    fn require_draw(&self) -> bool {
        Widget::<X>::require_draw(&self.inner)
    }

    fn on_refresh(&mut self, state: &mut State<X>, _: &X) -> Result<()> {
        let layout_name = state.client_set.current_workspace().layout_name();
        self.inner.set_text(format!("[{layout_name}]"));

        Ok(())
    }
}