From 462ba3b2c81b630f698f9d927b6f82e500943ea1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 18 Apr 2020 21:29:14 +0200 Subject: Implement and expose `Element::on_event` --- native/src/element.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/native/src/element.rs b/native/src/element.rs index 4e7c7fc6..f29580fc 100644 --- a/native/src/element.rs +++ b/native/src/element.rs @@ -227,6 +227,28 @@ where self.widget.layout(renderer, limits) } + /// Processes a runtime [`Event`]. + /// + /// [`Event`]: enum.Event.html + pub fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + renderer: &Renderer, + clipboard: Option<&dyn Clipboard>, + ) { + self.widget.on_event( + event, + layout, + cursor_position, + messages, + renderer, + clipboard, + ); + } + /// Draws the [`Element`] and its children using the given [`Layout`]. /// /// [`Element`]: struct.Element.html -- cgit From f30043ddc265abd38c74ae1c7df8177d2be66eb0 Mon Sep 17 00:00:00 2001 From: kat <47599466+katktv@users.noreply.github.com> Date: Wed, 22 Apr 2020 15:07:01 +0300 Subject: Fix a typo in ROADMAP --- ROADMAP.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ROADMAP.md b/ROADMAP.md index db0c6625..c47c08ff 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -13,7 +13,7 @@ Once a step is completed, it is collapsed and added to this list: * [x] Scrollables / Clippables ([#24]) * [x] Text input widget ([#25]) * [x] TodoMVC example ([#26]) - * [x] Async actions ([#27]) + * [x] Async actions ([#28]) * [x] Custom layout engine ([#52]) * [x] Event subscriptions ([#122]) * [x] Custom styling ([#146]) -- cgit From 6786b8a3aaf87bd9875b2253ac0986f050cc8445 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 23 Apr 2020 22:17:00 +0200 Subject: Implement `Default` for `Font` --- core/src/font.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/src/font.rs b/core/src/font.rs index be49c825..3f9ad2b5 100644 --- a/core/src/font.rs +++ b/core/src/font.rs @@ -16,3 +16,9 @@ pub enum Font { bytes: &'static [u8], }, } + +impl Default for Font { + fn default() -> Font { + Font::Default + } +} -- cgit From 0300b649d7f99bd63494a9672e3a295bca7ec5d7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 23 Apr 2020 22:17:11 +0200 Subject: Make `Font` an associated type of `text::Renderer` --- native/src/element.rs | 2 +- native/src/renderer/null.rs | 2 ++ native/src/widget/checkbox.rs | 4 ++-- native/src/widget/radio.rs | 4 ++-- native/src/widget/text.rs | 39 +++++++++++++++++++++++---------------- src/widget.rs | 4 ++-- wgpu/src/renderer/widget/text.rs | 2 ++ wgpu/src/widget.rs | 4 ++++ wgpu/src/widget/text.rs | 7 +++++++ 9 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 wgpu/src/widget/text.rs diff --git a/native/src/element.rs b/native/src/element.rs index f29580fc..73e39012 100644 --- a/native/src/element.rs +++ b/native/src/element.rs @@ -81,7 +81,7 @@ where /// /// ``` /// # mod counter { - /// # use iced_native::{text, Text}; + /// # type Text = iced_native::Text; /// # /// # #[derive(Debug, Clone, Copy)] /// # pub enum Message {} diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index 9033a7da..2c7babdb 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -47,6 +47,8 @@ impl row::Renderer for Null { } impl text::Renderer for Null { + type Font = Font; + const DEFAULT_SIZE: u16 = 20; fn measure( diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index d611993f..4d167df7 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -3,7 +3,7 @@ use std::hash::Hash; use crate::{ input::{mouse, ButtonState}, - layout, row, text, Align, Clipboard, Element, Event, Font, Hasher, + layout, row, text, Align, Clipboard, Element, Event, Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, }; @@ -186,7 +186,7 @@ where label_layout.bounds(), &self.label, self.text_size, - Font::Default, + Default::default(), None, HorizontalAlignment::Left, VerticalAlignment::Center, diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index 0ec621bf..a13d86a0 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -1,7 +1,7 @@ //! Create choices using radio buttons. use crate::{ input::{mouse, ButtonState}, - layout, row, text, Align, Clipboard, Element, Event, Font, Hasher, + layout, row, text, Align, Clipboard, Element, Event, Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, }; @@ -155,7 +155,7 @@ where label_layout.bounds(), &self.label, ::DEFAULT_SIZE, - Font::Default, + Default::default(), None, HorizontalAlignment::Left, VerticalAlignment::Center, diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index dc7c33ec..d60aa468 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -1,7 +1,7 @@ //! Write some text for your users to read. use crate::{ - layout, Color, Element, Font, Hasher, HorizontalAlignment, Layout, Length, - Point, Rectangle, Size, VerticalAlignment, Widget, + layout, Color, Element, Hasher, HorizontalAlignment, Layout, Length, Point, + Rectangle, Size, VerticalAlignment, Widget, }; use std::hash::Hash; @@ -11,7 +11,7 @@ use std::hash::Hash; /// # Example /// /// ``` -/// # use iced_native::Text; +/// # type Text = iced_native::Text; /// # /// Text::new("I <3 iced!") /// .color([0.0, 0.0, 1.0]) @@ -20,18 +20,18 @@ use std::hash::Hash; /// /// ![Text drawn by `iced_wgpu`](https://github.com/hecrj/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/text.png?raw=true) #[derive(Debug, Clone)] -pub struct Text { +pub struct Text { content: String, size: Option, color: Option, - font: Font, + font: Renderer::Font, width: Length, height: Length, horizontal_alignment: HorizontalAlignment, vertical_alignment: VerticalAlignment, } -impl Text { +impl Text { /// Create a new fragment of [`Text`] with the given contents. /// /// [`Text`]: struct.Text.html @@ -40,7 +40,7 @@ impl Text { content: label.into(), size: None, color: None, - font: Font::Default, + font: Default::default(), width: Length::Shrink, height: Length::Shrink, horizontal_alignment: HorizontalAlignment::Left, @@ -69,8 +69,8 @@ impl Text { /// /// [`Text`]: struct.Text.html /// [`Font`]: ../../struct.Font.html - pub fn font(mut self, font: Font) -> Self { - self.font = font; + pub fn font(mut self, font: impl Into) -> Self { + self.font = font.into(); self } @@ -112,7 +112,7 @@ impl Text { } } -impl Widget for Text +impl Widget for Text where Renderer: self::Renderer, { @@ -163,7 +163,8 @@ where } fn hash_layout(&self, state: &mut Hasher) { - std::any::TypeId::of::().hash(state); + struct Marker; + std::any::TypeId::of::().hash(state); self.content.hash(state); self.size.hash(state); @@ -181,6 +182,11 @@ where /// [renderer]: ../../renderer/index.html /// [`UserInterface`]: ../../struct.UserInterface.html pub trait Renderer: crate::Renderer { + /// The font type used for [`Text`]. + /// + /// [`Text`]: struct.Text.html + type Font: Default + Copy; + /// The default size of [`Text`]. /// /// [`Text`]: struct.Text.html @@ -194,7 +200,7 @@ pub trait Renderer: crate::Renderer { &self, content: &str, size: u16, - font: Font, + font: Self::Font, bounds: Size, ) -> (f32, f32); @@ -217,18 +223,19 @@ pub trait Renderer: crate::Renderer { bounds: Rectangle, content: &str, size: u16, - font: Font, + font: Self::Font, color: Option, horizontal_alignment: HorizontalAlignment, vertical_alignment: VerticalAlignment, ) -> Self::Output; } -impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> where - Renderer: self::Renderer, + Renderer: self::Renderer + 'a, { - fn from(text: Text) -> Element<'a, Message, Renderer> { + fn from(text: Text) -> Element<'a, Message, Renderer> { Element::new(text) } } diff --git a/src/widget.rs b/src/widget.rs index 03e3192b..e33a6b2c 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -20,7 +20,7 @@ mod platform { pub use iced_wgpu::widget::{ button, checkbox, container, pane_grid, progress_bar, radio, - scrollable, slider, text_input, + scrollable, slider, text_input, Text, }; #[cfg(feature = "canvas")] @@ -39,7 +39,7 @@ mod platform { pub use iced_winit::svg::{Handle, Svg}; } - pub use iced_winit::{Space, Text}; + pub use iced_winit::Space; #[doc(no_inline)] pub use { diff --git a/wgpu/src/renderer/widget/text.rs b/wgpu/src/renderer/widget/text.rs index 80bff574..f27cc430 100644 --- a/wgpu/src/renderer/widget/text.rs +++ b/wgpu/src/renderer/widget/text.rs @@ -7,6 +7,8 @@ use iced_native::{ use std::f32; impl text::Renderer for Renderer { + type Font = Font; + const DEFAULT_SIZE: u16 = 20; fn measure( diff --git a/wgpu/src/widget.rs b/wgpu/src/widget.rs index c3a47dff..32ccad17 100644 --- a/wgpu/src/widget.rs +++ b/wgpu/src/widget.rs @@ -17,6 +17,8 @@ pub mod scrollable; pub mod slider; pub mod text_input; +mod text; + #[doc(no_inline)] pub use button::Button; #[doc(no_inline)] @@ -36,6 +38,8 @@ pub use slider::Slider; #[doc(no_inline)] pub use text_input::TextInput; +pub use text::Text; + #[cfg(feature = "canvas")] #[cfg_attr(docsrs, doc(cfg(feature = "canvas")))] pub mod canvas; diff --git a/wgpu/src/widget/text.rs b/wgpu/src/widget/text.rs new file mode 100644 index 00000000..1053ea97 --- /dev/null +++ b/wgpu/src/widget/text.rs @@ -0,0 +1,7 @@ +//! Write some text for your users to read. +use crate::Renderer; + +/// A paragraph of text. +/// +/// This is an alias of an `iced_native` text with an `iced_wgpu::Renderer`. +pub type Text = iced_native::Text; -- cgit From ce2ed35a1aa2e74ce5417926cc7e56415af36df7 Mon Sep 17 00:00:00 2001 From: Olivier Pinon Date: Sat, 25 Apr 2020 02:03:17 +0200 Subject: #321 Fix async examples by feature-gating Command implementations + Add pokedex example in CI so that at least one async example is runned on CI --- .github/workflows/test.yml | 2 ++ futures/src/command.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 520d8da9..9e73d3d3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,3 +29,5 @@ jobs: run: cargo check --package iced --target wasm32-unknown-unknown - name: Check compilation of `tour` example run: cargo build --package tour --target wasm32-unknown-unknown + - name: Check compilation of `pokedex` example + run: cargo build --package pokedex --target wasm32-unknown-unknown diff --git a/futures/src/command.rs b/futures/src/command.rs index d4f99b82..a4b3d64b 100644 --- a/futures/src/command.rs +++ b/futures/src/command.rs @@ -27,6 +27,7 @@ impl Command { /// Creates a [`Command`] that performs the action of the given future. /// /// [`Command`]: struct.Command.html + #[cfg(not(target_arch = "wasm32"))] pub fn perform( future: impl Future + 'static + Send, f: impl Fn(T) -> A + 'static + Send, @@ -36,9 +37,23 @@ impl Command { } } + /// Creates a [`Command`] that performs the action of the given future. + /// + /// [`Command`]: struct.Command.html + #[cfg(target_arch = "wasm32")] + pub fn perform( + future: impl Future + 'static, + f: impl Fn(T) -> A + 'static + Send, + ) -> Command { + Command { + futures: vec![Box::pin(future.map(f))], + } + } + /// Applies a transformation to the result of a [`Command`]. /// /// [`Command`]: struct.Command.html + #[cfg(not(target_arch = "wasm32"))] pub fn map( mut self, f: impl Fn(T) -> A + 'static + Send + Sync, @@ -62,6 +77,33 @@ impl Command { } } + /// Applies a transformation to the result of a [`Command`]. + /// + /// [`Command`]: struct.Command.html + #[cfg(target_arch = "wasm32")] + pub fn map( + mut self, + f: impl Fn(T) -> A + 'static, + ) -> Command + where + T: 'static, + { + let f = std::sync::Arc::new(f); + + Command { + futures: self + .futures + .drain(..) + .map(|future| { + let f = f.clone(); + + Box::pin(future.map(move |result| f(result))) + as BoxFuture + }) + .collect(), + } + } + /// Creates a [`Command`] that performs the actions of all the given /// commands. /// @@ -85,6 +127,7 @@ impl Command { } } +#[cfg(not(target_arch = "wasm32"))] impl From for Command where A: Future + 'static + Send, @@ -96,6 +139,19 @@ where } } +#[cfg(target_arch = "wasm32")] +impl From for Command +where + A: Future + 'static, +{ + fn from(future: A) -> Self { + Self { + futures: vec![future.boxed_local()], + } + } +} + + impl std::fmt::Debug for Command { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Command").finish() -- cgit From f1e18d09354ec8726954581961e3907ab42b521e Mon Sep 17 00:00:00 2001 From: Olivier Pinon Date: Sat, 25 Apr 2020 02:05:17 +0200 Subject: #321 cargo fmt --- futures/src/command.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/futures/src/command.rs b/futures/src/command.rs index a4b3d64b..25acbda0 100644 --- a/futures/src/command.rs +++ b/futures/src/command.rs @@ -81,10 +81,7 @@ impl Command { /// /// [`Command`]: struct.Command.html #[cfg(target_arch = "wasm32")] - pub fn map( - mut self, - f: impl Fn(T) -> A + 'static, - ) -> Command + pub fn map(mut self, f: impl Fn(T) -> A + 'static) -> Command where T: 'static, { @@ -151,7 +148,6 @@ where } } - impl std::fmt::Debug for Command { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Command").finish() -- cgit From 63f54edf0c4008bb9e4011959863c09e88f4ca77 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 25 Apr 2020 02:29:40 +0200 Subject: Use `Rc` in `Command::map` for Wasm --- futures/src/command.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/futures/src/command.rs b/futures/src/command.rs index 25acbda0..063e9b68 100644 --- a/futures/src/command.rs +++ b/futures/src/command.rs @@ -85,7 +85,7 @@ impl Command { where T: 'static, { - let f = std::sync::Arc::new(f); + let f = std::rc::Rc::new(f); Command { futures: self -- cgit From e87f3acff4a8481611ce09775c6d34a004fab686 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 26 Apr 2020 17:09:03 +0200 Subject: Render meshes after quads in `iced_wgpu` --- wgpu/src/renderer.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 481b310c..ca9364c1 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -339,6 +339,18 @@ impl Renderer { ) { let bounds = layer.bounds * scale_factor; + if !layer.quads.is_empty() { + self.quad_pipeline.draw( + device, + encoder, + &layer.quads, + transformation, + scale_factor, + bounds, + target, + ); + } + if !layer.meshes.is_empty() { let scaled = transformation * Transformation::scale(scale_factor, scale_factor); @@ -355,18 +367,6 @@ impl Renderer { ); } - if !layer.quads.is_empty() { - self.quad_pipeline.draw( - device, - encoder, - &layer.quads, - transformation, - scale_factor, - bounds, - target, - ); - } - #[cfg(any(feature = "image", feature = "svg"))] { if !layer.images.is_empty() { -- cgit From 3f4770fd28bd23fb1560371dc9f81ba09e3a81cc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 26 Apr 2020 17:18:17 +0200 Subject: Bump versions :tada: --- futures/Cargo.toml | 2 +- native/Cargo.toml | 2 +- wgpu/Cargo.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/futures/Cargo.toml b/futures/Cargo.toml index e0815d9d..f0e2a104 100644 --- a/futures/Cargo.toml +++ b/futures/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_futures" -version = "0.1.1" +version = "0.1.2" authors = ["Héctor Ramón Jiménez "] edition = "2018" description = "Commands, subscriptions, and runtimes for Iced" diff --git a/native/Cargo.toml b/native/Cargo.toml index ca58d75c..7e9c2a5a 100644 --- a/native/Cargo.toml +++ b/native/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_native" -version = "0.2.1" +version = "0.2.2" authors = ["Héctor Ramón Jiménez "] edition = "2018" description = "A renderer-agnostic library for native GUIs" diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 0794b970..00f18472 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_wgpu" -version = "0.2.1" +version = "0.2.2" authors = ["Héctor Ramón Jiménez "] edition = "2018" description = "A wgpu renderer for Iced" -- cgit From da2ab420cee41f5570568ce73b7c49613c794c55 Mon Sep 17 00:00:00 2001 From: Ethan Pailes Date: Sun, 26 Apr 2020 14:59:35 -0400 Subject: document that img handle constructors guess fmt This patch documents the fact that a couple of the image handle constructors know how to guess the image format based on the data that they are provided. I had to dig through `iced` sources until I discovered that those routines ultimately boil down to stuff like [image::load_from_memory][1] from the `image` crate, so I thought I would save others the trouble of doing the same reverse-engineering [1]: https://docs.rs/image/0.23.4/image/fn.load_from_memory.html --- native/src/widget/image.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index 6bd0fd68..132f249d 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -123,6 +123,8 @@ pub struct Handle { impl Handle { /// Creates an image [`Handle`] pointing to the image of the given path. /// + /// Makes an educated guess about the image format by examining the data in the file. + /// /// [`Handle`]: struct.Handle.html pub fn from_path>(path: T) -> Handle { Self::from_data(Data::Path(path.into())) @@ -145,6 +147,8 @@ impl Handle { /// Creates an image [`Handle`] containing the image data directly. /// + /// Makes an educated guess about the image format by examining the given data. + /// /// This is useful if you already have your image loaded in-memory, maybe /// because you downloaded or generated it procedurally. /// -- cgit