Merge pull request #2544 from iced-rs/display-hover-when-focused

Display top contents in `hover` widget when focused
This commit is contained in:
Héctor Ramón 2024-08-08 01:37:51 +02:00 committed by GitHub
commit 1c8850023f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 258 additions and 71 deletions

View file

@ -304,7 +304,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
self.widget.operate(tree, layout, renderer, operation);
}
@ -440,7 +440,7 @@ where
state: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
self.element
.widget

View file

@ -41,7 +41,7 @@ where
&mut self,
_layout: Layout<'_>,
_renderer: &Renderer,
_operation: &mut dyn widget::Operation<()>,
_operation: &mut dyn widget::Operation,
) {
}

View file

@ -92,7 +92,7 @@ where
&mut self,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
self.overlay.operate(layout, renderer, operation);
}
@ -144,7 +144,7 @@ where
&mut self,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
self.content.operate(layout, renderer, operation);
}

View file

@ -132,7 +132,7 @@ where
&mut self,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
operation.container(None, layout.bounds(), &mut |operation| {
self.children.iter_mut().zip(layout.children()).for_each(

View file

@ -105,7 +105,7 @@ where
_state: &mut Tree,
_layout: Layout<'_>,
_renderer: &Renderer,
_operation: &mut dyn Operation<()>,
_operation: &mut dyn Operation,
) {
}

View file

@ -12,11 +12,12 @@ use crate::{Rectangle, Vector};
use std::any::Any;
use std::fmt;
use std::marker::PhantomData;
use std::sync::Arc;
/// A piece of logic that can traverse the widget tree of an application in
/// order to query or update some widget state.
pub trait Operation<T>: Send {
pub trait Operation<T = ()>: Send {
/// Operates on a widget that contains other widgets.
///
/// The `operate_on_children` function can be called to return control to
@ -53,6 +54,46 @@ pub trait Operation<T>: Send {
}
}
impl<T, O> Operation<O> for Box<T>
where
T: Operation<O> + ?Sized,
{
fn container(
&mut self,
id: Option<&Id>,
bounds: Rectangle,
operate_on_children: &mut dyn FnMut(&mut dyn Operation<O>),
) {
self.as_mut().container(id, bounds, operate_on_children);
}
fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) {
self.as_mut().focusable(state, id);
}
fn scrollable(
&mut self,
state: &mut dyn Scrollable,
id: Option<&Id>,
bounds: Rectangle,
translation: Vector,
) {
self.as_mut().scrollable(state, id, bounds, translation);
}
fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) {
self.as_mut().text_input(state, id);
}
fn custom(&mut self, state: &mut dyn Any, id: Option<&Id>) {
self.as_mut().custom(state, id);
}
fn finish(&self) -> Outcome<O> {
self.as_ref().finish()
}
}
/// The result of an [`Operation`].
pub enum Outcome<T> {
/// The [`Operation`] produced no result.
@ -78,9 +119,62 @@ where
}
}
/// Wraps the [`Operation`] in a black box, erasing its returning type.
pub fn black_box<'a, T, O>(
operation: &'a mut dyn Operation<T>,
) -> impl Operation<O> + 'a
where
T: 'a,
{
struct BlackBox<'a, T> {
operation: &'a mut dyn Operation<T>,
}
impl<'a, T, O> Operation<O> for BlackBox<'a, T> {
fn container(
&mut self,
id: Option<&Id>,
bounds: Rectangle,
operate_on_children: &mut dyn FnMut(&mut dyn Operation<O>),
) {
self.operation.container(id, bounds, &mut |operation| {
operate_on_children(&mut BlackBox { operation });
});
}
fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) {
self.operation.focusable(state, id);
}
fn scrollable(
&mut self,
state: &mut dyn Scrollable,
id: Option<&Id>,
bounds: Rectangle,
translation: Vector,
) {
self.operation.scrollable(state, id, bounds, translation);
}
fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) {
self.operation.text_input(state, id);
}
fn custom(&mut self, state: &mut dyn Any, id: Option<&Id>) {
self.operation.custom(state, id);
}
fn finish(&self) -> Outcome<O> {
Outcome::None
}
}
BlackBox { operation }
}
/// Maps the output of an [`Operation`] using the given function.
pub fn map<A, B>(
operation: Box<dyn Operation<A>>,
operation: impl Operation<A>,
f: impl Fn(A) -> B + Send + Sync + 'static,
) -> impl Operation<B>
where
@ -88,13 +182,14 @@ where
B: 'static,
{
#[allow(missing_debug_implementations)]
struct Map<A, B> {
operation: Box<dyn Operation<A>>,
struct Map<O, A, B> {
operation: O,
f: Arc<dyn Fn(A) -> B + Send + Sync>,
}
impl<A, B> Operation<B> for Map<A, B>
impl<O, A, B> Operation<B> for Map<O, A, B>
where
O: Operation<A>,
A: 'static,
B: 'static,
{
@ -155,10 +250,7 @@ where
let Self { operation, .. } = self;
MapRef {
operation: operation.as_mut(),
}
.container(id, bounds, operate_on_children);
MapRef { operation }.container(id, bounds, operate_on_children);
}
fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) {
@ -201,6 +293,87 @@ where
}
}
/// Chains the output of an [`Operation`] with the provided function to
/// build a new [`Operation`].
pub fn chain<A, B, O>(
operation: impl Operation<A> + 'static,
f: fn(A) -> O,
) -> impl Operation<B>
where
A: 'static,
B: Send + 'static,
O: Operation<B> + 'static,
{
struct Chain<T, O, A, B>
where
T: Operation<A>,
O: Operation<B>,
{
operation: T,
next: fn(A) -> O,
_result: PhantomData<B>,
}
impl<T, O, A, B> Operation<B> for Chain<T, O, A, B>
where
T: Operation<A> + 'static,
O: Operation<B> + 'static,
A: 'static,
B: Send + 'static,
{
fn container(
&mut self,
id: Option<&Id>,
bounds: Rectangle,
operate_on_children: &mut dyn FnMut(&mut dyn Operation<B>),
) {
self.operation.container(id, bounds, &mut |operation| {
operate_on_children(&mut black_box(operation));
});
}
fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) {
self.operation.focusable(state, id);
}
fn scrollable(
&mut self,
state: &mut dyn Scrollable,
id: Option<&Id>,
bounds: Rectangle,
translation: crate::Vector,
) {
self.operation.scrollable(state, id, bounds, translation);
}
fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) {
self.operation.text_input(state, id);
}
fn custom(&mut self, state: &mut dyn std::any::Any, id: Option<&Id>) {
self.operation.custom(state, id);
}
fn finish(&self) -> Outcome<B> {
match self.operation.finish() {
Outcome::None => Outcome::None,
Outcome::Some(value) => {
Outcome::Chain(Box::new((self.next)(value)))
}
Outcome::Chain(operation) => {
Outcome::Chain(Box::new(chain(operation, self.next)))
}
}
}
}
Chain {
operation,
next: f,
_result: PhantomData,
}
}
/// Produces an [`Operation`] that applies the given [`Operation`] to the
/// children of a container with the given [`Id`].
pub fn scope<T: 'static>(

