Create iced_widget subcrate and re-organize the whole codebase

This commit is contained in:
Héctor Ramón Jiménez 2023-03-04 05:37:11 +01:00
parent c54409d171
commit 3a0d34c024
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
209 changed files with 1959 additions and 2183 deletions

9
src/advanced.rs Normal file
View file

@ -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};

View file

@ -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>,
Self::Executor,
crate::renderer::window::Compositor<Self::Theme>,
crate::renderer::Compositor<Self::Theme>,
>(settings.into(), renderer_settings)?)
}
}
struct Instance<A: Application>(A);
impl<A> iced_winit::Program for Instance<A>
impl<A> crate::native::Program for Instance<A>
where
A: Application,
{
@ -231,7 +231,7 @@ where
}
}
impl<A> crate::runtime::Application for Instance<A>
impl<A> crate::shell::Application for Instance<A>
where
A: Application,
{

View file

@ -1,3 +0,0 @@
//! Access the clipboard.
#[cfg(not(target_arch = "wasm32"))]
pub use crate::runtime::clipboard::{read, write};

View file

@ -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>;

View file

@ -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<iced_winit::Error> for Error {
fn from(error: iced_winit::Error) -> Error {
impl From<shell::Error> 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)
}
}

View file

@ -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;

View file

@ -1,2 +0,0 @@
//! Listen and react to keyboard events.
pub use crate::runtime::keyboard::{Event, KeyCode, Modifiers};

View file

@ -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 iced_native::theme;
pub use runtime::event;
pub use runtime::font;
pub use runtime::subscription;
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};
}
#[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<Theme = style::Theme> = renderer::Renderer<Theme>;
#[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>;

View file

@ -1,2 +0,0 @@
//! Listen and react to mouse events.
pub use crate::runtime::mouse::{Button, Event, Interaction, ScrollDelta};

View file

@ -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>;
}

View file

@ -1,6 +0,0 @@
use crate::Error;
/// The result of running an [`Application`].
///
/// [`Application`]: crate::Application
pub type Result = std::result::Result<(), Error>;

View file

@ -1,2 +1,2 @@
//! Listen and react to touch events.
pub use crate::runtime::touch::{Event, Finger};
pub use crate::core::touch::{Event, Finger};

View file

@ -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<Message, Renderer = crate::Renderer> =
iced_native::widget::Radio<Message, Renderer>;
}
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<P, Message, Renderer>(program: P) -> Canvas<P, Message, Renderer>
where
Renderer: iced_renderer::geometry::Renderer,
P: canvas::Program<Message, Renderer>,
{
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<Handle>;
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<Message>() -> Command<Message>
where
Message: 'static,
{
Command::widget(operation::focusable::focus_previous())
}
/// Focuses the next focusable widget.
pub fn focus_next<Message>() -> Command<Message>
where
Message: 'static,
{
Command::widget(operation::focusable::focus_next())
}

View file

@ -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<Geometry>{
/// // 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<P, Message, Renderer = crate::Renderer>
where
Renderer: iced_renderer::geometry::Renderer,
P: Program<Message, Renderer>,
{
width: Length,
height: Length,
program: P,
message_: PhantomData<Message>,
theme_: PhantomData<Renderer>,
}
impl<P, Message, Renderer> Canvas<P, Message, Renderer>
where
Renderer: iced_renderer::geometry::Renderer,
P: Program<Message, Renderer>,
{
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<Length>) -> Self {
self.width = width.into();
self
}
/// Sets the height of the [`Canvas`].
pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height.into();
self
}
}
impl<P, Message, Renderer> Widget<Message, Renderer>
for Canvas<P, Message, Renderer>
where
Renderer: iced_renderer::geometry::Renderer,
P: Program<Message, Renderer>,
{
fn tag(&self) -> tree::Tag {
struct Tag<T>(T);
tree::Tag::of::<Tag<P::State>>()
}
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::<P::State>();
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::<P::State>();
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::<P::State>();
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<Canvas<P, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Message: 'a,
Renderer: 'a + iced_renderer::geometry::Renderer,
P: Program<Message, Renderer> + 'a,
{
fn from(
canvas: Canvas<P, Message, Renderer>,
) -> Element<'a, Message, Renderer> {
Element::new(canvas)
}
}

View file

@ -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<Point> {
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<Point> {
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<Point> {
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,
}
}
}

View file

@ -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),
}

View file

@ -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<Message, Renderer = crate::Renderer>
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<Message>) {
(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<Renderer::Geometry>;
/// 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<Message, Renderer, T> Program<Message, Renderer> for &T
where
Renderer: iced_renderer::geometry::Renderer,
T: Program<Message, Renderer>,
{
type State = T::State;
fn update(
&self,
state: &mut Self::State,
event: Event,
bounds: Rectangle,
cursor: Cursor,
) -> (event::Status, Option<Message>) {
T::update(self, state, event, bounds, cursor)
}
fn draw(
&self,
state: &Self::State,
renderer: &Renderer,
theme: &Renderer::Theme,
bounds: Rectangle,
cursor: Cursor,
) -> Vec<Renderer::Geometry> {
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)
}
}

View file

@ -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<Message, Renderer<Theme>> for QRCode<'a> {
fn width(&self) -> Length {
Length::Shrink
}
fn height(&self) -> Length {
Length::Shrink
}
fn layout(
&self,
_renderer: &Renderer<Theme>,
_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: &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<QRCode<'a>>
for Element<'a, Message, Renderer<Theme>>
{
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<qrcode::Color>,
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<Self, Error> {
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<Self, Error> {
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<Self, Error> {
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<Version> 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<ErrorCorrection> 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<qrcode::types::QrError> 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,
}
}
}

View file

@ -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::*;