cosmic/widget/dropdown/
mod.rs

1// Copyright 2023 System76 <info@system76.com>
2// Copyright 2019 Héctor Ramón, Iced contributors
3// SPDX-License-Identifier: MPL-2.0 AND MIT
4
5//! Displays a list of options in a popover menu on select.
6
7use std::borrow::Cow;
8
9pub mod menu;
10pub use menu::Menu;
11
12pub mod multi;
13pub mod operation;
14
15mod widget;
16pub use widget::*;
17
18use crate::surface;
19pub use iced_core::widget::Id;
20use iced_core::window;
21
22/// Displays a list of options in a popover menu on select.
23pub fn dropdown<
24    'a,
25    S: AsRef<str> + std::clone::Clone + Send + Sync + 'static,
26    Message: 'static + Clone,
27>(
28    selections: impl Into<Cow<'a, [S]>>,
29    selected: Option<usize>,
30    on_selected: impl Fn(usize) -> Message + Send + Sync + 'static,
31) -> Dropdown<'a, S, Message, Message> {
32    Dropdown::new(selections.into(), selected, on_selected)
33}
34
35/// Displays a list of options in a popover menu on select.
36/// AppMessage must be the App's toplevel message.
37pub fn popup_dropdown<
38    'a,
39    S: AsRef<str> + std::clone::Clone + Send + Sync + 'static,
40    Message: 'static + Clone,
41    AppMessage: 'static + Clone,
42>(
43    selections: impl Into<Cow<'a, [S]>>,
44    selected: Option<usize>,
45    on_selected: impl Fn(usize) -> Message + Send + Sync + 'static,
46    _parent_id: window::Id,
47    _on_surface_action: impl Fn(surface::Action) -> Message + Send + Sync + 'static,
48    _map_action: impl Fn(Message) -> AppMessage + Send + Sync + 'static,
49) -> Dropdown<'a, S, Message, AppMessage> {
50    let dropdown: Dropdown<'_, S, Message, AppMessage> =
51        Dropdown::new(selections.into(), selected, on_selected);
52
53    #[cfg(all(feature = "winit", feature = "wayland", target_os = "linux"))]
54    let dropdown = dropdown.with_popup(_parent_id, _on_surface_action, _map_action);
55
56    dropdown
57}
58
59// /// Produces a [`Task`] that closes the [`Dropdown`].
60// pub fn close<Message: 'static>(id: Id) -> iced_runtime::Task<Message> {
61//     iced_runtime::task::effect(iced_runtime::Action::Widget(Box::new(operation::close(id))))
62// }
63
64// /// Produces a [`Task`] that opens the [`Dropdown`].
65// pub fn open<Message: 'static>(id: Id) -> iced_runtime::Task<Message> {
66//     iced_runtime::task::effect(iced_runtime::Action::Widget(Box::new(operation::open(id))))
67// }