Skip to content

Commit 509c81d

Browse files
authored
Support for Sway's scratchpad (#2064)
1 parent a243303 commit 509c81d

File tree

9 files changed

+94
-0
lines changed

9 files changed

+94
-0
lines changed

files/icons/awesome4.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pomodoro_paused = "\uf04c" # fa-pause
5858
pomodoro_started = "\uf04b" # fa-play
5959
pomodoro_stopped = "\uf04d" # fa-stop
6060
resolution = "\uf096" # fa-square-o
61+
scratchpad = "\uf2d2" # fa-window-restore
6162
tasks = "\uf0ae" # fa-tasks
6263
tea = "\uf0f4" # fa-coffee
6364
thermometer = "\uf2c8" # fa-thermometer-3

files/icons/awesome5.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pomodoro_paused = "\uf04c" # fa-pause
5858
pomodoro_started = "\uf04b" # fa-play
5959
pomodoro_stopped = "\uf04d" # fa-stop
6060
resolution = "\uf096" # fa-square-o
61+
scratchpad = "\uf2d2" # fa-window-restore
6162
tasks = "\uf0ae"
6263
tea = "\uf0f4"
6364
thermometer = "\uf2c8"

files/icons/awesome6.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pomodoro_paused = "\uf04c" # fa-pause
6262
pomodoro_started = "\uf04b" # fa-play
6363
pomodoro_stopped = "\uf04d" # fa-stop
6464
resolution = "\uf096" # fa-square-o
65+
scratchpad = "\uf2d2" # fa-window-restore
6566
tasks = "\uf0ae"
6667
tea = "\uf0f4"
6768
thermometer = "\uf2c8"

files/icons/emoji.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pomodoro_paused = "⏸️"
5454
pomodoro_started = "▶️"
5555
pomodoro_stopped = "⏹️"
5656
resolution = "🔳"
57+
scratchpad = "🗔"
5758
tasks = ""
5859
tea = ""
5960
thermometer = "🌡️"

files/icons/material-nf.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pomodoro_paused = "\U000f03e4" # nf-md-pause
9191
pomodoro_started = "\U000f040a" # nf-md-play
9292
pomodoro_stopped = "\U000f04db" # nf-md-stop
9393
resolution = "\U000f0293" # nf-md-fullscreen
94+
scratchpad = "\U000f05b2" # nf-md-window_restore
9495
tasks = "\U000f05c7" # nf-md-playlist_check
9596
tea = "\U000f0d9e" # nf-md-tea
9697
thermometer = [

files/icons/material.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pomodoro_paused = "\ue034" # pause
7575
pomodoro_started = "\ue037" # play_arrow
7676
pomodoro_stopped = "\uef6a" # play_disabled ef6a | TODO: broken?
7777
resolution = "\uf152" # crop-square-rounded
78+
scratchpad = "\ue883" # flip_to_front
7879
tasks = "\ue8f9" # work
7980
tea = "\uefef" # coffee
8081
thermometer = "\ue1ff" # device_thermostat | TODO: broken?

src/blocks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ define_blocks!(
188188
privacy,
189189
rofication,
190190
service_status,
191+
scratchpad,
191192
sound,
192193
speedtest,
193194
keyboard_layout,

src/blocks/scratchpad.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//! Scratchpad indicator
2+
//!
3+
//! # Configuration
4+
//!
5+
//! Key | Values | Default
6+
//! ----|--------|--------
7+
//! `format` | A string to customise the output of this block | ` $icon $count.eng(range:1..) |`
8+
//!
9+
//! Placeholder | Value | Type | Unit
10+
//! ------------|--------------------------------------------|--------|-----
11+
//! `icon` | A static icon | Icon | -
12+
//! `count` | Number of windows in scratchpad | Number | -
13+
//!
14+
//! # Example
15+
//!
16+
//! ```toml
17+
//! [[block]]
18+
//! block = "scratchpad"
19+
//! ```
20+
//!
21+
//! # Icons Used
22+
//! - `scratchpad`
23+
24+
use swayipc_async::{Connection, Event as SwayEvent, EventType, Node, WindowChange};
25+
26+
use super::prelude::*;
27+
28+
#[derive(Deserialize, Debug, SmartDefault)]
29+
#[serde(deny_unknown_fields, default)]
30+
pub struct Config {
31+
pub format: FormatConfig,
32+
}
33+
34+
fn count_scratchpad_windows(node: &Node) -> usize {
35+
node.find_as_ref(|n| n.name.as_deref() == Some("__i3_scratch"))
36+
.map_or(0, |node| node.floating_nodes.len())
37+
}
38+
39+
pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
40+
let format = config
41+
.format
42+
.with_default(" $icon $count.eng(range:1..) |")?;
43+
44+
let connection_for_events = Connection::new()
45+
.await
46+
.error("failed to open connection with swayipc")?;
47+
48+
let mut connection_for_tree = Connection::new()
49+
.await
50+
.error("failed to open connection with swayipc")?;
51+
52+
let mut events = connection_for_events
53+
.subscribe(&[EventType::Window])
54+
.await
55+
.error("could not subscribe to window events")?;
56+
57+
loop {
58+
let mut widget = Widget::new().with_format(format.clone());
59+
60+
let root_node = connection_for_tree
61+
.get_tree()
62+
.await
63+
.error("could not get windows tree")?;
64+
let count = count_scratchpad_windows(&root_node);
65+
66+
widget.set_values(map! {
67+
"icon" => Value::icon("scratchpad"),
68+
"count" => Value::number(count),
69+
});
70+
71+
api.set_widget(widget)?;
72+
73+
loop {
74+
let event = events
75+
.next()
76+
.await
77+
.error("swayipc channel closed")?
78+
.error("bad event")?;
79+
80+
match event {
81+
SwayEvent::Window(e) if e.change == WindowChange::Move => break,
82+
_ => continue,
83+
}
84+
}
85+
}
86+
}

src/icons.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl Default for Icons {
8484
"pomodoro_started" => "STARTED",
8585
"pomodoro_stopped" => "STOPPED",
8686
"resolution" => "RES",
87+
"scratchpad" => "[]",
8788
"tasks" => "TSK",
8889
"tea" => "TEA",
8990
"thermometer" => "TEMP",

0 commit comments

Comments
 (0)