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

fix: able to insert duplicate keys into collection

This replaces the custom `Collection` implementation with `IndexMap` from the crate of the same name.

Fixes #28.
This commit is contained in:
Jake Stanger 2022-11-05 17:32:01 +00:00
parent 5ebc84c7b9
commit 3a83bd31ab
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
8 changed files with 24 additions and 185 deletions

View file

@ -47,10 +47,10 @@ impl Module<gtk::Box> for FocusedModule {
.expect("Failed to get read lock on toplevels")
.clone();
toplevels.into_iter().find(|(top, _)| top.active)
toplevels.into_iter().find(|(_, (top, _))| top.active)
});
if let Some((top, _)) = focused {
if let Some((_, (top, _))) = focused {
tx.try_send(ModuleUpdateEvent::Update((top.title.clone(), top.app_id)))?;
}

View file

@ -1,5 +1,4 @@
use super::open_state::OpenState;
use crate::collection::Collection;
use crate::icon::get_icon;
use crate::modules::launcher::{ItemEvent, LauncherUpdate};
use crate::modules::ModuleUpdateEvent;
@ -7,6 +6,7 @@ use crate::popup::Popup;
use crate::wayland::ToplevelInfo;
use gtk::prelude::*;
use gtk::{Button, IconTheme, Image, Orientation};
use indexmap::IndexMap;
use std::rc::Rc;
use std::sync::RwLock;
use tokio::sync::mpsc::Sender;
@ -16,17 +16,17 @@ pub struct Item {
pub app_id: String,
pub favorite: bool,
pub open_state: OpenState,
pub windows: Collection<usize, Window>,
pub windows: IndexMap<usize, Window>,
pub name: String,
}
impl Item {
pub const fn new(app_id: String, open_state: OpenState, favorite: bool) -> Self {
pub fn new(app_id: String, open_state: OpenState, favorite: bool) -> Self {
Self {
app_id,
favorite,
open_state,
windows: Collection::new(),
windows: IndexMap::new(),
name: String::new(),
}
}
@ -78,7 +78,7 @@ impl Item {
&self
.windows
.iter()
.map(|win| &win.open_state)
.map(|(_, win)| &win.open_state)
.collect::<Vec<_>>(),
);
self.open_state = new_state;
@ -91,7 +91,7 @@ impl From<ToplevelInfo> for Item {
let name = toplevel.title.clone();
let app_id = toplevel.app_id.clone();
let mut windows = Collection::new();
let mut windows = IndexMap::new();
windows.insert(toplevel.id, toplevel.into());
Self {

View file

@ -3,7 +3,6 @@ mod open_state;
use self::item::{Item, ItemButton, Window};
use self::open_state::OpenState;
use crate::collection::Collection;
use crate::icon::find_desktop_file;
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
use crate::wayland;
@ -12,6 +11,7 @@ use color_eyre::{Help, Report};
use glib::Continue;
use gtk::prelude::*;
use gtk::{Button, IconTheme, Orientation};
use indexmap::IndexMap;
use serde::Deserialize;
use std::process::{Command, Stdio};
use std::sync::{Arc, Mutex};
@ -90,8 +90,8 @@ impl Module<gtk::Box> for LauncherModule {
Item::new(app_id.to_string(), OpenState::Closed, true),
)
})
.collect::<Collection<_, _>>(),
None => Collection::new(),
.collect::<IndexMap<_, _>>(),
None => IndexMap::new(),
};
let items = Arc::new(Mutex::new(items));
@ -108,7 +108,7 @@ impl Module<gtk::Box> for LauncherModule {
let mut items = items.lock().expect("Failed to get lock on items");
for (window, _) in open_windows.clone() {
for (_, (window, _)) in open_windows.clone() {
let item = items.get_mut(&window.app_id);
match item {
Some(item) => {
@ -121,7 +121,7 @@ impl Module<gtk::Box> for LauncherModule {
}
let items = items.iter();
for item in items {
for (_, item) in items {
tx.try_send(ModuleUpdateEvent::Update(LauncherUpdate::AddItem(
item.clone(),
)))?;
@ -282,7 +282,7 @@ impl Module<gtk::Box> for LauncherModule {
let id = match event {
ItemEvent::FocusItem(app_id) => items
.get(&app_id)
.and_then(|item| item.windows.first().map(|win| win.id)),
.and_then(|item| item.windows.first().map(|(_, win)| win.id)),
ItemEvent::FocusWindow(id) => Some(id),
ItemEvent::OpenItem(_) => unreachable!(),
};
@ -325,7 +325,7 @@ impl Module<gtk::Box> for LauncherModule {
let show_icons = self.show_icons;
let orientation = info.bar_position.get_orientation();
let mut buttons = Collection::<String, ItemButton>::new();
let mut buttons = IndexMap::<String, ItemButton>::new();
let controller_tx2 = context.controller_tx.clone();
context.widget_rx.attach(None, move |event| {
@ -431,7 +431,7 @@ impl Module<gtk::Box> for LauncherModule {
placeholder.set_width_request(MAX_WIDTH);
container.add(&placeholder);
let mut buttons = Collection::<String, Collection<usize, Button>>::new();
let mut buttons = IndexMap::<String, IndexMap<usize, Button>>::new();
{
let container = container.clone();
@ -443,7 +443,7 @@ impl Module<gtk::Box> for LauncherModule {
let window_buttons = item
.windows
.into_iter()
.map(|win| {
.map(|(_, win)| {
let button = Button::builder()
.label(&clamp(&win.name))
.height_request(40)
@ -509,7 +509,7 @@ impl Module<gtk::Box> for LauncherModule {
// add app's buttons
if let Some(buttons) = buttons.get(&app_id) {
for button in buttons {
for (_, button) in buttons {
button.style_context().add_class("popup-item");
container.add(button);
}