Move winit logic from iced to iced_winit

- Added new `renderer::Windowed` trait. This shoud allow users to easily
  try different renderers by simply changing one line.
- Renamed `UserInterface` traits to `Application`, as the `run` method
  takes total control of the current thread.
- Moved `MouseCursor` back to `iced_native`. The new
  `renderer::Windowed` trait returns one on `draw`.
- Split `iced_native` renderer in multiple modules, for consistency.
This commit is contained in:
Héctor Ramón Jiménez 2019-10-09 05:36:49 +02:00
parent 1a93f0ef4a
commit a92a0b73ed
13 changed files with 245 additions and 155 deletions

View file

@ -206,6 +206,7 @@ mod element;
mod event;
mod hasher;
mod layout;
mod mouse_cursor;
mod node;
mod style;
mod user_interface;
@ -223,6 +224,7 @@ pub use element::Element;
pub use event::Event;
pub use hasher::Hasher;
pub use layout::Layout;
pub use mouse_cursor::MouseCursor;
pub use node::Node;
pub use renderer::Renderer;
pub use style::Style;

View file

@ -0,0 +1,35 @@
/// The state of the mouse cursor.
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum MouseCursor {
/// The cursor is out of the bounds of the user interface.
OutOfBounds,
/// The cursor is over a non-interactive widget.
Idle,
/// The cursor is over a clickable widget.
Pointer,
/// The cursor is over a busy widget.
Working,
/// The cursor is over a grabbable widget.
Grab,
/// The cursor is grabbing a widget.
Grabbing,
}
#[cfg(feature = "winit")]
impl From<MouseCursor> for winit::window::CursorIcon {
fn from(mouse_cursor: MouseCursor) -> winit::window::CursorIcon {
match mouse_cursor {
MouseCursor::OutOfBounds => winit::window::CursorIcon::Default,
MouseCursor::Idle => winit::window::CursorIcon::Default,
MouseCursor::Pointer => winit::window::CursorIcon::Hand,
MouseCursor::Working => winit::window::CursorIcon::Progress,
MouseCursor::Grab => winit::window::CursorIcon::Grab,
MouseCursor::Grabbing => winit::window::CursorIcon::Grabbing,
}
}
}

View file

@ -1,8 +1,10 @@
//! Write your own renderer.
//!
//! There is not a common entrypoint or trait for a __renderer__ in Iced.
//! Instead, every [`Widget`] constrains its generic `Renderer` type as
//! necessary.
//! You will need to implement the `Renderer` trait first. It simply contains
//! a `Primitive` associated type.
//!
//! There is no common trait to draw all the widgets. Instead, every [`Widget`]
//! constrains its generic `Renderer` type as necessary.
//!
//! This approach is flexible and composable. For instance, the
//! [`Text`] widget only needs a [`text::Renderer`] while a [`Checkbox`] widget
@ -17,32 +19,13 @@
//! [`text::Renderer`]: ../widget/text/trait.Renderer.html
//! [`Checkbox`]: ../widget/checkbox/struct.Checkbox.html
//! [`checkbox::Renderer`]: ../widget/checkbox/trait.Renderer.html
use crate::{Color, Layout, Point, Widget};
mod debugger;
mod windowed;
pub use debugger::Debugger;
pub use windowed::Windowed;
pub trait Renderer {
type Primitive;
}
/// A renderer able to graphically explain a [`Layout`].
///
/// [`Layout`]: ../struct.Layout.html
pub trait Debugger: Renderer {
/// Explains the [`Layout`] of an [`Element`] for debugging purposes.
///
/// This will be called when [`Element::explain`] has been used. It should
/// _explain_ the given [`Layout`] graphically.
///
/// A common approach consists in recursively rendering the bounds of the
/// [`Layout`] and its children.
///
/// [`Layout`]: struct.Layout.html
/// [`Element`]: struct.Element.html
/// [`Element::explain`]: struct.Element.html#method.explain
fn explain<Message>(
&mut self,
widget: &dyn Widget<Message, Self>,
layout: Layout<'_>,
cursor_position: Point,
color: Color,
) -> Self::Primitive;
}

View file

@ -0,0 +1,25 @@
use crate::{Color, Layout, Point, Widget};
/// A renderer able to graphically explain a [`Layout`].
///
/// [`Layout`]: ../struct.Layout.html
pub trait Debugger: super::Renderer {
/// Explains the [`Layout`] of an [`Element`] for debugging purposes.
///
/// This will be called when [`Element::explain`] has been used. It should
/// _explain_ the given [`Layout`] graphically.
///
/// A common approach consists in recursively rendering the bounds of the
/// [`Layout`] and its children.
///
/// [`Layout`]: struct.Layout.html
/// [`Element`]: struct.Element.html
/// [`Element::explain`]: struct.Element.html#method.explain
fn explain<Message>(
&mut self,
widget: &dyn Widget<Message, Self>,
layout: Layout<'_>,
cursor_position: Point,
color: Color,
) -> Self::Primitive;
}

View file

@ -0,0 +1,17 @@
use crate::MouseCursor;
use raw_window_handle::HasRawWindowHandle;
pub trait Windowed: super::Renderer {
type Target;
fn new<W: HasRawWindowHandle>(window: &W) -> Self;
fn target(&self, width: u16, height: u16) -> Self::Target;
fn draw(
&mut self,
target: &mut Self::Target,
primitive: &Self::Primitive,
) -> MouseCursor;
}