2022-08-14 14:30:13 +01:00
|
|
|
mod item;
|
2022-08-25 23:42:57 +01:00
|
|
|
mod open_state;
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2024-01-07 23:50:10 +00:00
|
|
|
use self::item::{AppearanceOptions, Item, ItemButton, Window};
|
2022-10-10 20:15:24 +01:00
|
|
|
use self::open_state::OpenState;
|
2024-01-07 23:50:10 +00:00
|
|
|
use super::{Module, ModuleInfo, ModuleParts, ModulePopup, ModuleUpdateEvent, WidgetContext};
|
2023-04-29 22:08:02 +01:00
|
|
|
use crate::clients::wayland::{self, ToplevelEvent};
|
2022-11-28 21:55:08 +00:00
|
|
|
use crate::config::CommonConfig;
|
2023-01-29 17:46:02 +00:00
|
|
|
use crate::desktop_file::find_desktop_file;
|
2023-12-17 23:51:43 +00:00
|
|
|
use crate::{arc_mut, glib_recv, lock, send_async, spawn, try_send, write_lock};
|
2022-09-25 22:49:00 +01:00
|
|
|
use color_eyre::{Help, Report};
|
2022-08-14 14:30:13 +01:00
|
|
|
use gtk::prelude::*;
|
2023-01-29 18:38:57 +00:00
|
|
|
use gtk::{Button, Orientation};
|
2022-11-05 17:32:01 +00:00
|
|
|
use indexmap::IndexMap;
|
2022-08-14 14:30:13 +01:00
|
|
|
use serde::Deserialize;
|
2022-09-25 22:49:00 +01:00
|
|
|
use std::process::{Command, Stdio};
|
2023-06-29 23:16:31 +01:00
|
|
|
use std::sync::Arc;
|
2023-12-17 23:51:43 +00:00
|
|
|
use tokio::sync::{broadcast, mpsc};
|
2022-09-25 22:49:00 +01:00
|
|
|
use tracing::{debug, error, trace};
|
2022-08-14 14:30:13 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct LauncherModule {
|
2022-09-25 22:49:00 +01:00
|
|
|
/// List of app IDs (or classes) to always show regardless of open state,
|
2022-08-28 16:57:41 +01:00
|
|
|
/// in the order specified.
|
2022-08-14 14:30:13 +01:00
|
|
|
favorites: Option<Vec<String>>,
|
2022-08-28 16:57:41 +01:00
|
|
|
/// Whether to show application names on the bar.
|
2022-08-14 20:40:11 +01:00
|
|
|
#[serde(default = "crate::config::default_false")]
|
2022-08-14 14:30:13 +01:00
|
|
|
show_names: bool,
|
2022-08-28 16:57:41 +01:00
|
|
|
/// Whether to show application icons on the bar.
|
2022-08-14 20:40:11 +01:00
|
|
|
#[serde(default = "crate::config::default_true")]
|
2022-08-14 14:30:13 +01:00
|
|
|
show_icons: bool,
|
|
|
|
|
2023-04-22 22:18:36 +01:00
|
|
|
#[serde(default = "default_icon_size")]
|
|
|
|
icon_size: i32,
|
|
|
|
|
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 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
2023-04-22 22:18:36 +01:00
|
|
|
const fn default_icon_size() -> i32 {
|
|
|
|
32
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum LauncherUpdate {
|
|
|
|
/// Adds item
|
|
|
|
AddItem(Item),
|
|
|
|
/// Adds window to item with `app_id`
|
|
|
|
AddWindow(String, Window),
|
|
|
|
/// Removes item with `app_id`
|
|
|
|
RemoveItem(String),
|
|
|
|
/// Removes window from item with `app_id`.
|
2022-10-10 20:15:24 +01:00
|
|
|
RemoveWindow(String, usize),
|
2022-09-25 22:49:00 +01:00
|
|
|
/// Sets title for `app_id`
|
2022-10-10 20:15:24 +01:00
|
|
|
Title(String, usize, String),
|
|
|
|
/// Marks the item with `app_id` as focused or not focused
|
|
|
|
Focus(String, bool),
|
2022-09-25 22:49:00 +01:00
|
|
|
/// Declares the item with `app_id` has been hovered over
|
|
|
|
Hover(String),
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ItemEvent {
|
|
|
|
FocusItem(String),
|
2022-10-10 20:15:24 +01:00
|
|
|
FocusWindow(usize),
|
2022-09-25 22:49:00 +01:00
|
|
|
OpenItem(String),
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
enum ItemOrWindow {
|
|
|
|
Item(Item),
|
|
|
|
Window(Window),
|
|
|
|
}
|
2022-08-25 23:42:57 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
enum ItemOrWindowId {
|
|
|
|
Item,
|
|
|
|
Window,
|
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
impl Module<gtk::Box> for LauncherModule {
|
|
|
|
type SendMessage = LauncherUpdate;
|
|
|
|
type ReceiveMessage = ItemEvent;
|
|
|
|
|
2022-12-04 23:23:22 +00:00
|
|
|
fn name() -> &'static str {
|
|
|
|
"launcher"
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
fn spawn_controller(
|
|
|
|
&self,
|
|
|
|
_info: &ModuleInfo,
|
2024-01-07 23:42:34 +00:00
|
|
|
context: &WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
2023-12-17 23:51:43 +00:00
|
|
|
mut rx: mpsc::Receiver<Self::ReceiveMessage>,
|
2022-09-25 22:49:00 +01:00
|
|
|
) -> crate::Result<()> {
|
2022-11-06 22:53:48 +00:00
|
|
|
let items = self
|
|
|
|
.favorites
|
|
|
|
.as_ref()
|
|
|
|
.map_or_else(IndexMap::new, |favorites| {
|
|
|
|
favorites
|
|
|
|
.iter()
|
|
|
|
.map(|app_id| {
|
|
|
|
(
|
|
|
|
app_id.to_string(),
|
|
|
|
Item::new(app_id.to_string(), OpenState::Closed, true),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<IndexMap<_, _>>()
|
|
|
|
});
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2023-06-29 23:16:31 +01:00
|
|
|
let items = arc_mut!(items);
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
let items2 = Arc::clone(&items);
|
2024-01-07 23:42:34 +00:00
|
|
|
|
2024-01-07 23:50:10 +00:00
|
|
|
let tx = context.tx.clone();
|
|
|
|
let tx2 = context.tx.clone();
|
|
|
|
|
|
|
|
let wl = context.client::<wayland::Client>();
|
2023-04-29 22:08:02 +01:00
|
|
|
spawn(async move {
|
|
|
|
let items = items2;
|
|
|
|
let tx = tx2;
|
|
|
|
|
2024-01-07 23:50:10 +00:00
|
|
|
let mut wlrx = wl.subscribe_toplevels();
|
|
|
|
let handles = wl.toplevel_info_all();
|
2023-04-29 22:08:02 +01:00
|
|
|
|
2024-01-07 23:50:10 +00:00
|
|
|
for info in handles {
|
2023-04-29 22:08:02 +01:00
|
|
|
let mut items = lock!(items);
|
|
|
|
let item = items.get_mut(&info.app_id);
|
|
|
|
match item {
|
|
|
|
Some(item) => {
|
2024-01-07 23:50:10 +00:00
|
|
|
item.merge_toplevel(info.clone());
|
2023-04-29 22:08:02 +01:00
|
|
|
}
|
|
|
|
None => {
|
2024-01-07 23:50:10 +00:00
|
|
|
items.insert(info.app_id.clone(), Item::from(info.clone()));
|
2022-09-25 22:49:00 +01:00
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
2023-04-29 22:08:02 +01:00
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
{
|
2023-02-25 14:24:21 +00:00
|
|
|
let items = lock!(items);
|
2022-10-10 20:15:24 +01:00
|
|
|
let items = items.iter();
|
2022-11-05 17:32:01 +00:00
|
|
|
for (_, item) in items {
|
2023-04-29 22:08:02 +01:00
|
|
|
try_send!(
|
|
|
|
tx,
|
|
|
|
ModuleUpdateEvent::Update(LauncherUpdate::AddItem(item.clone()))
|
|
|
|
);
|
2022-10-10 20:15:24 +01:00
|
|
|
}
|
2023-04-29 22:08:02 +01:00
|
|
|
}
|
2022-08-21 23:36:07 +01:00
|
|
|
|
2022-10-10 20:15:24 +01:00
|
|
|
let send_update = |update: LauncherUpdate| tx.send(ModuleUpdateEvent::Update(update));
|
2022-09-25 22:49:00 +01:00
|
|
|
|
2022-10-10 20:15:24 +01:00
|
|
|
while let Ok(event) = wlrx.recv().await {
|
|
|
|
trace!("event: {:?}", event);
|
2022-09-25 22:49:00 +01:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
match event {
|
2024-01-07 23:50:10 +00:00
|
|
|
ToplevelEvent::New(info) => {
|
|
|
|
let app_id = info.app_id.clone();
|
2022-09-25 22:49:00 +01:00
|
|
|
|
|
|
|
let new_item = {
|
2022-12-11 22:45:52 +00:00
|
|
|
let mut items = lock!(items);
|
2023-04-29 22:08:02 +01:00
|
|
|
let item = items.get_mut(&info.app_id);
|
2022-10-10 21:59:44 +01:00
|
|
|
match item {
|
2022-09-25 22:49:00 +01:00
|
|
|
None => {
|
2024-01-07 23:50:10 +00:00
|
|
|
let item: Item = info.into();
|
2023-07-16 20:38:16 +01:00
|
|
|
|
2024-01-07 23:50:10 +00:00
|
|
|
items.insert(app_id.clone(), item.clone());
|
2022-09-25 22:49:00 +01:00
|
|
|
|
|
|
|
ItemOrWindow::Item(item)
|
|
|
|
}
|
|
|
|
Some(item) => {
|
2024-01-07 23:50:10 +00:00
|
|
|
let window = item.merge_toplevel(info);
|
2022-09-25 22:49:00 +01:00
|
|
|
ItemOrWindow::Window(window)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match new_item {
|
|
|
|
ItemOrWindow::Item(item) => {
|
|
|
|
send_update(LauncherUpdate::AddItem(item)).await
|
|
|
|
}
|
|
|
|
ItemOrWindow::Window(window) => {
|
2024-01-07 23:50:10 +00:00
|
|
|
send_update(LauncherUpdate::AddWindow(app_id, window)).await
|
2022-09-25 22:49:00 +01:00
|
|
|
}
|
|
|
|
}?;
|
|
|
|
}
|
2024-01-07 23:50:10 +00:00
|
|
|
ToplevelEvent::Update(info) => {
|
2023-04-29 22:08:02 +01:00
|
|
|
if let Some(item) = lock!(items).get_mut(&info.app_id) {
|
|
|
|
item.set_window_focused(info.id, info.focused);
|
|
|
|
item.set_window_name(info.id, info.title.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
send_update(LauncherUpdate::Focus(info.app_id.clone(), info.focused))
|
|
|
|
.await?;
|
|
|
|
send_update(LauncherUpdate::Title(
|
|
|
|
info.app_id.clone(),
|
|
|
|
info.id,
|
|
|
|
info.title.clone(),
|
|
|
|
))
|
|
|
|
.await?;
|
|
|
|
}
|
2024-01-07 23:50:10 +00:00
|
|
|
ToplevelEvent::Remove(info) => {
|
2022-09-25 22:49:00 +01:00
|
|
|
let remove_item = {
|
2022-12-11 22:45:52 +00:00
|
|
|
let mut items = lock!(items);
|
2023-04-29 22:08:02 +01:00
|
|
|
let item = items.get_mut(&info.app_id);
|
2022-10-10 21:59:44 +01:00
|
|
|
match item {
|
2022-09-25 22:49:00 +01:00
|
|
|
Some(item) => {
|
2024-01-07 23:50:10 +00:00
|
|
|
item.unmerge_toplevel(&info);
|
2022-09-25 22:49:00 +01:00
|
|
|
|
|
|
|
if item.windows.is_empty() {
|
2024-02-02 20:05:29 +00:00
|
|
|
items.shift_remove(&info.app_id);
|
2022-09-25 22:49:00 +01:00
|
|
|
Some(ItemOrWindowId::Item)
|
|
|
|
} else {
|
|
|
|
Some(ItemOrWindowId::Window)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match remove_item {
|
|
|
|
Some(ItemOrWindowId::Item) => {
|
2023-04-29 22:08:02 +01:00
|
|
|
send_update(LauncherUpdate::RemoveItem(info.app_id.clone()))
|
|
|
|
.await?;
|
2022-09-25 22:49:00 +01:00
|
|
|
}
|
|
|
|
Some(ItemOrWindowId::Window) => {
|
2023-04-29 22:08:02 +01:00
|
|
|
send_update(LauncherUpdate::RemoveWindow(
|
|
|
|
info.app_id.clone(),
|
|
|
|
info.id,
|
|
|
|
))
|
|
|
|
.await?;
|
2022-09-25 22:49:00 +01:00
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2022-08-25 23:42:57 +01:00
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2023-04-29 22:08:02 +01:00
|
|
|
Ok::<(), Report>(())
|
2022-09-25 22:49:00 +01:00
|
|
|
});
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
// listen to ui events
|
2024-01-07 23:50:10 +00:00
|
|
|
let wl = context.client::<wayland::Client>();
|
2022-09-25 22:49:00 +01:00
|
|
|
spawn(async move {
|
|
|
|
while let Some(event) = rx.recv().await {
|
|
|
|
if let ItemEvent::OpenItem(app_id) = event {
|
|
|
|
find_desktop_file(&app_id).map_or_else(
|
|
|
|
|| error!("Could not find desktop file for {}", app_id),
|
|
|
|
|file| {
|
|
|
|
if let Err(err) = Command::new("gtk-launch")
|
|
|
|
.arg(
|
|
|
|
file.file_name()
|
|
|
|
.expect("File segment missing from path to desktop file"),
|
|
|
|
)
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.spawn()
|
|
|
|
{
|
|
|
|
error!(
|
|
|
|
"{:?}",
|
|
|
|
Report::new(err)
|
|
|
|
.wrap_err("Failed to run gtk-launch command.")
|
|
|
|
.suggestion("Perhaps the desktop file is invalid?")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
2023-04-29 22:10:13 +01:00
|
|
|
send_async!(tx, ModuleUpdateEvent::ClosePopup);
|
|
|
|
|
2022-10-10 20:15:24 +01:00
|
|
|
let id = match event {
|
2024-01-07 23:50:10 +00:00
|
|
|
ItemEvent::FocusItem(app_id) => {
|
|
|
|
lock!(items).get(&app_id).and_then(|item| {
|
|
|
|
item.windows
|
|
|
|
.iter()
|
|
|
|
.find(|(_, win)| !win.open_state.is_focused())
|
|
|
|
.or_else(|| item.windows.first())
|
|
|
|
.map(|(_, win)| win.id)
|
|
|
|
})
|
|
|
|
}
|
2023-06-29 16:42:03 +01:00
|
|
|
ItemEvent::FocusWindow(id) => Some(id),
|
2022-10-10 20:15:24 +01:00
|
|
|
ItemEvent::OpenItem(_) => unreachable!(),
|
2022-09-25 22:49:00 +01:00
|
|
|
};
|
|
|
|
|
2022-10-10 20:15:24 +01:00
|
|
|
if let Some(id) = id {
|
2024-01-07 23:50:10 +00:00
|
|
|
if let Some(window) = lock!(items)
|
|
|
|
.iter()
|
|
|
|
.find_map(|(_, item)| item.windows.get(&id))
|
2023-04-29 22:08:02 +01:00
|
|
|
{
|
2023-06-29 16:42:03 +01:00
|
|
|
debug!("Focusing window {id}: {}", window.name);
|
2024-01-07 23:50:10 +00:00
|
|
|
wl.toplevel_focus(window.id);
|
2023-04-29 22:08:02 +01:00
|
|
|
}
|
2022-09-25 22:49:00 +01:00
|
|
|
}
|
|
|
|
}
|
2022-08-14 14:47:28 +01:00
|
|
|
}
|
2022-09-25 22:49:00 +01:00
|
|
|
});
|
2022-08-25 23:42:57 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
Ok(())
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
fn into_widget(
|
|
|
|
self,
|
|
|
|
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
2022-10-15 16:27:25 +01:00
|
|
|
info: &ModuleInfo,
|
2023-07-16 18:57:00 +01:00
|
|
|
) -> crate::Result<ModuleParts<gtk::Box>> {
|
2023-01-29 18:38:57 +00:00
|
|
|
let icon_theme = info.icon_theme;
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2022-10-15 16:27:25 +01:00
|
|
|
let container = gtk::Box::new(info.bar_position.get_orientation(), 0);
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
{
|
|
|
|
let container = container.clone();
|
2023-01-29 18:38:57 +00:00
|
|
|
let icon_theme = icon_theme.clone();
|
|
|
|
|
|
|
|
let controller_tx = context.controller_tx.clone();
|
2022-09-25 22:49:00 +01:00
|
|
|
|
2023-04-22 22:18:36 +01:00
|
|
|
let appearance_options = AppearanceOptions {
|
|
|
|
show_names: self.show_names,
|
|
|
|
show_icons: self.show_icons,
|
|
|
|
icon_size: self.icon_size,
|
|
|
|
};
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
let show_names = self.show_names;
|
2023-08-13 14:23:05 +01:00
|
|
|
let bar_position = info.bar_position;
|
2022-09-25 22:49:00 +01:00
|
|
|
|
2022-11-05 17:32:01 +00:00
|
|
|
let mut buttons = IndexMap::<String, ItemButton>::new();
|
2022-09-25 22:49:00 +01:00
|
|
|
|
2023-12-17 23:51:43 +00:00
|
|
|
let tx = context.tx.clone();
|
2023-12-31 00:50:03 +00:00
|
|
|
let rx = context.subscribe();
|
2023-12-17 23:51:43 +00:00
|
|
|
glib_recv!(rx, event => {
|
2022-09-25 22:49:00 +01:00
|
|
|
match event {
|
|
|
|
LauncherUpdate::AddItem(item) => {
|
2024-01-13 17:15:19 +00:00
|
|
|
debug!("Adding item with id '{}' to the bar: {item:?}", item.app_id);
|
2022-09-25 22:49:00 +01:00
|
|
|
|
|
|
|
if let Some(button) = buttons.get(&item.app_id) {
|
|
|
|
button.set_open(true);
|
2024-01-13 17:15:19 +00:00
|
|
|
button.set_focused(item.open_state.is_focused());
|
2022-09-25 22:49:00 +01:00
|
|
|
} else {
|
|
|
|
let button = ItemButton::new(
|
|
|
|
&item,
|
2023-04-22 22:18:36 +01:00
|
|
|
appearance_options,
|
2022-09-25 22:49:00 +01:00
|
|
|
&icon_theme,
|
2023-08-13 14:23:05 +01:00
|
|
|
bar_position,
|
2023-12-17 23:51:43 +00:00
|
|
|
&tx,
|
2023-01-29 18:38:57 +00:00
|
|
|
&controller_tx,
|
2022-09-25 22:49:00 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
container.add(&button.button);
|
|
|
|
buttons.insert(item.app_id, button);
|
|
|
|
}
|
|
|
|
}
|
2023-07-16 20:38:16 +01:00
|
|
|
LauncherUpdate::AddWindow(app_id, win) => {
|
2022-09-25 22:49:00 +01:00
|
|
|
if let Some(button) = buttons.get(&app_id) {
|
2022-10-15 18:01:09 +01:00
|
|
|
button.set_open(true);
|
2023-07-16 20:38:16 +01:00
|
|
|
button.set_focused(win.open_state.is_focused());
|
2022-10-15 18:01:09 +01:00
|
|
|
|
2022-12-11 22:45:52 +00:00
|
|
|
let mut menu_state = write_lock!(button.menu_state);
|
2022-09-25 22:49:00 +01:00
|
|
|
menu_state.num_windows += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LauncherUpdate::RemoveItem(app_id) => {
|
|
|
|
debug!("Removing item with id {}", app_id);
|
|
|
|
|
|
|
|
if let Some(button) = buttons.get(&app_id) {
|
|
|
|
if button.persistent {
|
|
|
|
button.set_open(false);
|
|
|
|
if button.show_names {
|
|
|
|
button.button.set_label(&app_id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
container.remove(&button.button);
|
2024-02-02 20:05:29 +00:00
|
|
|
buttons.shift_remove(&app_id);
|
2022-09-25 22:49:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-16 20:24:23 +01:00
|
|
|
LauncherUpdate::RemoveWindow(app_id, win_id) => {
|
|
|
|
debug!("Removing window {win_id} with id {app_id}");
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
if let Some(button) = buttons.get(&app_id) {
|
2023-07-16 20:24:23 +01:00
|
|
|
button.set_focused(false);
|
|
|
|
|
2022-12-11 22:45:52 +00:00
|
|
|
let mut menu_state = write_lock!(button.menu_state);
|
2022-09-25 22:49:00 +01:00
|
|
|
menu_state.num_windows -= 1;
|
|
|
|
}
|
|
|
|
}
|
2022-10-10 20:15:24 +01:00
|
|
|
LauncherUpdate::Focus(app_id, focus) => {
|
|
|
|
debug!("Changing focus to {} on item with id {}", focus, app_id);
|
2022-09-25 22:49:00 +01:00
|
|
|
|
2022-10-10 20:15:24 +01:00
|
|
|
if let Some(button) = buttons.get(&app_id) {
|
|
|
|
button.set_focused(focus);
|
2022-09-25 22:49:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
LauncherUpdate::Title(app_id, _, name) => {
|
|
|
|
debug!("Updating title for item with id {}: {:?}", app_id, name);
|
|
|
|
|
|
|
|
if show_names {
|
|
|
|
if let Some(button) = buttons.get(&app_id) {
|
2022-10-10 20:15:24 +01:00
|
|
|
button.button.set_label(&name);
|
2022-09-25 22:49:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LauncherUpdate::Hover(_) => {}
|
|
|
|
};
|
|
|
|
});
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
2023-12-17 23:51:43 +00:00
|
|
|
let rx = context.subscribe();
|
2023-07-16 18:57:00 +01:00
|
|
|
let popup = self
|
2023-12-17 23:51:43 +00:00
|
|
|
.into_popup(context.controller_tx, rx, info)
|
2023-07-16 18:57:00 +01:00
|
|
|
.into_popup_parts(vec![]); // since item buttons are dynamic, they pass their geometry directly
|
|
|
|
|
|
|
|
Ok(ModuleParts {
|
2022-09-25 22:49:00 +01:00
|
|
|
widget: container,
|
|
|
|
popup,
|
|
|
|
})
|
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
fn into_popup(
|
|
|
|
self,
|
2023-12-17 23:51:43 +00:00
|
|
|
controller_tx: mpsc::Sender<Self::ReceiveMessage>,
|
2023-12-31 00:50:03 +00:00
|
|
|
rx: broadcast::Receiver<Self::SendMessage>,
|
2023-01-29 18:38:57 +00:00
|
|
|
_info: &ModuleInfo,
|
2022-09-25 22:49:00 +01:00
|
|
|
) -> Option<gtk::Box> {
|
2022-10-14 23:48:28 +01:00
|
|
|
const MAX_WIDTH: i32 = 250;
|
|
|
|
|
2023-05-06 00:40:06 +01:00
|
|
|
let container = gtk::Box::new(Orientation::Vertical, 0);
|
2022-10-14 23:48:28 +01:00
|
|
|
|
2022-11-05 17:32:29 +00:00
|
|
|
// we need some content to force the container to have a size
|
2022-10-14 23:48:28 +01:00
|
|
|
let placeholder = Button::with_label("PLACEHOLDER");
|
|
|
|
placeholder.set_width_request(MAX_WIDTH);
|
|
|
|
container.add(&placeholder);
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2022-11-05 17:32:01 +00:00
|
|
|
let mut buttons = IndexMap::<String, IndexMap<usize, Button>>::new();
|
2022-08-14 14:30:13 +01:00
|
|
|
|
|
|
|
{
|
2022-09-25 22:49:00 +01:00
|
|
|
let container = container.clone();
|
2023-12-17 23:51:43 +00:00
|
|
|
glib_recv!(rx, event => {
|
2022-09-25 22:49:00 +01:00
|
|
|
match event {
|
|
|
|
LauncherUpdate::AddItem(item) => {
|
|
|
|
let app_id = item.app_id.clone();
|
2022-11-05 17:32:29 +00:00
|
|
|
trace!("Adding item with id '{app_id}' to the popup: {item:?}");
|
2022-09-25 22:49:00 +01:00
|
|
|
|
|
|
|
let window_buttons = item
|
|
|
|
.windows
|
|
|
|
.into_iter()
|
2022-11-05 17:32:01 +00:00
|
|
|
.map(|(_, win)| {
|
2022-09-25 22:49:00 +01:00
|
|
|
let button = Button::builder()
|
2023-03-19 16:20:17 +00:00
|
|
|
.label(clamp(&win.name))
|
2022-09-25 22:49:00 +01:00
|
|
|
.height_request(40)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
{
|
|
|
|
let tx = controller_tx.clone();
|
2023-04-29 22:08:02 +01:00
|
|
|
button.connect_clicked(move |_| {
|
2022-12-11 22:45:52 +00:00
|
|
|
try_send!(tx, ItemEvent::FocusWindow(win.id));
|
2022-09-25 22:49:00 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
(win.id, button)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
buttons.insert(app_id, window_buttons);
|
|
|
|
}
|
|
|
|
LauncherUpdate::AddWindow(app_id, win) => {
|
2022-11-05 17:32:29 +00:00
|
|
|
debug!(
|
|
|
|
"Adding new window to popup for '{app_id}': '{}' ({})",
|
|
|
|
win.name, win.id
|
|
|
|
);
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
if let Some(buttons) = buttons.get_mut(&app_id) {
|
|
|
|
let button = Button::builder()
|
|
|
|
.height_request(40)
|
2023-03-19 16:20:17 +00:00
|
|
|
.label(clamp(&win.name))
|
2022-09-25 22:49:00 +01:00
|
|
|
.build();
|
|
|
|
|
|
|
|
{
|
|
|
|
let tx = controller_tx.clone();
|
2023-06-22 23:21:02 +01:00
|
|
|
button.connect_clicked(move |_button| {
|
2022-12-11 22:45:52 +00:00
|
|
|
try_send!(tx, ItemEvent::FocusWindow(win.id));
|
2022-09-25 22:49:00 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
buttons.insert(win.id, button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LauncherUpdate::RemoveWindow(app_id, win_id) => {
|
2022-11-05 17:32:29 +00:00
|
|
|
debug!("Removing window from popup for '{app_id}': {win_id}");
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
if let Some(buttons) = buttons.get_mut(&app_id) {
|
2024-02-02 20:05:29 +00:00
|
|
|
buttons.shift_remove(&win_id);
|
2022-09-25 22:49:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
LauncherUpdate::Title(app_id, win_id, title) => {
|
2022-11-05 17:32:29 +00:00
|
|
|
debug!(
|
|
|
|
"Updating window title on popup for '{app_id}'/{win_id} to '{title}'"
|
|
|
|
);
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
if let Some(buttons) = buttons.get_mut(&app_id) {
|
|
|
|
if let Some(button) = buttons.get(&win_id) {
|
2022-10-10 20:15:24 +01:00
|
|
|
button.set_label(&title);
|
2022-09-25 22:49:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LauncherUpdate::Hover(app_id) => {
|
|
|
|
// empty current buttons
|
|
|
|
for child in container.children() {
|
|
|
|
container.remove(&child);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add app's buttons
|
|
|
|
if let Some(buttons) = buttons.get(&app_id) {
|
2022-11-05 17:32:01 +00:00
|
|
|
for (_, button) in buttons {
|
2022-10-14 23:49:11 +01:00
|
|
|
button.style_context().add_class("popup-item");
|
2022-09-25 22:49:00 +01:00
|
|
|
container.add(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
container.show_all();
|
2022-10-14 23:48:28 +01:00
|
|
|
container.set_width_request(MAX_WIDTH);
|
2022-09-25 22:49:00 +01:00
|
|
|
}
|
|
|
|
}
|
2022-08-14 14:30:13 +01:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
Some(container)
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
}
|
2022-10-14 23:48:28 +01:00
|
|
|
|
|
|
|
/// Clamps a string at 24 characters.
|
|
|
|
///
|
|
|
|
/// This is a hacky number derived from
|
|
|
|
/// "what fits inside the 250px popup"
|
|
|
|
/// and probably won't hold up with wide fonts.
|
2023-04-29 22:08:02 +01:00
|
|
|
///
|
|
|
|
/// TODO: Migrate this to truncate system
|
|
|
|
///
|
2022-10-14 23:48:28 +01:00
|
|
|
fn clamp(str: &str) -> String {
|
|
|
|
const MAX_CHARS: usize = 24;
|
|
|
|
|
|
|
|
if str.len() > MAX_CHARS {
|
|
|
|
str.chars().take(MAX_CHARS - 3).collect::<String>() + "..."
|
|
|
|
} else {
|
|
|
|
str.to_string()
|
|
|
|
}
|
|
|
|
}
|