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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! Widgets for the penrose status bar
use crate::{
    bar::widgets::Widget,
    core::{Context, TextStyle},
    Result,
};
use penrose::{
    core::{ClientSpace, State},
    pure::geometry::Rect,
    x::XConn,
    Color,
};

const PADDING: u32 = 3;

/// The focus state of a given workspace being rendered within a [WorkspacesWidget].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FocusState {
    /// The workspace is not currently focused on any screen.
    Unfocused,
    /// The workspace is focused on the screen that the widget is rendered on.
    FocusedOnThisScreen,
    /// The workspace is focused on a screen that the widget is not rendered on.
    FocusedOnOtherScreen,
}

impl FocusState {
    /// Whether or not this workspace is focused on any active screen.
    pub fn focused(&self) -> bool {
        matches!(self, Self::FocusedOnOtherScreen | Self::FocusedOnThisScreen)
    }
}

/// A UI implementation for the [WorkspacesWidget] widget.
pub trait WorkspacesUi {
    /// Update the UI properties of the parent [WorkspacesWidget] as part of startup and refresh
    /// hooks.
    ///
    /// The boolean return of this method is used to indicate to the parent widget that a redraw
    /// is now required. If state has not changed since the last time this method was called then
    /// you should return `false` to reduce unnecessary rendering.
    #[allow(unused_variables)]
    fn update_from_state<X>(
        &mut self,
        workspace_meta: &[WsMeta],
        focused_tags: &[String],
        state: &State<X>,
        x: &X,
    ) -> bool
    where
        X: XConn,
    {
        false
    }

    /// The current UI tag string to be shown for a given workspace.
    fn ui_tag(&self, workspace_meta: &WsMeta) -> String {
        workspace_meta.tag.clone()
    }

    /// The background color to be used for the parent [WorkspacesWidget].
    fn background_color(&self) -> Color;

    /// The foreground and background color to be used for rendering a given workspace.
    ///
    /// The [FocusState] provided indicates the current state of the workspace itself, while
    /// `screen_has_focus` is used to indicate whether or not the screen the parent
    /// [WorkspacesWidget] is on is currently focused or not.
    fn colors_for_workspace(
        &self,
        workspace_meta: &WsMeta,
        focus_state: FocusState,
        screen_has_focus: bool,
    ) -> (Color, Color);
}

/// The default UI style of a [WorkspacesWidget].
#[derive(Debug, Clone, PartialEq)]
pub struct DefaultUi {
    fg_1: Color,
    fg_2: Color,
    bg_1: Color,
    bg_2: Color,
}

impl DefaultUi {
    fn new(style: TextStyle, highlight: impl Into<Color>, empty_fg: impl Into<Color>) -> Self {
        Self {
            fg_1: style.fg,
            fg_2: empty_fg.into(),
            bg_1: highlight.into(),
            bg_2: style.bg.unwrap_or_else(|| 0x000000.into()),
        }
    }
}

impl WorkspacesUi for DefaultUi {
    fn background_color(&self) -> Color {
        self.bg_2
    }

    fn colors_for_workspace(
        &self,
        &WsMeta { occupied, .. }: &WsMeta,
        focus_state: FocusState,
        screen_has_focus: bool,
    ) -> (Color, Color) {
        use FocusState::*;

        match focus_state {
            FocusedOnThisScreen if screen_has_focus && occupied => (self.fg_1, self.bg_1),
            FocusedOnThisScreen if screen_has_focus => (self.fg_2, self.bg_1),
            FocusedOnThisScreen => (self.fg_1, self.fg_2),
            FocusedOnOtherScreen => (self.bg_1, self.fg_2),
            Unfocused if occupied => (self.fg_1, self.bg_2),
            Unfocused => (self.fg_2, self.bg_2),
        }
    }
}

/// Metadata around the content of a particular workspace within the current
/// window manager state.
#[derive(Clone, Debug, PartialEq)]
pub struct WsMeta {
    tag: String,
    occupied: bool,
    extent: (u32, u32),
}

impl WsMeta {
    /// The tag used by the window manager for this workspace
    pub fn tag(&self) -> &str {
        &self.tag
    }

    /// Whether or not this workspace currently contains any clients
    pub fn occupied(&self) -> bool {
        self.occupied
    }

    fn from_state<X>(state: &State<X>) -> Vec<Self>
    where
        X: XConn,
    {
        state
            .client_set
            .ordered_workspaces()
            .map(WsMeta::from)
            .collect()
    }
}

impl From<&ClientSpace> for WsMeta {
    fn from(w: &ClientSpace) -> Self {
        Self {
            tag: w.tag().to_owned(),
            occupied: !w.is_empty(),
            extent: (0, 0),
        }
    }
}

fn focused_workspaces<X>(state: &State<X>) -> Vec<String>
where
    X: XConn,
{
    let mut indexed_screens: Vec<(usize, String)> = state
        .client_set
        .screens()
        .map(|s| (s.index(), s.workspace.tag().to_owned()))
        .collect();

    indexed_screens.sort_by_key(|(ix, _)| *ix);

    indexed_screens.into_iter().map(|(_, tag)| tag).collect()
}

/// A simple workspace indicator for a status bar using a default UI and colorscheme
pub type Workspaces = WorkspacesWidget<DefaultUi>;

