wip: menus
This commit is contained in:
parent
a406859e65
commit
93e3d4fd42
25 changed files with 1358 additions and 243 deletions
|
|
@ -3,7 +3,11 @@
|
|||
// SPDX-License-Identifier: MPL-2.0 AND MIT
|
||||
|
||||
mod appearance;
|
||||
use std::borrow::Cow;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
pub use appearance::{Appearance, StyleSheet};
|
||||
use iced::advanced::widget;
|
||||
|
||||
use crate::widget::{icon, Container};
|
||||
use iced_core::event::{self, Event};
|
||||
|
|
@ -21,11 +25,12 @@ use iced_widget::scrollable::Scrollable;
|
|||
pub struct Menu<'a, S, Message>
|
||||
where
|
||||
S: AsRef<str>,
|
||||
[S]: std::borrow::ToOwned,
|
||||
{
|
||||
state: &'a mut State,
|
||||
options: &'a [S],
|
||||
icons: &'a [icon::Handle],
|
||||
hovered_option: &'a mut Option<usize>,
|
||||
state: State,
|
||||
options: Cow<'a, [S]>,
|
||||
icons: Cow<'a, [icon::Handle]>,
|
||||
hovered_option: Arc<Mutex<Option<usize>>>,
|
||||
selected_option: Option<usize>,
|
||||
on_selected: Box<dyn FnMut(usize) -> Message + 'a>,
|
||||
on_option_hovered: Option<&'a dyn Fn(usize) -> Message>,
|
||||
|
|
@ -36,14 +41,17 @@ where
|
|||
style: (),
|
||||
}
|
||||
|
||||
impl<'a, S: AsRef<str>, Message: 'a> Menu<'a, S, Message> {
|
||||
impl<'a, S: AsRef<str>, Message: 'a + std::clone::Clone> Menu<'a, S, Message>
|
||||
where
|
||||
[S]: std::borrow::ToOwned,
|
||||
{
|
||||
/// Creates a new [`Menu`] with the given [`State`], a list of options, and
|
||||
/// the message to produced when an option is selected.
|
||||
pub fn new(
|
||||
state: &'a mut State,
|
||||
options: &'a [S],
|
||||
icons: &'a [icon::Handle],
|
||||
hovered_option: &'a mut Option<usize>,
|
||||
state: State,
|
||||
options: Cow<'a, [S]>,
|
||||
icons: Cow<'a, [icon::Handle]>,
|
||||
hovered_option: Arc<Mutex<Option<usize>>>,
|
||||
selected_option: Option<usize>,
|
||||
on_selected: impl FnMut(usize) -> Message + 'a,
|
||||
on_option_hovered: Option<&'a dyn Fn(usize) -> Message>,
|
||||
|
|
@ -102,20 +110,35 @@ impl<'a, S: AsRef<str>, Message: 'a> Menu<'a, S, Message> {
|
|||
) -> overlay::Element<'a, Message, crate::Theme, crate::Renderer> {
|
||||
overlay::Element::new(Box::new(Overlay::new(self, target_height, position)))
|
||||
}
|
||||
|
||||
/// Turns the [`Menu`] into a popup [`Element`] at the given target
|
||||
/// position.
|
||||
///
|
||||
/// The `target_height` will be used to display the menu either on top
|
||||
/// of the target or under it, depending on the screen position and the
|
||||
/// dimensions of the [`Menu`].
|
||||
#[must_use]
|
||||
pub fn popup(self, position: Point, target_height: f32) -> crate::Element<'a, Message> {
|
||||
Overlay::new(self, target_height, position).into()
|
||||
}
|
||||
}
|
||||
|
||||
/// The local state of a [`Menu`].
|
||||
#[must_use]
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct State {
|
||||
tree: Tree,
|
||||
pub(crate) tree: Arc<Mutex<Tree>>,
|
||||
}
|
||||
|
||||
// XXX The tree is only used on one thread, but this is needed to make a message
|
||||
unsafe impl Send for State {}
|
||||
unsafe impl Sync for State {}
|
||||
|
||||
impl State {
|
||||
/// Creates a new [`State`] for a [`Menu`].
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
tree: Tree::empty(),
|
||||
tree: Arc::new(Mutex::new(Tree::empty())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -127,7 +150,7 @@ impl Default for State {
|
|||
}
|
||||
|
||||
struct Overlay<'a, Message> {
|
||||
state: &'a mut Tree,
|
||||
state: Arc<Mutex<Tree>>,
|
||||
container: Container<'a, Message, crate::Theme, crate::Renderer>,
|
||||
width: f32,
|
||||
target_height: f32,
|
||||
|
|
@ -140,7 +163,10 @@ impl<'a, Message: 'a> Overlay<'a, Message> {
|
|||
menu: Menu<'a, S, Message>,
|
||||
target_height: f32,
|
||||
position: Point,
|
||||
) -> Self {
|
||||
) -> Self
|
||||
where
|
||||
[S]: ToOwned,
|
||||
{
|
||||
let Menu {
|
||||
state,
|
||||
options,
|
||||
|
|
@ -172,10 +198,11 @@ impl<'a, Message: 'a> Overlay<'a, Message> {
|
|||
))
|
||||
.class(crate::style::Container::Dropdown);
|
||||
|
||||
state.tree.diff(&mut container as &mut dyn Widget<_, _, _>);
|
||||
|
||||
let mut tree_guard = state.tree.lock().unwrap();
|
||||
tree_guard.diff(&mut container as &mut dyn Widget<_, _, _>);
|
||||
drop(tree_guard);
|
||||
Self {
|
||||
state: &mut state.tree,
|
||||
state: state.tree.clone(),
|
||||
container,
|
||||
width,
|
||||
target_height,
|
||||
|
|
@ -183,20 +210,17 @@ impl<'a, Message: 'a> Overlay<'a, Message> {
|
|||
position,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message> iced_core::Overlay<Message, crate::Theme, crate::Renderer>
|
||||
for Overlay<'a, Message>
|
||||
{
|
||||
fn layout(&mut self, renderer: &crate::Renderer, bounds: Size) -> layout::Node {
|
||||
let position = self.position;
|
||||
let space_below = bounds.height - (position.y + self.target_height);
|
||||
let space_above = position.y;
|
||||
fn _layout(&self, renderer: &crate::Renderer, bounds: Size) -> layout::Node {
|
||||
let mut guard = self.state.lock().unwrap();
|
||||
|
||||
let space_below = bounds.height - (self.position.y + self.target_height);
|
||||
let space_above = self.position.y;
|
||||
|
||||
let limits = layout::Limits::new(
|
||||
Size::ZERO,
|
||||
Size::new(
|
||||
bounds.width - position.x,
|
||||
bounds.width - self.position.x,
|
||||
if space_below > space_above {
|
||||
space_below
|
||||
} else {
|
||||
|
|
@ -206,16 +230,16 @@ impl<'a, Message> iced_core::Overlay<Message, crate::Theme, crate::Renderer>
|
|||
)
|
||||
.width(self.width);
|
||||
|
||||
let node = self.container.layout(self.state, renderer, &limits);
|
||||
let node = self.container.layout(&mut guard, renderer, &limits);
|
||||
|
||||
node.clone().move_to(if space_below > space_above {
|
||||
position + Vector::new(0.0, self.target_height)
|
||||
self.position + Vector::new(0.0, self.target_height)
|
||||
} else {
|
||||
position - Vector::new(0.0, node.size().height)
|
||||
self.position - Vector::new(0.0, node.size().height)
|
||||
})
|
||||
}
|
||||
|
||||
fn on_event(
|
||||
fn _on_event(
|
||||
&mut self,
|
||||
event: Event,
|
||||
layout: Layout<'_>,
|
||||
|
|
@ -225,24 +249,28 @@ impl<'a, Message> iced_core::Overlay<Message, crate::Theme, crate::Renderer>
|
|||
shell: &mut Shell<'_, Message>,
|
||||
) -> event::Status {
|
||||
let bounds = layout.bounds();
|
||||
let mut guard = self.state.lock().unwrap();
|
||||
|
||||
let state = &mut *guard;
|
||||
self.container.on_event(
|
||||
self.state, event, layout, cursor, renderer, clipboard, shell, &bounds,
|
||||
state, event, layout, cursor, renderer, clipboard, shell, &bounds,
|
||||
)
|
||||
}
|
||||
|
||||
fn mouse_interaction(
|
||||
fn _mouse_interaction(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
viewport: &Rectangle,
|
||||
renderer: &crate::Renderer,
|
||||
) -> mouse::Interaction {
|
||||
let guard = self.state.lock().unwrap();
|
||||
|
||||
self.container
|
||||
.mouse_interaction(self.state, layout, cursor, viewport, renderer)
|
||||
.mouse_interaction(&guard, layout, cursor, viewport, renderer)
|
||||
}
|
||||
|
||||
fn draw(
|
||||
fn _draw(
|
||||
&self,
|
||||
renderer: &mut crate::Renderer,
|
||||
theme: &crate::Theme,
|
||||
|
|
@ -250,6 +278,8 @@ impl<'a, Message> iced_core::Overlay<Message, crate::Theme, crate::Renderer>
|
|||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
) {
|
||||
let state = self.state.lock().unwrap();
|
||||
|
||||
let appearance = theme.appearance(&self.style);
|
||||
let bounds = layout.bounds();
|
||||
|
||||
|
|
@ -267,14 +297,127 @@ impl<'a, Message> iced_core::Overlay<Message, crate::Theme, crate::Renderer>
|
|||
);
|
||||
|
||||
self.container
|
||||
.draw(self.state, renderer, theme, style, layout, cursor, &bounds);
|
||||
.draw(&*state, renderer, theme, style, layout, cursor, &bounds);
|
||||
}
|
||||
}
|
||||
|
||||
struct List<'a, S: AsRef<str>, Message> {
|
||||
options: &'a [S],
|
||||
icons: &'a [icon::Handle],
|
||||
hovered_option: &'a mut Option<usize>,
|
||||
impl<'a, Message: 'a> iced_core::Overlay<Message, crate::Theme, crate::Renderer>
|
||||
for Overlay<'a, Message>
|
||||
{
|
||||
fn layout(&mut self, renderer: &crate::Renderer, bounds: Size) -> layout::Node {
|
||||
let mut guard = self.state.lock().unwrap();
|
||||
|
||||
self._layout(renderer, bounds)
|
||||
}
|
||||
|
||||
fn on_event(
|
||||
&mut self,
|
||||
event: Event,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
renderer: &crate::Renderer,
|
||||
clipboard: &mut dyn Clipboard,
|
||||
shell: &mut Shell<'_, Message>,
|
||||
) -> event::Status {
|
||||
self._on_event(event, layout, cursor, renderer, clipboard, shell)
|
||||
}
|
||||
|
||||
fn mouse_interaction(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
viewport: &Rectangle,
|
||||
renderer: &crate::Renderer,
|
||||
) -> mouse::Interaction {
|
||||
self._mouse_interaction(layout, cursor, viewport, renderer)
|
||||
}
|
||||
|
||||
fn draw(
|
||||
&self,
|
||||
renderer: &mut crate::Renderer,
|
||||
theme: &crate::Theme,
|
||||
style: &renderer::Style,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
) {
|
||||
let guard = self.state.lock().unwrap();
|
||||
|
||||
self._draw(renderer, theme, style, layout, cursor);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message: 'a> crate::widget::Widget<Message, crate::Theme, crate::Renderer>
|
||||
for Overlay<'a, Message>
|
||||
{
|
||||
fn size(&self) -> Size<Length> {
|
||||
Size::new(Length::Fixed(self.width), Length::Shrink)
|
||||
}
|
||||
|
||||
fn layout(
|
||||
&self,
|
||||
_tree: &mut iced_core::widget::Tree,
|
||||
renderer: &crate::Renderer,
|
||||
limits: &iced::Limits,
|
||||
) -> layout::Node {
|
||||
let mut guard = self.state.lock().unwrap();
|
||||
|
||||
let limits = limits.width(self.width);
|
||||
|
||||
self.container.layout(&mut guard, renderer, &limits)
|
||||
}
|
||||
|
||||
fn mouse_interaction(
|
||||
&self,
|
||||
_tree: &Tree,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
viewport: &Rectangle,
|
||||
renderer: &crate::Renderer,
|
||||
) -> mouse::Interaction {
|
||||
self._mouse_interaction(layout, cursor, viewport, renderer)
|
||||
}
|
||||
|
||||
fn on_event(
|
||||
&mut self,
|
||||
_tree: &mut Tree,
|
||||
event: Event,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
renderer: &crate::Renderer,
|
||||
clipboard: &mut dyn Clipboard,
|
||||
shell: &mut Shell<'_, Message>,
|
||||
_viewport: &Rectangle,
|
||||
) -> event::Status {
|
||||
self._on_event(event, layout, cursor, renderer, clipboard, shell)
|
||||
}
|
||||
|
||||
fn draw(
|
||||
&self,
|
||||
tree: &Tree,
|
||||
renderer: &mut crate::Renderer,
|
||||
theme: &crate::Theme,
|
||||
style: &renderer::Style,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
_viewport: &Rectangle,
|
||||
) {
|
||||
self._draw(renderer, theme, style, layout, cursor);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message: Clone + 'a> From<Overlay<'a, Message>> for crate::Element<'a, Message> {
|
||||
fn from(widget: Overlay<'a, Message>) -> Self {
|
||||
Element::new(widget)
|
||||
}
|
||||
}
|
||||
|
||||
struct List<'a, S: AsRef<str>, Message>
|
||||
where
|
||||
[S]: std::borrow::ToOwned,
|
||||
{
|
||||
options: Cow<'a, [S]>,
|
||||
icons: Cow<'a, [icon::Handle]>,
|
||||
hovered_option: Arc<Mutex<Option<usize>>>,
|
||||
selected_option: Option<usize>,
|
||||
on_selected: Box<dyn FnMut(usize) -> Message + 'a>,
|
||||
on_option_hovered: Option<&'a dyn Fn(usize) -> Message>,
|
||||
|
|
@ -285,6 +428,8 @@ struct List<'a, S: AsRef<str>, Message> {
|
|||
|
||||
impl<'a, S: AsRef<str>, Message> Widget<Message, crate::Theme, crate::Renderer>
|
||||
for List<'a, S, Message>
|
||||
where
|
||||
[S]: std::borrow::ToOwned,
|
||||
{
|
||||
fn size(&self) -> Size<Length> {
|
||||
Size::new(Length::Fill, Length::Shrink)
|
||||
|
|
@ -330,8 +475,9 @@ impl<'a, S: AsRef<str>, Message> Widget<Message, crate::Theme, crate::Renderer>
|
|||
) -> event::Status {
|
||||
match event {
|
||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
|
||||
let hovered_guard = self.hovered_option.lock().unwrap();
|
||||
if cursor.is_over(layout.bounds()) {
|
||||
if let Some(index) = *self.hovered_option {
|
||||
if let Some(index) = *hovered_guard {
|
||||
shell.publish((self.on_selected)(index));
|
||||
return event::Status::Captured;
|
||||
}
|
||||
|
|
@ -348,14 +494,15 @@ impl<'a, S: AsRef<str>, Message> Widget<Message, crate::Theme, crate::Renderer>
|
|||
+ self.padding.vertical();
|
||||
|
||||
let new_hovered_option = (cursor_position.y / option_height) as usize;
|
||||
let mut hovered_guard = self.hovered_option.lock().unwrap();
|
||||
|
||||
if let Some(on_option_hovered) = self.on_option_hovered {
|
||||
if *self.hovered_option != Some(new_hovered_option) {
|
||||
if *hovered_guard != Some(new_hovered_option) {
|
||||
shell.publish(on_option_hovered(new_hovered_option));
|
||||
}
|
||||
}
|
||||
|
||||
*self.hovered_option = Some(new_hovered_option);
|
||||
*hovered_guard = Some(new_hovered_option);
|
||||
}
|
||||
}
|
||||
Event::Touch(touch::Event::FingerPressed { .. }) => {
|
||||
|
|
@ -367,10 +514,11 @@ impl<'a, S: AsRef<str>, Message> Widget<Message, crate::Theme, crate::Renderer>
|
|||
let option_height =
|
||||
f32::from(self.text_line_height.to_absolute(Pixels(text_size)))
|
||||
+ self.padding.vertical();
|
||||
let mut hovered_guard = self.hovered_option.lock().unwrap();
|
||||
|
||||
*self.hovered_option = Some((cursor_position.y / option_height) as usize);
|
||||
*hovered_guard = Some((cursor_position.y / option_height) as usize);
|
||||
|
||||
if let Some(index) = *self.hovered_option {
|
||||
if let Some(index) = *hovered_guard {
|
||||
shell.publish((self.on_selected)(index));
|
||||
return event::Status::Captured;
|
||||
}
|
||||
|
|
@ -434,6 +582,8 @@ impl<'a, S: AsRef<str>, Message> Widget<Message, crate::Theme, crate::Renderer>
|
|||
height: option_height,
|
||||
};
|
||||
|
||||
let hovered_guard = self.hovered_option.lock().unwrap();
|
||||
|
||||
let (color, font) = if self.selected_option == Some(i) {
|
||||
let item_x = bounds.x + appearance.border_width;
|
||||
let item_width = appearance.border_width.mul_add(-2.0, bounds.width);
|
||||
|
|
@ -471,7 +621,7 @@ impl<'a, S: AsRef<str>, Message> Widget<Message, crate::Theme, crate::Renderer>
|
|||
);
|
||||
|
||||
(appearance.selected_text_color, crate::font::semibold())
|
||||
} else if *self.hovered_option == Some(i) {
|
||||
} else if *hovered_guard == Some(i) {
|
||||
let item_x = bounds.x + appearance.border_width;
|
||||
let item_width = appearance.border_width.mul_add(-2.0, bounds.width);
|
||||
|
||||
|
|
@ -538,6 +688,8 @@ impl<'a, S: AsRef<str>, Message> Widget<Message, crate::Theme, crate::Renderer>
|
|||
|
||||
impl<'a, S: AsRef<str>, Message: 'a> From<List<'a, S, Message>>
|
||||
for Element<'a, Message, crate::Theme, crate::Renderer>
|
||||
where
|
||||
[S]: std::borrow::ToOwned,
|
||||
{
|
||||
fn from(list: List<'a, S, Message>) -> Self {
|
||||
Element::new(list)
|
||||
|
|
|
|||
|
|
@ -13,10 +13,14 @@ mod widget;
|
|||
pub use widget::*;
|
||||
|
||||
/// Displays a list of options in a popover menu on select.
|
||||
pub fn dropdown<'a, S: AsRef<str>, Message: 'a>(
|
||||
pub fn dropdown<
|
||||
'a,
|
||||
S: AsRef<str> + std::clone::Clone + Send + Sync + 'static,
|
||||
Message: 'static + Clone,
|
||||
>(
|
||||
selections: &'a [S],
|
||||
selected: Option<usize>,
|
||||
on_selected: impl Fn(usize) -> Message + 'a,
|
||||
on_selected: impl Fn(usize) -> Message + Send + Sync + 'static,
|
||||
) -> Dropdown<'a, S, Message> {
|
||||
Dropdown::new(selections, selected, on_selected)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,9 +3,10 @@
|
|||
// SPDX-License-Identifier: MPL-2.0 AND MIT
|
||||
|
||||
use super::menu::{self, Menu};
|
||||
use crate::widget::icon;
|
||||
use crate::widget::icon::{self, Handle};
|
||||
use crate::Element;
|
||||
use derive_setters::Setters;
|
||||
use iced::Radians;
|
||||
use iced::{window, Radians};
|
||||
use iced_core::event::{self, Event};
|
||||
use iced_core::text::{self, Paragraph, Text};
|
||||
use iced_core::widget::tree::{self, Tree};
|
||||
|
|
@ -14,14 +15,23 @@ use iced_core::{
|
|||
Clipboard, Layout, Length, Padding, Pixels, Rectangle, Shell, Size, Vector, Widget,
|
||||
};
|
||||
use iced_widget::pick_list::{self, Catalog};
|
||||
use std::borrow::Cow;
|
||||
use std::ffi::OsStr;
|
||||
use std::hash::{DefaultHasher, Hash, Hasher};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, LazyLock, Mutex};
|
||||
|
||||
pub type DropdownView<Message> = Arc<dyn Fn() -> Element<'static, Message> + Send + Sync>;
|
||||
static AUTOSIZE_ID: LazyLock<crate::widget::Id> =
|
||||
LazyLock::new(|| crate::widget::Id::new("cosmic-applet-autosize"));
|
||||
/// A widget for selecting a single value from a list of selections.
|
||||
#[derive(Setters)]
|
||||
pub struct Dropdown<'a, S: AsRef<str>, Message> {
|
||||
pub struct Dropdown<'a, S: AsRef<str> + Send + Sync + Clone + 'static, Message>
|
||||
where
|
||||
[S]: std::borrow::ToOwned,
|
||||
{
|
||||
#[setters(skip)]
|
||||
on_selected: Box<dyn Fn(usize) -> Message + 'a>,
|
||||
on_selected: Arc<dyn Fn(usize) -> Message + Send + Sync>,
|
||||
#[setters(skip)]
|
||||
selections: &'a [S],
|
||||
#[setters]
|
||||
|
|
@ -38,9 +48,29 @@ pub struct Dropdown<'a, S: AsRef<str>, Message> {
|
|||
text_line_height: text::LineHeight,
|
||||
#[setters(strip_option)]
|
||||
font: Option<crate::font::Font>,
|
||||
#[cfg(feature = "wayland")]
|
||||
#[setters(skip)]
|
||||
on_open: Option<
|
||||
Arc<
|
||||
dyn Fn(
|
||||
iced_runtime::platform_specific::wayland::popup::SctkPopupSettings,
|
||||
DropdownView<Message>,
|
||||
) -> Message
|
||||
+ 'a,
|
||||
>,
|
||||
>,
|
||||
#[setters(skip)]
|
||||
on_close_popup: Option<Box<dyn Fn(window::Id) -> Message + 'a>>,
|
||||
#[setters(strip_option)]
|
||||
window_id: Option<window::Id>,
|
||||
#[cfg(feature = "wayland")]
|
||||
positioner: iced_runtime::platform_specific::wayland::popup::SctkPositioner,
|
||||
}
|
||||
|
||||
impl<'a, S: AsRef<str>, Message> Dropdown<'a, S, Message> {
|
||||
impl<'a, S: AsRef<str> + Send + Sync + Clone + 'static, Message: 'static> Dropdown<'a, S, Message>
|
||||
where
|
||||
[S]: std::borrow::ToOwned,
|
||||
{
|
||||
/// The default gap.
|
||||
pub const DEFAULT_GAP: f32 = 4.0;
|
||||
|
||||
|
|
@ -52,10 +82,10 @@ impl<'a, S: AsRef<str>, Message> Dropdown<'a, S, Message> {
|
|||
pub fn new(
|
||||
selections: &'a [S],
|
||||
selected: Option<usize>,
|
||||
on_selected: impl Fn(usize) -> Message + 'a,
|
||||
on_selected: impl Fn(usize) -> Message + 'static + Send + Sync,
|
||||
) -> Self {
|
||||
Self {
|
||||
on_selected: Box::new(on_selected),
|
||||
on_selected: Arc::new(on_selected),
|
||||
selections,
|
||||
icons: &[],
|
||||
selected,
|
||||
|
|
@ -65,12 +95,54 @@ impl<'a, S: AsRef<str>, Message> Dropdown<'a, S, Message> {
|
|||
text_size: None,
|
||||
text_line_height: text::LineHeight::Relative(1.2),
|
||||
font: None,
|
||||
#[cfg(feature = "wayland")]
|
||||
on_open: None,
|
||||
window_id: None,
|
||||
#[cfg(feature = "wayland")]
|
||||
positioner: iced_runtime::platform_specific::wayland::popup::SctkPositioner::default(),
|
||||
on_close_popup: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "wayland")]
|
||||
/// Handle dropdown requests for popup creation.
|
||||
/// Intended to be used with [`crate::app::message::get_popup`]
|
||||
pub fn with_popup(
|
||||
mut self,
|
||||
parent_id: window::Id,
|
||||
on_open: impl Fn(
|
||||
iced_runtime::platform_specific::wayland::popup::SctkPopupSettings,
|
||||
DropdownView<Message>,
|
||||
) -> Message
|
||||
+ 'a,
|
||||
) -> Self {
|
||||
self.window_id = Some(parent_id);
|
||||
self.on_open = Some(Arc::new(on_open));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "wayland")]
|
||||
pub fn with_positioner(
|
||||
mut self,
|
||||
positioner: iced_runtime::platform_specific::wayland::popup::SctkPositioner,
|
||||
) -> Self {
|
||||
self.positioner = positioner;
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "wayland")]
|
||||
/// Handle dropdown requests for popup removal.
|
||||
/// Intended to be used with [`crate::app::message::destroy_popup`]
|
||||
pub fn on_close_popup(mut self, on_close: impl Fn(window::Id) -> Message + 'a) -> Self {
|
||||
self.on_close_popup = Some(Box::new(on_close));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: AsRef<str>, Message: 'a> Widget<Message, crate::Theme, crate::Renderer>
|
||||
for Dropdown<'a, S, Message>
|
||||
impl<'a, S: AsRef<str> + Send + Sync + Clone + 'static, Message: 'static + std::clone::Clone>
|
||||
Widget<Message, crate::Theme, crate::Renderer> for Dropdown<'a, S, Message>
|
||||
where
|
||||
[S]: std::borrow::ToOwned,
|
||||
{
|
||||
fn tag(&self) -> tree::Tag {
|
||||
tree::Tag::of::<State>()
|
||||
|
|
@ -158,10 +230,22 @@ impl<'a, S: AsRef<str>, Message: 'a> Widget<Message, crate::Theme, crate::Render
|
|||
layout,
|
||||
cursor,
|
||||
shell,
|
||||
self.on_selected.as_ref(),
|
||||
#[cfg(feature = "wayland")]
|
||||
self.on_open.clone(),
|
||||
#[cfg(feature = "wayland")]
|
||||
self.positioner.clone(),
|
||||
self.on_selected.clone(),
|
||||
self.selected,
|
||||
self.selections,
|
||||
|| tree.state.downcast_mut::<State>(),
|
||||
self.window_id,
|
||||
self.on_close_popup.as_deref(),
|
||||
self.icons,
|
||||
self.gap,
|
||||
self.padding,
|
||||
self.text_size,
|
||||
self.font,
|
||||
self.selected,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -211,6 +295,9 @@ impl<'a, S: AsRef<str>, Message: 'a> Widget<Message, crate::Theme, crate::Render
|
|||
renderer: &crate::Renderer,
|
||||
translation: Vector,
|
||||
) -> Option<overlay::Element<'b, Message, crate::Theme, crate::Renderer>> {
|
||||
#[cfg(feature = "wayland")]
|
||||
return None;
|
||||
|
||||
let state = tree.state.downcast_mut::<State>();
|
||||
|
||||
overlay(
|
||||
|
|
@ -225,7 +312,7 @@ impl<'a, S: AsRef<str>, Message: 'a> Widget<Message, crate::Theme, crate::Render
|
|||
self.selections,
|
||||
self.icons,
|
||||
self.selected,
|
||||
&self.on_selected,
|
||||
self.on_selected.as_ref(),
|
||||
translation,
|
||||
)
|
||||
}
|
||||
|
|
@ -242,8 +329,10 @@ impl<'a, S: AsRef<str>, Message: 'a> Widget<Message, crate::Theme, crate::Render
|
|||
// }
|
||||
}
|
||||
|
||||
impl<'a, S: AsRef<str>, Message: 'a> From<Dropdown<'a, S, Message>>
|
||||
for crate::Element<'a, Message>
|
||||
impl<'a, S: AsRef<str> + Send + Sync + Clone + 'static, Message: 'static + std::clone::Clone>
|
||||
From<Dropdown<'a, S, Message>> for crate::Element<'a, Message>
|
||||
where
|
||||
[S]: std::borrow::ToOwned,
|
||||
{
|
||||
fn from(pick_list: Dropdown<'a, S, Message>) -> Self {
|
||||
Self::new(pick_list)
|
||||
|
|
@ -251,15 +340,16 @@ impl<'a, S: AsRef<str>, Message: 'a> From<Dropdown<'a, S, Message>>
|
|||
}
|
||||
|
||||
/// The local state of a [`Dropdown`].
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct State {
|
||||
icon: Option<svg::Handle>,
|
||||
menu: menu::State,
|
||||
keyboard_modifiers: keyboard::Modifiers,
|
||||
is_open: bool,
|
||||
hovered_option: Option<usize>,
|
||||
is_open: Arc<AtomicBool>,
|
||||
hovered_option: Arc<Mutex<Option<usize>>>,
|
||||
hashes: Vec<u64>,
|
||||
selections: Vec<crate::Plain>,
|
||||
popup_id: window::Id,
|
||||
}
|
||||
|
||||
impl State {
|
||||
|
|
@ -276,10 +366,11 @@ impl State {
|
|||
},
|
||||
menu: menu::State::default(),
|
||||
keyboard_modifiers: keyboard::Modifiers::default(),
|
||||
is_open: false,
|
||||
hovered_option: None,
|
||||
is_open: Arc::new(AtomicBool::new(false)),
|
||||
hovered_option: Arc::new(Mutex::new(None)),
|
||||
selections: Vec::new(),
|
||||
hashes: Vec::new(),
|
||||
popup_id: window::Id::unique(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -349,31 +440,111 @@ pub fn layout(
|
|||
/// Processes an [`Event`] and updates the [`State`] of a [`Dropdown`]
|
||||
/// accordingly.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn update<'a, S: AsRef<str>, Message>(
|
||||
pub fn update<
|
||||
'a,
|
||||
S: AsRef<str> + Send + Sync + std::clone::Clone + 'static,
|
||||
Message: Clone + 'static,
|
||||
>(
|
||||
event: &Event,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
shell: &mut Shell<'_, Message>,
|
||||
on_selected: &dyn Fn(usize) -> Message,
|
||||
#[cfg(feature = "wayland")] on_open: Option<
|
||||
Arc<
|
||||
dyn Fn(
|
||||
iced_runtime::platform_specific::wayland::popup::SctkPopupSettings,
|
||||
DropdownView<Message>,
|
||||
) -> Message
|
||||
+ 'a,
|
||||
>,
|
||||
>,
|
||||
#[cfg(feature = "wayland")]
|
||||
positioner: iced_runtime::platform_specific::wayland::popup::SctkPositioner,
|
||||
on_selected: Arc<dyn Fn(usize) -> Message + Send + Sync + 'static>,
|
||||
selected: Option<usize>,
|
||||
selections: &[S],
|
||||
state: impl FnOnce() -> &'a mut State,
|
||||
window_id: Option<window::Id>,
|
||||
on_close: Option<&'a dyn Fn(window::Id) -> Message>,
|
||||
icons: &[icon::Handle],
|
||||
gap: f32,
|
||||
padding: Padding,
|
||||
text_size: Option<f32>,
|
||||
font: Option<crate::font::Font>,
|
||||
selected_option: Option<usize>,
|
||||
) -> event::Status {
|
||||
match event {
|
||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||
| Event::Touch(touch::Event::FingerPressed { .. }) => {
|
||||
let state = state();
|
||||
|
||||
if state.is_open {
|
||||
let is_open = state.is_open.load(Ordering::Relaxed);
|
||||
if is_open {
|
||||
// Event wasn't processed by overlay, so cursor was clicked either outside it's
|
||||
// bounds or on the drop-down, either way we close the overlay.
|
||||
state.is_open = false;
|
||||
|
||||
state.is_open.store(false, Ordering::Relaxed);
|
||||
if let Some(on_close) = on_close {
|
||||
shell.publish(on_close(state.popup_id));
|
||||
}
|
||||
event::Status::Captured
|
||||
} else if cursor.is_over(layout.bounds()) {
|
||||
state.is_open = true;
|
||||
state.hovered_option = selected;
|
||||
state.is_open.store(true, Ordering::Relaxed);
|
||||
let mut hovered_guard = state.hovered_option.lock().unwrap();
|
||||
*hovered_guard = selected;
|
||||
let id = window::Id::unique();
|
||||
state.popup_id = id;
|
||||
#[cfg(feature = "wayland")]
|
||||
if let Some((on_open, parent)) = on_open.zip(window_id) {
|
||||
use iced_runtime::platform_specific::wayland::popup::{
|
||||
SctkPopupSettings, SctkPositioner,
|
||||
};
|
||||
let bounds = layout.bounds();
|
||||
let anchor_rect = Rectangle {
|
||||
x: bounds.x as i32,
|
||||
y: bounds.y as i32,
|
||||
width: bounds.width as i32,
|
||||
height: bounds.height as i32,
|
||||
};
|
||||
let icon_width = if icons.is_empty() { 0.0 } else { 24.0 };
|
||||
let measure = |_label: &str, selection_paragraph: &crate::Paragraph| -> f32 {
|
||||
selection_paragraph.min_width().round()
|
||||
};
|
||||
let pad_width = padding.horizontal().mul_add(2.0, 16.0);
|
||||
|
||||
let selections_width = selections
|
||||
.iter()
|
||||
.zip(state.selections.iter_mut())
|
||||
.map(|(label, selection)| measure(label.as_ref(), selection.raw()))
|
||||
.fold(0.0, |next, current| current.max(next));
|
||||
|
||||
let icons: Cow<'static, [Handle]> = Cow::Owned(icons.to_vec());
|
||||
let selections: Cow<'static, [S]> = Cow::Owned(selections.to_vec());
|
||||
let state = state.clone();
|
||||
|
||||
shell.publish(on_open(
|
||||
SctkPopupSettings {
|
||||
parent,
|
||||
id,
|
||||
positioner: SctkPositioner {
|
||||
size: Some((selections_width as u32 + gap as u32 + pad_width as u32 + icon_width as u32, 10)),
|
||||
anchor_rect,
|
||||
anchor: cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Anchor::Top,
|
||||
gravity:cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Gravity::Bottom,
|
||||
reactive: true,
|
||||
..Default::default()
|
||||
},
|
||||
parent_size: None,
|
||||
grab: true,
|
||||
close_with_children: true
|
||||
},
|
||||
Arc::new(move || {
|
||||
Element::from(
|
||||
menu_widget(
|
||||
bounds, &state, gap, padding,text_size.unwrap_or(14.0), selections.clone(), icons.clone(), selected_option, on_selected.clone()
|
||||
)
|
||||
)
|
||||
}),
|
||||
));
|
||||
}
|
||||
event::Status::Captured
|
||||
} else {
|
||||
event::Status::Ignored
|
||||
|
|
@ -383,11 +554,9 @@ pub fn update<'a, S: AsRef<str>, Message>(
|
|||
delta: mouse::ScrollDelta::Lines { .. },
|
||||
}) => {
|
||||
let state = state();
|
||||
let is_open = state.is_open.load(Ordering::Relaxed);
|
||||
|
||||
if state.keyboard_modifiers.command()
|
||||
&& cursor.is_over(layout.bounds())
|
||||
&& !state.is_open
|
||||
{
|
||||
if state.keyboard_modifiers.command() && cursor.is_over(layout.bounds()) && !is_open {
|
||||
let next_index = selected.map(|index| index + 1).unwrap_or_default();
|
||||
|
||||
if selections.len() < next_index {
|
||||
|
|
@ -423,9 +592,70 @@ pub fn mouse_interaction(layout: Layout<'_>, cursor: mouse::Cursor) -> mouse::In
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "wayland")]
|
||||
/// Returns the current menu widget of a [`Dropdown`].
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn menu_widget<
|
||||
S: AsRef<str> + Send + Sync + Clone + 'static,
|
||||
Message: 'static + std::clone::Clone,
|
||||
>(
|
||||
bounds: Rectangle,
|
||||
state: &State,
|
||||
gap: f32,
|
||||
padding: Padding,
|
||||
text_size: f32,
|
||||
selections: Cow<'static, [S]>,
|
||||
icons: Cow<'static, [icon::Handle]>,
|
||||
selected_option: Option<usize>,
|
||||
on_selected: Arc<dyn Fn(usize) -> Message + Send + Sync + 'static>,
|
||||
) -> crate::Element<'static, Message>
|
||||
where
|
||||
[S]: std::borrow::ToOwned,
|
||||
{
|
||||
let icon_width = if icons.is_empty() { 0.0 } else { 24.0 };
|
||||
let measure = |_label: &str, selection_paragraph: &crate::Paragraph| -> f32 {
|
||||
selection_paragraph.min_width().round()
|
||||
};
|
||||
let selections_width = selections
|
||||
.iter()
|
||||
.zip(state.selections.iter())
|
||||
.map(|(label, selection)| measure(label.as_ref(), selection.raw()))
|
||||
.fold(0.0, |next, current| current.max(next));
|
||||
let pad_width = padding.horizontal().mul_add(2.0, 16.0);
|
||||
|
||||
let width = selections_width + gap + pad_width + icon_width;
|
||||
let is_open = state.is_open.clone();
|
||||
let menu: Menu<'static, S, Message> = Menu::new(
|
||||
state.menu.clone(),
|
||||
selections,
|
||||
icons,
|
||||
state.hovered_option.clone(),
|
||||
selected_option,
|
||||
move |option| {
|
||||
is_open.store(false, Ordering::Relaxed);
|
||||
|
||||
(on_selected)(option)
|
||||
},
|
||||
None,
|
||||
)
|
||||
.width(width)
|
||||
.padding(padding)
|
||||
.text_size(text_size);
|
||||
|
||||
crate::widget::autosize::autosize(
|
||||
menu.popup(iced::Point::new(0., 0.), bounds.height),
|
||||
AUTOSIZE_ID.clone(),
|
||||
)
|
||||
.auto_height(true)
|
||||
.auto_width(true)
|
||||
.min_height(1.)
|
||||
.min_width(width)
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Returns the current overlay of a [`Dropdown`].
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn overlay<'a, S: AsRef<str>, Message: 'a>(
|
||||
pub fn overlay<'a, S: AsRef<str> + Send + Sync + Clone + 'static, Message: std::clone::Clone + 'a>(
|
||||
layout: Layout<'_>,
|
||||
_renderer: &crate::Renderer,
|
||||
state: &'a mut State,
|
||||
|
|
@ -439,18 +669,21 @@ pub fn overlay<'a, S: AsRef<str>, Message: 'a>(
|
|||
selected_option: Option<usize>,
|
||||
on_selected: &'a dyn Fn(usize) -> Message,
|
||||
translation: Vector,
|
||||
) -> Option<overlay::Element<'a, Message, crate::Theme, crate::Renderer>> {
|
||||
if state.is_open {
|
||||
) -> Option<overlay::Element<'a, Message, crate::Theme, crate::Renderer>>
|
||||
where
|
||||
[S]: std::borrow::ToOwned,
|
||||
{
|
||||
if state.is_open.load(Ordering::Relaxed) {
|
||||
let bounds = layout.bounds();
|
||||
|
||||
let menu = Menu::new(
|
||||
&mut state.menu,
|
||||
selections,
|
||||
icons,
|
||||
&mut state.hovered_option,
|
||||
state.menu.clone(),
|
||||
Cow::Borrowed(selections),
|
||||
Cow::Borrowed(icons),
|
||||
state.hovered_option.clone(),
|
||||
selected_option,
|
||||
|option| {
|
||||
state.is_open = false;
|
||||
state.is_open.store(false, Ordering::Relaxed);
|
||||
|
||||
(on_selected)(option)
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue