mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-07-03 19:51:03 +02:00
Major module refactor (#19)
* refactor: major module restructuring Modules now implement a "controller", which allows for separation of logic from UI code and enforces a tighter structure around how modules should be written. The introduction of this change required major refactoring or even rewriting of all modules. This also better integrates the popup into modules, making it easier for data to be passed around without fetching the same thing twice The refactor also improves some client code, switching from `ksway` to the much more stable `swayipc-async`. Partial multi-monitor for the tray module has been added. BREAKING CHANGE: The `mpd` module config has changed, moving the icons to their own object.
This commit is contained in:
parent
daafa0943e
commit
720ba7bfb0
26 changed files with 2381 additions and 1846 deletions
|
@ -1,292 +1,299 @@
|
|||
use super::open_state::OpenState;
|
||||
use crate::collection::Collection;
|
||||
use crate::icon::{find_desktop_file, get_icon};
|
||||
use crate::modules::launcher::open_state::OpenState;
|
||||
use crate::modules::launcher::popup::Popup;
|
||||
use crate::modules::launcher::FocusEvent;
|
||||
use crate::sway::SwayNode;
|
||||
use crate::Report;
|
||||
use color_eyre::Help;
|
||||
use crate::icon::get_icon;
|
||||
use crate::modules::launcher::{ItemEvent, LauncherUpdate};
|
||||
use crate::modules::ModuleUpdateEvent;
|
||||
use crate::popup::Popup;
|
||||
use crate::sway::node::{get_node_id, is_node_xwayland};
|
||||
use gtk::prelude::*;
|
||||
use gtk::{Button, IconTheme, Image};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::rc::Rc;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use tokio::spawn;
|
||||
use tokio::sync::mpsc;
|
||||
use tracing::error;
|
||||
use std::sync::RwLock;
|
||||
use swayipc_async::Node;
|
||||
use tokio::sync::mpsc::Sender;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LauncherItem {
|
||||
pub struct Item {
|
||||
pub app_id: String,
|
||||
pub favorite: bool,
|
||||
pub windows: Rc<RwLock<Collection<i32, LauncherWindow>>>,
|
||||
pub state: Arc<RwLock<State>>,
|
||||
pub button: Button,
|
||||
pub open_state: OpenState,
|
||||
pub windows: Collection<i64, Window>,
|
||||
pub name: Option<String>,
|
||||
pub is_xwayland: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LauncherWindow {
|
||||
pub con_id: i32,
|
||||
impl Item {
|
||||
pub const fn new(app_id: String, open_state: OpenState, favorite: bool) -> Self {
|
||||
Self {
|
||||
app_id,
|
||||
favorite,
|
||||
open_state,
|
||||
windows: Collection::new(),
|
||||
name: None,
|
||||
is_xwayland: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Merges the provided node into this launcher item
|
||||
pub fn merge_node(&mut self, node: Node) -> Window {
|
||||
let id = node.id;
|
||||
|
||||
if self.windows.is_empty() {
|
||||
self.name = node.name.clone();
|
||||
}
|
||||
|
||||
self.is_xwayland = self.is_xwayland || is_node_xwayland(&node);
|
||||
|
||||
let window: Window = node.into();
|
||||
self.windows.insert(id, window.clone());
|
||||
|
||||
self.recalculate_open_state();
|
||||
|
||||
window
|
||||
}
|
||||
|
||||
pub fn unmerge_node(&mut self, node: &Node) {
|
||||
self.windows.remove(&node.id);
|
||||
self.recalculate_open_state();
|
||||
}
|
||||
|
||||
pub fn get_name(&self) -> &str {
|
||||
self.name.as_ref().unwrap_or(&self.app_id)
|
||||
}
|
||||
|
||||
pub fn set_window_name(&mut self, window_id: i64, name: Option<String>) {
|
||||
if let Some(window) = self.windows.get_mut(&window_id) {
|
||||
if let OpenState::Open { focused: true, .. } = window.open_state {
|
||||
self.name = name.clone();
|
||||
}
|
||||
|
||||
window.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_unfocused(&mut self) {
|
||||
let focused = self
|
||||
.windows
|
||||
.iter_mut()
|
||||
.find(|window| window.open_state.is_focused());
|
||||
|
||||
if let Some(focused) = focused {
|
||||
focused.open_state = OpenState::Open {
|
||||
focused: false,
|
||||
urgent: focused.open_state.is_urgent(),
|
||||
};
|
||||
|
||||
self.recalculate_open_state();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_window_focused(&mut self, window_id: i64, focused: bool) {
|
||||
if let Some(window) = self.windows.get_mut(&window_id) {
|
||||
window.open_state =
|
||||
OpenState::merge_states(&[&window.open_state, &OpenState::focused(focused)]);
|
||||
|
||||
self.recalculate_open_state();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_window_urgent(&mut self, window_id: i64, urgent: bool) {
|
||||
if let Some(window) = self.windows.get_mut(&window_id) {
|
||||
window.open_state =
|
||||
OpenState::merge_states(&[&window.open_state, &OpenState::urgent(urgent)]);
|
||||
|
||||
self.recalculate_open_state();
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets this item's open state
|
||||
/// to the merged result of its windows' open states
|
||||
fn recalculate_open_state(&mut self) {
|
||||
let new_state = OpenState::merge_states(
|
||||
&self
|
||||
.windows
|
||||
.iter()
|
||||
.map(|win| &win.open_state)
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
self.open_state = new_state;
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Node> for Item {
|
||||
fn from(node: Node) -> Self {
|
||||
let app_id = get_node_id(&node).to_string();
|
||||
let open_state = OpenState::from_node(&node);
|
||||
let name = node.name.clone();
|
||||
|
||||
let is_xwayland = is_node_xwayland(&node);
|
||||
|
||||
let mut windows = Collection::new();
|
||||
windows.insert(node.id, node.into());
|
||||
|
||||
Self {
|
||||
app_id,
|
||||
favorite: false,
|
||||
open_state,
|
||||
windows,
|
||||
name,
|
||||
is_xwayland,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Window {
|
||||
pub id: i64,
|
||||
pub name: Option<String>,
|
||||
pub open_state: OpenState,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct State {
|
||||
pub is_xwayland: bool,
|
||||
pub open_state: OpenState,
|
||||
impl From<Node> for Window {
|
||||
fn from(node: Node) -> Self {
|
||||
let open_state = OpenState::from_node(&node);
|
||||
|
||||
Self {
|
||||
id: node.id,
|
||||
name: node.name,
|
||||
open_state,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ButtonConfig {
|
||||
pub icon_theme: IconTheme,
|
||||
pub struct MenuState {
|
||||
pub num_windows: usize,
|
||||
}
|
||||
|
||||
pub struct ItemButton {
|
||||
pub button: Button,
|
||||
pub persistent: bool,
|
||||
pub show_names: bool,
|
||||
pub show_icons: bool,
|
||||
pub popup: Popup,
|
||||
pub tx: mpsc::Sender<FocusEvent>,
|
||||
pub menu_state: Rc<RwLock<MenuState>>,
|
||||
}
|
||||
|
||||
impl LauncherItem {
|
||||
pub fn new(app_id: String, favorite: bool, config: &ButtonConfig) -> Self {
|
||||
let button = Button::new();
|
||||
button.style_context().add_class("item");
|
||||
impl ItemButton {
|
||||
pub fn new(
|
||||
item: &Item,
|
||||
show_names: bool,
|
||||
show_icons: bool,
|
||||
icon_theme: &IconTheme,
|
||||
tx: &Sender<ModuleUpdateEvent<LauncherUpdate>>,
|
||||
controller_tx: &Sender<ItemEvent>,
|
||||
) -> Self {
|
||||
let mut button = Button::builder();
|
||||
|
||||
let state = State {
|
||||
open_state: OpenState::Closed,
|
||||
is_xwayland: false,
|
||||
};
|
||||
|
||||
let item = Self {
|
||||
app_id,
|
||||
favorite,
|
||||
windows: Rc::new(RwLock::new(Collection::new())),
|
||||
state: Arc::new(RwLock::new(state)),
|
||||
button,
|
||||
};
|
||||
|
||||
item.configure_button(config);
|
||||
item
|
||||
}
|
||||
|
||||
pub fn from_node(node: &SwayNode, config: &ButtonConfig) -> Self {
|
||||
let button = Button::new();
|
||||
button.style_context().add_class("item");
|
||||
|
||||
let windows = Collection::from((
|
||||
node.id,
|
||||
LauncherWindow {
|
||||
con_id: node.id,
|
||||
name: node.name.clone(),
|
||||
open_state: OpenState::from_node(node),
|
||||
},
|
||||
));
|
||||
|
||||
let state = State {
|
||||
open_state: OpenState::from_node(node),
|
||||
is_xwayland: node.is_xwayland(),
|
||||
};
|
||||
|
||||
let item = Self {
|
||||
app_id: node.get_id().to_string(),
|
||||
favorite: false,
|
||||
windows: Rc::new(RwLock::new(windows)),
|
||||
state: Arc::new(RwLock::new(state)),
|
||||
button,
|
||||
};
|
||||
|
||||
item.configure_button(config);
|
||||
item
|
||||
}
|
||||
|
||||
fn configure_button(&self, config: &ButtonConfig) {
|
||||
let button = &self.button;
|
||||
|
||||
let windows = self
|
||||
.windows
|
||||
.read()
|
||||
.expect("Failed to get read lock on windows");
|
||||
|
||||
let name = if windows.len() == 1 {
|
||||
windows
|
||||
.first()
|
||||
.expect("Failed to get first window")
|
||||
.name
|
||||
.as_ref()
|
||||
} else {
|
||||
Some(&self.app_id)
|
||||
};
|
||||
|
||||
if let Some(name) = name {
|
||||
self.set_title(name, config);
|
||||
if show_names {
|
||||
button = button.label(item.get_name());
|
||||
}
|
||||
|
||||
if config.show_icons {
|
||||
let icon = get_icon(&config.icon_theme, &self.app_id, 32);
|
||||
if show_icons {
|
||||
let icon = get_icon(icon_theme, &item.app_id, 32);
|
||||
if icon.is_some() {
|
||||
let image = Image::from_pixbuf(icon.as_ref());
|
||||
button.set_image(Some(&image));
|
||||
button.set_always_show_image(true);
|
||||
button = button.image(&image).always_show_image(true);
|
||||
}
|
||||
}
|
||||
|
||||
let app_id = self.app_id.clone();
|
||||
let state = Arc::clone(&self.state);
|
||||
let tx_click = config.tx.clone();
|
||||
let button = button.build();
|
||||
|
||||
let (focus_tx, mut focus_rx) = mpsc::channel(32);
|
||||
let style_context = button.style_context();
|
||||
style_context.add_class("item");
|
||||
|
||||
button.connect_clicked(move |_| {
|
||||
let state = state.read().expect("Failed to get read lock on state");
|
||||
if state.open_state.is_open() {
|
||||
focus_tx.try_send(()).expect("Failed to send focus event");
|
||||
} else {
|
||||
// attempt to find desktop file and launch
|
||||
match find_desktop_file(&app_id) {
|
||||
Some(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?")
|
||||
);
|
||||
}
|
||||
}
|
||||
None => error!("Could not find desktop file for {}", app_id),
|
||||
}
|
||||
}
|
||||
});
|
||||
if item.favorite {
|
||||
style_context.add_class("favorite");
|
||||
}
|
||||
if item.open_state.is_open() {
|
||||
style_context.add_class("open");
|
||||
}
|
||||
if item.open_state.is_focused() {
|
||||
style_context.add_class("focused");
|
||||
}
|
||||
if item.open_state.is_urgent() {
|
||||
style_context.add_class("urgent");
|
||||
}
|
||||
|
||||
let app_id = self.app_id.clone();
|
||||
let state = Arc::clone(&self.state);
|
||||
|
||||
spawn(async move {
|
||||
while focus_rx.recv().await == Some(()) {
|
||||
let state = state.read().expect("Failed to get read lock on state");
|
||||
if state.is_xwayland {
|
||||
tx_click
|
||||
.try_send(FocusEvent::Class(app_id.clone()))
|
||||
.expect("Failed to send focus event");
|
||||
{
|
||||
let app_id = item.app_id.clone();
|
||||
let tx = controller_tx.clone();
|
||||
button.connect_clicked(move |button| {
|
||||
// lazy check :|
|
||||
let style_context = button.style_context();
|
||||
if style_context.has_class("open") {
|
||||
tx.try_send(ItemEvent::FocusItem(app_id.clone()))
|
||||
.expect("Failed to send item focus event");
|
||||
} else {
|
||||
tx_click
|
||||
.try_send(FocusEvent::AppId(app_id.clone()))
|
||||
.expect("Failed to send focus event");
|
||||
tx.try_send(ItemEvent::OpenItem(app_id.clone()))
|
||||
.expect("Failed to send item open event");
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
let popup = config.popup.clone();
|
||||
let popup2 = config.popup.clone();
|
||||
let windows = Rc::clone(&self.windows);
|
||||
let tx_hover = config.tx.clone();
|
||||
let menu_state = Rc::new(RwLock::new(MenuState {
|
||||
num_windows: item.windows.len(),
|
||||
}));
|
||||
|
||||
button.connect_enter_notify_event(move |button, _| {
|
||||
let windows = windows.read().expect("Failed to get read lock on windows");
|
||||
if windows.len() > 1 {
|
||||
popup.set_windows(windows.as_slice(), &tx_hover);
|
||||
popup.show(button);
|
||||
}
|
||||
{
|
||||
let app_id = item.app_id.clone();
|
||||
let tx = tx.clone();
|
||||
let menu_state = menu_state.clone();
|
||||
|
||||
Inhibit(false)
|
||||
});
|
||||
button.connect_enter_notify_event(move |button, _| {
|
||||
let menu_state = menu_state
|
||||
.read()
|
||||
.expect("Failed to get read lock on item menu state");
|
||||
|
||||
{}
|
||||
if menu_state.num_windows > 1 {
|
||||
tx.try_send(ModuleUpdateEvent::Update(LauncherUpdate::Hover(
|
||||
app_id.clone(),
|
||||
)))
|
||||
.expect("Failed to send item open popup event");
|
||||
|
||||
button.connect_leave_notify_event(move |_, e| {
|
||||
let (_, y) = e.position();
|
||||
// hover boundary
|
||||
if y > 2.0 {
|
||||
popup2.hide();
|
||||
}
|
||||
tx.try_send(ModuleUpdateEvent::OpenPopup(Popup::button_pos(button)))
|
||||
.expect("Failed to send item open popup event");
|
||||
} else {
|
||||
tx.try_send(ModuleUpdateEvent::ClosePopup)
|
||||
.expect("Failed to send item close popup event");
|
||||
}
|
||||
|
||||
Inhibit(false)
|
||||
});
|
||||
|
||||
let style = button.style_context();
|
||||
|
||||
style.add_class("launcher-item");
|
||||
self.update_button_classes(&self.state.read().expect("Failed to get read lock on state"));
|
||||
Inhibit(false)
|
||||
});
|
||||
}
|
||||
|
||||
button.show_all();
|
||||
}
|
||||
|
||||
pub fn set_title(&self, title: &str, config: &ButtonConfig) {
|
||||
if config.show_names {
|
||||
self.button.set_label(title);
|
||||
} else {
|
||||
self.button.set_tooltip_text(Some(title));
|
||||
};
|
||||
}
|
||||
|
||||
/// Updates the classnames on the GTK button
|
||||
/// based on its current state.
|
||||
///
|
||||
/// State must be passed as an arg here rather than
|
||||
/// using `self.state` to avoid a weird `RwLock` issue.
|
||||
pub fn update_button_classes(&self, state: &State) {
|
||||
let style = self.button.style_context();
|
||||
|
||||
if self.favorite {
|
||||
style.add_class("favorite");
|
||||
} else {
|
||||
style.remove_class("favorite");
|
||||
}
|
||||
|
||||
if state.open_state.is_open() {
|
||||
style.add_class("open");
|
||||
} else {
|
||||
style.remove_class("open");
|
||||
}
|
||||
|
||||
if state.open_state.is_focused() {
|
||||
style.add_class("focused");
|
||||
} else {
|
||||
style.remove_class("focused");
|
||||
}
|
||||
|
||||
if state.open_state.is_urgent() {
|
||||
style.add_class("urgent");
|
||||
} else {
|
||||
style.remove_class("urgent");
|
||||
Self {
|
||||
button,
|
||||
persistent: item.favorite,
|
||||
show_names,
|
||||
menu_state,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the open state for a specific window on the item
|
||||
/// and updates the item state based on all its windows.
|
||||
pub fn set_window_open_state(&self, window_id: i32, new_state: OpenState, state: &mut State) {
|
||||
let mut windows = self
|
||||
.windows
|
||||
.write()
|
||||
.expect("Failed to get write lock on windows");
|
||||
pub fn set_open(&self, open: bool) {
|
||||
self.update_class("open", open);
|
||||
|
||||
let window = windows.iter_mut().find(|w| w.con_id == window_id);
|
||||
if let Some(window) = window {
|
||||
window.open_state = new_state;
|
||||
|
||||
state.open_state =
|
||||
OpenState::merge_states(windows.iter().map(|w| &w.open_state).collect());
|
||||
if !open {
|
||||
self.set_focused(false);
|
||||
self.set_urgent(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the open state on the item and all its windows.
|
||||
/// This overrides the existing open states.
|
||||
pub fn set_open_state(&self, new_state: OpenState, state: &mut State) {
|
||||
state.open_state = new_state;
|
||||
let mut windows = self
|
||||
.windows
|
||||
.write()
|
||||
.expect("Failed to get write lock on windows");
|
||||
pub fn set_focused(&self, focused: bool) {
|
||||
self.update_class("focused", focused);
|
||||
}
|
||||
|
||||
windows
|
||||
.iter_mut()
|
||||
.for_each(|window| window.open_state = new_state);
|
||||
pub fn set_urgent(&self, urgent: bool) {
|
||||
self.update_class("urgent", urgent);
|
||||
}
|
||||
|
||||
/// Adds or removes a class to the button based on `toggle`.
|
||||
fn update_class(&self, class: &str, toggle: bool) {
|
||||
let style_context = self.button.style_context();
|
||||
|
||||
if toggle {
|
||||
style_context.add_class(class);
|
||||
} else {
|
||||
style_context.remove_class(class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +1,30 @@
|
|||
mod item;
|
||||
mod open_state;
|
||||
mod popup;
|
||||
|
||||
use crate::collection::Collection;
|
||||
use crate::modules::launcher::item::{ButtonConfig, LauncherItem, LauncherWindow};
|
||||
use crate::icon::find_desktop_file;
|
||||
use crate::modules::launcher::item::{Item, ItemButton, Window};
|
||||
use crate::modules::launcher::open_state::OpenState;
|
||||
use crate::modules::launcher::popup::Popup;
|
||||
use crate::modules::{Module, ModuleInfo};
|
||||
use crate::sway::{get_client, SwayNode};
|
||||
use color_eyre::{Report, Result};
|
||||
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
|
||||
use crate::sway::get_sub_client;
|
||||
use crate::sway::node::{get_node_id, get_open_windows};
|
||||
use crate::{await_sync, get_client};
|
||||
use color_eyre::{Help, Report};
|
||||
use glib::Continue;
|
||||
use gtk::prelude::*;
|
||||
use gtk::{IconTheme, Orientation};
|
||||
use gtk::{Button, IconTheme, Orientation};
|
||||
use serde::Deserialize;
|
||||
use std::rc::Rc;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use swayipc_async::WindowChange;
|
||||
use tokio::spawn;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::task::spawn_blocking;
|
||||
use tracing::debug;
|
||||
use tokio::sync::mpsc::{Receiver, Sender};
|
||||
use tracing::{debug, error, trace};
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct LauncherModule {
|
||||
/// List of app IDs (or classes) to always show regardles of open state,
|
||||
/// List of app IDs (or classes) to always show regardless of open state,
|
||||
/// in the order specified.
|
||||
favorites: Option<Vec<String>>,
|
||||
/// Whether to show application names on the bar.
|
||||
|
@ -34,277 +38,501 @@ pub struct LauncherModule {
|
|||
icon_theme: Option<String>,
|
||||
}
|
||||
|
||||
#[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`.
|
||||
RemoveWindow(String, i64),
|
||||
/// Sets title for `app_id`
|
||||
Title(String, i64, Option<String>),
|
||||
/// Focuses first `app_id`, unfocuses second `app_id` (if present)
|
||||
Focus(String, Option<String>),
|
||||
/// Marks the item with `app_id` as urgent or not urgent
|
||||
Urgent(String, bool),
|
||||
/// Declares the item with `app_id` has been hovered over
|
||||
Hover(String),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum FocusEvent {
|
||||
AppId(String),
|
||||
Class(String),
|
||||
ConId(i32),
|
||||
pub enum ItemEvent {
|
||||
FocusItem(String),
|
||||
FocusWindow(i64),
|
||||
OpenItem(String),
|
||||
}
|
||||
|
||||
type AppId = String;
|
||||
|
||||
struct Launcher {
|
||||
items: Collection<AppId, LauncherItem>,
|
||||
container: gtk::Box,
|
||||
button_config: ButtonConfig,
|
||||
enum ItemOrWindow {
|
||||
Item(Item),
|
||||
Window(Window),
|
||||
}
|
||||
|
||||
impl Launcher {
|
||||
fn new(favorites: Vec<String>, container: gtk::Box, button_config: ButtonConfig) -> Self {
|
||||
let items = favorites
|
||||
.into_iter()
|
||||
.map(|app_id| {
|
||||
(
|
||||
app_id.clone(),
|
||||
LauncherItem::new(app_id, true, &button_config),
|
||||
)
|
||||
})
|
||||
.collect::<Collection<_, _>>();
|
||||
|
||||
for item in &items {
|
||||
container.add(&item.button);
|
||||
}
|
||||
|
||||
Self {
|
||||
items,
|
||||
container,
|
||||
button_config,
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a new window to the launcher.
|
||||
/// This gets added to an existing group
|
||||
/// if an instance of the program is already open.
|
||||
fn add_window(&mut self, node: SwayNode) {
|
||||
let id = node.get_id().to_string();
|
||||
|
||||
debug!("Adding window with ID {}", id);
|
||||
|
||||
if let Some(item) = self.items.get_mut(&id) {
|
||||
let mut state = item
|
||||
.state
|
||||
.write()
|
||||
.expect("Failed to get write lock on state");
|
||||
let new_open_state = OpenState::from_node(&node);
|
||||
state.open_state = OpenState::merge_states(vec![&state.open_state, &new_open_state]);
|
||||
state.is_xwayland = node.is_xwayland();
|
||||
|
||||
item.update_button_classes(&state);
|
||||
|
||||
let mut windows = item
|
||||
.windows
|
||||
.write()
|
||||
.expect("Failed to get write lock on windows");
|
||||
|
||||
windows.insert(
|
||||
node.id,
|
||||
LauncherWindow {
|
||||
con_id: node.id,
|
||||
name: node.name,
|
||||
open_state: new_open_state,
|
||||
},
|
||||
);
|
||||
} else {
|
||||
let item = LauncherItem::from_node(&node, &self.button_config);
|
||||
|
||||
self.container.add(&item.button);
|
||||
self.items.insert(id, item);
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes a window from the launcher.
|
||||
/// This removes it from the group if multiple instances were open.
|
||||
/// The button will remain on the launcher if it is favourited.
|
||||
fn remove_window(&mut self, window: &SwayNode) {
|
||||
let id = window.get_id().to_string();
|
||||
|
||||
debug!("Removing window with ID {}", id);
|
||||
|
||||
let item = self.items.get_mut(&id);
|
||||
|
||||
let remove = if let Some(item) = item {
|
||||
let windows = Rc::clone(&item.windows);
|
||||
let mut windows = windows
|
||||
.write()
|
||||
.expect("Failed to get write lock on windows");
|
||||
|
||||
windows.remove(&window.id);
|
||||
|
||||
if windows.is_empty() {
|
||||
let mut state = item.state.write().expect("Failed to get lock on windows");
|
||||
state.open_state = OpenState::Closed;
|
||||
item.update_button_classes(&state);
|
||||
|
||||
if item.favorite {
|
||||
false
|
||||
} else {
|
||||
self.container.remove(&item.button);
|
||||
true
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
if remove {
|
||||
self.items.remove(&id);
|
||||
}
|
||||
}
|
||||
|
||||
/// Unfocuses the currently focused window
|
||||
/// and focuses the newly focused one.
|
||||
fn set_window_focused(&mut self, node: &SwayNode) {
|
||||
let id = node.get_id().to_string();
|
||||
|
||||
debug!("Setting window with ID {} focused", id);
|
||||
|
||||
let prev_focused = self.items.iter_mut().find(|item| {
|
||||
item.state
|
||||
.read()
|
||||
.expect("Failed to get read lock on state")
|
||||
.open_state
|
||||
.is_focused()
|
||||
});
|
||||
|
||||
if let Some(prev_focused) = prev_focused {
|
||||
let mut state = prev_focused
|
||||
.state
|
||||
.write()
|
||||
.expect("Failed to get write lock on state");
|
||||
|
||||
// if a window from the same item took focus,
|
||||
// we don't need to unfocus the item.
|
||||
if prev_focused.app_id != id {
|
||||
prev_focused.set_open_state(OpenState::open(), &mut state);
|
||||
prev_focused.update_button_classes(&state);
|
||||
}
|
||||
}
|
||||
|
||||
let item = self.items.get_mut(&id);
|
||||
if let Some(item) = item {
|
||||
let mut state = item
|
||||
.state
|
||||
.write()
|
||||
.expect("Failed to get write lock on state");
|
||||
item.set_window_open_state(node.id, OpenState::focused(), &mut state);
|
||||
item.update_button_classes(&state);
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates the window title for the given node.
|
||||
fn set_window_title(&mut self, window: SwayNode) {
|
||||
let id = window.get_id().to_string();
|
||||
let item = self.items.get_mut(&id);
|
||||
|
||||
debug!("Updating title for window with ID {}", id);
|
||||
|
||||
if let (Some(item), Some(name)) = (item, window.name) {
|
||||
let mut windows = item
|
||||
.windows
|
||||
.write()
|
||||
.expect("Failed to get write lock on windows");
|
||||
if windows.len() == 1 {
|
||||
item.set_title(&name, &self.button_config);
|
||||
} else if let Some(window) = windows.get_mut(&window.id) {
|
||||
window.name = Some(name);
|
||||
} else {
|
||||
// This should never happen
|
||||
// But makes more sense to wipe title than keep old one in case of error
|
||||
item.set_title("", &self.button_config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates the window urgency based on the given node.
|
||||
fn set_window_urgent(&mut self, node: &SwayNode) {
|
||||
let id = node.get_id().to_string();
|
||||
let item = self.items.get_mut(&id);
|
||||
|
||||
debug!(
|
||||
"Setting urgency to {} for window with ID {}",
|
||||
node.urgent, id
|
||||
);
|
||||
|
||||
if let Some(item) = item {
|
||||
let mut state = item
|
||||
.state
|
||||
.write()
|
||||
.expect("Failed to get write lock on state");
|
||||
|
||||
item.set_window_open_state(node.id, OpenState::urgent(node.urgent), &mut state);
|
||||
item.update_button_classes(&state);
|
||||
}
|
||||
}
|
||||
enum ItemOrWindowId {
|
||||
Item,
|
||||
Window,
|
||||
}
|
||||
|
||||
impl Module<gtk::Box> for LauncherModule {
|
||||
fn into_widget(self, info: &ModuleInfo) -> Result<gtk::Box> {
|
||||
let icon_theme = IconTheme::new();
|
||||
type SendMessage = LauncherUpdate;
|
||||
type ReceiveMessage = ItemEvent;
|
||||
|
||||
if let Some(theme) = self.icon_theme {
|
||||
icon_theme.set_custom_theme(Some(&theme));
|
||||
}
|
||||
|
||||
let popup = Popup::new(
|
||||
"popup-launcher",
|
||||
info.app,
|
||||
info.monitor,
|
||||
Orientation::Vertical,
|
||||
info.bar_position,
|
||||
);
|
||||
let container = gtk::Box::new(Orientation::Horizontal, 0);
|
||||
|
||||
let (ui_tx, mut ui_rx) = mpsc::channel(32);
|
||||
|
||||
let button_config = ButtonConfig {
|
||||
icon_theme,
|
||||
show_names: self.show_names,
|
||||
show_icons: self.show_icons,
|
||||
popup,
|
||||
tx: ui_tx,
|
||||
fn spawn_controller(
|
||||
&self,
|
||||
_info: &ModuleInfo,
|
||||
tx: Sender<ModuleUpdateEvent<Self::SendMessage>>,
|
||||
mut rx: Receiver<Self::ReceiveMessage>,
|
||||
) -> crate::Result<()> {
|
||||
let items = match &self.favorites {
|
||||
Some(favorites) => favorites
|
||||
.iter()
|
||||
.map(|app_id| {
|
||||
(
|
||||
app_id.to_string(),
|
||||
Item::new(app_id.to_string(), OpenState::Closed, true),
|
||||
)
|
||||
})
|
||||
.collect::<Collection<_, _>>(),
|
||||
None => Collection::new(),
|
||||
};
|
||||
|
||||
let mut launcher = Launcher::new(
|
||||
self.favorites.unwrap_or_default(),
|
||||
container.clone(),
|
||||
button_config,
|
||||
);
|
||||
let items = Arc::new(Mutex::new(items));
|
||||
|
||||
let open_windows = {
|
||||
let sway = get_client();
|
||||
let mut sway = sway.lock().expect("Failed to get lock on Sway IPC client");
|
||||
sway.get_open_windows()
|
||||
}?;
|
||||
let open_windows = await_sync(async {
|
||||
let sway = get_client().await;
|
||||
let mut sway = sway.lock().await;
|
||||
get_open_windows(&mut sway).await
|
||||
})?;
|
||||
|
||||
for window in open_windows {
|
||||
launcher.add_window(window);
|
||||
{
|
||||
let mut items = items.lock().expect("Failed to get lock on items");
|
||||
for window in open_windows {
|
||||
let id = get_node_id(&window).to_string();
|
||||
|
||||
let item = items.get_mut(&id);
|
||||
match item {
|
||||
Some(item) => {
|
||||
item.merge_node(window);
|
||||
}
|
||||
None => {
|
||||
items.insert(id, window.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let items = items.iter();
|
||||
for item in items {
|
||||
tx.try_send(ModuleUpdateEvent::Update(LauncherUpdate::AddItem(
|
||||
item.clone(),
|
||||
)))?;
|
||||
}
|
||||
}
|
||||
|
||||
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
|
||||
let items2 = Arc::clone(&items);
|
||||
spawn(async move {
|
||||
let items = items2;
|
||||
|
||||
spawn_blocking(move || {
|
||||
let srx = {
|
||||
let sway = get_client();
|
||||
let mut sway = sway.lock().expect("Failed to get lock on Sway IPC client");
|
||||
let mut srx = {
|
||||
let sway = get_sub_client();
|
||||
sway.subscribe_window()
|
||||
};
|
||||
|
||||
while let Ok(payload) = srx.recv() {
|
||||
tx.send(payload)
|
||||
.expect("Failed to send window event payload");
|
||||
while let Ok(event) = srx.recv().await {
|
||||
trace!("event: {:?}", event);
|
||||
|
||||
let window = event.container;
|
||||
let id = get_node_id(&window).to_string();
|
||||
|
||||
let send_update =
|
||||
|update: LauncherUpdate| tx.send(ModuleUpdateEvent::Update(update));
|
||||
|
||||
let items = || items.lock().expect("Failed to get lock on items");
|
||||
|
||||
match event.change {
|
||||
WindowChange::New => {
|
||||
let new_item = {
|
||||
let mut items = items();
|
||||
match items.get_mut(&id) {
|
||||
None => {
|
||||
let item: Item = window.into();
|
||||
items.insert(id.clone(), item.clone());
|
||||
|
||||
ItemOrWindow::Item(item)
|
||||
}
|
||||
Some(item) => {
|
||||
let window = item.merge_node(window);
|
||||
ItemOrWindow::Window(window)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
match new_item {
|
||||
ItemOrWindow::Item(item) => {
|
||||
send_update(LauncherUpdate::AddItem(item)).await
|
||||
}
|
||||
ItemOrWindow::Window(window) => {
|
||||
send_update(LauncherUpdate::AddWindow(id, window)).await
|
||||
}
|
||||
}?;
|
||||
}
|
||||
WindowChange::Close => {
|
||||
let remove_item = {
|
||||
let mut items = items();
|
||||
match items.get_mut(&id) {
|
||||
Some(item) => {
|
||||
item.unmerge_node(&window);
|
||||
|
||||
if item.windows.is_empty() {
|
||||
items.remove(&id);
|
||||
Some(ItemOrWindowId::Item)
|
||||
} else {
|
||||
Some(ItemOrWindowId::Window)
|
||||
}
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
};
|
||||
|
||||
match remove_item {
|
||||
Some(ItemOrWindowId::Item) => {
|
||||
send_update(LauncherUpdate::RemoveItem(id)).await?;
|
||||
}
|
||||
Some(ItemOrWindowId::Window) => {
|
||||
send_update(LauncherUpdate::RemoveWindow(id, window.id)).await?;
|
||||
}
|
||||
None => {}
|
||||
};
|
||||
}
|
||||
WindowChange::Focus => {
|
||||
let prev_id = {
|
||||
let mut items = items();
|
||||
|
||||
let prev_focused =
|
||||
items.iter_mut().find(|item| item.open_state.is_focused());
|
||||
if let Some(prev_focused) = prev_focused {
|
||||
prev_focused.set_unfocused();
|
||||
Some(prev_focused.app_id.to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let mut update_title = false;
|
||||
if let Some(item) = items().get_mut(&id) {
|
||||
item.set_window_focused(window.id, true);
|
||||
|
||||
// might be switching focus between windows of same app
|
||||
if item.windows.len() > 1 {
|
||||
item.set_window_name(window.id, window.name.clone());
|
||||
update_title = true;
|
||||
}
|
||||
}
|
||||
|
||||
send_update(LauncherUpdate::Focus(id.clone(), prev_id)).await?;
|
||||
|
||||
if update_title {
|
||||
send_update(LauncherUpdate::Title(id, window.id, window.name)).await?;
|
||||
}
|
||||
}
|
||||
WindowChange::Title => {
|
||||
if let Some(item) = items().get_mut(&id) {
|
||||
item.set_window_name(window.id, window.name.clone());
|
||||
}
|
||||
|
||||
send_update(LauncherUpdate::Title(id, window.id, window.name)).await?;
|
||||
}
|
||||
WindowChange::Urgent => {
|
||||
if let Some(item) = items().get_mut(&id) {
|
||||
item.set_window_urgent(window.id, window.urgent);
|
||||
}
|
||||
|
||||
send_update(LauncherUpdate::Urgent(id, window.urgent)).await?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Ok::<(), mpsc::error::SendError<ModuleUpdateEvent<LauncherUpdate>>>(())
|
||||
});
|
||||
|
||||
// listen to ui events
|
||||
spawn(async move {
|
||||
let sway = get_client().await;
|
||||
|
||||
while let Some(event) = rx.recv().await {
|
||||
trace!("{:?}", event);
|
||||
|
||||
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 {
|
||||
let selector = {
|
||||
let items = items.lock().expect("Failed to get lock on items");
|
||||
|
||||
match event {
|
||||
ItemEvent::FocusItem(app_id) => items.get(&app_id).map(|item| {
|
||||
if item.is_xwayland {
|
||||
format!("[class={}]", app_id)
|
||||
} else {
|
||||
format!("[app_id={}]", app_id)
|
||||
}
|
||||
}),
|
||||
ItemEvent::FocusWindow(con_id) => Some(format!("[con_id={}]", con_id)),
|
||||
ItemEvent::OpenItem(_) => unreachable!(),
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(selector) = selector {
|
||||
let mut sway = sway.lock().await;
|
||||
sway.run_command(format!("{} focus", selector)).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok::<(), swayipc_async::Error>(())
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn into_widget(
|
||||
self,
|
||||
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
||||
_info: &ModuleInfo,
|
||||
) -> crate::Result<ModuleWidget<gtk::Box>> {
|
||||
let icon_theme = IconTheme::new();
|
||||
if let Some(ref theme) = self.icon_theme {
|
||||
icon_theme.set_custom_theme(Some(theme));
|
||||
}
|
||||
|
||||
let container = gtk::Box::new(Orientation::Horizontal, 0);
|
||||
|
||||
{
|
||||
let container = container.clone();
|
||||
|
||||
let show_names = self.show_names;
|
||||
let show_icons = self.show_icons;
|
||||
|
||||
let mut buttons = Collection::<String, ItemButton>::new();
|
||||
|
||||
let controller_tx2 = context.controller_tx.clone();
|
||||
context.widget_rx.attach(None, move |event| {
|
||||
match event {
|
||||
LauncherUpdate::AddItem(item) => {
|
||||
debug!("Adding item with id {}", item.app_id);
|
||||
|
||||
if let Some(button) = buttons.get(&item.app_id) {
|
||||
button.set_open(true);
|
||||
} else {
|
||||
let button = ItemButton::new(
|
||||
&item,
|
||||
show_names,
|
||||
show_icons,
|
||||
&icon_theme,
|
||||
&context.tx,
|
||||
&controller_tx2,
|
||||
);
|
||||
|
||||
container.add(&button.button);
|
||||
buttons.insert(item.app_id, button);
|
||||
}
|
||||
}
|
||||
LauncherUpdate::AddWindow(app_id, _) => {
|
||||
if let Some(button) = buttons.get(&app_id) {
|
||||
let mut menu_state = button
|
||||
.menu_state
|
||||
.write()
|
||||
.expect("Failed to get write lock on item menu state");
|
||||
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);
|
||||
buttons.remove(&app_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
LauncherUpdate::RemoveWindow(app_id, _) => {
|
||||
if let Some(button) = buttons.get(&app_id) {
|
||||
let mut menu_state = button
|
||||
.menu_state
|
||||
.write()
|
||||
.expect("Failed to get write lock on item menu state");
|
||||
menu_state.num_windows -= 1;
|
||||
}
|
||||
}
|
||||
LauncherUpdate::Focus(new, prev) => {
|
||||
debug!(
|
||||
"Changing focus to item with id {} (removing from {:?})",
|
||||
new, prev
|
||||
);
|
||||
|
||||
if let Some(prev) = prev {
|
||||
if let Some(button) = buttons.get(&prev) {
|
||||
button.set_focused(false);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(button) = buttons.get(&new) {
|
||||
button.set_focused(true);
|
||||
}
|
||||
}
|
||||
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) {
|
||||
button.button.set_label(&name.unwrap_or_default());
|
||||
}
|
||||
}
|
||||
}
|
||||
LauncherUpdate::Urgent(app_id, urgent) => {
|
||||
debug!("Updating urgency for item with id {}: {}", app_id, urgent);
|
||||
|
||||
if let Some(button) = buttons.get(&app_id) {
|
||||
button.set_urgent(urgent);
|
||||
}
|
||||
}
|
||||
LauncherUpdate::Hover(_) => {}
|
||||
};
|
||||
|
||||
Continue(true)
|
||||
});
|
||||
}
|
||||
|
||||
let popup = self.into_popup(context.controller_tx, context.popup_rx);
|
||||
Ok(ModuleWidget {
|
||||
widget: container,
|
||||
popup,
|
||||
})
|
||||
}
|
||||
|
||||
fn into_popup(
|
||||
self,
|
||||
controller_tx: Sender<Self::ReceiveMessage>,
|
||||
rx: glib::Receiver<Self::SendMessage>,
|
||||
) -> Option<gtk::Box> {
|
||||
let container = gtk::Box::new(Orientation::Vertical, 0);
|
||||
|
||||
let mut buttons = Collection::<String, Collection<i64, Button>>::new();
|
||||
|
||||
{
|
||||
let container = container.clone();
|
||||
rx.attach(None, move |event| {
|
||||
match event.change.as_str() {
|
||||
"new" => launcher.add_window(event.container),
|
||||
"close" => launcher.remove_window(&event.container),
|
||||
"focus" => launcher.set_window_focused(&event.container),
|
||||
"title" => launcher.set_window_title(event.container),
|
||||
"urgent" => launcher.set_window_urgent(&event.container),
|
||||
match event {
|
||||
LauncherUpdate::AddItem(item) => {
|
||||
let app_id = item.app_id.clone();
|
||||
|
||||
let window_buttons = item
|
||||
.windows
|
||||
.into_iter()
|
||||
.map(|win| {
|
||||
let button = Button::builder()
|
||||
.label(win.name.as_ref().unwrap_or(&String::new()))
|
||||
.height_request(40)
|
||||
.width_request(100)
|
||||
.build();
|
||||
|
||||
{
|
||||
let tx = controller_tx.clone();
|
||||
button.connect_clicked(move |button| {
|
||||
tx.try_send(ItemEvent::FocusWindow(win.id))
|
||||
.expect("Failed to send window click event");
|
||||
|
||||
if let Some(win) = button.window() {
|
||||
win.hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
(win.id, button)
|
||||
})
|
||||
.collect();
|
||||
|
||||
buttons.insert(app_id, window_buttons);
|
||||
}
|
||||
LauncherUpdate::AddWindow(app_id, win) => {
|
||||
if let Some(buttons) = buttons.get_mut(&app_id) {
|
||||
let button = Button::builder()
|
||||
.label(win.name.as_ref().unwrap_or(&String::new()))
|
||||
.height_request(40)
|
||||
.width_request(100)
|
||||
.build();
|
||||
|
||||
{
|
||||
let tx = controller_tx.clone();
|
||||
button.connect_clicked(move |button| {
|
||||
tx.try_send(ItemEvent::FocusWindow(win.id))
|
||||
.expect("Failed to send window click event");
|
||||
|
||||
if let Some(win) = button.window() {
|
||||
win.hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
buttons.insert(win.id, button);
|
||||
}
|
||||
}
|
||||
LauncherUpdate::RemoveWindow(app_id, win_id) => {
|
||||
if let Some(buttons) = buttons.get_mut(&app_id) {
|
||||
buttons.remove(&win_id);
|
||||
}
|
||||
}
|
||||
LauncherUpdate::Title(app_id, win_id, title) => {
|
||||
if let Some(buttons) = buttons.get_mut(&app_id) {
|
||||
if let Some(button) = buttons.get(&win_id) {
|
||||
if let Some(title) = title {
|
||||
button.set_label(&title);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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) {
|
||||
for button in buttons {
|
||||
container.add(button);
|
||||
}
|
||||
|
||||
container.show_all();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
|
@ -312,22 +540,6 @@ impl Module<gtk::Box> for LauncherModule {
|
|||
});
|
||||
}
|
||||
|
||||
spawn(async move {
|
||||
let sway = get_client();
|
||||
|
||||
while let Some(event) = ui_rx.recv().await {
|
||||
let selector = match event {
|
||||
FocusEvent::AppId(app_id) => format!("[app_id={}]", app_id),
|
||||
FocusEvent::Class(class) => format!("[class={}]", class),
|
||||
FocusEvent::ConId(id) => format!("[con_id={}]", id),
|
||||
};
|
||||
let mut sway = sway.lock().expect("Failed to get lock on Sway IPC client");
|
||||
sway.run(format!("{} focus", selector))?;
|
||||
}
|
||||
|
||||
Ok::<(), Report>(())
|
||||
});
|
||||
|
||||
Ok(container)
|
||||
Some(container)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::sway::SwayNode;
|
||||
use swayipc_async::Node;
|
||||
|
||||
/// Open state for a launcher item, or item window.
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Copy)]
|
||||
|
@ -9,25 +9,17 @@ pub enum OpenState {
|
|||
|
||||
impl OpenState {
|
||||
/// Creates from `SwayNode`
|
||||
pub const fn from_node(node: &SwayNode) -> Self {
|
||||
pub const fn from_node(node: &Node) -> Self {
|
||||
Self::Open {
|
||||
focused: node.focused,
|
||||
urgent: node.urgent,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates open without focused/urgent
|
||||
pub const fn open() -> Self {
|
||||
Self::Open {
|
||||
focused: false,
|
||||
urgent: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates open with focused
|
||||
pub const fn focused() -> Self {
|
||||
pub const fn focused(focused: bool) -> Self {
|
||||
Self::Open {
|
||||
focused: true,
|
||||
focused,
|
||||
urgent: false,
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +51,7 @@ impl OpenState {
|
|||
/// This is effectively an OR operation,
|
||||
/// so sets state to open and flags to true if any state is open
|
||||
/// or any instance of the flag is true.
|
||||
pub fn merge_states(states: Vec<&Self>) -> Self {
|
||||
pub fn merge_states(states: &[&Self]) -> Self {
|
||||
states.iter().fold(Self::Closed, |merged, current| {
|
||||
if merged.is_open() || current.is_open() {
|
||||
Self::Open {
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
use crate::modules::launcher::item::LauncherWindow;
|
||||
use crate::modules::launcher::FocusEvent;
|
||||
pub use crate::popup::Popup;
|
||||
use gtk::prelude::*;
|
||||
use gtk::Button;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
impl Popup {
|
||||
pub fn set_windows(&self, windows: &[LauncherWindow], tx: &mpsc::Sender<FocusEvent>) {
|
||||
// clear
|
||||
for child in self.container.children() {
|
||||
self.container.remove(&child);
|
||||
}
|
||||
|
||||
for window in windows {
|
||||
let mut button_builder = Button::builder().height_request(40);
|
||||
|
||||
if let Some(name) = &window.name {
|
||||
button_builder = button_builder.label(name);
|
||||
}
|
||||
|
||||
let button = button_builder.build();
|
||||
|
||||
let con_id = window.con_id;
|
||||
let window = self.window.clone();
|
||||
let tx = tx.clone();
|
||||
button.connect_clicked(move |_| {
|
||||
tx.try_send(FocusEvent::ConId(con_id))
|
||||
.expect("Failed to send focus event");
|
||||
window.hide();
|
||||
});
|
||||
|
||||
self.container.add(&button);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue