Skip to content

Commit

Permalink
Support for Sway's scratchpad (#2064)
Browse files Browse the repository at this point in the history
  • Loading branch information
qehgt authored Jul 25, 2024
1 parent a243303 commit 509c81d
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions files/icons/awesome4.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pomodoro_paused = "\uf04c" # fa-pause
pomodoro_started = "\uf04b" # fa-play
pomodoro_stopped = "\uf04d" # fa-stop
resolution = "\uf096" # fa-square-o
scratchpad = "\uf2d2" # fa-window-restore
tasks = "\uf0ae" # fa-tasks
tea = "\uf0f4" # fa-coffee
thermometer = "\uf2c8" # fa-thermometer-3
Expand Down
1 change: 1 addition & 0 deletions files/icons/awesome5.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pomodoro_paused = "\uf04c" # fa-pause
pomodoro_started = "\uf04b" # fa-play
pomodoro_stopped = "\uf04d" # fa-stop
resolution = "\uf096" # fa-square-o
scratchpad = "\uf2d2" # fa-window-restore
tasks = "\uf0ae"
tea = "\uf0f4"
thermometer = "\uf2c8"
Expand Down
1 change: 1 addition & 0 deletions files/icons/awesome6.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pomodoro_paused = "\uf04c" # fa-pause
pomodoro_started = "\uf04b" # fa-play
pomodoro_stopped = "\uf04d" # fa-stop
resolution = "\uf096" # fa-square-o
scratchpad = "\uf2d2" # fa-window-restore
tasks = "\uf0ae"
tea = "\uf0f4"
thermometer = "\uf2c8"
Expand Down
1 change: 1 addition & 0 deletions files/icons/emoji.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pomodoro_paused = "⏸️"
pomodoro_started = "▶️"
pomodoro_stopped = "⏹️"
resolution = "🔳"
scratchpad = "🗔"
tasks = ""
tea = ""
thermometer = "🌡️"
Expand Down
1 change: 1 addition & 0 deletions files/icons/material-nf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pomodoro_paused = "\U000f03e4" # nf-md-pause
pomodoro_started = "\U000f040a" # nf-md-play
pomodoro_stopped = "\U000f04db" # nf-md-stop
resolution = "\U000f0293" # nf-md-fullscreen
scratchpad = "\U000f05b2" # nf-md-window_restore
tasks = "\U000f05c7" # nf-md-playlist_check
tea = "\U000f0d9e" # nf-md-tea
thermometer = [
Expand Down
1 change: 1 addition & 0 deletions files/icons/material.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pomodoro_paused = "\ue034" # pause
pomodoro_started = "\ue037" # play_arrow
pomodoro_stopped = "\uef6a" # play_disabled ef6a | TODO: broken?
resolution = "\uf152" # crop-square-rounded
scratchpad = "\ue883" # flip_to_front
tasks = "\ue8f9" # work
tea = "\uefef" # coffee
thermometer = "\ue1ff" # device_thermostat | TODO: broken?
Expand Down
1 change: 1 addition & 0 deletions src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ define_blocks!(
privacy,
rofication,
service_status,
scratchpad,
sound,
speedtest,
keyboard_layout,
Expand Down
86 changes: 86 additions & 0 deletions src/blocks/scratchpad.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//! Scratchpad indicator
//!
//! # Configuration
//!
//! Key | Values | Default
//! ----|--------|--------
//! `format` | A string to customise the output of this block | ` $icon $count.eng(range:1..) |`
//!
//! Placeholder | Value | Type | Unit
//! ------------|--------------------------------------------|--------|-----
//! `icon` | A static icon | Icon | -
//! `count` | Number of windows in scratchpad | Number | -
//!
//! # Example
//!
//! ```toml
//! [[block]]
//! block = "scratchpad"
//! ```
//!
//! # Icons Used
//! - `scratchpad`

use swayipc_async::{Connection, Event as SwayEvent, EventType, Node, WindowChange};

use super::prelude::*;

#[derive(Deserialize, Debug, SmartDefault)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
pub format: FormatConfig,
}

fn count_scratchpad_windows(node: &Node) -> usize {
node.find_as_ref(|n| n.name.as_deref() == Some("__i3_scratch"))
.map_or(0, |node| node.floating_nodes.len())
}

pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let format = config
.format
.with_default(" $icon $count.eng(range:1..) |")?;

let connection_for_events = Connection::new()
.await
.error("failed to open connection with swayipc")?;

let mut connection_for_tree = Connection::new()
.await
.error("failed to open connection with swayipc")?;

let mut events = connection_for_events
.subscribe(&[EventType::Window])
.await
.error("could not subscribe to window events")?;

loop {
let mut widget = Widget::new().with_format(format.clone());

let root_node = connection_for_tree
.get_tree()
.await
.error("could not get windows tree")?;
let count = count_scratchpad_windows(&root_node);

widget.set_values(map! {
"icon" => Value::icon("scratchpad"),
"count" => Value::number(count),
});

api.set_widget(widget)?;

loop {
let event = events
.next()
.await
.error("swayipc channel closed")?
.error("bad event")?;

match event {
SwayEvent::Window(e) if e.change == WindowChange::Move => break,
_ => continue,
}
}
}
}
1 change: 1 addition & 0 deletions src/icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl Default for Icons {
"pomodoro_started" => "STARTED",
"pomodoro_stopped" => "STOPPED",
"resolution" => "RES",
"scratchpad" => "[]",
"tasks" => "TSK",
"tea" => "TEA",
"thermometer" => "TEMP",
Expand Down

0 comments on commit 509c81d

Please sign in to comment.