2023-04-29 22:08:02 +01:00
|
|
|
use crate::clients::wayland::{self, ToplevelEvent};
|
2023-01-28 23:01:44 +00:00
|
|
|
use crate::config::{CommonConfig, TruncateMode};
|
2023-05-06 00:40:06 +01:00
|
|
|
use crate::gtk_helpers::add_class;
|
2023-01-29 17:46:02 +00:00
|
|
|
use crate::image::ImageProvider;
|
2022-09-25 22:49:00 +01:00
|
|
|
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
|
2023-04-29 22:08:02 +01:00
|
|
|
use crate::{send_async, try_send};
|
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::*;
|
2023-01-29 18:38:57 +00:00
|
|
|
use gtk::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};
|
2023-05-20 14:36:04 +01:00
|
|
|
use tracing::debug;
|
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-11-28 21:55:08 +00:00
|
|
|
|
2023-01-28 23:01:44 +00:00
|
|
|
truncate: Option<TruncateMode>,
|
|
|
|
|
2022-11-28 21:55:08 +00:00
|
|
|
#[serde(flatten)]
|
2022-12-04 23:23:22 +00:00
|
|
|
pub common: Option<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 = ();
|
|
|
|
|
2022-12-04 23:23:22 +00:00
|
|
|
fn name() -> &'static str {
|
|
|
|
"focused"
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
fn spawn_controller(
|
|
|
|
&self,
|
|
|
|
_info: &ModuleInfo,
|
|
|
|
tx: Sender<ModuleUpdateEvent<Self::SendMessage>>,
|
|
|
|
_rx: Receiver<Self::ReceiveMessage>,
|
|
|
|
) -> Result<()> {
|
|
|
|
spawn(async move {
|
2023-04-29 22:08:02 +01:00
|
|
|
let (mut wlrx, handles) = {
|
2022-10-04 23:26:07 +01:00
|
|
|
let wl = wayland::get_client().await;
|
|
|
|
wl.subscribe_toplevels()
|
2022-08-25 21:53:42 +01:00
|
|
|
};
|
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
let focused = handles.values().find_map(|handle| {
|
|
|
|
handle
|
|
|
|
.info()
|
|
|
|
.and_then(|info| if info.focused { Some(info) } else { None })
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(focused) = focused {
|
|
|
|
try_send!(
|
|
|
|
tx,
|
|
|
|
ModuleUpdateEvent::Update((focused.title.clone(), focused.app_id))
|
|
|
|
);
|
|
|
|
};
|
2022-08-25 21:53:42 +01:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
while let Ok(event) = wlrx.recv().await {
|
|
|
|
if let ToplevelEvent::Update(handle) = event {
|
|
|
|
let info = handle.info().unwrap_or_default();
|
|
|
|
|
|
|
|
if info.focused {
|
|
|
|
debug!("Changing focus");
|
|
|
|
send_async!(
|
|
|
|
tx,
|
|
|
|
ModuleUpdateEvent::Update((info.title.clone(), info.app_id.clone()))
|
|
|
|
);
|
|
|
|
}
|
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>> {
|
2023-01-29 18:38:57 +00:00
|
|
|
let icon_theme = info.icon_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
|
|
|
|
2023-05-06 00:40:06 +01:00
|
|
|
let icon = gtk::Image::new();
|
|
|
|
add_class(&icon, "icon");
|
|
|
|
|
|
|
|
let label = Label::new(None);
|
|
|
|
add_class(&label, "label");
|
2022-09-25 22:49:00 +01:00
|
|
|
|
2023-01-28 23:01:44 +00:00
|
|
|
if let Some(truncate) = self.truncate {
|
|
|
|
truncate.truncate_label(&label);
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
container.add(&icon);
|
|
|
|
container.add(&label);
|
|
|
|
|
|
|
|
{
|
2023-01-29 18:38:57 +00:00
|
|
|
let icon_theme = icon_theme.clone();
|
2022-09-25 22:49:00 +01:00
|
|
|
context.widget_rx.attach(None, move |(name, id)| {
|
2022-08-14 20:40:11 +01:00
|
|
|
if self.show_icon {
|
2023-05-20 14:36:04 +01:00
|
|
|
ImageProvider::parse(&id, &icon_theme, self.icon_size)
|
|
|
|
.map(|image| image.load_into_image(icon.clone()));
|
2022-08-14 20:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|