From 5100b5d0a1f654ec1254b7765ceadfb9091d6939 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 24 Feb 2023 23:24:48 +0100 Subject: Introduce `iced_renderer` subcrate featuring runtime renderer fallback --- src/application.rs | 4 ++-- src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/application.rs b/src/application.rs index 9a1c1855..b9871556 100644 --- a/src/application.rs +++ b/src/application.rs @@ -198,11 +198,11 @@ pub trait Application: Sized { default_font: settings.default_font, default_text_size: settings.default_text_size, antialiasing: if settings.antialiasing { - Some(crate::renderer::settings::Antialiasing::MSAAx4) + Some(crate::renderer::Antialiasing::MSAAx4) } else { None }, - ..crate::renderer::Settings::from_env() + ..crate::renderer::Settings::default() }; Ok(crate::runtime::application::run::< diff --git a/src/lib.rs b/src/lib.rs index 31ec4f48..bb162f2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -182,7 +182,7 @@ pub mod touch; pub mod widget; pub mod window; -use iced_wgpu as renderer; +use iced_renderer as renderer; use iced_winit as runtime; pub use iced_native::theme; -- cgit From 3f6e28fa9b1b8d911f765c9efb5249a9e0c942d5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Feb 2023 20:47:13 +0100 Subject: Use `iced_renderer` instead of `iced_graphics` in root crate --- src/error.rs | 2 +- src/widget.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/error.rs b/src/error.rs index 0bfa3ff1..5326718f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -13,7 +13,7 @@ pub enum Error { /// The application graphics context could not be created. #[error("the application graphics context could not be created")] - GraphicsCreationFailed(iced_graphics::Error), + GraphicsCreationFailed(iced_renderer::Error), } impl From for Error { diff --git a/src/widget.rs b/src/widget.rs index e2b0537e..04cb0e50 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -165,7 +165,7 @@ pub use vertical_slider::VerticalSlider; #[cfg(feature = "canvas")] #[cfg_attr(docsrs, doc(cfg(feature = "canvas")))] -pub use iced_graphics::widget::canvas; +pub use iced_renderer::widget::canvas; #[cfg(feature = "canvas")] #[cfg_attr(docsrs, doc(cfg(feature = "canvas")))] @@ -192,7 +192,7 @@ pub mod image { #[cfg(feature = "qr_code")] #[cfg_attr(docsrs, doc(cfg(feature = "qr_code")))] -pub use iced_graphics::widget::qr_code; +pub use iced_renderer::widget::qr_code; #[cfg(feature = "svg")] #[cfg_attr(docsrs, doc(cfg(feature = "svg")))] -- cgit From 5fd5d1cdf8e5354788dc40729c4565ef377d3bba Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 1 Mar 2023 21:34:26 +0100 Subject: Implement `Canvas` support for `iced_tiny_skia` --- src/widget.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/widget.rs b/src/widget.rs index 04cb0e50..f3a66101 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -170,9 +170,10 @@ pub use iced_renderer::widget::canvas; #[cfg(feature = "canvas")] #[cfg_attr(docsrs, doc(cfg(feature = "canvas")))] /// Creates a new [`Canvas`]. -pub fn canvas(program: P) -> Canvas +pub fn canvas(program: P) -> Canvas where - P: canvas::Program, + Renderer: canvas::Renderer, + P: canvas::Program, { Canvas::new(program) } -- cgit From 6cc48b5c62bac287b8f9f1c79c1fb7486c51b18f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 3 Mar 2023 04:57:55 +0100 Subject: Move `Canvas` and `QRCode` to `iced` crate Rename `canvas` modules to `geometry` in graphics subcrates --- src/widget.rs | 8 +- src/widget/canvas.rs | 238 ++++++++++++++++++++++++++++++++++ src/widget/canvas/cursor.rs | 64 +++++++++ src/widget/canvas/event.rs | 21 +++ src/widget/canvas/program.rs | 108 ++++++++++++++++ src/widget/qr_code.rs | 300 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 735 insertions(+), 4 deletions(-) create mode 100644 src/widget/canvas.rs create mode 100644 src/widget/canvas/cursor.rs create mode 100644 src/widget/canvas/event.rs create mode 100644 src/widget/canvas/program.rs create mode 100644 src/widget/qr_code.rs (limited to 'src') diff --git a/src/widget.rs b/src/widget.rs index f3a66101..19434e84 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -165,14 +165,14 @@ pub use vertical_slider::VerticalSlider; #[cfg(feature = "canvas")] #[cfg_attr(docsrs, doc(cfg(feature = "canvas")))] -pub use iced_renderer::widget::canvas; +pub mod canvas; #[cfg(feature = "canvas")] #[cfg_attr(docsrs, doc(cfg(feature = "canvas")))] /// Creates a new [`Canvas`]. -pub fn canvas(program: P) -> Canvas +pub fn canvas(program: P) -> Canvas where - Renderer: canvas::Renderer, + Renderer: iced_renderer::geometry::Renderer, P: canvas::Program, { Canvas::new(program) @@ -193,7 +193,7 @@ pub mod image { #[cfg(feature = "qr_code")] #[cfg_attr(docsrs, doc(cfg(feature = "qr_code")))] -pub use iced_renderer::widget::qr_code; +pub mod qr_code; #[cfg(feature = "svg")] #[cfg_attr(docsrs, doc(cfg(feature = "svg")))] diff --git a/src/widget/canvas.rs b/src/widget/canvas.rs new file mode 100644 index 00000000..bc5995c6 --- /dev/null +++ b/src/widget/canvas.rs @@ -0,0 +1,238 @@ +//! Draw 2D graphics for your users. +pub mod event; + +mod cursor; +mod program; + +pub use cursor::Cursor; +pub use event::Event; +pub use program::Program; + +pub use iced_renderer::geometry::*; + +use crate::{Length, Point, Rectangle, Size, Vector}; + +use iced_native::layout::{self, Layout}; +use iced_native::mouse; +use iced_native::renderer; +use iced_native::widget::tree::{self, Tree}; +use iced_native::{Clipboard, Element, Shell, Widget}; + +use std::marker::PhantomData; + +/// A widget capable of drawing 2D graphics. +/// +/// ## Drawing a simple circle +/// If you want to get a quick overview, here's how we can draw a simple circle: +/// +/// ```no_run +/// use iced::widget::canvas::{self, Canvas, Cursor, Fill, Frame, Geometry, Path, Program}; +/// use iced::{Color, Rectangle, Theme, Renderer}; +/// +/// // First, we define the data we need for drawing +/// #[derive(Debug)] +/// struct Circle { +/// radius: f32, +/// } +/// +/// // Then, we implement the `Program` trait +/// impl Program<()> for Circle { +/// type State = (); +/// +/// fn draw(&self, _state: &(), renderer: &Renderer, _theme: &Theme, bounds: Rectangle, _cursor: Cursor) -> Vec{ +/// // We prepare a new `Frame` +/// let mut frame = Frame::new(renderer, bounds.size()); +/// +/// // We create a `Path` representing a simple circle +/// let circle = Path::circle(frame.center(), self.radius); +/// +/// // And fill it with some color +/// frame.fill(&circle, Color::BLACK); +/// +/// // Finally, we produce the geometry +/// vec![frame.into_geometry()] +/// } +/// } +/// +/// // Finally, we simply use our `Circle` to create the `Canvas`! +/// let canvas = Canvas::new(Circle { radius: 50.0 }); +/// ``` +#[derive(Debug)] +pub struct Canvas +where + Renderer: iced_renderer::geometry::Renderer, + P: Program, +{ + width: Length, + height: Length, + program: P, + message_: PhantomData, + theme_: PhantomData, +} + +impl Canvas +where + Renderer: iced_renderer::geometry::Renderer, + P: Program, +{ + const DEFAULT_SIZE: f32 = 100.0; + + /// Creates a new [`Canvas`]. + pub fn new(program: P) -> Self { + Canvas { + width: Length::Fixed(Self::DEFAULT_SIZE), + height: Length::Fixed(Self::DEFAULT_SIZE), + program, + message_: PhantomData, + theme_: PhantomData, + } + } + + /// Sets the width of the [`Canvas`]. + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); + self + } + + /// Sets the height of the [`Canvas`]. + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into(); + self + } +} + +impl Widget + for Canvas +where + Renderer: iced_renderer::geometry::Renderer, + P: Program, +{ + fn tag(&self) -> tree::Tag { + struct Tag(T); + tree::Tag::of::>() + } + + fn state(&self) -> tree::State { + tree::State::new(P::State::default()) + } + + fn width(&self) -> Length { + self.width + } + + fn height(&self) -> Length { + self.height + } + + fn layout( + &self, + _renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + let limits = limits.width(self.width).height(self.height); + let size = limits.resolve(Size::ZERO); + + layout::Node::new(size) + } + + fn on_event( + &mut self, + tree: &mut Tree, + event: iced_native::Event, + layout: Layout<'_>, + cursor_position: Point, + _renderer: &Renderer, + _clipboard: &mut dyn Clipboard, + shell: &mut Shell<'_, Message>, + ) -> event::Status { + let bounds = layout.bounds(); + + let canvas_event = match event { + iced_native::Event::Mouse(mouse_event) => { + Some(Event::Mouse(mouse_event)) + } + iced_native::Event::Touch(touch_event) => { + Some(Event::Touch(touch_event)) + } + iced_native::Event::Keyboard(keyboard_event) => { + Some(Event::Keyboard(keyboard_event)) + } + _ => None, + }; + + let cursor = Cursor::from_window_position(cursor_position); + + if let Some(canvas_event) = canvas_event { + let state = tree.state.downcast_mut::(); + + let (event_status, message) = + self.program.update(state, canvas_event, bounds, cursor); + + if let Some(message) = message { + shell.publish(message); + } + + return event_status; + } + + event::Status::Ignored + } + + fn mouse_interaction( + &self, + tree: &Tree, + layout: Layout<'_>, + cursor_position: Point, + _viewport: &Rectangle, + _renderer: &Renderer, + ) -> mouse::Interaction { + let bounds = layout.bounds(); + let cursor = Cursor::from_window_position(cursor_position); + let state = tree.state.downcast_ref::(); + + self.program.mouse_interaction(state, bounds, cursor) + } + + fn draw( + &self, + tree: &Tree, + renderer: &mut Renderer, + theme: &Renderer::Theme, + _style: &renderer::Style, + layout: Layout<'_>, + cursor_position: Point, + _viewport: &Rectangle, + ) { + let bounds = layout.bounds(); + + if bounds.width < 1.0 || bounds.height < 1.0 { + return; + } + + let cursor = Cursor::from_window_position(cursor_position); + let state = tree.state.downcast_ref::(); + + renderer.with_translation( + Vector::new(bounds.x, bounds.y), + |renderer| { + renderer.draw( + self.program.draw(state, renderer, theme, bounds, cursor), + ); + }, + ); + } +} + +impl<'a, P, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Message: 'a, + Renderer: 'a + iced_renderer::geometry::Renderer, + P: Program + 'a, +{ + fn from( + canvas: Canvas, + ) -> Element<'a, Message, Renderer> { + Element::new(canvas) + } +} diff --git a/src/widget/canvas/cursor.rs b/src/widget/canvas/cursor.rs new file mode 100644 index 00000000..ef6a7771 --- /dev/null +++ b/src/widget/canvas/cursor.rs @@ -0,0 +1,64 @@ +use crate::{Point, Rectangle}; + +/// The mouse cursor state. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Cursor { + /// The cursor has a defined position. + Available(Point), + + /// The cursor is currently unavailable (i.e. out of bounds or busy). + Unavailable, +} + +impl Cursor { + // TODO: Remove this once this type is used in `iced_native` to encode + // proper cursor availability + pub(crate) fn from_window_position(position: Point) -> Self { + if position.x < 0.0 || position.y < 0.0 { + Cursor::Unavailable + } else { + Cursor::Available(position) + } + } + + /// Returns the absolute position of the [`Cursor`], if available. + pub fn position(&self) -> Option { + match self { + Cursor::Available(position) => Some(*position), + Cursor::Unavailable => None, + } + } + + /// Returns the relative position of the [`Cursor`] inside the given bounds, + /// if available. + /// + /// If the [`Cursor`] is not over the provided bounds, this method will + /// return `None`. + pub fn position_in(&self, bounds: &Rectangle) -> Option { + if self.is_over(bounds) { + self.position_from(bounds.position()) + } else { + None + } + } + + /// Returns the relative position of the [`Cursor`] from the given origin, + /// if available. + pub fn position_from(&self, origin: Point) -> Option { + match self { + Cursor::Available(position) => { + Some(Point::new(position.x - origin.x, position.y - origin.y)) + } + Cursor::Unavailable => None, + } + } + + /// Returns whether the [`Cursor`] is currently over the provided bounds + /// or not. + pub fn is_over(&self, bounds: &Rectangle) -> bool { + match self { + Cursor::Available(position) => bounds.contains(*position), + Cursor::Unavailable => false, + } + } +} diff --git a/src/widget/canvas/event.rs b/src/widget/canvas/event.rs new file mode 100644 index 00000000..7c733a4d --- /dev/null +++ b/src/widget/canvas/event.rs @@ -0,0 +1,21 @@ +//! Handle events of a canvas. +use iced_native::keyboard; +use iced_native::mouse; +use iced_native::touch; + +pub use iced_native::event::Status; + +/// A [`Canvas`] event. +/// +/// [`Canvas`]: crate::widget::Canvas +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Event { + /// A mouse event. + Mouse(mouse::Event), + + /// A touch event. + Touch(touch::Event), + + /// A keyboard event. + Keyboard(keyboard::Event), +} diff --git a/src/widget/canvas/program.rs b/src/widget/canvas/program.rs new file mode 100644 index 00000000..fd15663a --- /dev/null +++ b/src/widget/canvas/program.rs @@ -0,0 +1,108 @@ +use crate::widget::canvas::event::{self, Event}; +use crate::widget::canvas::mouse; +use crate::widget::canvas::Cursor; +use crate::Rectangle; + +/// The state and logic of a [`Canvas`]. +/// +/// A [`Program`] can mutate internal state and produce messages for an +/// application. +/// +/// [`Canvas`]: crate::widget::Canvas +pub trait Program +where + Renderer: iced_renderer::geometry::Renderer, +{ + /// The internal state mutated by the [`Program`]. + type State: Default + 'static; + + /// Updates the [`State`](Self::State) of the [`Program`]. + /// + /// When a [`Program`] is used in a [`Canvas`], the runtime will call this + /// method for each [`Event`]. + /// + /// This method can optionally return a `Message` to notify an application + /// of any meaningful interactions. + /// + /// By default, this method does and returns nothing. + /// + /// [`Canvas`]: crate::widget::Canvas + fn update( + &self, + _state: &mut Self::State, + _event: Event, + _bounds: Rectangle, + _cursor: Cursor, + ) -> (event::Status, Option) { + (event::Status::Ignored, None) + } + + /// Draws the state of the [`Program`], producing a bunch of [`Geometry`]. + /// + /// [`Geometry`] can be easily generated with a [`Frame`] or stored in a + /// [`Cache`]. + /// + /// [`Frame`]: crate::widget::canvas::Frame + /// [`Cache`]: crate::widget::canvas::Cache + fn draw( + &self, + state: &Self::State, + renderer: &Renderer, + theme: &Renderer::Theme, + bounds: Rectangle, + cursor: Cursor, + ) -> Vec; + + /// Returns the current mouse interaction of the [`Program`]. + /// + /// The interaction returned will be in effect even if the cursor position + /// is out of bounds of the program's [`Canvas`]. + /// + /// [`Canvas`]: crate::widget::Canvas + fn mouse_interaction( + &self, + _state: &Self::State, + _bounds: Rectangle, + _cursor: Cursor, + ) -> mouse::Interaction { + mouse::Interaction::default() + } +} + +impl Program for &T +where + Renderer: iced_renderer::geometry::Renderer, + T: Program, +{ + type State = T::State; + + fn update( + &self, + state: &mut Self::State, + event: Event, + bounds: Rectangle, + cursor: Cursor, + ) -> (event::Status, Option) { + T::update(self, state, event, bounds, cursor) + } + + fn draw( + &self, + state: &Self::State, + renderer: &Renderer, + theme: &Renderer::Theme, + bounds: Rectangle, + cursor: Cursor, + ) -> Vec { + T::draw(self, state, renderer, theme, bounds, cursor) + } + + fn mouse_interaction( + &self, + state: &Self::State, + bounds: Rectangle, + cursor: Cursor, + ) -> mouse::Interaction { + T::mouse_interaction(self, state, bounds, cursor) + } +} diff --git a/src/widget/qr_code.rs b/src/widget/qr_code.rs new file mode 100644 index 00000000..66442d5d --- /dev/null +++ b/src/widget/qr_code.rs @@ -0,0 +1,300 @@ +//! Encode and display information in a QR code. +use crate::widget::canvas; +use crate::Renderer; + +use iced_native::layout; +use iced_native::renderer; +use iced_native::widget::Tree; +use iced_native::{ + Color, Element, Layout, Length, Point, Rectangle, Size, Vector, Widget, +}; +use thiserror::Error; + +const DEFAULT_CELL_SIZE: u16 = 4; +const QUIET_ZONE: usize = 2; + +/// A type of matrix barcode consisting of squares arranged in a grid which +/// can be read by an imaging device, such as a camera. +#[derive(Debug)] +pub struct QRCode<'a> { + state: &'a State, + dark: Color, + light: Color, + cell_size: u16, +} + +impl<'a> QRCode<'a> { + /// Creates a new [`QRCode`] with the provided [`State`]. + pub fn new(state: &'a State) -> Self { + Self { + cell_size: DEFAULT_CELL_SIZE, + dark: Color::BLACK, + light: Color::WHITE, + state, + } + } + + /// Sets both the dark and light [`Color`]s of the [`QRCode`]. + pub fn color(mut self, dark: Color, light: Color) -> Self { + self.dark = dark; + self.light = light; + self + } + + /// Sets the size of the squares of the grid cell of the [`QRCode`]. + pub fn cell_size(mut self, cell_size: u16) -> Self { + self.cell_size = cell_size; + self + } +} + +impl<'a, Message, Theme> Widget> for QRCode<'a> { + fn width(&self) -> Length { + Length::Shrink + } + + fn height(&self) -> Length { + Length::Shrink + } + + fn layout( + &self, + _renderer: &Renderer, + _limits: &layout::Limits, + ) -> layout::Node { + let side_length = (self.state.width + 2 * QUIET_ZONE) as f32 + * f32::from(self.cell_size); + + layout::Node::new(Size::new(side_length, side_length)) + } + + fn draw( + &self, + _state: &Tree, + renderer: &mut Renderer, + _theme: &Theme, + _style: &renderer::Style, + layout: Layout<'_>, + _cursor_position: Point, + _viewport: &Rectangle, + ) { + use iced_native::Renderer as _; + + let bounds = layout.bounds(); + let side_length = self.state.width + 2 * QUIET_ZONE; + + // Reuse cache if possible + let geometry = + self.state.cache.draw(renderer, bounds.size(), |frame| { + // Scale units to cell size + frame.scale(f32::from(self.cell_size)); + + // Draw background + frame.fill_rectangle( + Point::ORIGIN, + Size::new(side_length as f32, side_length as f32), + self.light, + ); + + // Avoid drawing on the quiet zone + frame.translate(Vector::new( + QUIET_ZONE as f32, + QUIET_ZONE as f32, + )); + + // Draw contents + self.state + .contents + .iter() + .enumerate() + .filter(|(_, value)| **value == qrcode::Color::Dark) + .for_each(|(index, _)| { + let row = index / self.state.width; + let column = index % self.state.width; + + frame.fill_rectangle( + Point::new(column as f32, row as f32), + Size::UNIT, + self.dark, + ); + }); + }); + + let translation = Vector::new(bounds.x, bounds.y); + + renderer.with_translation(translation, |renderer| { + renderer.draw_primitive(geometry.0); + }); + } +} + +impl<'a, Message, Theme> From> + for Element<'a, Message, Renderer> +{ + fn from(qr_code: QRCode<'a>) -> Self { + Self::new(qr_code) + } +} + +/// The state of a [`QRCode`]. +/// +/// It stores the data that will be displayed. +#[derive(Debug)] +pub struct State { + contents: Vec, + width: usize, + cache: canvas::Cache, +} + +impl State { + /// Creates a new [`State`] with the provided data. + /// + /// This method uses an [`ErrorCorrection::Medium`] and chooses the smallest + /// size to display the data. + pub fn new(data: impl AsRef<[u8]>) -> Result { + let encoded = qrcode::QrCode::new(data)?; + + Ok(Self::build(encoded)) + } + + /// Creates a new [`State`] with the provided [`ErrorCorrection`]. + pub fn with_error_correction( + data: impl AsRef<[u8]>, + error_correction: ErrorCorrection, + ) -> Result { + let encoded = qrcode::QrCode::with_error_correction_level( + data, + error_correction.into(), + )?; + + Ok(Self::build(encoded)) + } + + /// Creates a new [`State`] with the provided [`Version`] and + /// [`ErrorCorrection`]. + pub fn with_version( + data: impl AsRef<[u8]>, + version: Version, + error_correction: ErrorCorrection, + ) -> Result { + let encoded = qrcode::QrCode::with_version( + data, + version.into(), + error_correction.into(), + )?; + + Ok(Self::build(encoded)) + } + + fn build(encoded: qrcode::QrCode) -> Self { + let width = encoded.width(); + let contents = encoded.into_colors(); + + Self { + contents, + width, + cache: canvas::Cache::new(), + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +/// The size of a [`QRCode`]. +/// +/// The higher the version the larger the grid of cells, and therefore the more +/// information the [`QRCode`] can carry. +pub enum Version { + /// A normal QR code version. It should be between 1 and 40. + Normal(u8), + + /// A micro QR code version. It should be between 1 and 4. + Micro(u8), +} + +impl From for qrcode::Version { + fn from(version: Version) -> Self { + match version { + Version::Normal(v) => qrcode::Version::Normal(i16::from(v)), + Version::Micro(v) => qrcode::Version::Micro(i16::from(v)), + } + } +} + +/// The error correction level. +/// +/// It controls the amount of data that can be damaged while still being able +/// to recover the original information. +/// +/// A higher error correction level allows for more corrupted data. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ErrorCorrection { + /// Low error correction. 7% of the data can be restored. + Low, + /// Medium error correction. 15% of the data can be restored. + Medium, + /// Quartile error correction. 25% of the data can be restored. + Quartile, + /// High error correction. 30% of the data can be restored. + High, +} + +impl From for qrcode::EcLevel { + fn from(ec_level: ErrorCorrection) -> Self { + match ec_level { + ErrorCorrection::Low => qrcode::EcLevel::L, + ErrorCorrection::Medium => qrcode::EcLevel::M, + ErrorCorrection::Quartile => qrcode::EcLevel::Q, + ErrorCorrection::High => qrcode::EcLevel::H, + } + } +} + +/// An error that occurred when building a [`State`] for a [`QRCode`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)] +pub enum Error { + /// The data is too long to encode in a QR code for the chosen [`Version`]. + #[error( + "The data is too long to encode in a QR code for the chosen version" + )] + DataTooLong, + + /// The chosen [`Version`] and [`ErrorCorrection`] combination is invalid. + #[error( + "The chosen version and error correction level combination is invalid." + )] + InvalidVersion, + + /// One or more characters in the provided data are not supported by the + /// chosen [`Version`]. + #[error( + "One or more characters in the provided data are not supported by the \ + chosen version" + )] + UnsupportedCharacterSet, + + /// The chosen ECI designator is invalid. A valid designator should be + /// between 0 and 999999. + #[error( + "The chosen ECI designator is invalid. A valid designator should be \ + between 0 and 999999." + )] + InvalidEciDesignator, + + /// A character that does not belong to the character set was found. + #[error("A character that does not belong to the character set was found")] + InvalidCharacter, +} + +impl From for Error { + fn from(error: qrcode::types::QrError) -> Self { + use qrcode::types::QrError; + + match error { + QrError::DataTooLong => Error::DataTooLong, + QrError::InvalidVersion => Error::InvalidVersion, + QrError::UnsupportedCharacterSet => Error::UnsupportedCharacterSet, + QrError::InvalidEciDesignator => Error::InvalidEciDesignator, + QrError::InvalidCharacter => Error::InvalidCharacter, + } + } +} -- cgit From 3a0d34c0240f4421737a6a08761f99d6f8140d02 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 4 Mar 2023 05:37:11 +0100 Subject: Create `iced_widget` subcrate and re-organize the whole codebase --- src/advanced.rs | 9 ++ src/application.rs | 12 +- src/clipboard.rs | 3 - src/element.rs | 5 - src/error.rs | 16 ++- src/executor.rs | 14 -- src/keyboard.rs | 2 - src/lib.rs | 134 +++++++++++++++---- src/mouse.rs | 2 - src/overlay.rs | 18 --- src/result.rs | 6 - src/touch.rs | 2 +- src/widget.rs | 239 ---------------------------------- src/widget/canvas.rs | 238 ---------------------------------- src/widget/canvas/cursor.rs | 64 --------- src/widget/canvas/event.rs | 21 --- src/widget/canvas/program.rs | 108 ---------------- src/widget/qr_code.rs | 300 ------------------------------------------- src/window.rs | 5 +- 19 files changed, 136 insertions(+), 1062 deletions(-) create mode 100644 src/advanced.rs delete mode 100644 src/clipboard.rs delete mode 100644 src/element.rs delete mode 100644 src/executor.rs delete mode 100644 src/keyboard.rs delete mode 100644 src/mouse.rs delete mode 100644 src/overlay.rs delete mode 100644 src/result.rs delete mode 100644 src/widget.rs delete mode 100644 src/widget/canvas.rs delete mode 100644 src/widget/canvas/cursor.rs delete mode 100644 src/widget/canvas/event.rs delete mode 100644 src/widget/canvas/program.rs delete mode 100644 src/widget/qr_code.rs (limited to 'src') diff --git a/src/advanced.rs b/src/advanced.rs new file mode 100644 index 00000000..714076e0 --- /dev/null +++ b/src/advanced.rs @@ -0,0 +1,9 @@ +//! Leverage advanced concepts like custom widgets. +pub use crate::core::image; +pub use crate::core::layout::{self, Layout}; +pub use crate::core::overlay::{self, Overlay}; +pub use crate::core::renderer::{self, Renderer}; +pub use crate::core::svg; +pub use crate::core::text::{self, Text}; +pub use crate::core::widget::{self, Widget}; +pub use crate::core::{Clipboard, Shell}; diff --git a/src/application.rs b/src/application.rs index b9871556..f5cf3317 100644 --- a/src/application.rs +++ b/src/application.rs @@ -1,7 +1,7 @@ //! Build interactive cross-platform applications. use crate::{Command, Element, Executor, Settings, Subscription}; -pub use iced_native::application::{Appearance, StyleSheet}; +pub use crate::style::application::{Appearance, StyleSheet}; /// An interactive cross-platform application. /// @@ -198,24 +198,24 @@ pub trait Application: Sized { default_font: settings.default_font, default_text_size: settings.default_text_size, antialiasing: if settings.antialiasing { - Some(crate::renderer::Antialiasing::MSAAx4) + Some(crate::graphics::Antialiasing::MSAAx4) } else { None }, ..crate::renderer::Settings::default() }; - Ok(crate::runtime::application::run::< + Ok(crate::shell::application::run::< Instance, Self::Executor, - crate::renderer::window::Compositor, + crate::renderer::Compositor, >(settings.into(), renderer_settings)?) } } struct Instance(A); -impl iced_winit::Program for Instance +impl crate::native::Program for Instance where A: Application, { @@ -231,7 +231,7 @@ where } } -impl crate::runtime::Application for Instance +impl crate::shell::Application for Instance where A: Application, { diff --git a/src/clipboard.rs b/src/clipboard.rs deleted file mode 100644 index dde17051..00000000 --- a/src/clipboard.rs +++ /dev/null @@ -1,3 +0,0 @@ -//! Access the clipboard. -#[cfg(not(target_arch = "wasm32"))] -pub use crate::runtime::clipboard::{read, write}; diff --git a/src/element.rs b/src/element.rs deleted file mode 100644 index 2eb1bb4d..00000000 --- a/src/element.rs +++ /dev/null @@ -1,5 +0,0 @@ -/// A generic widget. -/// -/// This is an alias of an `iced_native` element with a default `Renderer`. -pub type Element<'a, Message, Renderer = crate::Renderer> = - crate::runtime::Element<'a, Message, Renderer>; diff --git a/src/error.rs b/src/error.rs index 5326718f..111bedf2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,6 @@ -use iced_futures::futures; +use crate::futures; +use crate::graphics; +use crate::shell; /// An error that occurred while running an application. #[derive(Debug, thiserror::Error)] @@ -13,19 +15,19 @@ pub enum Error { /// The application graphics context could not be created. #[error("the application graphics context could not be created")] - GraphicsCreationFailed(iced_renderer::Error), + GraphicsCreationFailed(graphics::Error), } -impl From for Error { - fn from(error: iced_winit::Error) -> Error { +impl From for Error { + fn from(error: shell::Error) -> Error { match error { - iced_winit::Error::ExecutorCreationFailed(error) => { + shell::Error::ExecutorCreationFailed(error) => { Error::ExecutorCreationFailed(error) } - iced_winit::Error::WindowCreationFailed(error) => { + shell::Error::WindowCreationFailed(error) => { Error::WindowCreationFailed(Box::new(error)) } - iced_winit::Error::GraphicsCreationFailed(error) => { + shell::Error::GraphicsCreationFailed(error) => { Error::GraphicsCreationFailed(error) } } diff --git a/src/executor.rs b/src/executor.rs deleted file mode 100644 index 36ae274e..00000000 --- a/src/executor.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! Choose your preferred executor to power your application. -pub use crate::runtime::Executor; - -/// A default cross-platform executor. -/// -/// - On native platforms, it will use: -/// - `iced_futures::backend::native::tokio` when the `tokio` feature is enabled. -/// - `iced_futures::backend::native::async-std` when the `async-std` feature is -/// enabled. -/// - `iced_futures::backend::native::smol` when the `smol` feature is enabled. -/// - `iced_futures::backend::native::thread_pool` otherwise. -/// -/// - On Wasm, it will use `iced_futures::backend::wasm::wasm_bindgen`. -pub type Default = iced_futures::backend::default::Executor; diff --git a/src/keyboard.rs b/src/keyboard.rs deleted file mode 100644 index 2134a66b..00000000 --- a/src/keyboard.rs +++ /dev/null @@ -1,2 +0,0 @@ -//! Listen and react to keyboard events. -pub use crate::runtime::keyboard::{Event, KeyCode, Modifiers}; diff --git a/src/lib.rs b/src/lib.rs index bb162f2d..b71b7781 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -164,51 +164,133 @@ #![forbid(rust_2018_idioms, unsafe_code)] #![allow(clippy::inherent_to_string, clippy::type_complexity)] #![cfg_attr(docsrs, feature(doc_cfg))] +use iced_widget::graphics; +use iced_widget::renderer; +use iced_widget::style; +use iced_winit as shell; +use iced_winit::core; +use iced_winit::native; + +pub use iced_futures::futures; -mod element; mod error; -mod result; mod sandbox; pub mod application; -pub mod clipboard; -pub mod executor; -pub mod keyboard; -pub mod mouse; -pub mod overlay; pub mod settings; pub mod time; -pub mod touch; -pub mod widget; pub mod window; -use iced_renderer as renderer; -use iced_winit as runtime; +#[cfg(feature = "advanced")] +pub mod advanced; + +pub use style::theme; + +pub use crate::core::alignment; +pub use crate::core::event; +pub use crate::core::{ + color, Alignment, Background, Color, ContentFit, Length, Padding, Point, + Rectangle, Size, Vector, +}; +pub use crate::native::Command; +pub use native::subscription; + +pub mod clipboard { + //! Access the clipboard. + pub use crate::shell::clipboard::{read, write}; +} + +pub mod executor { + //! Choose your preferred executor to power your application. + pub use iced_futures::Executor; + + /// A default cross-platform executor. + /// + /// - On native platforms, it will use: + /// - `iced_futures::backend::native::tokio` when the `tokio` feature is enabled. + /// - `iced_futures::backend::native::async-std` when the `async-std` feature is + /// enabled. + /// - `iced_futures::backend::native::smol` when the `smol` feature is enabled. + /// - `iced_futures::backend::native::thread_pool` otherwise. + /// + /// - On Wasm, it will use `iced_futures::backend::wasm::wasm_bindgen`. + pub type Default = iced_futures::backend::default::Executor; +} + +pub mod font { + //! Load and use fonts. + pub use crate::core::font::*; + pub use crate::native::font::*; +} + +pub mod keyboard { + //! Listen and react to keyboard events. + pub use crate::core::keyboard::{Event, KeyCode, Modifiers}; +} + +pub mod mouse { + //! Listen and react to mouse events. + pub use crate::core::mouse::{Button, Event, Interaction, ScrollDelta}; +} -pub use iced_native::theme; -pub use runtime::event; -pub use runtime::font; -pub use runtime::subscription; +#[cfg(feature = "system")] +pub mod system { + //! Retrieve system information. + pub use crate::native::system::Information; + pub use crate::shell::system::*; +} + +pub mod overlay { + //! Display interactive elements on top of other widgets. + + /// A generic [`Overlay`]. + /// + /// This is an alias of an `iced_native` element with a default `Renderer`. + /// + /// [`Overlay`]: iced_native::Overlay + pub type Element<'a, Message, Renderer = crate::Renderer> = + crate::core::overlay::Element<'a, Message, Renderer>; + + pub use iced_widget::overlay::*; +} + +pub mod touch { + //! Listen and react to touch events. + pub use crate::core::touch::{Event, Finger}; +} + +pub mod widget { + //! Use the built-in widgets or create your own. + pub use iced_widget::*; + + // We hide the re-exported modules by `iced_widget` + mod core {} + mod graphics {} + mod native {} + mod renderer {} + mod style {} +} pub use application::Application; -pub use element::Element; pub use error::Error; pub use event::Event; pub use executor::Executor; pub use font::Font; -pub use renderer::Renderer; -pub use result::Result; pub use sandbox::Sandbox; pub use settings::Settings; pub use subscription::Subscription; pub use theme::Theme; -pub use runtime::alignment; -pub use runtime::futures; -pub use runtime::{ - color, Alignment, Background, Color, Command, ContentFit, Length, Padding, - Point, Rectangle, Size, Vector, -}; +/// The default renderer. +pub type Renderer = renderer::Renderer; -#[cfg(feature = "system")] -pub use runtime::system; +/// A generic widget. +/// +/// This is an alias of an `iced_native` element with a default `Renderer`. +pub type Element<'a, Message, Renderer = crate::Renderer> = + crate::core::Element<'a, Message, Renderer>; + +/// The result of running an [`Application`]. +/// +/// [`Application`]: crate::Application +pub type Result = std::result::Result<(), Error>; diff --git a/src/mouse.rs b/src/mouse.rs deleted file mode 100644 index d61ed09a..00000000 --- a/src/mouse.rs +++ /dev/null @@ -1,2 +0,0 @@ -//! Listen and react to mouse events. -pub use crate::runtime::mouse::{Button, Event, Interaction, ScrollDelta}; diff --git a/src/overlay.rs b/src/overlay.rs deleted file mode 100644 index c0f4c492..00000000 --- a/src/overlay.rs +++ /dev/null @@ -1,18 +0,0 @@ -//! Display interactive elements on top of other widgets. - -/// A generic [`Overlay`]. -/// -/// This is an alias of an `iced_native` element with a default `Renderer`. -/// -/// [`Overlay`]: iced_native::Overlay -pub type Element<'a, Message, Renderer = crate::Renderer> = - iced_native::overlay::Element<'a, Message, Renderer>; - -pub mod menu { - //! Build and show dropdown menus. - pub use iced_native::overlay::menu::{Appearance, State, StyleSheet}; - - /// A widget that produces a message when clicked. - pub type Menu<'a, Message, Renderer = crate::Renderer> = - iced_native::overlay::Menu<'a, Message, Renderer>; -} diff --git a/src/result.rs b/src/result.rs deleted file mode 100644 index ef565bd6..00000000 --- a/src/result.rs +++ /dev/null @@ -1,6 +0,0 @@ -use crate::Error; - -/// The result of running an [`Application`]. -/// -/// [`Application`]: crate::Application -pub type Result = std::result::Result<(), Error>; diff --git a/src/touch.rs b/src/touch.rs index 0b77c386..f2bdfca8 100644 --- a/src/touch.rs +++ b/src/touch.rs @@ -1,2 +1,2 @@ //! Listen and react to touch events. -pub use crate::runtime::touch::{Event, Finger}; +pub use crate::core::touch::{Event, Finger}; diff --git a/src/widget.rs b/src/widget.rs deleted file mode 100644 index 19434e84..00000000 --- a/src/widget.rs +++ /dev/null @@ -1,239 +0,0 @@ -//! Display information and interactive controls in your application. -pub use iced_native::widget::helpers::*; - -pub use iced_native::{column, row}; - -/// A container that distributes its contents vertically. -pub type Column<'a, Message, Renderer = crate::Renderer> = - iced_native::widget::Column<'a, Message, Renderer>; - -/// A container that distributes its contents horizontally. -pub type Row<'a, Message, Renderer = crate::Renderer> = - iced_native::widget::Row<'a, Message, Renderer>; - -pub mod text { - //! Write some text for your users to read. - pub use iced_native::widget::text::{Appearance, StyleSheet}; - - /// A paragraph of text. - pub type Text<'a, Renderer = crate::Renderer> = - iced_native::widget::Text<'a, Renderer>; -} - -pub mod button { - //! Allow your users to perform actions by pressing a button. - pub use iced_native::widget::button::{Appearance, StyleSheet}; - - /// A widget that produces a message when clicked. - pub type Button<'a, Message, Renderer = crate::Renderer> = - iced_native::widget::Button<'a, Message, Renderer>; -} - -pub mod checkbox { - //! Show toggle controls using checkboxes. - pub use iced_native::widget::checkbox::{Appearance, Icon, StyleSheet}; - - /// A box that can be checked. - pub type Checkbox<'a, Message, Renderer = crate::Renderer> = - iced_native::widget::Checkbox<'a, Message, Renderer>; -} - -pub mod container { - //! Decorate content and apply alignment. - pub use iced_native::widget::container::{Appearance, StyleSheet}; - - /// An element decorating some content. - pub type Container<'a, Message, Renderer = crate::Renderer> = - iced_native::widget::Container<'a, Message, Renderer>; -} - -pub mod pane_grid { - //! Let your users split regions of your application and organize layout dynamically. - //! - //! [![Pane grid - Iced](https://thumbs.gfycat.com/MixedFlatJellyfish-small.gif)](https://gfycat.com/mixedflatjellyfish) - //! - //! # Example - //! The [`pane_grid` example] showcases how to use a [`PaneGrid`] with resizing, - //! drag and drop, and hotkey support. - //! - //! [`pane_grid` example]: https://github.com/iced-rs/iced/tree/0.8/examples/pane_grid - pub use iced_native::widget::pane_grid::{ - Axis, Configuration, Direction, DragEvent, Line, Node, Pane, - ResizeEvent, Split, State, StyleSheet, - }; - - /// A collection of panes distributed using either vertical or horizontal splits - /// to completely fill the space available. - /// - /// [![Pane grid - Iced](https://thumbs.gfycat.com/MixedFlatJellyfish-small.gif)](https://gfycat.com/mixedflatjellyfish) - pub type PaneGrid<'a, Message, Renderer = crate::Renderer> = - iced_native::widget::PaneGrid<'a, Message, Renderer>; - - /// The content of a [`Pane`]. - pub type Content<'a, Message, Renderer = crate::Renderer> = - iced_native::widget::pane_grid::Content<'a, Message, Renderer>; - - /// The title bar of a [`Pane`]. - pub type TitleBar<'a, Message, Renderer = crate::Renderer> = - iced_native::widget::pane_grid::TitleBar<'a, Message, Renderer>; -} - -pub mod pick_list { - //! Display a dropdown list of selectable values. - pub use iced_native::widget::pick_list::{ - Appearance, Handle, Icon, StyleSheet, - }; - - /// A widget allowing the selection of a single value from a list of options. - pub type PickList<'a, T, Message, Renderer = crate::Renderer> = - iced_native::widget::PickList<'a, T, Message, Renderer>; -} - -pub mod radio { - //! Create choices using radio buttons. - pub use iced_native::widget::radio::{Appearance, StyleSheet}; - - /// A circular button representing a choice. - pub type Radio = - iced_native::widget::Radio; -} - -pub mod scrollable { - //! Navigate an endless amount of content with a scrollbar. - pub use iced_native::widget::scrollable::{ - snap_to, style::Scrollbar, style::Scroller, Id, Properties, - RelativeOffset, StyleSheet, - }; - - /// A widget that can vertically display an infinite amount of content - /// with a scrollbar. - pub type Scrollable<'a, Message, Renderer = crate::Renderer> = - iced_native::widget::Scrollable<'a, Message, Renderer>; -} - -pub mod toggler { - //! Show toggle controls using togglers. - pub use iced_native::widget::toggler::{Appearance, StyleSheet}; - - /// A toggler widget. - pub type Toggler<'a, Message, Renderer = crate::Renderer> = - iced_native::widget::Toggler<'a, Message, Renderer>; -} - -pub mod text_input { - //! Display fields that can be filled with text. - pub use iced_native::widget::text_input::{ - focus, move_cursor_to, move_cursor_to_end, move_cursor_to_front, - select_all, Appearance, Id, StyleSheet, - }; - - /// A field that can be filled with text. - pub type TextInput<'a, Message, Renderer = crate::Renderer> = - iced_native::widget::TextInput<'a, Message, Renderer>; -} - -pub mod tooltip { - //! Display a widget over another. - pub use iced_native::widget::tooltip::Position; - - /// A widget allowing the selection of a single value from a list of options. - pub type Tooltip<'a, Message, Renderer = crate::Renderer> = - iced_native::widget::Tooltip<'a, Message, Renderer>; -} - -pub use iced_native::widget::progress_bar; -pub use iced_native::widget::rule; -pub use iced_native::widget::slider; -pub use iced_native::widget::vertical_slider; -pub use iced_native::widget::Space; - -pub use button::Button; -pub use checkbox::Checkbox; -pub use container::Container; -pub use pane_grid::PaneGrid; -pub use pick_list::PickList; -pub use progress_bar::ProgressBar; -pub use radio::Radio; -pub use rule::Rule; -pub use scrollable::Scrollable; -pub use slider::Slider; -pub use text::Text; -pub use text_input::TextInput; -pub use toggler::Toggler; -pub use tooltip::Tooltip; -pub use vertical_slider::VerticalSlider; - -#[cfg(feature = "canvas")] -#[cfg_attr(docsrs, doc(cfg(feature = "canvas")))] -pub mod canvas; - -#[cfg(feature = "canvas")] -#[cfg_attr(docsrs, doc(cfg(feature = "canvas")))] -/// Creates a new [`Canvas`]. -pub fn canvas(program: P) -> Canvas -where - Renderer: iced_renderer::geometry::Renderer, - P: canvas::Program, -{ - Canvas::new(program) -} - -#[cfg(feature = "image")] -#[cfg_attr(docsrs, doc(cfg(feature = "image")))] -pub mod image { - //! Display images in your user interface. - pub use iced_native::image::Handle; - - /// A frame that displays an image. - pub type Image = iced_native::widget::Image; - - pub use iced_native::widget::image::viewer; - pub use viewer::Viewer; -} - -#[cfg(feature = "qr_code")] -#[cfg_attr(docsrs, doc(cfg(feature = "qr_code")))] -pub mod qr_code; - -#[cfg(feature = "svg")] -#[cfg_attr(docsrs, doc(cfg(feature = "svg")))] -pub mod svg { - //! Display vector graphics in your application. - pub use iced_native::svg::Handle; - pub use iced_native::widget::svg::{Appearance, StyleSheet, Svg}; -} - -#[cfg(feature = "canvas")] -#[cfg_attr(docsrs, doc(cfg(feature = "canvas")))] -pub use canvas::Canvas; - -#[cfg(feature = "image")] -#[cfg_attr(docsrs, doc(cfg(feature = "image")))] -pub use image::Image; - -#[cfg(feature = "qr_code")] -#[cfg_attr(docsrs, doc(cfg(feature = "qr_code")))] -pub use qr_code::QRCode; - -#[cfg(feature = "svg")] -#[cfg_attr(docsrs, doc(cfg(feature = "svg")))] -pub use svg::Svg; - -use crate::Command; -use iced_native::widget::operation; - -/// Focuses the previous focusable widget. -pub fn focus_previous() -> Command -where - Message: 'static, -{ - Command::widget(operation::focusable::focus_previous()) -} - -/// Focuses the next focusable widget. -pub fn focus_next() -> Command -where - Message: 'static, -{ - Command::widget(operation::focusable::focus_next()) -} diff --git a/src/widget/canvas.rs b/src/widget/canvas.rs deleted file mode 100644 index bc5995c6..00000000 --- a/src/widget/canvas.rs +++ /dev/null @@ -1,238 +0,0 @@ -//! Draw 2D graphics for your users. -pub mod event; - -mod cursor; -mod program; - -pub use cursor::Cursor; -pub use event::Event; -pub use program::Program; - -pub use iced_renderer::geometry::*; - -use crate::{Length, Point, Rectangle, Size, Vector}; - -use iced_native::layout::{self, Layout}; -use iced_native::mouse; -use iced_native::renderer; -use iced_native::widget::tree::{self, Tree}; -use iced_native::{Clipboard, Element, Shell, Widget}; - -use std::marker::PhantomData; - -/// A widget capable of drawing 2D graphics. -/// -/// ## Drawing a simple circle -/// If you want to get a quick overview, here's how we can draw a simple circle: -/// -/// ```no_run -/// use iced::widget::canvas::{self, Canvas, Cursor, Fill, Frame, Geometry, Path, Program}; -/// use iced::{Color, Rectangle, Theme, Renderer}; -/// -/// // First, we define the data we need for drawing -/// #[derive(Debug)] -/// struct Circle { -/// radius: f32, -/// } -/// -/// // Then, we implement the `Program` trait -/// impl Program<()> for Circle { -/// type State = (); -/// -/// fn draw(&self, _state: &(), renderer: &Renderer, _theme: &Theme, bounds: Rectangle, _cursor: Cursor) -> Vec{ -/// // We prepare a new `Frame` -/// let mut frame = Frame::new(renderer, bounds.size()); -/// -/// // We create a `Path` representing a simple circle -/// let circle = Path::circle(frame.center(), self.radius); -/// -/// // And fill it with some color -/// frame.fill(&circle, Color::BLACK); -/// -/// // Finally, we produce the geometry -/// vec![frame.into_geometry()] -/// } -/// } -/// -/// // Finally, we simply use our `Circle` to create the `Canvas`! -/// let canvas = Canvas::new(Circle { radius: 50.0 }); -/// ``` -#[derive(Debug)] -pub struct Canvas -where - Renderer: iced_renderer::geometry::Renderer, - P: Program, -{ - width: Length, - height: Length, - program: P, - message_: PhantomData, - theme_: PhantomData, -} - -impl Canvas -where - Renderer: iced_renderer::geometry::Renderer, - P: Program, -{ - const DEFAULT_SIZE: f32 = 100.0; - - /// Creates a new [`Canvas`]. - pub fn new(program: P) -> Self { - Canvas { - width: Length::Fixed(Self::DEFAULT_SIZE), - height: Length::Fixed(Self::DEFAULT_SIZE), - program, - message_: PhantomData, - theme_: PhantomData, - } - } - - /// Sets the width of the [`Canvas`]. - pub fn width(mut self, width: impl Into) -> Self { - self.width = width.into(); - self - } - - /// Sets the height of the [`Canvas`]. - pub fn height(mut self, height: impl Into) -> Self { - self.height = height.into(); - self - } -} - -impl Widget - for Canvas -where - Renderer: iced_renderer::geometry::Renderer, - P: Program, -{ - fn tag(&self) -> tree::Tag { - struct Tag(T); - tree::Tag::of::>() - } - - fn state(&self) -> tree::State { - tree::State::new(P::State::default()) - } - - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height - } - - fn layout( - &self, - _renderer: &Renderer, - limits: &layout::Limits, - ) -> layout::Node { - let limits = limits.width(self.width).height(self.height); - let size = limits.resolve(Size::ZERO); - - layout::Node::new(size) - } - - fn on_event( - &mut self, - tree: &mut Tree, - event: iced_native::Event, - layout: Layout<'_>, - cursor_position: Point, - _renderer: &Renderer, - _clipboard: &mut dyn Clipboard, - shell: &mut Shell<'_, Message>, - ) -> event::Status { - let bounds = layout.bounds(); - - let canvas_event = match event { - iced_native::Event::Mouse(mouse_event) => { - Some(Event::Mouse(mouse_event)) - } - iced_native::Event::Touch(touch_event) => { - Some(Event::Touch(touch_event)) - } - iced_native::Event::Keyboard(keyboard_event) => { - Some(Event::Keyboard(keyboard_event)) - } - _ => None, - }; - - let cursor = Cursor::from_window_position(cursor_position); - - if let Some(canvas_event) = canvas_event { - let state = tree.state.downcast_mut::(); - - let (event_status, message) = - self.program.update(state, canvas_event, bounds, cursor); - - if let Some(message) = message { - shell.publish(message); - } - - return event_status; - } - - event::Status::Ignored - } - - fn mouse_interaction( - &self, - tree: &Tree, - layout: Layout<'_>, - cursor_position: Point, - _viewport: &Rectangle, - _renderer: &Renderer, - ) -> mouse::Interaction { - let bounds = layout.bounds(); - let cursor = Cursor::from_window_position(cursor_position); - let state = tree.state.downcast_ref::(); - - self.program.mouse_interaction(state, bounds, cursor) - } - - fn draw( - &self, - tree: &Tree, - renderer: &mut Renderer, - theme: &Renderer::Theme, - _style: &renderer::Style, - layout: Layout<'_>, - cursor_position: Point, - _viewport: &Rectangle, - ) { - let bounds = layout.bounds(); - - if bounds.width < 1.0 || bounds.height < 1.0 { - return; - } - - let cursor = Cursor::from_window_position(cursor_position); - let state = tree.state.downcast_ref::(); - - renderer.with_translation( - Vector::new(bounds.x, bounds.y), - |renderer| { - renderer.draw( - self.program.draw(state, renderer, theme, bounds, cursor), - ); - }, - ); - } -} - -impl<'a, P, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Message: 'a, - Renderer: 'a + iced_renderer::geometry::Renderer, - P: Program + 'a, -{ - fn from( - canvas: Canvas, - ) -> Element<'a, Message, Renderer> { - Element::new(canvas) - } -} diff --git a/src/widget/canvas/cursor.rs b/src/widget/canvas/cursor.rs deleted file mode 100644 index ef6a7771..00000000 --- a/src/widget/canvas/cursor.rs +++ /dev/null @@ -1,64 +0,0 @@ -use crate::{Point, Rectangle}; - -/// The mouse cursor state. -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum Cursor { - /// The cursor has a defined position. - Available(Point), - - /// The cursor is currently unavailable (i.e. out of bounds or busy). - Unavailable, -} - -impl Cursor { - // TODO: Remove this once this type is used in `iced_native` to encode - // proper cursor availability - pub(crate) fn from_window_position(position: Point) -> Self { - if position.x < 0.0 || position.y < 0.0 { - Cursor::Unavailable - } else { - Cursor::Available(position) - } - } - - /// Returns the absolute position of the [`Cursor`], if available. - pub fn position(&self) -> Option { - match self { - Cursor::Available(position) => Some(*position), - Cursor::Unavailable => None, - } - } - - /// Returns the relative position of the [`Cursor`] inside the given bounds, - /// if available. - /// - /// If the [`Cursor`] is not over the provided bounds, this method will - /// return `None`. - pub fn position_in(&self, bounds: &Rectangle) -> Option { - if self.is_over(bounds) { - self.position_from(bounds.position()) - } else { - None - } - } - - /// Returns the relative position of the [`Cursor`] from the given origin, - /// if available. - pub fn position_from(&self, origin: Point) -> Option { - match self { - Cursor::Available(position) => { - Some(Point::new(position.x - origin.x, position.y - origin.y)) - } - Cursor::Unavailable => None, - } - } - - /// Returns whether the [`Cursor`] is currently over the provided bounds - /// or not. - pub fn is_over(&self, bounds: &Rectangle) -> bool { - match self { - Cursor::Available(position) => bounds.contains(*position), - Cursor::Unavailable => false, - } - } -} diff --git a/src/widget/canvas/event.rs b/src/widget/canvas/event.rs deleted file mode 100644 index 7c733a4d..00000000 --- a/src/widget/canvas/event.rs +++ /dev/null @@ -1,21 +0,0 @@ -//! Handle events of a canvas. -use iced_native::keyboard; -use iced_native::mouse; -use iced_native::touch; - -pub use iced_native::event::Status; - -/// A [`Canvas`] event. -/// -/// [`Canvas`]: crate::widget::Canvas -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum Event { - /// A mouse event. - Mouse(mouse::Event), - - /// A touch event. - Touch(touch::Event), - - /// A keyboard event. - Keyboard(keyboard::Event), -} diff --git a/src/widget/canvas/program.rs b/src/widget/canvas/program.rs deleted file mode 100644 index fd15663a..00000000 --- a/src/widget/canvas/program.rs +++ /dev/null @@ -1,108 +0,0 @@ -use crate::widget::canvas::event::{self, Event}; -use crate::widget::canvas::mouse; -use crate::widget::canvas::Cursor; -use crate::Rectangle; - -/// The state and logic of a [`Canvas`]. -/// -/// A [`Program`] can mutate internal state and produce messages for an -/// application. -/// -/// [`Canvas`]: crate::widget::Canvas -pub trait Program -where - Renderer: iced_renderer::geometry::Renderer, -{ - /// The internal state mutated by the [`Program`]. - type State: Default + 'static; - - /// Updates the [`State`](Self::State) of the [`Program`]. - /// - /// When a [`Program`] is used in a [`Canvas`], the runtime will call this - /// method for each [`Event`]. - /// - /// This method can optionally return a `Message` to notify an application - /// of any meaningful interactions. - /// - /// By default, this method does and returns nothing. - /// - /// [`Canvas`]: crate::widget::Canvas - fn update( - &self, - _state: &mut Self::State, - _event: Event, - _bounds: Rectangle, - _cursor: Cursor, - ) -> (event::Status, Option) { - (event::Status::Ignored, None) - } - - /// Draws the state of the [`Program`], producing a bunch of [`Geometry`]. - /// - /// [`Geometry`] can be easily generated with a [`Frame`] or stored in a - /// [`Cache`]. - /// - /// [`Frame`]: crate::widget::canvas::Frame - /// [`Cache`]: crate::widget::canvas::Cache - fn draw( - &self, - state: &Self::State, - renderer: &Renderer, - theme: &Renderer::Theme, - bounds: Rectangle, - cursor: Cursor, - ) -> Vec; - - /// Returns the current mouse interaction of the [`Program`]. - /// - /// The interaction returned will be in effect even if the cursor position - /// is out of bounds of the program's [`Canvas`]. - /// - /// [`Canvas`]: crate::widget::Canvas - fn mouse_interaction( - &self, - _state: &Self::State, - _bounds: Rectangle, - _cursor: Cursor, - ) -> mouse::Interaction { - mouse::Interaction::default() - } -} - -impl Program for &T -where - Renderer: iced_renderer::geometry::Renderer, - T: Program, -{ - type State = T::State; - - fn update( - &self, - state: &mut Self::State, - event: Event, - bounds: Rectangle, - cursor: Cursor, - ) -> (event::Status, Option) { - T::update(self, state, event, bounds, cursor) - } - - fn draw( - &self, - state: &Self::State, - renderer: &Renderer, - theme: &Renderer::Theme, - bounds: Rectangle, - cursor: Cursor, - ) -> Vec { - T::draw(self, state, renderer, theme, bounds, cursor) - } - - fn mouse_interaction( - &self, - state: &Self::State, - bounds: Rectangle, - cursor: Cursor, - ) -> mouse::Interaction { - T::mouse_interaction(self, state, bounds, cursor) - } -} diff --git a/src/widget/qr_code.rs b/src/widget/qr_code.rs deleted file mode 100644 index 66442d5d..00000000 --- a/src/widget/qr_code.rs +++ /dev/null @@ -1,300 +0,0 @@ -//! Encode and display information in a QR code. -use crate::widget::canvas; -use crate::Renderer; - -use iced_native::layout; -use iced_native::renderer; -use iced_native::widget::Tree; -use iced_native::{ - Color, Element, Layout, Length, Point, Rectangle, Size, Vector, Widget, -}; -use thiserror::Error; - -const DEFAULT_CELL_SIZE: u16 = 4; -const QUIET_ZONE: usize = 2; - -/// A type of matrix barcode consisting of squares arranged in a grid which -/// can be read by an imaging device, such as a camera. -#[derive(Debug)] -pub struct QRCode<'a> { - state: &'a State, - dark: Color, - light: Color, - cell_size: u16, -} - -impl<'a> QRCode<'a> { - /// Creates a new [`QRCode`] with the provided [`State`]. - pub fn new(state: &'a State) -> Self { - Self { - cell_size: DEFAULT_CELL_SIZE, - dark: Color::BLACK, - light: Color::WHITE, - state, - } - } - - /// Sets both the dark and light [`Color`]s of the [`QRCode`]. - pub fn color(mut self, dark: Color, light: Color) -> Self { - self.dark = dark; - self.light = light; - self - } - - /// Sets the size of the squares of the grid cell of the [`QRCode`]. - pub fn cell_size(mut self, cell_size: u16) -> Self { - self.cell_size = cell_size; - self - } -} - -impl<'a, Message, Theme> Widget> for QRCode<'a> { - fn width(&self) -> Length { - Length::Shrink - } - - fn height(&self) -> Length { - Length::Shrink - } - - fn layout( - &self, - _renderer: &Renderer, - _limits: &layout::Limits, - ) -> layout::Node { - let side_length = (self.state.width + 2 * QUIET_ZONE) as f32 - * f32::from(self.cell_size); - - layout::Node::new(Size::new(side_length, side_length)) - } - - fn draw( - &self, - _state: &Tree, - renderer: &mut Renderer, - _theme: &Theme, - _style: &renderer::Style, - layout: Layout<'_>, - _cursor_position: Point, - _viewport: &Rectangle, - ) { - use iced_native::Renderer as _; - - let bounds = layout.bounds(); - let side_length = self.state.width + 2 * QUIET_ZONE; - - // Reuse cache if possible - let geometry = - self.state.cache.draw(renderer, bounds.size(), |frame| { - // Scale units to cell size - frame.scale(f32::from(self.cell_size)); - - // Draw background - frame.fill_rectangle( - Point::ORIGIN, - Size::new(side_length as f32, side_length as f32), - self.light, - ); - - // Avoid drawing on the quiet zone - frame.translate(Vector::new( - QUIET_ZONE as f32, - QUIET_ZONE as f32, - )); - - // Draw contents - self.state - .contents - .iter() - .enumerate() - .filter(|(_, value)| **value == qrcode::Color::Dark) - .for_each(|(index, _)| { - let row = index / self.state.width; - let column = index % self.state.width; - - frame.fill_rectangle( - Point::new(column as f32, row as f32), - Size::UNIT, - self.dark, - ); - }); - }); - - let translation = Vector::new(bounds.x, bounds.y); - - renderer.with_translation(translation, |renderer| { - renderer.draw_primitive(geometry.0); - }); - } -} - -impl<'a, Message, Theme> From> - for Element<'a, Message, Renderer> -{ - fn from(qr_code: QRCode<'a>) -> Self { - Self::new(qr_code) - } -} - -/// The state of a [`QRCode`]. -/// -/// It stores the data that will be displayed. -#[derive(Debug)] -pub struct State { - contents: Vec, - width: usize, - cache: canvas::Cache, -} - -impl State { - /// Creates a new [`State`] with the provided data. - /// - /// This method uses an [`ErrorCorrection::Medium`] and chooses the smallest - /// size to display the data. - pub fn new(data: impl AsRef<[u8]>) -> Result { - let encoded = qrcode::QrCode::new(data)?; - - Ok(Self::build(encoded)) - } - - /// Creates a new [`State`] with the provided [`ErrorCorrection`]. - pub fn with_error_correction( - data: impl AsRef<[u8]>, - error_correction: ErrorCorrection, - ) -> Result { - let encoded = qrcode::QrCode::with_error_correction_level( - data, - error_correction.into(), - )?; - - Ok(Self::build(encoded)) - } - - /// Creates a new [`State`] with the provided [`Version`] and - /// [`ErrorCorrection`]. - pub fn with_version( - data: impl AsRef<[u8]>, - version: Version, - error_correction: ErrorCorrection, - ) -> Result { - let encoded = qrcode::QrCode::with_version( - data, - version.into(), - error_correction.into(), - )?; - - Ok(Self::build(encoded)) - } - - fn build(encoded: qrcode::QrCode) -> Self { - let width = encoded.width(); - let contents = encoded.into_colors(); - - Self { - contents, - width, - cache: canvas::Cache::new(), - } - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -/// The size of a [`QRCode`]. -/// -/// The higher the version the larger the grid of cells, and therefore the more -/// information the [`QRCode`] can carry. -pub enum Version { - /// A normal QR code version. It should be between 1 and 40. - Normal(u8), - - /// A micro QR code version. It should be between 1 and 4. - Micro(u8), -} - -impl From for qrcode::Version { - fn from(version: Version) -> Self { - match version { - Version::Normal(v) => qrcode::Version::Normal(i16::from(v)), - Version::Micro(v) => qrcode::Version::Micro(i16::from(v)), - } - } -} - -/// The error correction level. -/// -/// It controls the amount of data that can be damaged while still being able -/// to recover the original information. -/// -/// A higher error correction level allows for more corrupted data. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum ErrorCorrection { - /// Low error correction. 7% of the data can be restored. - Low, - /// Medium error correction. 15% of the data can be restored. - Medium, - /// Quartile error correction. 25% of the data can be restored. - Quartile, - /// High error correction. 30% of the data can be restored. - High, -} - -impl From for qrcode::EcLevel { - fn from(ec_level: ErrorCorrection) -> Self { - match ec_level { - ErrorCorrection::Low => qrcode::EcLevel::L, - ErrorCorrection::Medium => qrcode::EcLevel::M, - ErrorCorrection::Quartile => qrcode::EcLevel::Q, - ErrorCorrection::High => qrcode::EcLevel::H, - } - } -} - -/// An error that occurred when building a [`State`] for a [`QRCode`]. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)] -pub enum Error { - /// The data is too long to encode in a QR code for the chosen [`Version`]. - #[error( - "The data is too long to encode in a QR code for the chosen version" - )] - DataTooLong, - - /// The chosen [`Version`] and [`ErrorCorrection`] combination is invalid. - #[error( - "The chosen version and error correction level combination is invalid." - )] - InvalidVersion, - - /// One or more characters in the provided data are not supported by the - /// chosen [`Version`]. - #[error( - "One or more characters in the provided data are not supported by the \ - chosen version" - )] - UnsupportedCharacterSet, - - /// The chosen ECI designator is invalid. A valid designator should be - /// between 0 and 999999. - #[error( - "The chosen ECI designator is invalid. A valid designator should be \ - between 0 and 999999." - )] - InvalidEciDesignator, - - /// A character that does not belong to the character set was found. - #[error("A character that does not belong to the character set was found")] - InvalidCharacter, -} - -impl From for Error { - fn from(error: qrcode::types::QrError) -> Self { - use qrcode::types::QrError; - - match error { - QrError::DataTooLong => Error::DataTooLong, - QrError::InvalidVersion => Error::InvalidVersion, - QrError::UnsupportedCharacterSet => Error::UnsupportedCharacterSet, - QrError::InvalidEciDesignator => Error::InvalidEciDesignator, - QrError::InvalidCharacter => Error::InvalidCharacter, - } - } -} diff --git a/src/window.rs b/src/window.rs index 2018053f..26239065 100644 --- a/src/window.rs +++ b/src/window.rs @@ -8,5 +8,6 @@ pub use icon::Icon; pub use position::Position; pub use settings::Settings; -#[cfg(not(target_arch = "wasm32"))] -pub use crate::runtime::window::*; +pub use crate::core::window::*; +pub use crate::native::window::*; +pub use crate::shell::window::*; -- cgit From f4cf488e0b083b5d7b7612c650917233163ee9cb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 5 Mar 2023 04:15:10 +0100 Subject: Remove generic `Hasher` and `Event` from `subscription::Recipe` --- src/advanced.rs | 5 +++++ src/lib.rs | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/advanced.rs b/src/advanced.rs index 714076e0..9621c3bc 100644 --- a/src/advanced.rs +++ b/src/advanced.rs @@ -7,3 +7,8 @@ pub use crate::core::svg; pub use crate::core::text::{self, Text}; pub use crate::core::widget::{self, Widget}; pub use crate::core::{Clipboard, Shell}; + +pub mod subscription { + //! Write your own subscriptions. + pub use crate::native::futures::subscription::{EventStream, Recipe}; +} diff --git a/src/lib.rs b/src/lib.rs index b71b7781..b9f87d5d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -193,7 +193,6 @@ pub use crate::core::{ Rectangle, Size, Vector, }; pub use crate::native::Command; -pub use native::subscription; pub mod clipboard { //! Access the clipboard. @@ -233,6 +232,13 @@ pub mod mouse { pub use crate::core::mouse::{Button, Event, Interaction, ScrollDelta}; } +pub mod subscription { + //! Listen to external events in your application. + pub use iced_futures::subscription::{ + events, events_with, run, run_with_id, unfold, Subscription, + }; +} + #[cfg(feature = "system")] pub mod system { //! Retrieve system information. -- cgit From 99e0a71504456976ba88040f5d1d3bbc347694ea Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 5 Mar 2023 06:35:20 +0100 Subject: Rename `iced_native` to `iced_runtime` --- src/advanced.rs | 2 +- src/application.rs | 2 +- src/lib.rs | 10 +++++----- src/window.rs | 3 +-- 4 files changed, 8 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/advanced.rs b/src/advanced.rs index 9621c3bc..7afba85c 100644 --- a/src/advanced.rs +++ b/src/advanced.rs @@ -10,5 +10,5 @@ pub use crate::core::{Clipboard, Shell}; pub mod subscription { //! Write your own subscriptions. - pub use crate::native::futures::subscription::{EventStream, Recipe}; + pub use crate::runtime::futures::subscription::{EventStream, Recipe}; } diff --git a/src/application.rs b/src/application.rs index f5cf3317..c9ddf840 100644 --- a/src/application.rs +++ b/src/application.rs @@ -215,7 +215,7 @@ pub trait Application: Sized { struct Instance(A); -impl crate::native::Program for Instance +impl crate::runtime::Program for Instance where A: Application, { diff --git a/src/lib.rs b/src/lib.rs index b9f87d5d..c59d5058 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -169,7 +169,7 @@ use iced_widget::renderer; use iced_widget::style; use iced_winit as shell; use iced_winit::core; -use iced_winit::native; +use iced_winit::runtime; pub use iced_futures::futures; @@ -192,11 +192,11 @@ pub use crate::core::{ color, Alignment, Background, Color, ContentFit, Length, Padding, Point, Rectangle, Size, Vector, }; -pub use crate::native::Command; +pub use crate::runtime::Command; pub mod clipboard { //! Access the clipboard. - pub use crate::shell::clipboard::{read, write}; + pub use crate::runtime::clipboard::{read, write}; } pub mod executor { @@ -219,7 +219,7 @@ pub mod executor { pub mod font { //! Load and use fonts. pub use crate::core::font::*; - pub use crate::native::font::*; + pub use crate::runtime::font::*; } pub mod keyboard { @@ -242,7 +242,7 @@ pub mod subscription { #[cfg(feature = "system")] pub mod system { //! Retrieve system information. - pub use crate::native::system::Information; + pub use crate::runtime::system::Information; pub use crate::shell::system::*; } diff --git a/src/window.rs b/src/window.rs index 26239065..824915b2 100644 --- a/src/window.rs +++ b/src/window.rs @@ -9,5 +9,4 @@ pub use position::Position; pub use settings::Settings; pub use crate::core::window::*; -pub use crate::native::window::*; -pub use crate::shell::window::*; +pub use crate::runtime::window::*; -- cgit