2022-11-06 23:38:51 +00:00
|
|
|
use crate::clients::wayland::{self, ToplevelChange};
|
2022-11-28 21:55:08 +00:00
|
|
|
use crate::config::CommonConfig;
|
2022-09-25 22:49:00 +01:00
|
|
|
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
|
2022-11-06 23:38:51 +00:00
|
|
|
use crate::{await_sync, icon};
|
2022-08-21 23:36:07 +01:00
|
|
|
use color_eyre::Result;
|
2022-08-14 20:40:11 +01:00
|
|
|
use glib::Continue;
|
|
|
|
use gtk::prelude::*;
|
2022-10-15 16:27:25 +01:00
|
|
|
use gtk::{IconTheme, Image, Label};
|
2022-08-14 20:40:11 +01:00
|
|
|
use serde::Deserialize;
|
2022-09-25 22:49:00 +01:00
|
|
|
use tokio::spawn;
|
|
|
|
use tokio::sync::mpsc::{Receiver, Sender};
|
2022-08-14 20:40:11 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct FocusedModule {
|
2022-08-28 16:57:41 +01:00
|
|
|
/// Whether to show icon on the bar.
|
2022-08-14 20:40:11 +01:00
|
|
|
#[serde(default = "crate::config::default_true")]
|
|
|
|
show_icon: bool,
|
2022-08-28 16:57:41 +01:00
|
|
|
/// Whether to show app name on the bar.
|
2022-08-14 20:40:11 +01:00
|
|
|
#[serde(default = "crate::config::default_true")]
|
|
|
|
show_title: bool,
|
|
|
|
|
2022-08-28 16:57:41 +01:00
|
|
|
/// Icon size in pixels.
|
2022-08-14 20:40:11 +01:00
|
|
|
#[serde(default = "default_icon_size")]
|
|
|
|
icon_size: i32,
|
2022-08-28 16:57:41 +01:00
|
|
|
/// GTK icon theme to use.
|
2022-08-14 20:40:11 +01:00
|
|
|
icon_theme: Option<String>,
|
2022-11-28 21:55:08 +00:00
|
|
|
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub common: CommonConfig,
|
2022-08-14 20:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const fn default_icon_size() -> i32 {
|
|
|
|
32
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Module<gtk::Box> for FocusedModule {
|
2022-09-25 22:49:00 +01:00
|
|
|
type SendMessage = (String, String);
|
|
|
|
type ReceiveMessage = ();
|
|
|
|
|
|
|
|
fn spawn_controller(
|
|
|
|
&self,
|
|
|
|
_info: &ModuleInfo,
|
|
|
|
tx: Sender<ModuleUpdateEvent<Self::SendMessage>>,
|
|
|
|
_rx: Receiver<Self::ReceiveMessage>,
|
|
|
|
) -> Result<()> {
|
|
|
|
let focused = await_sync(async {
|
2022-10-04 23:26:07 +01:00
|
|
|
let wl = wayland::get_client().await;
|
|
|
|
let toplevels = wl
|
|
|
|
.toplevels
|
|
|
|
.read()
|
|
|
|
.expect("Failed to get read lock on toplevels")
|
|
|
|
.clone();
|
2022-09-25 22:49:00 +01:00
|
|
|
|
2022-11-05 17:32:01 +00:00
|
|
|
toplevels.into_iter().find(|(_, (top, _))| top.active)
|
2022-10-04 23:26:07 +01:00
|
|
|
});
|
2022-08-14 20:40:11 +01:00
|
|
|
|
2022-11-05 17:32:01 +00:00
|
|
|
if let Some((_, (top, _))) = focused {
|
2022-10-10 21:59:44 +01:00
|
|
|
tx.try_send(ModuleUpdateEvent::Update((top.title.clone(), top.app_id)))?;
|
2022-08-14 20:40:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
spawn(async move {
|
2022-10-04 23:26:07 +01:00
|
|
|
let mut wlrx = {
|
|
|
|
let wl = wayland::get_client().await;
|
|
|
|
wl.subscribe_toplevels()
|
2022-08-25 21:53:42 +01:00
|
|
|
};
|
|
|
|
|
2022-10-04 23:26:07 +01:00
|
|
|
while let Ok(event) = wlrx.recv().await {
|
|
|
|
let update = match event.change {
|
|
|
|
ToplevelChange::Focus(focus) => focus,
|
|
|
|
ToplevelChange::Title(_) => event.toplevel.active,
|
2022-10-10 21:59:44 +01:00
|
|
|
_ => false,
|
2022-08-25 21:53:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if update {
|
2022-10-04 23:26:07 +01:00
|
|
|
tx.send(ModuleUpdateEvent::Update((
|
|
|
|
event.toplevel.title,
|
|
|
|
event.toplevel.app_id,
|
2022-09-25 22:49:00 +01:00
|
|
|
)))
|
2022-10-04 23:26:07 +01:00
|
|
|
.await
|
2022-09-25 22:49:00 +01:00
|
|
|
.expect("Failed to send focus update");
|
2022-08-14 20:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_widget(
|
|
|
|
self,
|
|
|
|
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
2022-10-15 16:27:25 +01:00
|
|
|
info: &ModuleInfo,
|
2022-09-25 22:49:00 +01:00
|
|
|
) -> Result<ModuleWidget<gtk::Box>> {
|
|
|
|
let icon_theme = IconTheme::new();
|
|
|
|
|
|
|
|
if let Some(theme) = self.icon_theme {
|
|
|
|
icon_theme.set_custom_theme(Some(&theme));
|
|
|
|
}
|
2022-08-14 20:40:11 +01:00
|
|
|
|
2022-10-15 16:27:25 +01:00
|
|
|
let container = gtk::Box::new(info.bar_position.get_orientation(), 5);
|
2022-09-25 22:49:00 +01:00
|
|
|
|
|
|
|
let icon = Image::builder().name("icon").build();
|
|
|
|
let label = Label::builder().name("label").build();
|
|
|
|
|
|
|
|
container.add(&icon);
|
|
|
|
container.add(&label);
|
|
|
|
|
|
|
|
{
|
|
|
|
context.widget_rx.attach(None, move |(name, id)| {
|
|
|
|
let pixbuf = icon::get_icon(&icon_theme, &id, self.icon_size);
|
2022-08-14 20:40:11 +01:00
|
|
|
|
|
|
|
if self.show_icon {
|
|
|
|
icon.set_pixbuf(pixbuf.as_ref());
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.show_title {
|
2022-09-25 22:49:00 +01:00
|
|
|
label.set_label(&name);
|
2022-08-14 20:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Continue(true)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
Ok(ModuleWidget {
|
|
|
|
widget: container,
|
|
|
|
popup: None,
|
|
|
|
})
|
2022-08-14 20:40:11 +01:00
|
|
|
}
|
|
|
|
}
|