Skip to content

Commit b4eae05

Browse files
committed
refactor: simplify implementation
1 parent 8bf0ffc commit b4eae05

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

src/gui/connected_tab/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct ConnectedTab {
4242
/// A notice sender to notify the auto attach tab to refresh
4343
pub auto_attach_notice: Cell<Option<nwg::NoticeSender>>,
4444

45-
pub connected_devices: RefCell<Vec<usbipd::UsbDevice>>,
45+
connected_devices: RefCell<Vec<usbipd::UsbDevice>>,
4646

4747
#[nwg_layout(flex_direction: FlexDirection::Row)]
4848
connected_tab_layout: nwg::FlexboxLayout,

src/gui/usbipd_gui.rs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use native_windows_gui as nwg;
99
use super::auto_attach_tab::AutoAttachTab;
1010
use super::connected_tab::ConnectedTab;
1111
use super::persisted_tab::PersistedTab;
12-
use crate::usbipd::UsbDevice;
12+
use crate::usbipd::{list_devices, UsbDevice};
1313
use crate::{
1414
auto_attach::AutoAttacher,
1515
win_utils::{self, DeviceNotification},
@@ -26,7 +26,7 @@ pub(super) trait GuiTab {
2626
#[derive(Default, NwgUi)]
2727
pub struct UsbipdGui {
2828
device_notification: Cell<DeviceNotification>,
29-
menu_tray_event_handler: RefCell<Option<nwg::EventHandler>>,
29+
menu_tray_event_handler: Cell<Option<nwg::EventHandler>>,
3030

3131
#[nwg_resource]
3232
embed: nwg::EmbedResource,
@@ -77,15 +77,15 @@ pub struct UsbipdGui {
7777

7878
// Tray icon
7979
#[nwg_control(icon: Some(&data.app_icon), tip: Some("WSL USB Manager"))]
80-
#[nwg_events(OnContextMenu: [UsbipdGui::show_menu_tray(RC_SELF)], MousePressLeftUp: [UsbipdGui::show(RC_SELF)])]
80+
#[nwg_events(OnContextMenu: [UsbipdGui::show_menu_tray], MousePressLeftUp: [UsbipdGui::show])]
8181
tray: nwg::TrayNotification,
8282

8383
// File menu
8484
#[nwg_control(parent: window, text: "File", popup: false)]
8585
menu_file: nwg::Menu,
8686

8787
#[nwg_control(parent: menu_file, text: "Refresh")]
88-
#[nwg_events(OnMenuItemSelected: [UsbipdGui::refresh(RC_SELF)])]
88+
#[nwg_events(OnMenuItemSelected: [UsbipdGui::refresh])]
8989
menu_file_refresh: nwg::MenuItem,
9090

9191
#[nwg_control(parent: menu_file)]
@@ -137,13 +137,13 @@ impl UsbipdGui {
137137
self.window.set_visible(false);
138138
}
139139

140-
fn show(self: &Rc<UsbipdGui>) {
140+
fn show(&self) {
141141
self.window.set_visible(true);
142142
}
143143

144144
fn show_menu_tray(self: &Rc<UsbipdGui>) {
145-
if let Some(handler) = self.menu_tray_event_handler.borrow().as_ref() {
146-
nwg::unbind_event_handler(handler);
145+
if let Some(handler) = self.menu_tray_event_handler.take() {
146+
nwg::unbind_event_handler(&handler);
147147
}
148148

149149
let mut menu_tray = nwg::Menu::default();
@@ -153,15 +153,12 @@ impl UsbipdGui {
153153
.build(&mut menu_tray)
154154
.unwrap();
155155

156-
let devices = self
157-
.connected_tab_content
158-
.connected_devices
159-
.borrow()
160-
.iter()
161-
.cloned()
156+
let devices = list_devices()
157+
.into_iter()
158+
.filter(|d| d.is_connected())
162159
.collect::<Vec<_>>();
163160

164-
let mut menu_items: Vec<(nwg::MenuItem, Rc<UsbDevice>)> = Vec::with_capacity(devices.len());
161+
let mut menu_items: Vec<(nwg::MenuItem, UsbDevice)> = Vec::with_capacity(devices.len());
165162
for device in devices {
166163
let device_name = device.description.as_deref();
167164
let vid_pid = device.vid_pid();
@@ -176,7 +173,7 @@ impl UsbipdGui {
176173
.new_menu_item(menu_tray.handle, &description, device.is_attached())
177174
.unwrap();
178175

179-
menu_items.push((menu_item, Rc::new(device.clone())));
176+
menu_items.push((menu_item, device));
180177
}
181178
}
182179

@@ -185,15 +182,15 @@ impl UsbipdGui {
185182
self.new_menu_separator(menu_tray.handle).unwrap();
186183
let exit_item = self.new_menu_item(menu_tray.handle, "Exit", false).unwrap();
187184

188-
let rc_self_weak = Rc::downgrade(&self);
189-
*self.menu_tray_event_handler.borrow_mut() = Some(nwg::full_bind_event_handler(
190-
&self.window.handle,
191-
move |evt, _evt_data, handle| {
192-
if let Some(rc_self) = rc_self_weak.upgrade() {
193-
match evt {
194-
nwg::Event::OnMenuItemSelected => {
185+
let rc_self_weak = Rc::downgrade(self);
186+
self.menu_tray_event_handler
187+
.replace(Some(nwg::full_bind_event_handler(
188+
&self.window.handle,
189+
move |evt, _evt_data, handle| {
190+
if let Some(rc_self) = rc_self_weak.upgrade() {
191+
if evt == nwg::Event::OnMenuItemSelected {
195192
if handle == open_item.handle {
196-
UsbipdGui::show(&rc_self);
193+
UsbipdGui::show(rc_self.as_ref());
197194
} else if handle == exit_item.handle {
198195
UsbipdGui::exit();
199196
} else {
@@ -208,18 +205,16 @@ impl UsbipdGui {
208205
}
209206
}
210207
}
211-
_ => (),
212208
}
213-
}
214-
},
215-
));
209+
},
210+
)));
216211

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

221216
fn new_menu_item(
222-
self: &Rc<UsbipdGui>,
217+
&self,
223218
parent: nwg::ControlHandle,
224219
text: &str,
225220
check: bool,
@@ -234,7 +229,7 @@ impl UsbipdGui {
234229
}
235230

236231
fn new_menu_separator(
237-
self: &Rc<UsbipdGui>,
232+
&self,
238233
parent: nwg::ControlHandle,
239234
) -> Result<nwg::MenuSeparator, nwg::NwgError> {
240235
let mut sep = nwg::MenuSeparator::default();
@@ -244,7 +239,7 @@ impl UsbipdGui {
244239
.map(|_| sep)
245240
}
246241

247-
fn refresh(self: &Rc<UsbipdGui>) {
242+
fn refresh(&self) {
248243
self.connected_tab_content.refresh();
249244
self.persisted_tab_content.refresh();
250245
self.auto_attach_tab_content.refresh();

src/usbipd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Display for UsbipState {
4747
}
4848

4949
/// A struct representing a USB device as returned by `usbipd`.
50-
#[derive(Debug, Deserialize, Clone)]
50+
#[derive(Debug, Deserialize)]
5151
pub struct UsbDevice {
5252
#[serde(rename = "BusId")]
5353
pub bus_id: Option<String>,

0 commit comments

Comments
 (0)