View file

@ -1,5 +1,5 @@
//! Operate on widgets that can be focused.
use crate::widget::operation::{Operation, Outcome};
use crate::widget::operation::{self, Operation, Outcome};
use crate::widget::Id;
use crate::Rectangle;
@ -58,19 +58,12 @@ pub fn focus<T>(target: Id) -> impl Operation<T> {
/// Produces an [`Operation`] that generates a [`Count`] and chains it with the
/// provided function to build a new [`Operation`].
pub fn count<T, O>(f: fn(Count) -> O) -> impl Operation<T>
where
O: Operation<T> + 'static,
{
struct CountFocusable<O> {
pub fn count() -> impl Operation<Count> {
struct CountFocusable {
count: Count,
next: fn(Count) -> O,
}
impl<T, O> Operation<T> for CountFocusable<O>
where
O: Operation<T> + 'static,
{
impl Operation<Count> for CountFocusable {
fn focusable(&mut self, state: &mut dyn Focusable, _id: Option<&Id>) {
if state.is_focused() {
self.count.focused = Some(self.count.total);
@ -83,26 +76,25 @@ where
&mut self,
_id: Option<&Id>,
_bounds: Rectangle,
operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
operate_on_children: &mut dyn FnMut(&mut dyn Operation<Count>),
) {
operate_on_children(self);
}
fn finish(&self) -> Outcome<T> {
Outcome::Chain(Box::new((self.next)(self.count)))
fn finish(&self) -> Outcome<Count> {
Outcome::Some(self.count)
}
}
CountFocusable {
count: Count::default(),
next: f,
}
}
/// Produces an [`Operation`] that searches for the current focused widget, and
/// - if found, focuses the previous focusable widget.
/// - if not found, focuses the last focusable widget.
pub fn focus_previous<T>() -> impl Operation<T> {
pub fn focus_previous() -> impl Operation {
struct FocusPrevious {
count: Count,
current: usize,
@ -136,13 +128,13 @@ pub fn focus_previous<T>() -> impl Operation<T> {
}
}
count(|count| FocusPrevious { count, current: 0 })
operation::chain(count(), |count| FocusPrevious { count, current: 0 })
}
/// Produces an [`Operation`] that searches for the current focused widget, and
/// - if found, focuses the next focusable widget.
/// - if not found, focuses the first focusable widget.
pub fn focus_next<T>() -> impl Operation<T> {
pub fn focus_next() -> impl Operation {
struct FocusNext {
count: Count,
current: usize,
@ -170,7 +162,7 @@ pub fn focus_next<T>() -> impl Operation<T> {
}
}
count(|count| FocusNext { count, current: 0 })
operation::chain(count(), |count| FocusNext { count, current: 0 })
}
/// Produces an [`Operation`] that searches for the current focused widget

View file

@ -347,7 +347,7 @@ mod toast {
state: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<()>,
operation: &mut dyn Operation,
) {
operation.container(None, layout.bounds(), &mut |operation| {
self.content.as_widget().operate(
@ -589,7 +589,7 @@ mod toast {
&mut self,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
operation.container(None, layout.bounds(), &mut |operation| {
self.toasts

View file

@ -59,7 +59,7 @@ pub enum Action<T> {
},
/// Run a widget operation.
Widget(Box<dyn widget::Operation<()>>),
Widget(Box<dyn widget::Operation>),
/// Run a clipboard action.
Clipboard(clipboard::Action),
@ -79,7 +79,7 @@ pub enum Action<T> {
impl<T> Action<T> {
/// Creates a new [`Action::Widget`] with the given [`widget::Operation`].
pub fn widget(operation: impl widget::Operation<()> + 'static) -> Self {
pub fn widget(operation: impl widget::Operation + 'static) -> Self {
Self::Widget(Box::new(operation))
}

View file

@ -205,7 +205,7 @@ where
pub fn operate(
&mut self,
renderer: &mut P::Renderer,
operations: impl Iterator<Item = Box<dyn Operation<()>>>,
operations: impl Iterator<Item = Box<dyn Operation>>,
bounds: Size,
debug: &mut Debug,
) {

View file

@ -131,13 +131,13 @@ where
&mut self,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
fn recurse<Message, Theme, Renderer>(
element: &mut overlay::Element<'_, Message, Theme, Renderer>,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) where
Renderer: renderer::Renderer,
{

View file

@ -178,7 +178,7 @@ where
pub fn operate(
&mut self,
renderer: &mut P::Renderer,
operations: impl Iterator<Item = Box<dyn Operation<()>>>,
operations: impl Iterator<Item = Box<dyn Operation>>,
bounds: Size,
debug: &mut Debug,
) {

View file

@ -566,7 +566,7 @@ where
pub fn operate(
&mut self,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
self.root.as_widget().operate(
&mut self.state,

View file

@ -236,7 +236,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<()>,
operation: &mut dyn Operation,
) {
operation.container(None, layout.bounds(), &mut |operation| {
self.content.as_widget().operate(

View file

@ -222,7 +222,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<()>,
operation: &mut dyn Operation,
) {
operation.container(None, layout.bounds(), &mut |operation| {
self.children

View file

@ -245,7 +245,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<()>,
operation: &mut dyn Operation,
) {
operation.container(
self.id.as_ref().map(|id| &id.0),

View file

@ -4,7 +4,8 @@ use crate::checkbox::{self, Checkbox};
use crate::combo_box::{self, ComboBox};
use crate::container::{self, Container};
use crate::core;
use crate::core::widget::operation;
use crate::core::widget::operation::{self, Operation};
use crate::core::window;
use crate::core::{Element, Length, Pixels, Widget};
use crate::keyed;
use crate::overlay;
@ -289,7 +290,7 @@ where
state: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn operation::Operation<()>,
operation: &mut dyn operation::Operation,
) {
self.content
.as_widget()
@ -397,6 +398,7 @@ where
struct Hover<'a, Message, Theme, Renderer> {
base: Element<'a, Message, Theme, Renderer>,
top: Element<'a, Message, Theme, Renderer>,
is_top_focused: bool,
is_top_overlay_active: bool,
}
@ -472,7 +474,9 @@ where
viewport,
);
if cursor.is_over(layout.bounds()) || self.is_top_overlay_active
if cursor.is_over(layout.bounds())
|| self.is_top_focused
|| self.is_top_overlay_active
{
let (top_layout, top_tree) = children.next().unwrap();
@ -491,7 +495,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn operation::Operation<()>,
operation: &mut dyn operation::Operation,
) {
let children = [&self.base, &self.top]
.into_iter()
@ -515,6 +519,24 @@ where
) -> event::Status {
let mut children = layout.children().zip(&mut tree.children);
let (base_layout, base_tree) = children.next().unwrap();
let (top_layout, top_tree) = children.next().unwrap();
if matches!(event, Event::Window(window::Event::RedrawRequested(_)))
{
let mut count_focused = operation::focusable::count();
self.top.as_widget_mut().operate(
top_tree,
top_layout,
renderer,
&mut operation::black_box(&mut count_focused),
);
self.is_top_focused = match count_focused.finish() {
operation::Outcome::Some(count) => count.focused.is_some(),
_ => false,
};
}
let top_status = if matches!(
event,
@ -523,10 +545,9 @@ where
| mouse::Event::ButtonReleased(_)
)
) || cursor.is_over(layout.bounds())
|| self.is_top_focused
|| self.is_top_overlay_active
{
let (top_layout, top_tree) = children.next().unwrap();
self.top.as_widget_mut().on_event(
top_tree,
event.clone(),
@ -612,6 +633,7 @@ where
Element::new(Hover {
base: base.into(),
top: top.into(),
is_top_focused: false,
is_top_overlay_active: false,
})
}

View file

@ -265,7 +265,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<()>,
operation: &mut dyn Operation,
) {
operation.container(None, layout.bounds(), &mut |operation| {
self.children

View file

@ -182,7 +182,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
self.with_element(|element| {
element.as_widget().operate(

View file

@ -59,7 +59,7 @@ pub trait Component<Message, Theme = crate::Theme, Renderer = crate::Renderer> {
fn operate(
&self,
_state: &mut Self::State,
_operation: &mut dyn widget::Operation<()>,
_operation: &mut dyn widget::Operation,
) {
}
@ -172,7 +172,7 @@ where
fn rebuild_element_with_operation(
&self,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
let heads = self.state.borrow_mut().take().unwrap().into_heads();
@ -358,7 +358,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
self.rebuild_element_with_operation(operation);

View file

@ -161,7 +161,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
let state = tree.state.downcast_mut::<State>();
let mut content = self.content.borrow_mut();

View file

@ -178,7 +178,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<()>,
operation: &mut dyn Operation,
) {
self.content.as_widget().operate(
&mut tree.children[0],

View file

@ -324,7 +324,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
operation.container(None, layout.bounds(), &mut |operation| {
self.contents

View file

@ -214,7 +214,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
let body_layout = if let Some(title_bar) = &self.title_bar {
let mut children = layout.children();

View file

@ -278,7 +278,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
let mut children = layout.children();
let padded = children.next().unwrap();

View file

@ -218,7 +218,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<()>,
operation: &mut dyn Operation,
) {
operation.container(None, layout.bounds(), &mut |operation| {
self.children
@ -470,7 +470,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<()>,
operation: &mut dyn Operation,
) {
self.row.operate(tree, layout, renderer, operation);
}

View file

@ -415,7 +415,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<()>,
operation: &mut dyn Operation,
) {
let state = tree.state.downcast_mut::<State>();

View file

@ -189,7 +189,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<()>,
operation: &mut dyn Operation,
) {
operation.container(None, layout.bounds(), &mut |operation| {
self.children

View file

@ -885,7 +885,7 @@ where
tree: &mut widget::Tree,
_layout: Layout<'_>,
_renderer: &Renderer,
operation: &mut dyn widget::Operation<()>,
operation: &mut dyn widget::Operation,
) {
let state = tree.state.downcast_mut::<State<Highlighter>>();

View file

@ -542,7 +542,7 @@ where
tree: &mut Tree,
_layout: Layout<'_>,
_renderer: &Renderer,
operation: &mut dyn Operation<()>,
operation: &mut dyn Operation,
) {
let state = tree.state.downcast_mut::<State<Renderer::Paragraph>>();

View file

@ -104,7 +104,7 @@ where
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<()>,
operation: &mut dyn Operation,
) {
self.content
.as_widget()
@ -236,7 +236,7 @@ where
&mut self,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<()>,
operation: &mut dyn Operation,
) {
self.content.operate(layout, renderer, operation);
}