Skip to content

Commit

Permalink
refactor: simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JPHutchins committed Nov 10, 2024
1 parent 8bf0ffc commit b4eae05
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/gui/connected_tab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct ConnectedTab {
/// A notice sender to notify the auto attach tab to refresh
pub auto_attach_notice: Cell<Option<nwg::NoticeSender>>,

pub connected_devices: RefCell<Vec<usbipd::UsbDevice>>,
connected_devices: RefCell<Vec<usbipd::UsbDevice>>,

#[nwg_layout(flex_direction: FlexDirection::Row)]
connected_tab_layout: nwg::FlexboxLayout,
Expand Down
55 changes: 25 additions & 30 deletions src/gui/usbipd_gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -26,7 +26,7 @@ pub(super) trait GuiTab {
#[derive(Default, NwgUi)]
pub struct UsbipdGui {
device_notification: Cell<DeviceNotification>,
menu_tray_event_handler: RefCell<Option<nwg::EventHandler>>,
menu_tray_event_handler: Cell<Option<nwg::EventHandler>>,

#[nwg_resource]
embed: nwg::EmbedResource,
Expand Down Expand Up @@ -77,15 +77,15 @@ 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
#[nwg_control(parent: window, text: "File", popup: false)]
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)]
Expand Down Expand Up @@ -137,13 +137,13 @@ impl UsbipdGui {
self.window.set_visible(false);
}

fn show(self: &Rc<UsbipdGui>) {
fn show(&self) {
self.window.set_visible(true);
}

fn show_menu_tray(self: &Rc<UsbipdGui>) {
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();
Expand All @@ -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::<Vec<_>>();

let mut menu_items: Vec<(nwg::MenuItem, Rc<UsbDevice>)> = 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();
Expand All @@ -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));
}
}

Expand All @@ -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 {
Expand All @@ -208,18 +205,16 @@ impl UsbipdGui {
}
}
}
_ => (),
}
}
},
));
},
)));

let (x, y) = nwg::GlobalCursor::position();
menu_tray.popup(x, y);
}

fn new_menu_item(
self: &Rc<UsbipdGui>,
&self,
parent: nwg::ControlHandle,
text: &str,
check: bool,
Expand All @@ -234,7 +229,7 @@ impl UsbipdGui {
}

fn new_menu_separator(
self: &Rc<UsbipdGui>,
&self,
parent: nwg::ControlHandle,
) -> Result<nwg::MenuSeparator, nwg::NwgError> {
let mut sep = nwg::MenuSeparator::default();
Expand All @@ -244,7 +239,7 @@ impl UsbipdGui {
.map(|_| sep)
}

fn refresh(self: &Rc<UsbipdGui>) {
fn refresh(&self) {
self.connected_tab_content.refresh();
self.persisted_tab_content.refresh();
self.auto_attach_tab_content.refresh();
Expand Down
2 changes: 1 addition & 1 deletion src/usbipd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand Down

0 comments on commit b4eae05

Please sign in to comment.