2022-08-14 14:30:13 +01:00
|
|
|
use crate::modules::{Module, ModuleInfo};
|
2022-08-25 21:53:42 +01:00
|
|
|
use crate::sway::{get_client, Workspace};
|
2022-08-21 23:36:07 +01:00
|
|
|
use color_eyre::{Report, Result};
|
2022-08-14 14:30:13 +01:00
|
|
|
use gtk::prelude::*;
|
|
|
|
use gtk::{Button, Orientation};
|
2022-08-25 21:53:42 +01:00
|
|
|
use ksway::IpcCommand;
|
2022-08-14 14:30:13 +01:00
|
|
|
use serde::Deserialize;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use tokio::spawn;
|
|
|
|
use tokio::sync::mpsc;
|
|
|
|
use tokio::task::spawn_blocking;
|
2022-08-25 21:53:42 +01:00
|
|
|
use tracing::{debug, trace};
|
2022-08-14 14:30:13 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct WorkspacesModule {
|
2022-08-14 16:23:41 +01:00
|
|
|
name_map: Option<HashMap<String, String>>,
|
|
|
|
|
2022-08-14 20:40:11 +01:00
|
|
|
#[serde(default = "crate::config::default_false")]
|
2022-08-14 16:23:41 +01:00
|
|
|
all_monitors: bool,
|
|
|
|
}
|
|
|
|
|
2022-08-14 14:30:13 +01:00
|
|
|
impl Workspace {
|
|
|
|
fn as_button(&self, name_map: &HashMap<String, String>, tx: &mpsc::Sender<String>) -> Button {
|
|
|
|
let button = Button::builder()
|
|
|
|
.label(name_map.get(self.name.as_str()).unwrap_or(&self.name))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let style_context = button.style_context();
|
|
|
|
style_context.add_class("item");
|
|
|
|
|
|
|
|
if self.focused {
|
|
|
|
style_context.add_class("focused");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let tx = tx.clone();
|
|
|
|
let name = self.name.clone();
|
2022-08-21 23:36:07 +01:00
|
|
|
button.connect_clicked(move |_item| {
|
|
|
|
tx.try_send(name.clone())
|
|
|
|
.expect("Failed to send workspace click event");
|
|
|
|
});
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
button
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Module<gtk::Box> for WorkspacesModule {
|
2022-08-21 23:36:07 +01:00
|
|
|
fn into_widget(self, info: &ModuleInfo) -> Result<gtk::Box> {
|
2022-08-14 14:30:13 +01:00
|
|
|
let container = gtk::Box::new(Orientation::Horizontal, 0);
|
|
|
|
|
|
|
|
let workspaces = {
|
2022-08-25 21:53:42 +01:00
|
|
|
trace!("Getting current workspaces");
|
|
|
|
let sway = get_client();
|
|
|
|
let mut sway = sway.lock().expect("Failed to get lock on Sway IPC client");
|
2022-08-21 23:36:07 +01:00
|
|
|
let raw = sway.ipc(IpcCommand::GetWorkspaces)?;
|
|
|
|
let workspaces = serde_json::from_slice::<Vec<Workspace>>(&raw)?;
|
2022-08-14 16:23:41 +01:00
|
|
|
|
2022-08-15 21:11:17 +01:00
|
|
|
if self.all_monitors {
|
|
|
|
workspaces
|
|
|
|
} else {
|
2022-08-14 16:23:41 +01:00
|
|
|
workspaces
|
|
|
|
.into_iter()
|
|
|
|
.filter(|workspace| workspace.output == info.output_name)
|
|
|
|
.collect()
|
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let name_map = self.name_map.unwrap_or_default();
|
|
|
|
|
|
|
|
let mut button_map: HashMap<String, Button> = HashMap::new();
|
|
|
|
|
|
|
|
let (ui_tx, mut ui_rx) = mpsc::channel(32);
|
|
|
|
|
|
|
|
for workspace in workspaces {
|
|
|
|
let item = workspace.as_button(&name_map, &ui_tx);
|
|
|
|
container.add(&item);
|
|
|
|
button_map.insert(workspace.name, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
|
|
|
|
|
2022-08-25 21:53:42 +01:00
|
|
|
spawn_blocking(move || {
|
|
|
|
trace!("Starting workspace event listener task");
|
|
|
|
let srx = {
|
|
|
|
let sway = get_client();
|
|
|
|
let mut sway = sway.lock().expect("Failed to get lock on Sway IPC client");
|
|
|
|
|
|
|
|
sway.subscribe_workspace()
|
|
|
|
};
|
2022-08-21 23:36:07 +01:00
|
|
|
|
2022-08-25 21:53:42 +01:00
|
|
|
while let Ok(payload) = srx.recv() {
|
|
|
|
tx.send(payload).expect("Failed to send workspace event");
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
let menubar = container.clone();
|
2022-08-14 16:23:41 +01:00
|
|
|
let output_name = info.output_name.to_string();
|
2022-08-14 14:30:13 +01:00
|
|
|
rx.attach(None, move |event| {
|
|
|
|
match event.change.as_str() {
|
|
|
|
"focus" => {
|
2022-08-21 23:36:07 +01:00
|
|
|
let old = event.old.and_then(|old| button_map.get(&old.name));
|
|
|
|
if let Some(old) = old {
|
|
|
|
old.style_context().remove_class("focused");
|
2022-08-14 16:23:41 +01:00
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2022-08-21 23:36:07 +01:00
|
|
|
let new = event.current.and_then(|new| button_map.get(&new.name));
|
|
|
|
if let Some(new) = new {
|
|
|
|
new.style_context().add_class("focused");
|
2022-08-14 16:23:41 +01:00
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
"init" => {
|
2022-08-21 23:36:07 +01:00
|
|
|
if let Some(workspace) = event.current {
|
|
|
|
if self.all_monitors || workspace.output == output_name {
|
|
|
|
let item = workspace.as_button(&name_map, &ui_tx);
|
|
|
|
|
|
|
|
item.show();
|
|
|
|
menubar.add(&item);
|
|
|
|
button_map.insert(workspace.name, item);
|
|
|
|
}
|
2022-08-14 16:23:41 +01:00
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
2022-08-24 21:27:19 +01:00
|
|
|
"move" => {
|
|
|
|
if let Some(workspace) = event.current {
|
|
|
|
if !self.all_monitors {
|
|
|
|
if workspace.output == output_name {
|
|
|
|
let item = workspace.as_button(&name_map, &ui_tx);
|
|
|
|
|
|
|
|
item.show();
|
|
|
|
menubar.add(&item);
|
|
|
|
button_map.insert(workspace.name, item);
|
|
|
|
} else if let Some(item) = button_map.get(&workspace.name) {
|
|
|
|
menubar.remove(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
"empty" => {
|
2022-08-21 23:36:07 +01:00
|
|
|
if let Some(workspace) = event.current {
|
|
|
|
if let Some(item) = button_map.get(&workspace.name) {
|
|
|
|
menubar.remove(item);
|
|
|
|
}
|
2022-08-14 16:23:41 +01:00
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
Continue(true)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
spawn(async move {
|
2022-08-25 21:53:42 +01:00
|
|
|
trace!("Setting up UI event handler");
|
|
|
|
let sway = get_client();
|
2022-08-14 14:30:13 +01:00
|
|
|
while let Some(name) = ui_rx.recv().await {
|
2022-08-25 21:53:42 +01:00
|
|
|
let mut sway = sway
|
|
|
|
.lock()
|
|
|
|
.expect("Failed to get write lock on Sway IPC client");
|
2022-08-21 23:36:07 +01:00
|
|
|
sway.run(format!("workspace {}", name))?;
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
2022-08-21 23:36:07 +01:00
|
|
|
|
|
|
|
Ok::<(), Report>(())
|
2022-08-14 14:30:13 +01:00
|
|
|
});
|
|
|
|
|
2022-08-21 23:36:07 +01:00
|
|
|
Ok(container)
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
}
|