From d693b84d71d6ebdff650a698b5c5fb1ac22ab88d Mon Sep 17 00:00:00 2001 From: nickbeth Date: Tue, 18 Feb 2025 14:18:48 +0100 Subject: [PATCH] connected_tab: fix State/Device column autosizing Because ListView only supports autosizing the last column to fill the remaining space, the State and Device column were swapped, since State has a know max width ("Not shared" string). When creating the list view, a dummy row is inserted with max-width content to size the State column properly. --- src/gui/connected_tab/mod.rs | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/gui/connected_tab/mod.rs b/src/gui/connected_tab/mod.rs index acd7765..d92550a 100644 --- a/src/gui/connected_tab/mod.rs +++ b/src/gui/connected_tab/mod.rs @@ -11,6 +11,7 @@ use nwg::stretch::{ geometry::{Rect, Size}, style::{Dimension as D, FlexDirection}, }; +use windows_sys::Win32::UI::Controls::LVSCW_AUTOSIZE; use windows_sys::Win32::UI::Controls::LVSCW_AUTOSIZE_USEHEADER; use windows_sys::Win32::UI::Shell::SIID_SHIELD; @@ -20,7 +21,7 @@ use crate::gui::{ nwg_ext::{BitmapEx, MenuItemEx}, usbipd_gui::GuiTab, }; -use crate::usbipd::{self, UsbDevice}; +use crate::usbipd::{self, UsbDevice, UsbipState}; const PADDING_LEFT: Rect = Rect { start: D::Points(8.0), @@ -137,16 +138,22 @@ impl ConnectedTab { } fn init_list(&self) { - let dv = &self.list_view; - dv.clear(); - dv.insert_column("Bus ID"); - dv.insert_column("Device"); - dv.insert_column("State"); - dv.set_headers_enabled(true); - - dv.set_column_width(0, LVSCW_AUTOSIZE_USEHEADER as isize); - dv.set_column_width(1, 415); - dv.set_column_width(2, LVSCW_AUTOSIZE_USEHEADER as isize); + let list = &self.list_view; + list.clear(); + list.insert_column("Bus ID"); + list.insert_column("State"); + list.insert_column("Device"); + list.set_headers_enabled(true); + + // Insert a dummy row, with max-width content in columns where AUTOSIZE is used + list.insert_items_row(None, &["-", &format!("{} ", UsbipState::None), "Device"]); + + list.set_column_width(0, LVSCW_AUTOSIZE_USEHEADER as isize); + list.set_column_width(1, LVSCW_AUTOSIZE as isize); + list.set_column_width(2, LVSCW_AUTOSIZE_USEHEADER as isize); + + // Clear the dummy row + list.clear(); } /// Clears the device list and reloads it with the currently connected devices. @@ -159,8 +166,8 @@ impl ConnectedTab { None, &[ device.bus_id.as_deref().unwrap_or("-"), - device.description.as_deref().unwrap_or("Unknown device"), &device.state().to_string(), + device.description.as_deref().unwrap_or("Unknown device"), ], ); }