From b4eae057bea252feda3080644b028665c54feafc Mon Sep 17 00:00:00 2001 From: "J.P. Hutchins" Date: Sun, 10 Nov 2024 14:03:39 -0800 Subject: [PATCH] refactor: simplify implementation --- src/gui/connected_tab/mod.rs | 2 +- src/gui/usbipd_gui.rs | 55 ++++++++++++++++-------------------- src/usbipd.rs | 2 +- 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/gui/connected_tab/mod.rs b/src/gui/connected_tab/mod.rs index dd4a341..acd7765 100644 --- a/src/gui/connected_tab/mod.rs +++ b/src/gui/connected_tab/mod.rs @@ -42,7 +42,7 @@ pub struct ConnectedTab { /// A notice sender to notify the auto attach tab to refresh pub auto_attach_notice: Cell>, - pub connected_devices: RefCell>, + connected_devices: RefCell>, #[nwg_layout(flex_direction: FlexDirection::Row)] connected_tab_layout: nwg::FlexboxLayout, diff --git a/src/gui/usbipd_gui.rs b/src/gui/usbipd_gui.rs index d06a0c8..fd2fcb3 100644 --- a/src/gui/usbipd_gui.rs +++ b/src/gui/usbipd_gui.rs @@ -9,7 +9,7 @@ use native_windows_gui as nwg; use super::auto_attach_tab::AutoAttachTab; use super::connected_tab::ConnectedTab; use super::persisted_tab::PersistedTab; -use crate::usbipd::UsbDevice; +use crate::usbipd::{list_devices, UsbDevice}; use crate::{ auto_attach::AutoAttacher, win_utils::{self, DeviceNotification}, @@ -26,7 +26,7 @@ pub(super) trait GuiTab { #[derive(Default, NwgUi)] pub struct UsbipdGui { device_notification: Cell, - menu_tray_event_handler: RefCell>, + menu_tray_event_handler: Cell>, #[nwg_resource] embed: nwg::EmbedResource, @@ -77,7 +77,7 @@ pub struct UsbipdGui { // Tray icon #[nwg_control(icon: Some(&data.app_icon), tip: Some("WSL USB Manager"))] - #[nwg_events(OnContextMenu: [UsbipdGui::show_menu_tray(RC_SELF)], MousePressLeftUp: [UsbipdGui::show(RC_SELF)])] + #[nwg_events(OnContextMenu: [UsbipdGui::show_menu_tray], MousePressLeftUp: [UsbipdGui::show])] tray: nwg::TrayNotification, // File menu @@ -85,7 +85,7 @@ pub struct UsbipdGui { menu_file: nwg::Menu, #[nwg_control(parent: menu_file, text: "Refresh")] - #[nwg_events(OnMenuItemSelected: [UsbipdGui::refresh(RC_SELF)])] + #[nwg_events(OnMenuItemSelected: [UsbipdGui::refresh])] menu_file_refresh: nwg::MenuItem, #[nwg_control(parent: menu_file)] @@ -137,13 +137,13 @@ impl UsbipdGui { self.window.set_visible(false); } - fn show(self: &Rc) { + fn show(&self) { self.window.set_visible(true); } fn show_menu_tray(self: &Rc) { - if let Some(handler) = self.menu_tray_event_handler.borrow().as_ref() { - nwg::unbind_event_handler(handler); + if let Some(handler) = self.menu_tray_event_handler.take() { + nwg::unbind_event_handler(&handler); } let mut menu_tray = nwg::Menu::default(); @@ -153,15 +153,12 @@ impl UsbipdGui { .build(&mut menu_tray) .unwrap(); - let devices = self - .connected_tab_content - .connected_devices - .borrow() - .iter() - .cloned() + let devices = list_devices() + .into_iter() + .filter(|d| d.is_connected()) .collect::>(); - let mut menu_items: Vec<(nwg::MenuItem, Rc)> = Vec::with_capacity(devices.len()); + let mut menu_items: Vec<(nwg::MenuItem, UsbDevice)> = Vec::with_capacity(devices.len()); for device in devices { let device_name = device.description.as_deref(); let vid_pid = device.vid_pid(); @@ -176,7 +173,7 @@ impl UsbipdGui { .new_menu_item(menu_tray.handle, &description, device.is_attached()) .unwrap(); - menu_items.push((menu_item, Rc::new(device.clone()))); + menu_items.push((menu_item, device)); } } @@ -185,15 +182,15 @@ impl UsbipdGui { self.new_menu_separator(menu_tray.handle).unwrap(); let exit_item = self.new_menu_item(menu_tray.handle, "Exit", false).unwrap(); - let rc_self_weak = Rc::downgrade(&self); - *self.menu_tray_event_handler.borrow_mut() = Some(nwg::full_bind_event_handler( - &self.window.handle, - move |evt, _evt_data, handle| { - if let Some(rc_self) = rc_self_weak.upgrade() { - match evt { - nwg::Event::OnMenuItemSelected => { + let rc_self_weak = Rc::downgrade(self); + self.menu_tray_event_handler + .replace(Some(nwg::full_bind_event_handler( + &self.window.handle, + move |evt, _evt_data, handle| { + if let Some(rc_self) = rc_self_weak.upgrade() { + if evt == nwg::Event::OnMenuItemSelected { if handle == open_item.handle { - UsbipdGui::show(&rc_self); + UsbipdGui::show(rc_self.as_ref()); } else if handle == exit_item.handle { UsbipdGui::exit(); } else { @@ -208,18 +205,16 @@ impl UsbipdGui { } } } - _ => (), } - } - }, - )); + }, + ))); let (x, y) = nwg::GlobalCursor::position(); menu_tray.popup(x, y); } fn new_menu_item( - self: &Rc, + &self, parent: nwg::ControlHandle, text: &str, check: bool, @@ -234,7 +229,7 @@ impl UsbipdGui { } fn new_menu_separator( - self: &Rc, + &self, parent: nwg::ControlHandle, ) -> Result { let mut sep = nwg::MenuSeparator::default(); @@ -244,7 +239,7 @@ impl UsbipdGui { .map(|_| sep) } - fn refresh(self: &Rc) { + fn refresh(&self) { self.connected_tab_content.refresh(); self.persisted_tab_content.refresh(); self.auto_attach_tab_content.refresh(); diff --git a/src/usbipd.rs b/src/usbipd.rs index 3d95668..3e30990 100644 --- a/src/usbipd.rs +++ b/src/usbipd.rs @@ -47,7 +47,7 @@ impl Display for UsbipState { } /// A struct representing a USB device as returned by `usbipd`. -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Deserialize)] pub struct UsbDevice { #[serde(rename = "BusId")] pub bus_id: Option,