Skip to content

Commit

Permalink
Merge pull request #816 from andrewdavidmackenzie/menus_improvements
Browse files Browse the repository at this point in the history
improve menu code
  • Loading branch information
andrewdavidmackenzie authored Jan 21, 2025
2 parents 6fc7a70 + 5b27f4e commit 575116b
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 340 deletions.
11 changes: 2 additions & 9 deletions src/views/about.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::views::info_row::{MENU_BAR_BUTTON_HOVER_STYLE, MENU_BAR_BUTTON_STYLE};
use crate::views::info_row::menu_bar_button;
use crate::{InfoDialogMessage, Message};
use iced::widget::button;
use iced::widget::button::Status::Hovered;
use iced::{Renderer, Theme};
use iced_aw::menu::Item;

Expand Down Expand Up @@ -35,12 +34,6 @@ pub fn about_button<'a>() -> Item<'a, Message, Theme, Renderer> {
.on_press(Message::Modal(InfoDialogMessage::AboutDialog))
.clip(true)
.height(iced::Length::Shrink)
.style(move |_theme, status| {
if status == Hovered {
MENU_BAR_BUTTON_HOVER_STYLE
} else {
MENU_BAR_BUTTON_STYLE
}
}),
.style(menu_bar_button),
)
}
38 changes: 5 additions & 33 deletions src/views/config_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ use iced::widget::Button;

use crate::views::hardware_view::HardwareConnection;
use crate::views::hardware_view::HardwareConnection::NoConnection;
use crate::views::info_row::{
MENU_BAR_BUTTON_HIGHLIGHT_STYLE, MENU_BAR_BUTTON_HOVER_STYLE, MENU_BAR_BUTTON_STYLE,
MENU_BUTTON_HOVER_STYLE, MENU_BUTTON_STYLE,
};
use iced::widget::button::Status::Hovered;
use crate::views::info_row::{menu_bar_button, menu_bar_highlight_button, menu_button};
use iced::{Renderer, Theme};
use iced_aw::menu::{Item, Menu};

Expand All @@ -20,13 +16,7 @@ pub fn view<'a>(

let mut load_from = Button::new("Load config from...")
.width(180)
.style(|_, status| {
if status == Hovered {
MENU_BUTTON_HOVER_STYLE
} else {
MENU_BUTTON_STYLE
}
});
.style(menu_button);

if hardware_connection != &NoConnection {
load_from = load_from.on_press(Message::Load);
Expand All @@ -36,13 +26,7 @@ pub fn view<'a>(
if unsaved_changes {
let mut save_as = Button::new("Save config as...")
.width(180)
.style(|_, status| {
if status == Hovered {
MENU_BUTTON_HOVER_STYLE
} else {
MENU_BUTTON_STYLE
}
});
.style(menu_button);

if hardware_connection != &NoConnection {
save_as = save_as.on_press(Message::Save);
Expand All @@ -52,20 +36,8 @@ pub fn view<'a>(
}

let mut button = match unsaved_changes {
true => Button::new("config: unsaved changes").style(|_, status| {
if status == Hovered {
MENU_BAR_BUTTON_HOVER_STYLE
} else {
MENU_BAR_BUTTON_HIGHLIGHT_STYLE
}
}),
false => Button::new("config").style(move |_theme, status| {
if status == Hovered {
MENU_BAR_BUTTON_HOVER_STYLE
} else {
MENU_BAR_BUTTON_STYLE
}
}),
true => Button::new("config: unsaved changes").style(menu_bar_highlight_button),
false => Button::new("config").style(menu_bar_button),
};
button = button.on_press(Message::MenuBarButtonClicked); // Needed for highlighting

Expand Down
47 changes: 7 additions & 40 deletions src/views/connect_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ const IROH_INFO_TEXT: &str = "To connect to a Pi using iroh-net, ensure piglet i
const TCP_INFO_TEXT: &str = "To connect to a Pi/Pi Pico using TCP, ensure it is reachable over the network. Retrieve the device's IP address and the port number from it (see piglet or porky docs) and enter below.";

use crate::views::dialog_styles::{
ACTIVE_TAB_BUTTON_STYLE, CONNECTION_ERROR_DISPLAY, INACTIVE_TAB_BUTTON_HOVER_STYLE,
INACTIVE_TAB_BUTTON_STYLE, INFO_TEXT_STYLE, MODAL_CANCEL_BUTTON_HOVER_STYLE,
MODAL_CANCEL_BUTTON_STYLE, MODAL_CONNECT_BUTTON_HOVER_STYLE, MODAL_CONNECT_BUTTON_STYLE,
cancel_button, connect_button, ACTIVE_TAB_BUTTON_STYLE, CONNECTION_ERROR_DISPLAY,
INACTIVE_TAB_BUTTON_HOVER_STYLE, INACTIVE_TAB_BUTTON_STYLE, INFO_TEXT_STYLE,
MODAL_CONTAINER_STYLE, TAB_BAR_STYLE, TEXT_BOX_CONTAINER_STYLE,
};
use iced::widget::button::Status::Hovered;
Expand Down Expand Up @@ -380,15 +379,7 @@ impl ConnectDialog {
.cycle_duration(Duration::from_secs_f32(2.0)),
)
.push(horizontal_space())
.push(
Button::new(Text::new("Connect")).style(move |_theme, status| {
if status == Hovered {
MODAL_CONNECT_BUTTON_HOVER_STYLE
} else {
MODAL_CONNECT_BUTTON_STYLE
}
}),
)
.push(Button::new(Text::new("Connect")).style(connect_button))
.align_y(iced::Alignment::Center)
}

Expand All @@ -398,13 +389,7 @@ impl ConnectDialog {
.push(
Button::new(Text::new("Cancel"))
.on_press(Message::ConnectDialog(HideConnectDialog))
.style(|_, status| {
if status == Hovered {
MODAL_CANCEL_BUTTON_HOVER_STYLE
} else {
MODAL_CANCEL_BUTTON_STYLE
}
}),
.style(cancel_button),
)
.push(horizontal_space())
.push(
Expand All @@ -413,13 +398,7 @@ impl ConnectDialog {
self.nodeid.clone(),
self.relay_url.clone(),
)))
.style(move |_theme, status| {
if status == Hovered {
MODAL_CONNECT_BUTTON_HOVER_STYLE
} else {
MODAL_CONNECT_BUTTON_STYLE
}
}),
.style(connect_button),
)
.align_y(iced::Alignment::Center)
}
Expand All @@ -430,13 +409,7 @@ impl ConnectDialog {
.push(
Button::new(Text::new("Cancel"))
.on_press(Message::ConnectDialog(HideConnectDialog))
.style(|_, status| {
if status == Hovered {
MODAL_CANCEL_BUTTON_HOVER_STYLE
} else {
MODAL_CANCEL_BUTTON_STYLE
}
}),
.style(cancel_button),
)
.push(horizontal_space())
.push(
Expand All @@ -445,13 +418,7 @@ impl ConnectDialog {
self.ip_address.clone(),
self.port_number.clone(),
)))
.style(move |_theme, status| {
if status == Hovered {
MODAL_CONNECT_BUTTON_HOVER_STYLE
} else {
MODAL_CONNECT_BUTTON_STYLE
}
}),
.style(connect_button),
)
.align_y(iced::Alignment::Center)
}
Expand Down
37 changes: 5 additions & 32 deletions src/views/connection_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
use crate::views::connect_dialog::ConnectDialogMessage;
use crate::views::hardware_view::HardwareView;
use crate::views::info_dialog::InfoDialogMessage::HardwareDetailsModal;
use crate::views::info_row::{
MENU_BAR_BUTTON_HOVER_STYLE, MENU_BAR_BUTTON_STYLE, MENU_BUTTON_HOVER_STYLE, MENU_BUTTON_STYLE,
};
use crate::views::info_row::{menu_bar_button, menu_button};
use crate::HardwareConnection::*;
use crate::Message;
use iced::widget::button::Status::Hovered;
use iced::widget::{button, text};
use iced::{Length, Renderer, Theme};
use iced_aw::menu::{Item, Menu};
Expand All @@ -22,13 +19,7 @@ pub fn view<'a>(hardware_view: &'a HardwareView) -> Item<'a, Message, Theme, Ren
button("Disconnect")
.width(Length::Fill)
.on_press(Message::Disconnect)
.style(|_, status| {
if status == Hovered {
MENU_BUTTON_HOVER_STYLE
} else {
MENU_BUTTON_STYLE
}
}),
.style(menu_button),
);

#[cfg(any(feature = "iroh", feature = "tcp"))]
Expand All @@ -38,13 +29,7 @@ pub fn view<'a>(hardware_view: &'a HardwareView) -> Item<'a, Message, Theme, Ren
.on_press(Message::ConnectDialog(
ConnectDialogMessage::ShowConnectDialog,
))
.style(|_, status| {
if status == Hovered {
MENU_BUTTON_HOVER_STYLE
} else {
MENU_BUTTON_STYLE
}
}),
.style(menu_button),
);

