Draft widget operations

This commit is contained in:
Héctor Ramón Jiménez 2022-07-28 02:46:51 +02:00
parent a003e797e8
commit 80688689aa
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
16 changed files with 395 additions and 55 deletions

View file

@ -452,6 +452,7 @@ impl<'a> Step {
.map(Element::from)
.collect()
)
.spacing(10)
]
.padding(20)
.spacing(10);
@ -594,10 +595,7 @@ fn ferris<'a>(width: u16) -> Container<'a, StepMessage> {
if cfg!(target_arch = "wasm32") {
image("tour/images/ferris.png")
} else {
image(format!(
"{}/../../tour/images/ferris.png",
env!("CARGO_MANIFEST_DIR")
))
image(format!("{}/images/ferris.png", env!("CARGO_MANIFEST_DIR")))
}
.width(Length::Units(width)),
)

View file

@ -12,7 +12,7 @@ use iced_winit::futures;
use iced_winit::futures::channel::mpsc;
use iced_winit::renderer;
use iced_winit::user_interface;
use iced_winit::{Clipboard, Debug, Proxy, Settings};
use iced_winit::{Clipboard, Command, Debug, Proxy, Settings};
use glutin::window::Window;
use std::mem::ManuallyDrop;
@ -39,9 +39,9 @@ where
debug.startup_started();
let mut event_loop = EventLoop::with_user_event();
let mut proxy = event_loop.create_proxy();
let proxy = event_loop.create_proxy();
let mut runtime = {
let runtime = {
let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
let proxy = Proxy::new(event_loop.create_proxy());
@ -54,8 +54,6 @@ where
runtime.enter(|| A::new(flags))
};
let subscription = application.subscription();
let context = {
let builder = settings.window.into_builder(
&application.title(),
@ -125,18 +123,6 @@ where
})?
};
let mut clipboard = Clipboard::connect(context.window());
application::run_command(
init_command,
&mut runtime,
&mut clipboard,
&mut proxy,
context.window(),
|| compositor.fetch_information(),
);
runtime.track(subscription);
let (mut sender, receiver) = mpsc::unbounded();
let mut instance = Box::pin(run_instance::<A, E, C>(
@ -144,11 +130,11 @@ where
compositor,
renderer,
runtime,
clipboard,
proxy,
debug,
receiver,
context,
init_command,
settings.exit_on_close_request,
));
@ -196,11 +182,11 @@ async fn run_instance<A, E, C>(
mut compositor: C,
mut renderer: A::Renderer,
mut runtime: Runtime<E, Proxy<A::Message>, A::Message>,
mut clipboard: Clipboard,
mut proxy: glutin::event_loop::EventLoopProxy<A::Message>,
mut debug: Debug,
mut receiver: mpsc::UnboundedReceiver<glutin::event::Event<'_, A::Message>>,
mut context: glutin::ContextWrapper<glutin::PossiblyCurrent, Window>,
init_command: Command<A::Message>,
exit_on_close_request: bool,
) where
A: Application + 'static,
@ -211,9 +197,26 @@ async fn run_instance<A, E, C>(
use glutin::event;
use iced_winit::futures::stream::StreamExt;
let mut clipboard = Clipboard::connect(&context.window());
let mut cache = user_interface::Cache::default();
let mut state = application::State::new(&application, context.window());
let mut viewport_version = state.viewport_version();
application::run_command(
&application,
&mut cache,
&state,
&mut renderer,
init_command,
&mut runtime,
&mut clipboard,
&mut proxy,
&mut debug,
context.window(),
|| compositor.fetch_information(),
);
runtime.track(application.subscription());
let mut user_interface =
ManuallyDrop::new(application::build_user_interface(
&mut application,
@ -258,12 +261,15 @@ async fn run_instance<A, E, C>(
user_interface::State::Outdated
)
{
let cache =
let mut cache =
ManuallyDrop::into_inner(user_interface).into_cache();
// Update application
application::update(
&mut application,
&mut cache,
&state,
&mut renderer,
&mut runtime,
&mut clipboard,
&mut proxy,

View file

@ -3,6 +3,8 @@ mod action;
pub use action::Action;
use crate::widget;
use iced_futures::MaybeSend;
use std::fmt;
@ -24,6 +26,13 @@ impl<T> Command<T> {
Self(iced_futures::Command::single(action))
}
/// Creates a [`Command`] that performs a [`widget::Operation`].
pub fn widget(operation: impl widget::Operation<T> + 'static) -> Self {
Self(iced_futures::Command::single(Action::Widget(
widget::Action::new(operation),
)))
}
/// Creates a [`Command`] that performs the action of the given future.
pub fn perform<A>(
future: impl Future<Output = T> + 'static + MaybeSend,
@ -51,6 +60,7 @@ impl<T> Command<T> {
) -> Command<A>
where
T: 'static,
A: 'static,
{
let Command(command) = self;

View file

@ -1,5 +1,6 @@
use crate::clipboard;
use crate::system;
use crate::widget;
use crate::window;
use iced_futures::MaybeSend;
@ -23,6 +24,9 @@ pub enum Action<T> {
/// Run a system action.
System(system::Action<T>),
/// Run a widget action.
Widget(widget::Action<T>),
}
impl<T> Action<T> {
@ -34,6 +38,7 @@ impl<T> Action<T> {
f: impl Fn(T) -> A + 'static + MaybeSend + Sync,
) -> Action<A>
where
A: 'static,
T: 'static,
{
use iced_futures::futures::FutureExt;
@ -43,6 +48,7 @@ impl<T> Action<T> {
Self::Clipboard(action) => Action::Clipboard(action.map(f)),
Self::Window(window) => Action::Window(window),
Self::System(system) => Action::System(system.map(f)),
Self::Widget(widget) => Action::Widget(widget.map(f)),
}
}
}
@ -56,6 +62,7 @@ impl<T> fmt::Debug for Action<T> {
}
Self::Window(action) => write!(f, "Action::Window({:?})", action),
Self::System(action) => write!(f, "Action::System({:?})", action),
Self::Widget(_action) => write!(f, "Action::Widget"),
}
}
}

View file

@ -32,8 +32,8 @@
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
)]
#![deny(
missing_debug_implementations,
missing_docs,
// missing_debug_implementations,
// missing_docs,
unused_results,
clippy::extra_unused_lifetimes,
clippy::from_over_into,

View file

@ -10,6 +10,7 @@ use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::widget;
use crate::widget::tree::{self, Tree};
use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size};
@ -63,6 +64,14 @@ where
/// Reconciliates the [`Widget`] with the provided [`Tree`].
fn diff(&self, _tree: &mut Tree) {}
/// Applies an [`Operation`] to the [`Widget`].
fn operate(
&self,
_layout: Layout<'_>,
_operation: &mut dyn widget::Operation<Message>,
) {
}
/// Processes a runtime [`Event`].
///
/// It receives:

View file

@ -4,6 +4,7 @@ use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::widget;
use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size, Vector};
/// A generic [`Overlay`].
@ -102,6 +103,15 @@ where
self.overlay
.draw(renderer, theme, style, layout, cursor_position)
}
/// Applies an [`Operation`] to the [`Element`].
pub fn operate(
&self,
layout: Layout<'_>,
operation: &mut dyn widget::Operation<Message>,
) {
self.overlay.operate(layout, operation);
}
}
struct Map<'a, A, B, Renderer> {

