penrose_ui/bar/widgets/
debug.rs

1//! Widgets for showing debug information about the current state of penrose
2use crate::bar::widgets::{Context, Result, Text, TextStyle, Widget};
3use penrose::{
4    core::State,
5    extensions::util::debug::{CurrentStateConfig, summarise_state},
6    x::XConn,
7};
8
9/// A text widget that shows the Xid of the current client
10#[derive(Clone, Debug, PartialEq)]
11pub struct ActiveWindowId {
12    inner: Text,
13}
14
15impl ActiveWindowId {
16    /// Create a new ActiveWindowId widget.
17    pub fn new(style: TextStyle, is_greedy: bool, right_justified: bool) -> Self {
18        Self {
19            inner: Text::new("", style, is_greedy, right_justified),
20        }
21    }
22}
23
24impl<X: XConn> Widget<X> for ActiveWindowId {
25    fn draw(&mut self, ctx: &mut Context<'_>, s: usize, f: bool, w: u32, h: u32) -> Result<()> {
26        Widget::<X>::draw(&mut self.inner, ctx, s, f, w, h)
27    }
28
29    fn current_extent(&mut self, ctx: &mut Context<'_>, h: u32) -> Result<(u32, u32)> {
30        Widget::<X>::current_extent(&mut self.inner, ctx, h)
31    }
32
33    fn is_greedy(&self) -> bool {
34        Widget::<X>::is_greedy(&self.inner)
35    }
36
37    fn require_draw(&self) -> bool {
38        Widget::<X>::require_draw(&self.inner)
39    }
40
41    fn on_refresh(&mut self, state: &mut State<X>, _: &mut X) -> Result<()> {
42        if let Some(id) = state.client_set.current_client() {
43            self.inner.set_text(format!("FOCUS={}", *id))
44        } else {
45            self.inner.set_text("FOCUS=None")
46        }
47
48        Ok(())
49    }
50}
51
52/// A text widget that shows a summary of the current Window Manager state.
53///
54/// Updates on refresh
55#[derive(Clone, Debug, PartialEq)]
56pub struct StateSummary {
57    inner: Text,
58    cfg: CurrentStateConfig,
59}
60
61impl StateSummary {
62    /// Create a new StateSummary widget.
63    pub fn new(style: TextStyle) -> Self {
64        Self {
65            inner: Text::new("", style, false, false),
66            cfg: CurrentStateConfig {
67                line_per_stat: false,
68                ..CurrentStateConfig::default()
69            },
70        }
71    }
72}
73
74impl<X: XConn> Widget<X> for StateSummary {
75    fn draw(&mut self, ctx: &mut Context<'_>, s: usize, f: bool, w: u32, h: u32) -> Result<()> {
76        Widget::<X>::draw(&mut self.inner, ctx, s, f, w, h)
77    }
78
79    fn current_extent(&mut self, ctx: &mut Context<'_>, h: u32) -> Result<(u32, u32)> {
80        Widget::<X>::current_extent(&mut self.inner, ctx, h)
81    }
82
83    fn is_greedy(&self) -> bool {
84        Widget::<X>::is_greedy(&self.inner)
85    }
86
87    fn require_draw(&self) -> bool {
88        Widget::<X>::require_draw(&self.inner)
89    }
90
91    fn on_refresh(&mut self, state: &mut State<X>, _: &mut X) -> Result<()> {
92        self.inner.set_text(summarise_state(state, &self.cfg));
93
94        Ok(())
95    }
96}