1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-04 20:21:02 +02:00

refactor: major client code changes

This does away with `lazy_static` singletons for all the clients, instead putting them all inside a `Clients` struct on the `Ironbar` struct.

Client code has been refactored in places to accommodate this, and module code has been updated to get the clients the new way.

The Wayland client has been re-written from the ground up to remove a lot of the needless complications, provide a nicer interface and reduce some duplicate data.

The MPD music client has been overhauled to use the `mpd_utils` crate, which simplifies the code within Ironbar and should offer more robustness and better recovery when connection is lost to the server.

The launcher module in particular has been affected by the refactor.
This commit is contained in:
Jake Stanger 2024-01-07 23:50:10 +00:00
parent 57b57ed002
commit c702f6fffa
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
33 changed files with 1081 additions and 1063 deletions

View file

@ -1,12 +1,11 @@
use super::open_state::OpenState;
use crate::clients::wayland::ToplevelHandle;
use crate::clients::wayland::ToplevelInfo;
use crate::config::BarPosition;
use crate::gtk_helpers::IronbarGtkExt;
use crate::image::ImageProvider;
use crate::modules::launcher::{ItemEvent, LauncherUpdate};
use crate::modules::ModuleUpdateEvent;
use crate::{read_lock, try_send};
use color_eyre::{Report, Result};
use glib::Propagation;
use gtk::prelude::*;
use gtk::{Button, IconTheme};
@ -15,7 +14,6 @@ use std::rc::Rc;
use std::sync::RwLock;
use tokio::sync::mpsc::Sender;
use tracing::error;
use wayland_client::protocol::wl_seat::WlSeat;
#[derive(Debug, Clone)]
pub struct Item {
@ -38,30 +36,24 @@ impl Item {
}
/// Merges the provided node into this launcher item
pub fn merge_toplevel(&mut self, handle: ToplevelHandle) -> Result<Window> {
let info = handle
.info()
.ok_or_else(|| Report::msg("Toplevel is missing associated info"))?;
pub fn merge_toplevel(&mut self, info: ToplevelInfo) -> Window {
let id = info.id;
if self.windows.is_empty() {
self.name = info.title;
self.name = info.title.clone();
}
let window = Window::try_from(handle)?;
let window = Window::from(info);
self.windows.insert(id, window.clone());
self.recalculate_open_state();
Ok(window)
window
}
pub fn unmerge_toplevel(&mut self, handle: &ToplevelHandle) {
if let Some(info) = handle.info() {
self.windows.remove(&info.id);
self.recalculate_open_state();
}
pub fn unmerge_toplevel(&mut self, info: &ToplevelInfo) {
self.windows.remove(&info.id);
self.recalculate_open_state();
}
pub fn set_window_name(&mut self, window_id: usize, name: String) {
@ -97,29 +89,24 @@ impl Item {
}
}
impl TryFrom<ToplevelHandle> for Item {
type Error = Report;
fn try_from(handle: ToplevelHandle) -> std::result::Result<Self, Self::Error> {
let info = handle
.info()
.ok_or_else(|| Report::msg("Toplevel is missing associated info"))?;
impl From<ToplevelInfo> for Item {
fn from(info: ToplevelInfo) -> Self {
let id = info.id;
let name = info.title.clone();
let app_id = info.app_id.clone();
let open_state = OpenState::from(&info);
let mut windows = IndexMap::new();
let window = Window::try_from(handle)?;
windows.insert(info.id, window);
let window = Window::from(info);
windows.insert(id, window);
Ok(Self {
Self {
app_id,
favorite: false,
open_state,
windows,
name,
})
}
}
}
@ -128,30 +115,17 @@ pub struct Window {
pub id: usize,
pub name: String,
pub open_state: OpenState,
handle: ToplevelHandle,
}
impl TryFrom<ToplevelHandle> for Window {
type Error = Report;
fn try_from(handle: ToplevelHandle) -> Result<Self, Self::Error> {
let info = handle
.info()
.ok_or_else(|| Report::msg("Toplevel is missing associated info"))?;
impl From<ToplevelInfo> for Window {
fn from(info: ToplevelInfo) -> Self {
let open_state = OpenState::from(&info);
Ok(Self {
Self {
id: info.id,
name: info.title,
open_state,
handle,
})
}
}
impl Window {
pub fn focus(&self, seat: &WlSeat) {
self.handle.focus(seat);
}
}
}

View file

@ -1,15 +1,12 @@
mod item;
mod open_state;
use self::item::{Item, ItemButton, Window};
use self::item::{AppearanceOptions, Item, ItemButton, Window};
use self::open_state::OpenState;
use super::{Module, ModuleInfo, ModuleParts, ModulePopup, ModuleUpdateEvent, WidgetContext};
use crate::clients::wayland::{self, ToplevelEvent};
use crate::config::CommonConfig;
use crate::desktop_file::find_desktop_file;
use crate::modules::launcher::item::AppearanceOptions;
use crate::modules::{
Module, ModuleInfo, ModuleParts, ModulePopup, ModuleUpdateEvent, WidgetContext,
};
use crate::{arc_mut, glib_recv, lock, send_async, spawn, try_send, write_lock};
use color_eyre::{Help, Report};
use gtk::prelude::*;
@ -112,27 +109,26 @@ impl Module<gtk::Box> for LauncherModule {
let items2 = Arc::clone(&items);
let tx = context.tx.clone();
let tx2 = context.tx.clone();
let wl = context.client::<wayland::Client>();
spawn(async move {
let items = items2;
let tx = tx2;
let (mut wlrx, handles) = {
let wl = wayland::get_client();
let wl = lock!(wl);
wl.subscribe_toplevels()
};
for handle in handles.values() {
let Some(info) = handle.info() else { continue };
let mut wlrx = wl.subscribe_toplevels();
let handles = wl.toplevel_info_all();
for info in handles {
let mut items = lock!(items);
let item = items.get_mut(&info.app_id);
match item {
Some(item) => {
item.merge_toplevel(handle.clone())?;
item.merge_toplevel(info.clone());
}
None => {
items.insert(info.app_id.clone(), Item::try_from(handle.clone())?);
items.insert(info.app_id.clone(), Item::from(info.clone()));
}
}
}
@ -154,22 +150,22 @@ impl Module<gtk::Box> for LauncherModule {
trace!("event: {:?}", event);
match event {
ToplevelEvent::New(handle) => {
let Some(info) = handle.info() else { continue };
ToplevelEvent::New(info) => {
let app_id = info.app_id.clone();
let new_item = {
let mut items = lock!(items);
let item = items.get_mut(&info.app_id);
match item {
None => {
let item: Item = handle.try_into()?;
let item: Item = info.into();
items.insert(info.app_id.clone(), item.clone());
items.insert(app_id.clone(), item.clone());
ItemOrWindow::Item(item)
}
Some(item) => {
let window = item.merge_toplevel(handle)?;
let window = item.merge_toplevel(info);
ItemOrWindow::Window(window)
}
}
@ -180,14 +176,11 @@ impl Module<gtk::Box> for LauncherModule {
send_update(LauncherUpdate::AddItem(item)).await
}
ItemOrWindow::Window(window) => {
send_update(LauncherUpdate::AddWindow(info.app_id.clone(), window))
.await
send_update(LauncherUpdate::AddWindow(app_id, window)).await
}
}?;
}
ToplevelEvent::Update(handle) => {
let Some(info) = handle.info() else { continue };
ToplevelEvent::Update(info) => {
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());
@ -202,15 +195,13 @@ impl Module<gtk::Box> for LauncherModule {
))
.await?;
}
ToplevelEvent::Remove(handle) => {
let Some(info) = handle.info() else { continue };
ToplevelEvent::Remove(info) => {
let remove_item = {
let mut items = lock!(items);
let item = items.get_mut(&info.app_id);
match item {
Some(item) => {
item.unmerge_toplevel(&handle);
item.unmerge_toplevel(&info);
if item.windows.is_empty() {
items.remove(&info.app_id);
@ -245,6 +236,7 @@ impl Module<gtk::Box> for LauncherModule {
});
// listen to ui events
let wl = context.client::<wayland::Client>();
spawn(async move {
while let Some(event) = rx.recv().await {
if let ItemEvent::OpenItem(app_id) = event {
@ -272,37 +264,29 @@ impl Module<gtk::Box> for LauncherModule {
} else {
send_async!(tx, ModuleUpdateEvent::ClosePopup);
let wl = wayland::get_client();
let items = lock!(items);
let id = match event {
ItemEvent::FocusItem(app_id) => 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)
}),
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)
})
}
ItemEvent::FocusWindow(id) => Some(id),
ItemEvent::OpenItem(_) => unreachable!(),
};
if let Some(id) = id {
if let Some(window) =
items.iter().find_map(|(_, item)| item.windows.get(&id))
if let Some(window) = lock!(items)
.iter()
.find_map(|(_, item)| item.windows.get(&id))
{
debug!("Focusing window {id}: {}", window.name);
let seat = lock!(wl)
.get_seats()
.pop()
.expect("Failed to get Wayland seat");
window.focus(&seat);
wl.toplevel_focus(window.id);
}
}
// roundtrip to immediately send activate event
lock!(wl).roundtrip();
}
}
});