Skip to content

Commit

Permalink
fix: able to insert duplicate keys into collection
Browse files Browse the repository at this point in the history
This replaces the custom `Collection` implementation with `IndexMap` from the crate of the same name.

Fixes #28.
  • Loading branch information
JakeStanger committed Nov 5, 2022
1 parent 5ebc84c commit 3a83bd3
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 185 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ tracing-error = "0.2.0"
tracing-appender = "0.2.2"
strip-ansi-escapes = "0.1.1"
color-eyre = "0.6.2"
futures-util = "0.3.21"
chrono = "0.4.19"
serde = { version = "1.0.141", features = ["derive"] }
serde_json = "1.0.82"
serde_yaml = "0.9.4"
toml = "0.5.9"
libcorn = "0.4.0"
lazy_static = "1.4.0"
async_once = "0.2.6"
indexmap = "1.9.1"
futures-util = "0.3.21"
chrono = "0.4.19"
regex = { version = "1.6.0", default-features = false, features = ["std"] }
stray = { version = "0.1.2" }
dirs = "4.0.0"
Expand Down
161 changes: 0 additions & 161 deletions src/collection.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod bar;
mod bridge_channel;
mod collection;
mod config;
mod icon;
mod logging;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/focused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))?;
}

Expand Down
12 changes: 6 additions & 6 deletions src/modules/launcher/item.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::open_state::OpenState;
use crate::collection::Collection;
use crate::icon::get_icon;
use crate::modules::launcher::{ItemEvent, LauncherUpdate};
use crate::modules::ModuleUpdateEvent;
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;
Expand All @@ -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(),
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions src/modules/launcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
Expand Down Expand Up @@ -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));
Expand All @@ -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) => {
Expand All @@ -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(),
)))?;
Expand Down Expand Up @@ -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!(),
};
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions src/wayland/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::{Env, ToplevelHandler};
use crate::collection::Collection;
use crate::wayland::toplevel::{ToplevelEvent, ToplevelInfo};
use crate::wayland::toplevel_manager::listen_for_toplevels;
use crate::wayland::ToplevelChange;
Expand All @@ -21,7 +20,7 @@ use wayland_protocols::wlr::unstable::foreign_toplevel::v1::client::{
pub struct WaylandClient {
pub outputs: Vec<OutputInfo>,
pub seats: Vec<WlSeat>,
pub toplevels: Arc<RwLock<Collection<usize, (ToplevelInfo, ZwlrForeignToplevelHandleV1)>>>,
pub toplevels: Arc<RwLock<IndexMap<usize, (ToplevelInfo, ZwlrForeignToplevelHandleV1)>>>,
toplevel_tx: broadcast::Sender<ToplevelEvent>,
_toplevel_rx: broadcast::Receiver<ToplevelEvent>,
}
Expand All @@ -35,7 +34,7 @@ impl WaylandClient {

let toplevel_tx2 = toplevel_tx.clone();

let toplevels = Arc::new(RwLock::new(Collection::new()));
let toplevels = Arc::new(RwLock::new(IndexMap::new()));
let toplevels2 = toplevels.clone();

// `queue` is not send so we need to handle everything inside the task
Expand Down

0 comments on commit 3a83bd3

Please sign in to comment.