2023-10-23 16:57:37 +02:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// Copyright 2019 Héctor Ramón, Iced contributors
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0 AND MIT
|
|
|
|
|
|
2024-05-20 20:01:47 +02:00
|
|
|
//! Displays a list of options in a popover menu on select.
|
|
|
|
|
|
2025-05-26 22:53:35 +02:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
2023-10-23 16:57:37 +02:00
|
|
|
pub mod menu;
|
|
|
|
|
pub use menu::Menu;
|
|
|
|
|
|
2023-11-20 17:04:37 +01:00
|
|
|
pub mod multi;
|
2025-11-18 18:35:27 +01:00
|
|
|
pub mod operation;
|
2023-11-20 17:04:37 +01:00
|
|
|
|
2023-10-23 16:57:37 +02:00
|
|
|
mod widget;
|
|
|
|
|
pub use widget::*;
|
|
|
|
|
|
2025-03-14 11:56:21 -04:00
|
|
|
use crate::surface;
|
2025-11-18 18:35:27 +01:00
|
|
|
pub use iced_core::widget::Id;
|
|
|
|
|
use iced_core::window;
|
2025-03-14 11:56:21 -04:00
|
|
|
|
2024-05-20 20:01:47 +02:00
|
|
|
/// Displays a list of options in a popover menu on select.
|
2025-03-14 11:56:21 -04:00
|
|
|
pub fn dropdown<
|
2025-05-26 22:53:35 +02:00
|
|
|
'a,
|
2025-03-14 11:56:21 -04:00
|
|
|
S: AsRef<str> + std::clone::Clone + Send + Sync + 'static,
|
|
|
|
|
Message: 'static + Clone,
|
|
|
|
|
>(
|
2025-05-26 22:53:35 +02:00
|
|
|
selections: impl Into<Cow<'a, [S]>>,
|
2023-10-23 16:57:37 +02:00
|
|
|
selected: Option<usize>,
|
2025-03-14 11:56:21 -04:00
|
|
|
on_selected: impl Fn(usize) -> Message + Send + Sync + 'static,
|
2025-05-26 22:53:35 +02:00
|
|
|
) -> Dropdown<'a, S, Message, Message> {
|
|
|
|
|
Dropdown::new(selections.into(), selected, on_selected)
|
2023-10-23 16:57:37 +02:00
|
|
|
}
|
2025-03-14 11:56:21 -04:00
|
|
|
|
|
|
|
|
/// Displays a list of options in a popover menu on select.
|
|
|
|
|
/// AppMessage must be the App's toplevel message.
|
|
|
|
|
pub fn popup_dropdown<
|
|
|
|
|
'a,
|
|
|
|
|
S: AsRef<str> + std::clone::Clone + Send + Sync + 'static,
|
|
|
|
|
Message: 'static + Clone,
|
|
|
|
|
AppMessage: 'static + Clone,
|
|
|
|
|
>(
|
2025-05-26 22:53:35 +02:00
|
|
|
selections: impl Into<Cow<'a, [S]>>,
|
2025-03-14 11:56:21 -04:00
|
|
|
selected: Option<usize>,
|
|
|
|
|
on_selected: impl Fn(usize) -> Message + Send + Sync + 'static,
|
|
|
|
|
_parent_id: window::Id,
|
|
|
|
|
_on_surface_action: impl Fn(surface::Action) -> Message + Send + Sync + 'static,
|
|
|
|
|
_map_action: impl Fn(Message) -> AppMessage + Send + Sync + 'static,
|
|
|
|
|
) -> Dropdown<'a, S, Message, AppMessage> {
|
|
|
|
|
let dropdown: Dropdown<'_, S, Message, AppMessage> =
|
2025-05-26 22:53:35 +02:00
|
|
|
Dropdown::new(selections.into(), selected, on_selected);
|
2025-03-14 11:56:21 -04:00
|
|
|
|
|
|
|
|
#[cfg(all(feature = "winit", feature = "wayland"))]
|
|
|
|
|
let dropdown = dropdown.with_popup(_parent_id, _on_surface_action, _map_action);
|
|
|
|
|
|
|
|
|
|
dropdown
|
|
|
|
|
}
|
2025-11-18 18:35:27 +01:00
|
|
|
|
|
|
|
|
/// Produces a [`Task`] that closes the [`Dropdown`].
|
|
|
|
|
pub fn close<Message: 'static>(id: Id) -> iced_runtime::Task<Message> {
|
|
|
|
|
iced_runtime::task::effect(iced_runtime::Action::Widget(Box::new(operation::close(id))))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Produces a [`Task`] that opens the [`Dropdown`].
|
|
|
|
|
pub fn open<Message: 'static>(id: Id) -> iced_runtime::Task<Message> {
|
|
|
|
|
iced_runtime::task::effect(iced_runtime::Action::Widget(Box::new(operation::open(id))))
|
|
|
|
|
}
|