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
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Instant;

use crossbeam_channel::Sender;
use serde_derive::Deserialize;
use swayipc::reply::{Event, Node, WindowChange, WorkspaceChange};
use swayipc::{Connection, EventType};

use crate::blocks::{Block, ConfigBlock, Update};
use crate::config::SharedConfig;
use crate::errors::*;
use crate::scheduler::Task;
use crate::widgets::text::TextWidget;
use crate::widgets::I3BarWidget;

#[derive(Copy, Clone, Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MarksType {
    All,
    Visible,
    None,
}

pub struct FocusedWindow {
    id: usize,
    text: TextWidget,
    title: Arc<Mutex<String>>,
    marks: Arc<Mutex<String>>,
    show_marks: MarksType,
    max_width: usize,
}

#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields, default)]
pub struct FocusedWindowConfig {
    /// Truncates titles if longer than max-width
    pub max_width: usize,

    /// Show marks in place of title (if exist)
    pub show_marks: MarksType,
}

impl Default for FocusedWindowConfig {
    fn default() -> Self {
        Self {
            max_width: 21,
            show_marks: MarksType::None,
        }
    }
}

impl ConfigBlock for FocusedWindow {
    type Config = FocusedWindowConfig;

    fn new(
        id: usize,
        block_config: Self::Config,
        shared_config: SharedConfig,
        tx: Sender<Task>,
    ) -> Result<Self> {
        let title = Arc::new(Mutex::new(String::from("")));
        let marks = Arc::new(Mutex::new(String::from("")));
        let marks_type = block_config.show_marks;

        let update_window = {
            let title = title.clone();

            move |new_title| {
                let mut title = title
                    .lock()
                    .expect("lock has been poisoned in `window` block");

                let changed = *title != new_title;
                *title = new_title;
                changed
            }
        };

        let close_window = {
            let title = title.clone();

            move |closed_title: String| {
                let mut title = title
                    .lock()
                    .expect("lock has been poisoned in `window` block");

                if *title == closed_title {
                    *title = "".to_string();
                    true
                } else {
                    false
                }
            }
        };

        let update_marks = {
            let marks = marks.clone();

            move |new_marks: Vec<String>| {
                let mut new_marks_str = String::from("");

                for mark in new_marks {
                    match marks_type {
                        MarksType::All => {
                            new_marks_str.push_str(&format!("[{}]", mark));
                        }
                        MarksType::Visible if !mark.starts_with('_') => {
                            new_marks_str.push_str(&format!("[{}]", mark));
                        }
                        _ => {}
                    }
                }

                let mut marks = marks
                    .lock()
                    .expect("lock has been poisoned in `window` block");

                let changed = *marks != new_marks_str;
                *marks = new_marks_str;
                changed
            }
        };

        let _test_conn =
            Connection::new().block_error("focused_window", "failed to acquire connect to IPC")?;

        thread::Builder::new()
            .name("focused_window".into())
            .spawn(move || {
                let conn = Connection::new().expect("failed to open connection with swayipc");

                let events = conn
                    .subscribe(&[EventType::Window, EventType::Workspace])
                    .expect("could not subscribe to window events");

                for event in events {
                    let updated = match event.expect("could not read event in `window` block") {
                        Event::Window(e) => match (e.change, e.container) {
                            (WindowChange::Mark, Node { marks, .. }) => update_marks(marks),
                            (WindowChange::Focus, Node { name, marks, .. }) => {
                                let updated_for_window = name.map(&update_window).unwrap_or(false);
                                let updated_for_marks = update_marks(marks);
                                updated_for_window || updated_for_marks
                            }
                            (
                                WindowChange::Title,
                                Node {
                                    focused: true,
                                    name: Some(name),
                                    ..
                                },
                            ) => update_window(name),
                            (
                                WindowChange::Close,
                                Node {
                                    name: Some(name), ..
                                },
                            ) => close_window(name),
                            _ => false,
                        },
                        Event::Workspace(e) if e.change == WorkspaceChange::Init => {
                            update_window("".to_string())
                        }
                        _ => false,
                    };

                    if updated {
                        tx.send(Task {
                            id,
                            update_time: Instant::now(),
                        })
                        .expect("could not communicate with channel in `window` block");
                    }
                }
            })
            .expect("failed to start watching thread for `window` block");

        let text = TextWidget::new(id, 0, shared_config);
        Ok(FocusedWindow {
            id,
            text,
            max_width: block_config.max_width,
            show_marks: block_config.show_marks,
            title,
            marks,
        })
    }
}

impl Block for FocusedWindow {
    fn update(&mut self) -> Result<Option<Update>> {
        let mut marks_string = (*self
            .marks
            .lock()
            .block_error("focused_window", "failed to acquire lock")?)
        .clone();
        marks_string = marks_string.chars().take(self.max_width).collect();
        let mut title_string = (*self
            .title
            .lock()
            .block_error("focused_window", "failed to acquire lock")?)
        .clone();
        title_string = title_string.chars().take(self.max_width).collect();
        let out_str = match self.show_marks {
            MarksType::None => title_string,
            _ => {
                if !marks_string.is_empty() {
                    marks_string
                } else {
                    title_string
                }
            }
        };
        self.text.set_text(out_str);

        Ok(None)
    }

    fn view(&self) -> Vec<&dyn I3BarWidget> {
        let title = &*self
            .title
            .lock()
            .expect("lock has been poisoned in `window` block");

        if title.is_empty() {
            vec![]
        } else {
            vec![&self.text]
        }
    }

    fn id(&self) -> usize {
        self.id
    }
}