impl Workspaces {
    /// Construct a new [WorkspacesWidget] using the [DefaultUi].
    pub fn new(style: TextStyle, highlight: impl Into<Color>, empty_fg: impl Into<Color>) -> Self {
        WorkspacesWidget::new_with_ui(DefaultUi::new(style, highlight, empty_fg))
    }
}

/// A simple workspace indicator for a status bar
#[derive(Debug, Clone, PartialEq)]
pub struct WorkspacesWidget<U>
where
    U: WorkspacesUi,
{
    workspaces: Vec<WsMeta>,
    focused_ws: Vec<String>, // focused ws per screen
    extent: Option<(u32, u32)>,
    ui: U,
    require_draw: bool,
}

impl<U> WorkspacesWidget<U>
where
    U: WorkspacesUi,
{
    /// Construct a new [WorkspacesWidget] with the specified [WorkspacesUi] implementation.
    pub fn new_with_ui(ui: U) -> Self {
        Self {
            workspaces: Vec::new(),
            focused_ws: Vec::new(), // set in startup hook
            extent: None,
            ui,
            require_draw: true,
        }
    }

    fn raw_tags(&self) -> Vec<&str> {
        self.workspaces.iter().map(|w| w.tag.as_ref()).collect()
    }

    fn update_from_state<X>(&mut self, state: &State<X>, x: &X)
    where
        X: XConn,
    {
        let focused_ws = focused_workspaces(state);
        let wss = WsMeta::from_state(state);

        let ui_updated = self.ui.update_from_state(&wss, &focused_ws, state, x);
        let tags_changed = self.tags_changed(&wss);

        if ui_updated || tags_changed {
            self.require_draw = true;
            self.extent = None;
        } else if self.focused_ws != focused_ws || self.occupied_changed(&wss) {
            self.require_draw = true;
        }

        self.focused_ws = focused_ws;
        self.workspaces = wss;
    }

    fn tags_changed(&self, workspaces: &[WsMeta]) -> bool {
        let new_tags: Vec<&str> = workspaces.iter().map(|w| w.tag.as_ref()).collect();

        self.raw_tags() == new_tags
    }

    // Called after tags_changed above so we assume that tags are matching
    fn occupied_changed(&self, workspaces: &[WsMeta]) -> bool {
        self.workspaces
            .iter()
            .zip(workspaces)
            .any(|(l, r)| l.occupied != r.occupied)
    }

    fn ws_colors(&self, meta: &WsMeta, screen: usize, screen_has_focus: bool) -> (Color, Color) {
        let focused = self.focused_ws.iter().any(|t| t == &meta.tag);
        let focused_on_this_screen = match &self.focused_ws.get(screen) {
            &Some(focused_tag) => &meta.tag == focused_tag,
            None => false,
        };

        let state = match (focused, focused_on_this_screen) {
            (false, _) => FocusState::Unfocused,
            (_, true) => FocusState::FocusedOnThisScreen,
            (true, false) => FocusState::FocusedOnOtherScreen,
        };

        self.ui.colors_for_workspace(meta, state, screen_has_focus)
    }
}

impl<X, U> Widget<X> for WorkspacesWidget<U>
where
    X: XConn,
    U: WorkspacesUi,
{
    fn draw(
        &mut self,
        ctx: &mut Context<'_>,
        screen: usize,
        screen_has_focus: bool,
        w: u32,
        h: u32,
    ) -> Result<()> {
        ctx.fill_rect(Rect::new(0, 0, w, h), self.ui.background_color())?;
        ctx.translate(PADDING as i32, 0);
        let (_, eh) = <Self as Widget<X>>::current_extent(self, ctx, h)?;

        for ws in self.workspaces.iter() {
            let (fg, bg) = self.ws_colors(ws, screen, screen_has_focus);
            ctx.fill_rect(Rect::new(0, 0, ws.extent.0, h), bg)?;
            ctx.draw_text(&self.ui.ui_tag(ws), h - eh, (PADDING, PADDING), fg)?;
            ctx.translate(ws.extent.0 as i32, 0);
        }

        self.require_draw = false;

        Ok(())
    }

    fn current_extent(&mut self, ctx: &mut Context<'_>, _h: u32) -> Result<(u32, u32)> {
        match self.extent {
            Some(extent) => Ok(extent),
            None => {
                let mut total = 0;
                let mut h_max = 0;
                for ws in self.workspaces.iter_mut() {
                    let (w, h) = ctx.text_extent(&self.ui.ui_tag(ws))?;
                    total += w + 2 * PADDING;
                    h_max = if h > h_max { h } else { h_max };
                    ws.extent = (w + 2 * PADDING, h);
                }

                let ext = (total + PADDING, h_max);
                self.extent = Some(ext);

                Ok(ext)
            }
        }
    }

    fn is_greedy(&self) -> bool {
        false
    }

    fn require_draw(&self) -> bool {
        self.require_draw
    }

    fn on_startup(&mut self, state: &mut State<X>, x: &X) -> Result<()> {
        self.update_from_state(state, x);

        Ok(())
    }

    fn on_refresh(&mut self, state: &mut State<X>, x: &X) -> Result<()> {
        self.update_from_state(state, x);

        Ok(())
    }
}