View file

@ -479,6 +479,27 @@ where
.unwrap_or(base_interaction)
}
/// Applies a [`widget::Operation`] to the [`UserInterface`].
pub fn operate(
&mut self,
renderer: &Renderer,
operation: &mut dyn widget::Operation<Message>,
) {
self.root
.as_widget()
.operate(Layout::new(&self.base), operation);
if let Some(layout) = self.overlay.as_ref() {
if let Some(overlay) = self.root.as_widget().overlay(
&mut self.state,
Layout::new(&self.base),
renderer,
) {
overlay.operate(Layout::new(layout), operation);
}
}
}
/// Relayouts and returns a new [`UserInterface`] using the provided
/// bounds.
pub fn relayout(self, bounds: Size, renderer: &mut Renderer) -> Self {

View file

@ -17,6 +17,7 @@ pub mod column;
pub mod container;
pub mod helpers;
pub mod image;
pub mod operation;
pub mod pane_grid;
pub mod pick_list;
pub mod progress_bar;
@ -26,6 +27,7 @@ pub mod rule;
pub mod scrollable;
pub mod slider;
pub mod space;
pub mod state;
pub mod svg;
pub mod text;
pub mod text_input;
@ -33,6 +35,9 @@ pub mod toggler;
pub mod tooltip;
pub mod tree;
mod action;
mod id;
#[doc(no_inline)]
pub use button::Button;
#[doc(no_inline)]
@ -76,6 +81,10 @@ pub use tooltip::Tooltip;
#[doc(no_inline)]
pub use tree::Tree;
pub use action::Action;
pub use id::Id;
pub use operation::Operation;
use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
@ -159,6 +168,14 @@ where
/// Reconciliates the [`Widget`] with the provided [`Tree`].
fn diff(&self, _tree: &mut Tree) {}
/// Applies an [`Operation`] to the [`Widget`].
fn operate(
&self,
_layout: Layout<'_>,
_operation: &mut dyn Operation<Message>,
) {
}
/// Processes a runtime [`Event`].
///
/// By default, it does nothing.

View file

@ -0,0 +1,78 @@
use crate::widget::state;
use crate::widget::{Id, Operation};
use iced_futures::MaybeSend;
pub struct Action<T>(Box<dyn Operation<T>>);
impl<T> Action<T> {
pub fn new(operation: impl Operation<T> + 'static) -> Self {
Self(Box::new(operation))
}
pub fn map<A>(
self,
f: impl Fn(T) -> A + 'static + MaybeSend + Sync,
) -> Action<A>
where
T: 'static,
A: 'static,
{
Action(Box::new(Map {
operation: self.0,
f: Box::new(f),
}))
}
pub fn into_operation(self) -> Box<dyn Operation<T>> {
self.0
}
}
struct Map<A, B> {
operation: Box<dyn Operation<A>>,
f: Box<dyn Fn(A) -> B>,
}
impl<A, B> Operation<B> for Map<A, B>
where
A: 'static,
B: 'static,
{
fn container(
&mut self,
id: Option<&Id>,
operate_on_children: &dyn Fn(&mut dyn Operation<B>),
) {
struct MapRef<'a, A, B> {
operation: &'a mut dyn Operation<A>,
f: &'a dyn Fn(A) -> B,
}
impl<'a, A, B> Operation<B> for MapRef<'a, A, B> {
fn container(
&mut self,
id: Option<&Id>,
operate_on_children: &dyn Fn(&mut dyn Operation<B>),
) {
let Self { operation, f } = self;
operation.container(id, &|operation| {
operate_on_children(&mut MapRef { operation, f });
});
}
}
let Self { operation, f } = self;
MapRef {
operation: operation.as_mut(),
f,
}
.container(id, operate_on_children);
}
fn focusable(&mut self, state: &mut dyn state::Focusable, id: Option<&Id>) {
self.operation.focusable(state, id);
}
}

