feat!(segmented_button): Add context menu support and integrations
This commit is contained in:
parent
d54af65a2a
commit
59a913c15d
13 changed files with 612 additions and 118 deletions
|
|
@ -5,11 +5,14 @@ use super::model::{Entity, Model, Selectable};
|
|||
use crate::iced_core::id::Internal;
|
||||
use crate::theme::{SegmentedButton as Style, THEME};
|
||||
use crate::widget::dnd_destination::DragId;
|
||||
use crate::widget::menu::menu_bar::{MenuBar, MenuBarState};
|
||||
use crate::widget::menu::{CloseCondition, ItemHeight, ItemWidth, MenuTree, PathHighlight};
|
||||
use crate::widget::{icon, Icon};
|
||||
use crate::{Element, Renderer};
|
||||
use derive_setters::Setters;
|
||||
use iced::clipboard::dnd::{self, DndAction, DndDestinationRectangle, DndEvent, OfferEvent};
|
||||
use iced::clipboard::mime::AllowedMimeTypes;
|
||||
use iced::touch::Finger;
|
||||
use iced::{
|
||||
alignment, event, keyboard, mouse, touch, Alignment, Background, Color, Command, Event, Length,
|
||||
Padding, Rectangle, Size,
|
||||
|
|
@ -22,6 +25,7 @@ use iced_core::{Border, Gradient, Point, Renderer as IcedRenderer, Shadow, Text}
|
|||
use slotmap::{Key, SecondaryMap};
|
||||
use std::borrow::Cow;
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::collections::HashSet;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::marker::PhantomData;
|
||||
use std::mem;
|
||||
|
|
@ -118,12 +122,18 @@ where
|
|||
/// Style to draw the widget in.
|
||||
#[setters(into)]
|
||||
pub(super) style: Style,
|
||||
/// The context menu to display when a context is activated
|
||||
#[setters(skip)]
|
||||
pub(super) context_menu:
|
||||
Option<Vec<crate::widget::menu::MenuTree<'a, Message, crate::Renderer>>>,
|
||||
/// Emits the ID of the item that was activated.
|
||||
#[setters(skip)]
|
||||
pub(super) on_activate: Option<Box<dyn Fn(Entity) -> Message + 'static>>,
|
||||
#[setters(skip)]
|
||||
pub(super) on_close: Option<Box<dyn Fn(Entity) -> Message + 'static>>,
|
||||
#[setters(skip)]
|
||||
pub(super) on_context: Option<Box<dyn Fn(Entity) -> Message + 'static>>,
|
||||
#[setters(skip)]
|
||||
pub(super) on_dnd_drop:
|
||||
Option<Box<dyn Fn(Entity, Vec<u8>, String, DndAction) -> Message + 'static>>,
|
||||
pub(super) mimes: Vec<String>,
|
||||
|
|
@ -169,8 +179,10 @@ where
|
|||
spacing: 0,
|
||||
line_height: LineHeight::default(),
|
||||
style: Style::default(),
|
||||
context_menu: None,
|
||||
on_activate: None,
|
||||
on_close: None,
|
||||
on_context: None,
|
||||
on_dnd_drop: None,
|
||||
on_dnd_enter: None,
|
||||
on_dnd_leave: None,
|
||||
|
|
@ -180,6 +192,27 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub fn context_menu(
|
||||
mut self,
|
||||
context_menu: Option<Vec<MenuTree<'a, Message, crate::Renderer>>>,
|
||||
) -> Self
|
||||
where
|
||||
Message: 'static,
|
||||
{
|
||||
self.context_menu = context_menu.map(|menus| {
|
||||
vec![MenuTree::with_children(
|
||||
crate::widget::row::<'static, Message>(),
|
||||
menus,
|
||||
)]
|
||||
});
|
||||
|
||||
if let Some(ref mut context_menu) = self.context_menu {
|
||||
context_menu.iter_mut().for_each(MenuTree::set_index);
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
/// Emitted when a tab is pressed.
|
||||
pub fn on_activate<T>(mut self, on_activate: T) -> Self
|
||||
where
|
||||
|
|
@ -198,6 +231,15 @@ where
|
|||
self
|
||||
}
|
||||
|
||||
/// Emitted when a button is right-clicked.
|
||||
pub fn on_context<T>(mut self, on_context: T) -> Self
|
||||
where
|
||||
T: Fn(Entity) -> Message + 'static,
|
||||
{
|
||||
self.on_context = Some(Box::new(on_context));
|
||||
self
|
||||
}
|
||||
|
||||
/// Check if an item is enabled.
|
||||
fn is_enabled(&self, key: Entity) -> bool {
|
||||
self.model.items.get(key).map_or(false, |item| item.enabled)
|
||||
|
|
@ -211,7 +253,7 @@ where
|
|||
self.on_dnd_drop = Some(Box::new(move |entity, data, mime, action| {
|
||||
dnd_drop_handler(entity, D::try_from((data, mime)).ok(), action)
|
||||
}));
|
||||
self.mimes = D::allowed().iter().map(|mime| mime.to_string()).collect();
|
||||
self.mimes = D::allowed().iter().cloned().collect();
|
||||
self
|
||||
}
|
||||
|
||||
|
|
@ -501,20 +543,61 @@ where
|
|||
SelectionMode: Default,
|
||||
Message: 'static + Clone,
|
||||
{
|
||||
fn children(&self) -> Vec<Tree> {
|
||||
let mut children = Vec::new();
|
||||
|
||||
// Assign the context menu's elements as this widget's children.
|
||||
if let Some(ref context_menu) = self.context_menu {
|
||||
let mut tree = Tree::empty();
|
||||
tree.state = tree::State::new(MenuBarState::default());
|
||||
tree.children = context_menu
|
||||
.iter()
|
||||
.map(|root| {
|
||||
let mut tree = Tree::empty();
|
||||
let flat = root
|
||||
.flattern()
|
||||
.iter()
|
||||
.map(|mt| Tree::new(mt.item.as_widget()))
|
||||
.collect();
|
||||
tree.children = flat;
|
||||
tree
|
||||
})
|
||||
.collect();
|
||||
|
||||
children.push(tree);
|
||||
}
|
||||
|
||||
children
|
||||
}
|
||||
|
||||
fn tag(&self) -> tree::Tag {
|
||||
tree::Tag::of::<LocalState>()
|
||||
}
|
||||
|
||||
fn state(&self) -> tree::State {
|
||||
#[allow(clippy::default_trait_access)]
|
||||
tree::State::new(LocalState {
|
||||
paragraphs: SecondaryMap::new(),
|
||||
text_hashes: SecondaryMap::new(),
|
||||
..LocalState::default()
|
||||
buttons_visible: Default::default(),
|
||||
buttons_offset: Default::default(),
|
||||
collapsed: Default::default(),
|
||||
focused: Default::default(),
|
||||
focused_item: Default::default(),
|
||||
hovered: Default::default(),
|
||||
known_length: Default::default(),
|
||||
internal_layout: Default::default(),
|
||||
context_cursor: Point::default(),
|
||||
show_context: Default::default(),
|
||||
wheel_timestamp: Default::default(),
|
||||
dnd_state: Default::default(),
|
||||
fingers_pressed: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
let state = tree.state.downcast_mut::<LocalState>();
|
||||
|
||||
for key in self.model.order.iter().copied() {
|
||||
if let Some(text) = self.model.text.get(key) {
|
||||
let (font, button_state) =
|
||||
|
|
@ -557,6 +640,8 @@ where
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: diff the context menu
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
@ -708,6 +793,20 @@ where
|
|||
}
|
||||
|
||||
if cursor_position.is_over(bounds) {
|
||||
let fingers_pressed = state.fingers_pressed.len();
|
||||
|
||||
match event {
|
||||
Event::Touch(touch::Event::FingerPressed { id, .. }) => {
|
||||
state.fingers_pressed.insert(id);
|
||||
}
|
||||
|
||||
Event::Touch(touch::Event::FingerLifted { id, .. }) => {
|
||||
state.fingers_pressed.remove(&id);
|
||||
}
|
||||
|
||||
_ => (),
|
||||
}
|
||||
|
||||
// Check for clicks on the previous and next tab buttons, when tabs are collapsed.
|
||||
if state.collapsed {
|
||||
// Check if the prev tab button was clicked.
|
||||
|
|
@ -756,15 +855,11 @@ where
|
|||
if let Some(on_close) = self.on_close.as_ref() {
|
||||
if cursor_position
|
||||
.is_over(close_bounds(bounds, f32::from(self.close_icon.size)))
|
||||
&& (left_button_released(&event)
|
||||
|| (touch_lifted(&event) && fingers_pressed == 1))
|
||||
{
|
||||
if let Event::Mouse(mouse::Event::ButtonReleased(
|
||||
mouse::Button::Left,
|
||||
))
|
||||
| Event::Touch(touch::Event::FingerLifted { .. }) = event
|
||||
{
|
||||
shell.publish(on_close(key));
|
||||
return event::Status::Captured;
|
||||
}
|
||||
shell.publish(on_close(key));
|
||||
return event::Status::Captured;
|
||||
}
|
||||
|
||||
// Emit close message if the tab is middle clicked.
|
||||
|
|
@ -786,6 +881,27 @@ where
|
|||
return event::Status::Captured;
|
||||
}
|
||||
}
|
||||
|
||||
// Present a context menu on a right click event.
|
||||
if let Some(on_context) = self.on_context.as_ref() {
|
||||
if right_button_released(&event)
|
||||
|| (touch_lifted(&event) && fingers_pressed == 2)
|
||||
{
|
||||
state.show_context = Some(key);
|
||||
state.context_cursor =
|
||||
cursor_position.position().unwrap_or_default();
|
||||
state.focused = true;
|
||||
state.focused_item = Item::Tab(key);
|
||||
|
||||
let menu_state =
|
||||
tree.children[0].state.downcast_mut::<MenuBarState>();
|
||||
menu_state.open = true;
|
||||
menu_state.view_cursor = cursor_position;
|
||||
|
||||
shell.publish(on_context(key));
|
||||
return event::Status::Captured;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
|
@ -1355,11 +1471,59 @@ where
|
|||
|
||||
fn overlay<'b>(
|
||||
&'b mut self,
|
||||
_tree: &'b mut Tree,
|
||||
_layout: iced_core::Layout<'_>,
|
||||
tree: &'b mut Tree,
|
||||
layout: iced_core::Layout<'_>,
|
||||
_renderer: &Renderer,
|
||||
) -> Option<iced_core::overlay::Element<'b, Message, crate::Theme, Renderer>> {
|
||||
None
|
||||
let state = tree.state.downcast_ref::<LocalState>();
|
||||
|
||||
let Some(entity) = state.show_context else {
|
||||
return None;
|
||||
};
|
||||
|
||||
let bounds = self
|
||||
.variant_bounds(state, layout.bounds())
|
||||
.find_map(|item| match item {
|
||||
ItemBounds::Button(e, bounds) if e == entity => Some(bounds),
|
||||
_ => None,
|
||||
});
|
||||
let Some(mut bounds) = bounds else {
|
||||
return None;
|
||||
};
|
||||
|
||||
let Some(context_menu) = self.context_menu.as_mut() else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if !tree.children[0].state.downcast_ref::<MenuBarState>().open {
|
||||
return None;
|
||||
}
|
||||
|
||||
bounds.x = state.context_cursor.x;
|
||||
bounds.y = state.context_cursor.y;
|
||||
|
||||
Some(
|
||||
crate::widget::menu::Menu {
|
||||
tree: &mut tree.children[0],
|
||||
menu_roots: context_menu,
|
||||
bounds_expand: 16,
|
||||
menu_overlays_parent: true,
|
||||
close_condition: CloseCondition {
|
||||
leave: false,
|
||||
click_outside: true,
|
||||
click_inside: true,
|
||||
},
|
||||
item_width: ItemWidth::Uniform(240),
|
||||
item_height: ItemHeight::Dynamic(40),
|
||||
bar_bounds: bounds,
|
||||
main_offset: -(bounds.height as i32),
|
||||
cross_offset: 0,
|
||||
root_bounds_list: vec![bounds],
|
||||
path_highlight: Some(PathHighlight::MenuActive),
|
||||
style: &crate::theme::menu_bar::MenuBarStyle::Default,
|
||||
}
|
||||
.overlay(),
|
||||
)
|
||||
}
|
||||
|
||||
fn drag_destinations(
|
||||
|
|
@ -1406,7 +1570,6 @@ where
|
|||
}
|
||||
|
||||
/// State that is maintained by each individual widget.
|
||||
#[derive(Default)]
|
||||
pub struct LocalState {
|
||||
/// Defines how many buttons to show at a time.
|
||||
pub(super) buttons_visible: usize,
|
||||
|
|
@ -1428,10 +1591,16 @@ pub struct LocalState {
|
|||
paragraphs: SecondaryMap<Entity, crate::Paragraph>,
|
||||
/// Used to detect changes in text.
|
||||
text_hashes: SecondaryMap<Entity, u64>,
|
||||
/// Location of cursor when context menu was opened.
|
||||
context_cursor: Point,
|
||||
/// Track whether an item is currently showing a context menu.
|
||||
show_context: Option<Entity>,
|
||||
/// Time since last tab activation from wheel movements.
|
||||
wheel_timestamp: Option<Instant>,
|
||||
/// Dnd state
|
||||
pub dnd_state: crate::widget::dnd_destination::State<Entity>,
|
||||
/// Tracks multi-touch events
|
||||
fingers_pressed: HashSet<Finger>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, PartialEq)]
|
||||
|
|
@ -1457,6 +1626,7 @@ impl operation::Focusable for LocalState {
|
|||
fn unfocus(&mut self) {
|
||||
self.focused = false;
|
||||
self.focused_item = Item::None;
|
||||
self.show_context = None;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1550,3 +1720,21 @@ fn draw_icon<Message: 'static>(
|
|||
viewport,
|
||||
);
|
||||
}
|
||||
|
||||
fn left_button_released(event: &Event) -> bool {
|
||||
matches!(
|
||||
event,
|
||||
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left,))
|
||||
)
|
||||
}
|
||||
|
||||
fn right_button_released(event: &Event) -> bool {
|
||||
matches!(
|
||||
event,
|
||||
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Right,))
|
||||
)
|
||||
}
|
||||
|
||||
fn touch_lifted(event: &Event) -> bool {
|
||||
matches!(event, Event::Touch(touch::Event::FingerLifted { .. }))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue