I feel as though my last posing of this question was poorly stated. I need help understanding how to implement an iced menu for my application.
There are four parameters total for the Menu::new() function.
- state: &'a mut State
- options: &'a [T]
- hovered_option: &'a mut Option usize
- last_selection: &'a mut Option T
I am confused as to how to implement this. For the state I am simply creating a new state, line 44, but am not sure if this is correct. I am fairly sure I have options correct, though could be wrong. I understand what types are expected for hovered_option and last_selection, though I do not understand what those parameters pertain to, and in effect what to pass.
The below code is my attempt to just create a menu in some bogus application but I get an error on line 57. This is where I get clueless. I do not understand how to place an overlay on top of a widget.
Cargo.toml
[dependencies]
iced = { version = "0.9.0" }
main.rs
use iced::{Settings, Application, executor, Command, Theme, Element, overlay::menu::{self, Menu}, widget::{container, row}, Length};
fn main() -> iced::Result {
Example::run(Settings::default())
}
#[derive(Debug)]
struct Example {
service: Option<Service>
}
#[derive(Debug, Clone)]
enum Message {
ServiceSelected(Service)
}
impl Application for Example {
type Executor = executor::Default;
type Message = Message;
type Theme = Theme;
type Flags = ();
fn new(_flags: ()) -> (Self, Command<Message>) {
(
Self { service: None },
Command::none()
)
}
fn title(&self) -> String {
String::from("Example")
}
fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> {
match message {
Message::ServiceSelected(service) => {
self.service = Some(service);
},
}
Command::none()
}
fn view(&self) -> Element<Message> {
let mut menu_state = menu::State::new();
let menu = Menu::new(
&mut menu_state,
&Service::ALL[..],
&mut Some(0),
&mut Some(Service::Login)
)
.padding(10)
.text_size(8)
.width(20.0);
let mut content = row![];
content = content.push(menu);
container(content)
.width(Length::Fill)
.height(Length::Fill)
.padding(10)
.into()
}
}
#[derive(Debug, Clone)]
enum Service {
Account,
Chat,
Login,
Test,
}
impl<'a> Service {
pub const ALL: [Service; 4] = [
Service::Account,
Service::Chat,
Service::Login,
Service::Test
];
}
impl<'a> std::fmt::Display for Service {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Service::Account => "Account",
Service::Chat => "Chat",
Service::Login => "Login",
Service::Test => "Test",
}
)
}
}