View file

@ -49,8 +49,8 @@ where
/// [`Column`]: widget::Column
pub fn column<Message, Renderer>(
children: Vec<Element<'_, Message, Renderer>>,
) -> widget::Row<'_, Message, Renderer> {
widget::Row::with_children(children)
) -> widget::Column<'_, Message, Renderer> {
widget::Column::with_children(children)
}
/// Creates a new [`Row`] with the given children.

38
native/src/widget/id.rs Normal file
View file

@ -0,0 +1,38 @@
use std::borrow;
use std::sync::atomic::{self, AtomicUsize};
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Id(Internal);
impl Id {
pub fn new(id: impl Into<borrow::Cow<'static, str>>) -> Self {
Self(Internal::Custom(id.into()))
}
pub fn unique() -> Self {
let id = NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed);
Self(Internal::Unique(id))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Internal {
Unique(usize),
Custom(borrow::Cow<'static, str>),
}
#[cfg(test)]
mod tests {
use super::Id;
#[test]
fn unique_generates_different_ids() {
let a = Id::unique();
let b = Id::unique();
assert_ne!(a, b);
}
}

View file

@ -0,0 +1,62 @@
use crate::widget::state;
use crate::widget::Id;
pub trait Operation<T> {
fn container(
&mut self,
id: Option<&Id>,
operate_on_children: &dyn Fn(&mut dyn Operation<T>),
);
fn focusable(
&mut self,
_state: &mut dyn state::Focusable,
_id: Option<&Id>,
) {
}
fn finish(&self) -> Outcome<T> {
Outcome::None
}
}
pub enum Outcome<T> {
None,
Some(T),
Chain(Box<dyn Operation<T>>),
}
pub fn focus<T>(target: Id) -> impl Operation<T> {
struct Focus {
target: Id,
}
impl<T> Operation<T> for Focus {
fn focusable(
&mut self,
state: &mut dyn state::Focusable,
id: Option<&Id>,
) {
if state.is_focused() {
match id {
Some(id) if id == &self.target => {
state.focus();
}
_ => {
state.unfocus();
}
}
}
}
fn container(
&mut self,
_id: Option<&Id>,
operate_on_children: &dyn Fn(&mut dyn Operation<T>),
) {
operate_on_children(self)
}
}
Focus { target }
}

View file

@ -0,0 +1,5 @@
pub trait Focusable {
fn is_focused(&self) -> bool;
fn focus(&mut self);
fn unfocus(&mut self);
}

View file

@ -19,6 +19,7 @@ use crate::mouse::{self, click};
use crate::renderer;
use crate::text::{self, Text};
use crate::touch;
use crate::widget::state;
use crate::widget::tree::{self, Tree};
use crate::{
Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle,
@ -942,6 +943,20 @@ impl State {
}
}
impl state::Focusable for State {
fn is_focused(&self) -> bool {
State::is_focused(self)
}
fn focus(&mut self) {
State::focus(self)
}
fn unfocus(&mut self) {
State::unfocus(self)
}
}
mod platform {
use crate::keyboard;

View file

@ -7,6 +7,7 @@ use crate::clipboard::{self, Clipboard};
use crate::conversion;
use crate::mouse;
use crate::renderer;
use crate::widget::operation;
use crate::{
Command, Debug, Error, Executor, Mode, Proxy, Runtime, Settings, Size,
Subscription,
@ -131,9 +132,9 @@ where
debug.startup_started();
let event_loop = EventLoop::with_user_event();
let mut proxy = event_loop.create_proxy();
let proxy = event_loop.create_proxy();
let mut runtime = {
let runtime = {
let proxy = Proxy::new(event_loop.create_proxy());
let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
@ -146,8 +147,6 @@ where
runtime.enter(|| A::new(flags))
};
let subscription = application.subscription();
let builder = settings.window.into_builder(
&application.title(),
application.mode(),
@ -176,20 +175,8 @@ where
.expect("Append canvas to HTML body");
}
let mut clipboard = Clipboard::connect(&window);
let (compositor, renderer) = C::new(compositor_settings, Some(&window))?;
run_command(
init_command,
&mut runtime,
&mut clipboard,
&mut proxy,
&window,
|| compositor.fetch_information(),
);
runtime.track(subscription);
let (mut sender, receiver) = mpsc::unbounded();
let mut instance = Box::pin(run_instance::<A, E, C>(
@ -197,10 +184,10 @@ where
compositor,
renderer,
runtime,
clipboard,
proxy,
debug,
receiver,
init_command,
window,
settings.exit_on_close_request,
));
@ -247,10 +234,10 @@ async fn run_instance<A, E, C>(
mut compositor: C,
mut renderer: A::Renderer,
mut runtime: Runtime<E, Proxy<A::Message>, A::Message>,
mut clipboard: Clipboard,
mut proxy: winit::event_loop::EventLoopProxy<A::Message>,
mut debug: Debug,
mut receiver: mpsc::UnboundedReceiver<winit::event::Event<'_, A::Message>>,
init_command: Command<A::Message>,
window: winit::window::Window,
exit_on_close_request: bool,
) where
@ -262,6 +249,8 @@ async fn run_instance<A, E, C>(
use iced_futures::futures::stream::StreamExt;
use winit::event;
let mut clipboard = Clipboard::connect(&window);
let mut cache = user_interface::Cache::default();
let mut surface = compositor.create_surface(&window);
let mut state = State::new(&application, &window);
@ -275,9 +264,24 @@ async fn run_instance<A, E, C>(
physical_size.height,
);
run_command(
&application,
&mut cache,
&state,
&mut renderer,
init_command,
&mut runtime,
&mut clipboard,
&mut proxy,
&mut debug,
&window,
|| compositor.fetch_information(),
);
runtime.track(application.subscription());
let mut user_interface = ManuallyDrop::new(build_user_interface(
&mut application,
user_interface::Cache::default(),
cache,
&mut renderer,
state.logical_size(),
&mut debug,
@ -318,12 +322,15 @@ async fn run_instance<A, E, C>(
user_interface::State::Outdated,
)
{
let cache =
let mut cache =
ManuallyDrop::into_inner(user_interface).into_cache();
// Update application
update(
&mut application,
&mut cache,
&state,
&mut renderer,
&mut runtime,
&mut clipboard,
&mut proxy,
@ -515,7 +522,7 @@ pub fn requests_exit(
/// Builds a [`UserInterface`] for the provided [`Application`], logging
/// [`struct@Debug`] information accordingly.
pub fn build_user_interface<'a, A: Application>(
application: &'a mut A,
application: &'a A,
cache: user_interface::Cache,
renderer: &mut A::Renderer,
size: Size,
@ -539,6 +546,9 @@ where
/// resulting [`Command`], and tracking its [`Subscription`].
pub fn update<A: Application, E: Executor>(
application: &mut A,
cache: &mut user_interface::Cache,
state: &State<A>,
renderer: &mut A::Renderer,
runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
clipboard: &mut Clipboard,
proxy: &mut winit::event_loop::EventLoopProxy<A::Message>,
@ -556,7 +566,19 @@ pub fn update<A: Application, E: Executor>(
let command = runtime.enter(|| application.update(message));
debug.update_finished();
run_command(command, runtime, clipboard, proxy, window, graphics_info);
run_command(
application,
cache,
state,
renderer,
command,
runtime,
clipboard,
proxy,
debug,
window,
graphics_info,
);
}
let subscription = application.subscription();
@ -564,14 +586,23 @@ pub fn update<A: Application, E: Executor>(
}
/// Runs the actions of a [`Command`].
pub fn run_command<Message: 'static + std::fmt::Debug + Send, E: Executor>(
command: Command<Message>,
runtime: &mut Runtime<E, Proxy<Message>, Message>,
pub fn run_command<A, E>(
application: &A,
cache: &mut user_interface::Cache,
state: &State<A>,
renderer: &mut A::Renderer,
command: Command<A::Message>,
runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
clipboard: &mut Clipboard,
proxy: &mut winit::event_loop::EventLoopProxy<Message>,
proxy: &mut winit::event_loop::EventLoopProxy<A::Message>,
debug: &mut Debug,
window: &winit::window::Window,
_graphics_info: impl FnOnce() -> compositor::Information + Copy,
) {
) where
A: Application,
E: Executor,
<A::Renderer as crate::Renderer>::Theme: StyleSheet,
{
use iced_native::command;
use iced_native::system;
use iced_native::window;
@ -627,6 +658,39 @@ pub fn run_command<Message: 'static + std::fmt::Debug + Send, E: Executor>(
}
}
},
command::Action::Widget(action) => {
let mut current_cache =
std::mem::replace(cache, user_interface::Cache::default());
let mut current_operation = Some(action.into_operation());
while let Some(mut operation) = current_operation.take() {
let mut user_interface = build_user_interface(
application,
current_cache,
renderer,
state.logical_size(),
debug,
);
user_interface.operate(renderer, operation.as_mut());
current_cache = user_interface.into_cache();
match operation.finish() {
operation::Outcome::None => {}
operation::Outcome::Some(message) => {
proxy
.send_event(message)
.expect("Send message to event loop");
}
operation::Outcome::Chain(next) => {
current_operation = Some(next);
}
}
}
*cache = current_cache;
}
}
}
}