if let Some(hardware_description) = hardware_view.hardware_description.as_ref() {
Expand All @@ -55,13 +40,7 @@ pub fn view<'a>(hardware_view: &'a HardwareView) -> Item<'a, Message, Theme, Ren
HashMap::default(),
)))
.width(Length::Fill)
.style(|_, status| {
if status == Hovered {
MENU_BUTTON_HOVER_STYLE
} else {
MENU_BUTTON_STYLE
}
}),
.style(menu_button),
);
menu_items.push(show_details);
}
Expand Down Expand Up @@ -92,13 +71,7 @@ pub fn view<'a>(hardware_view: &'a HardwareView) -> Item<'a, Message, Theme, Ren
Item::with_menu(
button(text(model_string))
.on_press(Message::MenuBarButtonClicked) // Needed for highlighting
.style(move |_theme, status| {
if status == Hovered {
MENU_BAR_BUTTON_HOVER_STYLE
} else {
MENU_BAR_BUTTON_STYLE
}
}),
.style(menu_bar_button),
Menu::new(menu_items).width(215.0).offset(10.0),
)
}
59 changes: 8 additions & 51 deletions src/views/devices_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use crate::discovery::DiscoveredDevice;
use crate::discovery::DiscoveryMethod::USBRaw;
use crate::views::hardware_view::HardwareConnection;
use crate::views::info_dialog::InfoDialogMessage::HardwareDetailsModal;
use crate::views::info_row::{MENU_BUTTON_HOVER_STYLE, MENU_BUTTON_STYLE};
use crate::views::info_row::menu_button;
#[cfg(feature = "usb")]
use crate::views::ssid_dialog::SsidDialogMessage;
use crate::HardwareConnection::*;
use crate::Message;
use iced::alignment;
use iced::widget::button::Status::Hovered;
use iced::widget::{button, text};
use iced::widget::{horizontal_space, row};
use iced::{Length, Renderer, Theme};
Expand Down Expand Up @@ -50,13 +49,7 @@ fn device_items<'a>(
hardware_details.clone(),
connect_options,
)))
.style(|_, status| {
if status == Hovered {
MENU_BUTTON_HOVER_STYLE
} else {
MENU_BUTTON_STYLE
}
}),
.style(menu_button),
));

// Add buttons to connect to the device for each available connection type, except
Expand All @@ -68,23 +61,11 @@ fn device_items<'a>(
button(text(format!("Connect via {}", hardware_connection.name())))
.on_press(Message::ConnectRequest(hardware_connection.clone()))
.width(Length::Fill)
.style(|_, status| {
if status == Hovered {
MENU_BUTTON_HOVER_STYLE
} else {
MENU_BUTTON_STYLE
}
})
.style(menu_button)
} else {
button(text("Connected to Device"))
.width(Length::Fill)
.style(|_, status| {
if status == Hovered {
MENU_BUTTON_HOVER_STYLE
} else {
MENU_BUTTON_STYLE
}
})
.style(menu_button)
};
device_menu_items.push(Item::new(connect_button));
}
Expand All @@ -102,13 +83,7 @@ fn device_items<'a>(
hardware_details.clone(),
ssid_spec.as_ref().and_then(|_wf| ssid_spec.clone()),
)))
.style(|_, status| {
if status == Hovered {
MENU_BUTTON_HOVER_STYLE
} else {
MENU_BUTTON_STYLE
}
}),
.style(menu_button),
));
}

Expand All @@ -117,13 +92,7 @@ fn device_items<'a>(
button("Reset Device Wi-Fi to Default")
.width(Length::Fill)
.on_press(Message::ResetSsid(hardware_details.serial.clone()))
.style(|_, status| {
if status == Hovered {
MENU_BUTTON_HOVER_STYLE
} else {
MENU_BUTTON_STYLE
}
}),
.style(menu_button),
));
}
}
Expand All @@ -135,13 +104,7 @@ fn device_items<'a>(
text(" >").align_y(alignment::Vertical::Center),
))
.on_press(Message::MenuBarButtonClicked) // Needed for highlighting
.style(|_: &Theme, status| {
if status == Hovered {
MENU_BUTTON_HOVER_STYLE
} else {
MENU_BUTTON_STYLE
}
});
.style(menu_button);

device_items.push(Item::with_menu(
device_button,
Expand All @@ -162,13 +125,7 @@ pub fn view<'a>(
Item::with_menu(
button(text(format!("devices ({})", device_items.len())))
.on_press(Message::MenuBarButtonClicked) // Needed for highlighting
.style(|_, status| {
if status == Hovered {
MENU_BUTTON_HOVER_STYLE
} else {
MENU_BUTTON_STYLE
}
}),
.style(menu_button),
Menu::new(device_items).width(320.0).offset(10.0),
)
}
Loading

0 comments on commit 575116b

Please sign in to comment.