Skip to content

Commit

Permalink
Merge pull request #759 from andrewdavidmackenzie/694
Browse files Browse the repository at this point in the history
Add local device to discovery menu
  • Loading branch information
andrewdavidmackenzie authored Dec 18, 2024
2 parents 5b910e4 + 3bb4fda commit 920082e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub type SerialNumber = String;
/// What method was used to discover a device? Currently, we support Iroh and USB
#[derive(Debug, Clone)]
pub enum DiscoveryMethod {
Local,
#[cfg(feature = "usb")]
USBRaw,
#[cfg(feature = "iroh")]
Expand All @@ -51,6 +52,7 @@ pub enum DiscoveryMethod {
impl Display for DiscoveryMethod {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
DiscoveryMethod::Local => f.write_str("Local"),
#[cfg(feature = "usb")]
USBRaw => f.write_str("USB"),
#[cfg(feature = "iroh")]
Expand Down
27 changes: 25 additions & 2 deletions src/piggui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use iced::{window, Element, Length, Padding, Pixels, Settings, Subscription, Tas
#[cfg(feature = "discovery")]
use std::collections::HashMap;

#[cfg(feature = "discovery")]
use crate::discovery::DiscoveryMethod::Local;
#[cfg(any(feature = "iroh", feature = "tcp"))]
use crate::views::connect_dialog::{
ConnectDialog, ConnectDialogMessage, ConnectDialogMessage::HideConnectDialog,
Expand Down Expand Up @@ -96,7 +98,7 @@ pub struct Piggui {
#[cfg(any(feature = "iroh", feature = "tcp"))]
connect_dialog: ConnectDialog,
#[cfg(feature = "discovery")]
discovered_devices: HashMap<String, DiscoveredDevice>, // TODO handle multiple discovery methods per serial number
discovered_devices: HashMap<String, DiscoveredDevice>,
#[cfg(feature = "usb")]
ssid_dialog: SsidDialog,
}
Expand Down Expand Up @@ -156,6 +158,27 @@ impl Piggui {
.map(|s| s.to_string());
#[cfg(target_arch = "wasm32")]
let config_filename = None;
#[cfg(feature = "discovery")]
let mut discovered_devices = HashMap::new();
#[cfg(feature = "discovery")]
let local_hardware = hw::driver::get();
#[cfg(feature = "discovery")]
let serial = local_hardware.description().unwrap().details.serial;
#[cfg(feature = "discovery")]
let mut hardware_connections = HashMap::new();
#[cfg(feature = "discovery")]
hardware_connections.insert("Local".to_string(), HardwareConnection::Local);
#[cfg(feature = "discovery")]
discovered_devices.insert(
serial,
DiscoveredDevice {
discovery_method: Local,
hardware_details: local_hardware.description().unwrap().details,
ssid_spec: None,
hardware_connections,
},
);

(
Self {
config_filename: config_filename.clone(),
Expand All @@ -167,7 +190,7 @@ impl Piggui {
#[cfg(any(feature = "iroh", feature = "tcp"))]
connect_dialog: ConnectDialog::new(),
#[cfg(feature = "discovery")]
discovered_devices: HashMap::new(),
discovered_devices,
#[cfg(feature = "usb")]
ssid_dialog: SsidDialog::new(),
},
Expand Down
18 changes: 0 additions & 18 deletions src/views/hardware_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,6 @@ pub fn view<'a>(
}),
);

#[cfg(not(target_arch = "wasm32"))]
let connect_local: Item<'a, Message, _, _> = Item::new(
button("Connect to local")
.width(Length::Fill)
.on_press(Message::ConnectRequest(Local))
.style(|_, status| {
if status == Hovered {
MENU_BUTTON_HOVER_STYLE
} else {
MENU_BUTTON_STYLE
}
}),
);

if let Some(hardware_description) = hardware_view.hardware_description.as_ref() {
let show_details = Item::new(
button("Display Device Details...")
Expand All @@ -275,8 +261,6 @@ pub fn view<'a>(
NoConnection => {
#[cfg(any(feature = "iroh", feature = "tcp"))]
menu_items.push(connect);
#[cfg(not(target_arch = "wasm32"))]
menu_items.push(connect_local);
}
#[cfg(not(target_arch = "wasm32"))]
Local => {
Expand All @@ -288,8 +272,6 @@ pub fn view<'a>(
_ => {
menu_items.push(connect);
menu_items.push(disconnect);
#[cfg(not(target_arch = "wasm32"))]
menu_items.push(connect_local);
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/views/hardware_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ pub enum HardwareViewMessage {
/// A type of connection to a piece of hardware
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub enum HardwareConnection {
#[cfg_attr(target_arch = "wasm32", default)]
NoConnection,
#[cfg(not(target_arch = "wasm32"))]
#[cfg_attr(not(target_arch = "wasm32"), default)]
#[default]
Local,
#[cfg(feature = "iroh")]
Iroh(NodeId, Option<RelayUrl>),
Expand Down

0 comments on commit 920082e

Please sign in to comment.