From 03b34931383e701c39c653a7662a616fe21a0947 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 14 Oct 2021 16:07:22 +0700 Subject: Remove trait-specific draw logic in `iced_native` --- graphics/src/lib.rs | 2 +- graphics/src/overlay/menu.rs | 106 +------------- graphics/src/renderer.rs | 92 ++---------- graphics/src/widget/button.rs | 92 +----------- graphics/src/widget/canvas.rs | 49 ++++--- graphics/src/widget/checkbox.rs | 56 +------- graphics/src/widget/column.rs | 46 +----- graphics/src/widget/container.rs | 61 +------- graphics/src/widget/image.rs | 18 +-- graphics/src/widget/image/viewer.rs | 55 +------ graphics/src/widget/pane_grid.rs | 272 +---------------------------------- graphics/src/widget/pick_list.rs | 81 +---------- graphics/src/widget/progress_bar.rs | 53 +------ graphics/src/widget/qr_code.rs | 97 ++++++------- graphics/src/widget/radio.rs | 58 +------- graphics/src/widget/row.rs | 46 +----- graphics/src/widget/rule.rs | 57 +------- graphics/src/widget/scrollable.rs | 87 +---------- graphics/src/widget/slider.rs | 101 +------------ graphics/src/widget/space.rs | 14 -- graphics/src/widget/svg.rs | 18 +-- graphics/src/widget/text.rs | 43 +----- graphics/src/widget/text_input.rs | 182 +---------------------- graphics/src/widget/toggler.rs | 80 +---------- graphics/src/widget/tooltip.rs | 151 +------------------ graphics/src/window/compositor.rs | 18 +-- graphics/src/window/gl_compositor.rs | 9 +- 27 files changed, 121 insertions(+), 1823 deletions(-) (limited to 'graphics') diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index 54cdcb77..9c113da6 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -4,7 +4,7 @@ //! ![The native path of the Iced ecosystem](https://github.com/hecrj/iced/blob/0525d76ff94e828b7b21634fa94a747022001c83/docs/graphs/native.png?raw=true) //! //! [`iced`]: https://github.com/hecrj/iced -#![deny(missing_docs)] +//#![deny(missing_docs)] #![deny(missing_debug_implementations)] #![deny(unused_results)] #![deny(unsafe_code)] diff --git a/graphics/src/overlay/menu.rs b/graphics/src/overlay/menu.rs index 53f47984..7dfb48b9 100644 --- a/graphics/src/overlay/menu.rs +++ b/graphics/src/overlay/menu.rs @@ -1,9 +1,8 @@ //! Build and show dropdown menus. -use crate::alignment; use crate::backend::{self, Backend}; -use crate::{Primitive, Renderer}; +use crate::Renderer; -use iced_native::{mouse, overlay, Color, Font, Padding, Point, Rectangle}; +use iced_native::overlay; pub use iced_style::menu::Style; @@ -12,105 +11,4 @@ where B: Backend + backend::Text, { type Style = Style; - - fn decorate( - &mut self, - bounds: Rectangle, - _cursor_position: Point, - style: &Style, - (primitives, mouse_cursor): Self::Output, - ) -> Self::Output { - ( - Primitive::Group { - primitives: vec![ - Primitive::Quad { - bounds, - background: style.background, - border_color: style.border_color, - border_width: style.border_width, - border_radius: 0.0, - }, - primitives, - ], - }, - mouse_cursor, - ) - } - - fn draw( - &mut self, - bounds: Rectangle, - cursor_position: Point, - viewport: &Rectangle, - options: &[T], - hovered_option: Option, - padding: Padding, - text_size: u16, - font: Font, - style: &Style, - ) -> Self::Output { - use std::f32; - - let is_mouse_over = bounds.contains(cursor_position); - let option_height = (text_size + padding.vertical()) as usize; - - let mut primitives = Vec::new(); - - let offset = viewport.y - bounds.y; - let start = (offset / option_height as f32) as usize; - let end = - ((offset + viewport.height) / option_height as f32).ceil() as usize; - - let visible_options = &options[start..end.min(options.len())]; - - for (i, option) in visible_options.iter().enumerate() { - let i = start + i; - let is_selected = hovered_option == Some(i); - - let bounds = Rectangle { - x: bounds.x, - y: bounds.y + (option_height * i) as f32, - width: bounds.width, - height: f32::from(text_size + padding.vertical()), - }; - - if is_selected { - primitives.push(Primitive::Quad { - bounds, - background: style.selected_background, - border_color: Color::TRANSPARENT, - border_width: 0.0, - border_radius: 0.0, - }); - } - - primitives.push(Primitive::Text { - content: option.to_string(), - bounds: Rectangle { - x: bounds.x + padding.left as f32, - y: bounds.center_y(), - width: f32::INFINITY, - ..bounds - }, - size: f32::from(text_size), - font, - color: if is_selected { - style.selected_text_color - } else { - style.text_color - }, - horizontal_alignment: alignment::Horizontal::Left, - vertical_alignment: alignment::Vertical::Center, - }); - } - - ( - Primitive::Group { primitives }, - if is_mouse_over { - mouse::Interaction::Pointer - } else { - mouse::Interaction::default() - }, - ) - } } diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index fa63991b..cedffe7e 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -1,30 +1,29 @@ use crate::{Backend, Defaults, Primitive}; -use iced_native::layout::{self, Layout}; -use iced_native::mouse; -use iced_native::{ - Background, Color, Element, Point, Rectangle, Vector, Widget, -}; +use iced_native::layout; +use iced_native::{Element, Rectangle}; /// A backend-agnostic renderer that supports all the built-in widgets. #[derive(Debug)] pub struct Renderer { backend: B, + primitive: Primitive, } impl Renderer { /// Creates a new [`Renderer`] from the given [`Backend`]. pub fn new(backend: B) -> Self { - Self { backend } + Self { + backend, + primitive: Primitive::None, + } } - /// Returns a reference to the [`Backend`] of the [`Renderer`]. pub fn backend(&self) -> &B { &self.backend } - /// Returns a mutable reference to the [`Backend`] of the [`Renderer`]. - pub fn backend_mut(&mut self) -> &mut B { - &mut self.backend + pub fn present(&mut self, f: impl FnOnce(&mut B, &Primitive)) { + f(&mut self.backend, &self.primitive); } } @@ -32,7 +31,6 @@ impl iced_native::Renderer for Renderer where B: Backend, { - type Output = (Primitive, mouse::Interaction); type Defaults = Defaults; fn layout<'a, Message>( @@ -47,75 +45,5 @@ where layout } - fn overlay( - &mut self, - (base_primitive, base_cursor): (Primitive, mouse::Interaction), - (overlay_primitives, overlay_cursor): (Primitive, mouse::Interaction), - overlay_bounds: Rectangle, - ) -> (Primitive, mouse::Interaction) { - ( - Primitive::Group { - primitives: vec![ - base_primitive, - Primitive::Clip { - bounds: Rectangle { - width: overlay_bounds.width + 0.5, - height: overlay_bounds.height + 0.5, - ..overlay_bounds - }, - offset: Vector::new(0, 0), - content: Box::new(overlay_primitives), - }, - ], - }, - if base_cursor > overlay_cursor { - base_cursor - } else { - overlay_cursor - }, - ) - } -} - -impl layout::Debugger for Renderer -where - B: Backend, -{ - fn explain( - &mut self, - defaults: &Defaults, - widget: &dyn Widget, - layout: Layout<'_>, - cursor_position: Point, - viewport: &Rectangle, - color: Color, - ) -> Self::Output { - let (primitive, cursor) = - widget.draw(self, defaults, layout, cursor_position, viewport); - - let mut primitives = Vec::new(); - - explain_layout(layout, color, &mut primitives); - primitives.push(primitive); - - (Primitive::Group { primitives }, cursor) - } -} - -fn explain_layout( - layout: Layout<'_>, - color: Color, - primitives: &mut Vec, -) { - primitives.push(Primitive::Quad { - bounds: layout.bounds(), - background: Background::Color(Color::TRANSPARENT), - border_radius: 0.0, - border_width: 1.0, - border_color: [0.6, 0.6, 0.6, 0.5].into(), - }); - - for child in layout.children() { - explain_layout(child, color, primitives); - } + fn with_layer(&mut self, _bounds: Rectangle, _f: impl FnOnce(&mut Self)) {} } diff --git a/graphics/src/widget/button.rs b/graphics/src/widget/button.rs index 60400ed8..990aac9f 100644 --- a/graphics/src/widget/button.rs +++ b/graphics/src/widget/button.rs @@ -1,12 +1,8 @@ //! Allow your users to perform actions by pressing a button. //! //! A [`Button`] has some local [`State`]. -use crate::defaults::{self, Defaults}; -use crate::{Backend, Primitive, Renderer}; -use iced_native::mouse; -use iced_native::{ - Background, Color, Element, Layout, Padding, Point, Rectangle, Vector, -}; +use crate::{Backend, Renderer}; +use iced_native::Padding; pub use iced_native::button::State; pub use iced_style::button::{Style, StyleSheet}; @@ -24,88 +20,4 @@ where const DEFAULT_PADDING: Padding = Padding::new(5); type Style = Box; - - fn draw( - &mut self, - _defaults: &Defaults, - bounds: Rectangle, - cursor_position: Point, - is_disabled: bool, - is_pressed: bool, - style: &Box, - content: &Element<'_, Message, Self>, - content_layout: Layout<'_>, - ) -> Self::Output { - let is_mouse_over = bounds.contains(cursor_position); - - let styling = if is_disabled { - style.disabled() - } else if is_mouse_over { - if is_pressed { - style.pressed() - } else { - style.hovered() - } - } else { - style.active() - }; - - let (content, _) = content.draw( - self, - &Defaults { - text: defaults::Text { - color: styling.text_color, - }, - }, - content_layout, - cursor_position, - &bounds, - ); - - ( - if styling.background.is_some() || styling.border_width > 0.0 { - let background = Primitive::Quad { - bounds, - background: styling - .background - .unwrap_or(Background::Color(Color::TRANSPARENT)), - border_radius: styling.border_radius, - border_width: styling.border_width, - border_color: styling.border_color, - }; - - if styling.shadow_offset == Vector::default() { - Primitive::Group { - primitives: vec![background, content], - } - } else { - // TODO: Implement proper shadow support - let shadow = Primitive::Quad { - bounds: Rectangle { - x: bounds.x + styling.shadow_offset.x, - y: bounds.y + styling.shadow_offset.y, - ..bounds - }, - background: Background::Color( - [0.0, 0.0, 0.0, 0.5].into(), - ), - border_radius: styling.border_radius, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }; - - Primitive::Group { - primitives: vec![shadow, background, content], - } - } - } else { - content - }, - if is_mouse_over && !is_disabled { - mouse::Interaction::Pointer - } else { - mouse::Interaction::default() - }, - ) - } } diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index 7897c8ec..7bf00ca5 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -3,12 +3,10 @@ //! A [`Canvas`] widget can be used to draw different kinds of 2D shapes in a //! [`Frame`]. It can be used for animation, data visualization, game graphics, //! and more! -use crate::{Backend, Defaults, Primitive, Renderer}; +use crate::{Backend, Defaults, Renderer}; use iced_native::layout; -use iced_native::mouse; use iced_native::{ - Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector, - Widget, + Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; use std::hash::Hash; use std::marker::PhantomData; @@ -190,28 +188,29 @@ where &self, _renderer: &mut Renderer, _defaults: &Defaults, - layout: Layout<'_>, - cursor_position: Point, + _layout: Layout<'_>, + _cursor_position: Point, _viewport: &Rectangle, - ) -> (Primitive, mouse::Interaction) { - let bounds = layout.bounds(); - let translation = Vector::new(bounds.x, bounds.y); - let cursor = Cursor::from_window_position(cursor_position); - - ( - Primitive::Translate { - translation, - content: Box::new(Primitive::Group { - primitives: self - .program - .draw(bounds, cursor) - .into_iter() - .map(Geometry::into_primitive) - .collect(), - }), - }, - self.program.mouse_interaction(bounds, cursor), - ) + ) { + // let bounds = layout.bounds(); + // let translation = Vector::new(bounds.x, bounds.y); + // let cursor = Cursor::from_window_position(cursor_position); + + // ( + // Primitive::Translate { + // translation, + // content: Box::new(Primitive::Group { + // primitives: self + // .program + // .draw(bounds, cursor) + // .into_iter() + // .map(Geometry::into_primitive) + // .collect(), + // }), + // }, + // self.program.mouse_interaction(bounds, cursor), + // ) + // TODO } fn hash_layout(&self, state: &mut Hasher) { diff --git a/graphics/src/widget/checkbox.rs b/graphics/src/widget/checkbox.rs index 620bfc9e..3b756525 100644 --- a/graphics/src/widget/checkbox.rs +++ b/graphics/src/widget/checkbox.rs @@ -1,10 +1,8 @@ //! Show toggle controls using checkboxes. -use crate::alignment; use crate::backend::{self, Backend}; -use crate::{Primitive, Rectangle, Renderer}; +use crate::Renderer; use iced_native::checkbox; -use iced_native::mouse; pub use iced_style::checkbox::{Style, StyleSheet}; @@ -22,56 +20,4 @@ where const DEFAULT_SIZE: u16 = 20; const DEFAULT_SPACING: u16 = 15; - - fn draw( - &mut self, - bounds: Rectangle, - is_checked: bool, - is_mouse_over: bool, - (label, _): Self::Output, - style_sheet: &Self::Style, - ) -> Self::Output { - let style = if is_mouse_over { - style_sheet.hovered(is_checked) - } else { - style_sheet.active(is_checked) - }; - - let checkbox = Primitive::Quad { - bounds, - background: style.background, - border_radius: style.border_radius, - border_width: style.border_width, - border_color: style.border_color, - }; - - ( - Primitive::Group { - primitives: if is_checked { - let check = Primitive::Text { - content: B::CHECKMARK_ICON.to_string(), - font: B::ICON_FONT, - size: bounds.height * 0.7, - bounds: Rectangle { - x: bounds.center_x(), - y: bounds.center_y(), - ..bounds - }, - color: style.checkmark_color, - horizontal_alignment: alignment::Horizontal::Center, - vertical_alignment: alignment::Vertical::Center, - }; - - vec![checkbox, check, label] - } else { - vec![checkbox, label] - }, - }, - if is_mouse_over { - mouse::Interaction::Pointer - } else { - mouse::Interaction::default() - }, - ) - } } diff --git a/graphics/src/widget/column.rs b/graphics/src/widget/column.rs index 0cf56842..567529e5 100644 --- a/graphics/src/widget/column.rs +++ b/graphics/src/widget/column.rs @@ -1,49 +1,5 @@ -use crate::{Backend, Primitive, Renderer}; -use iced_native::column; -use iced_native::mouse; -use iced_native::{Element, Layout, Point, Rectangle}; +use crate::Renderer; /// A container that distributes its contents vertically. pub type Column<'a, Message, Backend> = iced_native::Column<'a, Message, Renderer>; - -impl column::Renderer for Renderer -where - B: Backend, -{ - fn draw( - &mut self, - defaults: &Self::Defaults, - content: &[Element<'_, Message, Self>], - layout: Layout<'_>, - cursor_position: Point, - viewport: &Rectangle, - ) -> Self::Output { - let mut mouse_interaction = mouse::Interaction::default(); - - ( - Primitive::Group { - primitives: content - .iter() - .zip(layout.children()) - .map(|(child, layout)| { - let (primitive, new_mouse_interaction) = child.draw( - self, - defaults, - layout, - cursor_position, - viewport, - ); - - if new_mouse_interaction > mouse_interaction { - mouse_interaction = new_mouse_interaction; - } - - primitive - }) - .collect(), - }, - mouse_interaction, - ) - } -} diff --git a/graphics/src/widget/container.rs b/graphics/src/widget/container.rs index aae3e1d8..811a0c7f 100644 --- a/graphics/src/widget/container.rs +++ b/graphics/src/widget/container.rs @@ -1,8 +1,6 @@ //! Decorate content and apply alignment. use crate::container; -use crate::defaults::{self, Defaults}; -use crate::{Backend, Primitive, Renderer}; -use iced_native::{Background, Color, Element, Layout, Point, Rectangle}; +use crate::{Backend, Renderer}; pub use iced_style::container::{Style, StyleSheet}; @@ -18,61 +16,4 @@ where B: Backend, { type Style = Box; - - fn draw( - &mut self, - defaults: &Defaults, - bounds: Rectangle, - cursor_position: Point, - viewport: &Rectangle, - style_sheet: &Self::Style, - content: &Element<'_, Message, Self>, - content_layout: Layout<'_>, - ) -> Self::Output { - let style = style_sheet.style(); - - let defaults = Defaults { - text: defaults::Text { - color: style.text_color.unwrap_or(defaults.text.color), - }, - }; - - let (content, mouse_interaction) = content.draw( - self, - &defaults, - content_layout, - cursor_position, - viewport, - ); - - if let Some(background) = background(bounds, &style) { - ( - Primitive::Group { - primitives: vec![background, content], - }, - mouse_interaction, - ) - } else { - (content, mouse_interaction) - } - } -} - -pub(crate) fn background( - bounds: Rectangle, - style: &container::Style, -) -> Option { - if style.background.is_some() || style.border_width > 0.0 { - Some(Primitive::Quad { - bounds, - background: style - .background - .unwrap_or(Background::Color(Color::TRANSPARENT)), - border_radius: style.border_radius, - border_width: style.border_width, - border_color: style.border_color, - }) - } else { - None - } } diff --git a/graphics/src/widget/image.rs b/graphics/src/widget/image.rs index bdf03de3..b55ba32f 100644 --- a/graphics/src/widget/image.rs +++ b/graphics/src/widget/image.rs @@ -3,10 +3,8 @@ pub mod viewer; use crate::backend::{self, Backend}; -use crate::{Primitive, Renderer}; +use crate::Renderer; use iced_native::image; -use iced_native::mouse; -use iced_native::Layout; pub use iced_native::image::{Handle, Image, Viewer}; @@ -17,18 +15,4 @@ where fn dimensions(&self, handle: &image::Handle) -> (u32, u32) { self.backend().dimensions(handle) } - - fn draw( - &mut self, - handle: image::Handle, - layout: Layout<'_>, - ) -> Self::Output { - ( - Primitive::Image { - handle, - bounds: layout.bounds(), - }, - mouse::Interaction::default(), - ) - } } diff --git a/graphics/src/widget/image/viewer.rs b/graphics/src/widget/image/viewer.rs index 28dffc4f..ea7d8591 100644 --- a/graphics/src/widget/image/viewer.rs +++ b/graphics/src/widget/image/viewer.rs @@ -1,55 +1,2 @@ //! Zoom and pan on an image. -use crate::backend::{self, Backend}; -use crate::{Primitive, Renderer}; - -use iced_native::image; -use iced_native::image::viewer; -use iced_native::mouse; -use iced_native::{Rectangle, Size, Vector}; - -impl viewer::Renderer for Renderer -where - B: Backend + backend::Image, -{ - fn draw( - &mut self, - state: &viewer::State, - bounds: Rectangle, - image_size: Size, - translation: Vector, - handle: image::Handle, - is_mouse_over: bool, - ) -> Self::Output { - ( - { - Primitive::Clip { - bounds, - content: Box::new(Primitive::Translate { - translation, - content: Box::new(Primitive::Image { - handle, - bounds: Rectangle { - x: bounds.x, - y: bounds.y, - ..Rectangle::with_size(image_size) - }, - }), - }), - offset: Vector::new(0, 0), - } - }, - { - if state.is_cursor_grabbed() { - mouse::Interaction::Grabbing - } else if is_mouse_over - && (image_size.width > bounds.width - || image_size.height > bounds.height) - { - mouse::Interaction::Grab - } else { - mouse::Interaction::Idle - } - }, - ) - } -} +pub use iced_native::image::Viewer; diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index 92cdbb77..8c6b0f82 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -7,12 +7,8 @@ //! drag and drop, and hotkey support. //! //! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.3/examples/pane_grid -use crate::defaults; -use crate::{Backend, Color, Primitive, Renderer}; -use iced_native::container; -use iced_native::mouse; +use crate::{Backend, Renderer}; use iced_native::pane_grid; -use iced_native::{Element, Layout, Point, Rectangle, Vector}; pub use iced_native::pane_grid::{ Axis, Configuration, Content, Direction, DragEvent, Node, Pane, @@ -35,270 +31,4 @@ where B: Backend, { type Style = Box; - - fn draw( - &mut self, - defaults: &Self::Defaults, - content: &[(Pane, Content<'_, Message, Self>)], - dragging: Option<(Pane, Point)>, - resizing: Option<(Axis, Rectangle, bool)>, - layout: Layout<'_>, - style_sheet: &::Style, - cursor_position: Point, - viewport: &Rectangle, - ) -> Self::Output { - let pane_cursor_position = if dragging.is_some() { - // TODO: Remove once cursor availability is encoded in the type - // system - Point::new(-1.0, -1.0) - } else { - cursor_position - }; - - let mut mouse_interaction = mouse::Interaction::default(); - let mut dragged_pane = None; - - let mut panes: Vec<_> = content - .iter() - .zip(layout.children()) - .enumerate() - .map(|(i, ((id, pane), layout))| { - let (primitive, new_mouse_interaction) = pane.draw( - self, - defaults, - layout, - pane_cursor_position, - viewport, - ); - - if new_mouse_interaction > mouse_interaction { - mouse_interaction = new_mouse_interaction; - } - - if let Some((dragging, origin)) = dragging { - if *id == dragging { - dragged_pane = Some((i, layout, origin)); - } - } - - primitive - }) - .collect(); - - let mut primitives = if let Some((index, layout, origin)) = dragged_pane - { - let pane = panes.remove(index); - let bounds = layout.bounds(); - - // TODO: Fix once proper layering is implemented. - // This is a pretty hacky way to achieve layering. - let clip = Primitive::Clip { - bounds: Rectangle { - x: cursor_position.x - origin.x, - y: cursor_position.y - origin.y, - width: bounds.width + 0.5, - height: bounds.height + 0.5, - }, - offset: Vector::new(0, 0), - content: Box::new(Primitive::Translate { - translation: Vector::new( - cursor_position.x - bounds.x - origin.x, - cursor_position.y - bounds.y - origin.y, - ), - content: Box::new(pane), - }), - }; - - panes.push(clip); - - panes - } else { - panes - }; - - let (primitives, mouse_interaction) = - if let Some((axis, split_region, is_picked)) = resizing { - let highlight = if is_picked { - style_sheet.picked_split() - } else { - style_sheet.hovered_split() - }; - - if let Some(highlight) = highlight { - primitives.push(Primitive::Quad { - bounds: match axis { - Axis::Horizontal => Rectangle { - x: split_region.x, - y: (split_region.y - + (split_region.height - highlight.width) - / 2.0) - .round(), - width: split_region.width, - height: highlight.width, - }, - Axis::Vertical => Rectangle { - x: (split_region.x - + (split_region.width - highlight.width) - / 2.0) - .round(), - y: split_region.y, - width: highlight.width, - height: split_region.height, - }, - }, - background: highlight.color.into(), - border_radius: 0.0, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }); - } - - ( - primitives, - match axis { - Axis::Horizontal => { - mouse::Interaction::ResizingVertically - } - Axis::Vertical => { - mouse::Interaction::ResizingHorizontally - } - }, - ) - } else { - (primitives, mouse_interaction) - }; - - ( - Primitive::Group { primitives }, - if dragging.is_some() { - mouse::Interaction::Grabbing - } else { - mouse_interaction - }, - ) - } - - fn draw_pane( - &mut self, - defaults: &Self::Defaults, - bounds: Rectangle, - style_sheet: &::Style, - title_bar: Option<(&TitleBar<'_, Message, Self>, Layout<'_>)>, - body: (&Element<'_, Message, Self>, Layout<'_>), - cursor_position: Point, - viewport: &Rectangle, - ) -> Self::Output { - let style = style_sheet.style(); - let (body, body_layout) = body; - - let (body_primitive, body_interaction) = - body.draw(self, defaults, body_layout, cursor_position, viewport); - - let background = crate::widget::container::background(bounds, &style); - - if let Some((title_bar, title_bar_layout)) = title_bar { - let show_controls = bounds.contains(cursor_position); - let is_over_pick_area = - title_bar.is_over_pick_area(title_bar_layout, cursor_position); - - let (title_bar_primitive, title_bar_interaction) = title_bar.draw( - self, - defaults, - title_bar_layout, - cursor_position, - viewport, - show_controls, - ); - - ( - Primitive::Group { - primitives: vec![ - background.unwrap_or(Primitive::None), - title_bar_primitive, - body_primitive, - ], - }, - if title_bar_interaction > body_interaction { - title_bar_interaction - } else if is_over_pick_area { - mouse::Interaction::Grab - } else { - body_interaction - }, - ) - } else { - ( - if let Some(background) = background { - Primitive::Group { - primitives: vec![background, body_primitive], - } - } else { - body_primitive - }, - body_interaction, - ) - } - } - - fn draw_title_bar( - &mut self, - defaults: &Self::Defaults, - bounds: Rectangle, - style_sheet: &::Style, - content: (&Element<'_, Message, Self>, Layout<'_>), - controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>, - cursor_position: Point, - viewport: &Rectangle, - ) -> Self::Output { - let style = style_sheet.style(); - let (title_content, title_layout) = content; - - let defaults = Self::Defaults { - text: defaults::Text { - color: style.text_color.unwrap_or(defaults.text.color), - }, - }; - - let background = crate::widget::container::background(bounds, &style); - - let (title_primitive, title_interaction) = title_content.draw( - self, - &defaults, - title_layout, - cursor_position, - viewport, - ); - - if let Some((controls, controls_layout)) = controls { - let (controls_primitive, controls_interaction) = controls.draw( - self, - &defaults, - controls_layout, - cursor_position, - viewport, - ); - - ( - Primitive::Group { - primitives: vec![ - background.unwrap_or(Primitive::None), - title_primitive, - controls_primitive, - ], - }, - controls_interaction.max(title_interaction), - ) - } else { - ( - if let Some(background) = background { - Primitive::Group { - primitives: vec![background, title_primitive], - } - } else { - title_primitive - }, - title_interaction, - ) - } - } } diff --git a/graphics/src/widget/pick_list.rs b/graphics/src/widget/pick_list.rs index 532840b8..54f42cde 100644 --- a/graphics/src/widget/pick_list.rs +++ b/graphics/src/widget/pick_list.rs @@ -1,9 +1,8 @@ //! Display a dropdown list of selectable values. -use crate::alignment; use crate::backend::{self, Backend}; -use crate::{Primitive, Renderer}; +use crate::Renderer; -use iced_native::{mouse, Font, Padding, Point, Rectangle}; +use iced_native::Padding; use iced_style::menu; pub use iced_native::pick_list::State; @@ -24,80 +23,4 @@ where fn menu_style(style: &Box) -> menu::Style { style.menu() } - - fn draw( - &mut self, - bounds: Rectangle, - cursor_position: Point, - selected: Option, - placeholder: Option<&str>, - padding: Padding, - text_size: u16, - font: Font, - style: &Box, - ) -> Self::Output { - let is_mouse_over = bounds.contains(cursor_position); - let is_selected = selected.is_some(); - - let style = if is_mouse_over { - style.hovered() - } else { - style.active() - }; - - let background = Primitive::Quad { - bounds, - background: style.background, - border_color: style.border_color, - border_width: style.border_width, - border_radius: style.border_radius, - }; - - let arrow_down = Primitive::Text { - content: B::ARROW_DOWN_ICON.to_string(), - font: B::ICON_FONT, - size: bounds.height * style.icon_size, - bounds: Rectangle { - x: bounds.x + bounds.width - f32::from(padding.horizontal()), - y: bounds.center_y(), - ..bounds - }, - color: style.text_color, - horizontal_alignment: alignment::Horizontal::Right, - vertical_alignment: alignment::Vertical::Center, - }; - - ( - Primitive::Group { - primitives: if let Some(label) = - selected.or_else(|| placeholder.map(str::to_string)) - { - let label = Primitive::Text { - content: label, - size: f32::from(text_size), - font, - color: is_selected - .then(|| style.text_color) - .unwrap_or(style.placeholder_color), - bounds: Rectangle { - x: bounds.x + f32::from(padding.left), - y: bounds.center_y(), - ..bounds - }, - horizontal_alignment: alignment::Horizontal::Left, - vertical_alignment: alignment::Vertical::Center, - }; - - vec![background, label, arrow_down] - } else { - vec![background, arrow_down] - }, - }, - if is_mouse_over { - mouse::Interaction::Pointer - } else { - mouse::Interaction::default() - }, - ) - } } diff --git a/graphics/src/widget/progress_bar.rs b/graphics/src/widget/progress_bar.rs index 32ee42c6..3d21b7b9 100644 --- a/graphics/src/widget/progress_bar.rs +++ b/graphics/src/widget/progress_bar.rs @@ -2,10 +2,8 @@ //! //! A [`ProgressBar`] has a range of possible values and a current value, //! as well as a length, height and style. -use crate::{Backend, Primitive, Renderer}; -use iced_native::mouse; +use crate::{Backend, Renderer}; use iced_native::progress_bar; -use iced_native::{Color, Rectangle}; pub use iced_style::progress_bar::{Style, StyleSheet}; @@ -22,53 +20,4 @@ where type Style = Box; const DEFAULT_HEIGHT: u16 = 30; - - fn draw( - &self, - bounds: Rectangle, - range: std::ops::RangeInclusive, - value: f32, - style_sheet: &Self::Style, - ) -> Self::Output { - let style = style_sheet.style(); - let (range_start, range_end) = range.into_inner(); - - let active_progress_width = if range_start >= range_end { - 0.0 - } else { - bounds.width * (value - range_start) / (range_end - range_start) - }; - - let background = Primitive::Group { - primitives: vec![Primitive::Quad { - bounds: Rectangle { ..bounds }, - background: style.background, - border_radius: style.border_radius, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }], - }; - - ( - if active_progress_width > 0.0 { - let bar = Primitive::Quad { - bounds: Rectangle { - width: active_progress_width, - ..bounds - }, - background: style.bar, - border_radius: style.border_radius, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }; - - Primitive::Group { - primitives: vec![background, bar], - } - } else { - background - }, - mouse::Interaction::default(), - ) - } } diff --git a/graphics/src/widget/qr_code.rs b/graphics/src/widget/qr_code.rs index b3a01dd7..a809d99f 100644 --- a/graphics/src/widget/qr_code.rs +++ b/graphics/src/widget/qr_code.rs @@ -1,10 +1,10 @@ //! Encode and display information in a QR code. use crate::canvas; -use crate::{Backend, Defaults, Primitive, Renderer, Vector}; +use crate::{Backend, Defaults, Renderer}; use iced_native::{ - layout, mouse, Color, Element, Hasher, Layout, Length, Point, Rectangle, - Size, Widget, + layout, Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, + Widget, }; use thiserror::Error; @@ -82,53 +82,54 @@ where &self, _renderer: &mut Renderer, _defaults: &Defaults, - layout: Layout<'_>, + _layout: Layout<'_>, _cursor_position: Point, _viewport: &Rectangle, - ) -> (Primitive, mouse::Interaction) { - let bounds = layout.bounds(); - let side_length = self.state.width + 2 * QUIET_ZONE; - - // Reuse cache if possible - let geometry = self.state.cache.draw(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, - ); - }); - }); - - ( - Primitive::Translate { - translation: Vector::new(bounds.x, bounds.y), - content: Box::new(geometry.into_primitive()), - }, - mouse::Interaction::default(), - ) + ) { + // let bounds = layout.bounds(); + // let side_length = self.state.width + 2 * QUIET_ZONE; + + // // Reuse cache if possible + // let geometry = self.state.cache.draw(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, + // ); + // }); + // }); + + // ( + // Primitive::Translate { + // translation: Vector::new(bounds.x, bounds.y), + // content: Box::new(geometry.into_primitive()), + // }, + // mouse::Interaction::default(), + // ) + // TODO } } diff --git a/graphics/src/widget/radio.rs b/graphics/src/widget/radio.rs index fd3d8145..cd83f2ff 100644 --- a/graphics/src/widget/radio.rs +++ b/graphics/src/widget/radio.rs @@ -1,8 +1,6 @@ //! Create choices using radio buttons. -use crate::{Backend, Primitive, Renderer}; -use iced_native::mouse; +use crate::{Backend, Renderer}; use iced_native::radio; -use iced_native::{Background, Color, Rectangle}; pub use iced_style::radio::{Style, StyleSheet}; @@ -21,58 +19,4 @@ where const DEFAULT_SIZE: u16 = 28; const DEFAULT_SPACING: u16 = 15; - - fn draw( - &mut self, - bounds: Rectangle, - is_selected: bool, - is_mouse_over: bool, - (label, _): Self::Output, - style_sheet: &Self::Style, - ) -> Self::Output { - let style = if is_mouse_over { - style_sheet.hovered() - } else { - style_sheet.active() - }; - - let size = bounds.width; - let dot_size = size / 2.0; - - let radio = Primitive::Quad { - bounds, - background: style.background, - border_radius: size / 2.0, - border_width: style.border_width, - border_color: style.border_color, - }; - - ( - Primitive::Group { - primitives: if is_selected { - let radio_circle = Primitive::Quad { - bounds: Rectangle { - x: bounds.x + dot_size / 2.0, - y: bounds.y + dot_size / 2.0, - width: bounds.width - dot_size, - height: bounds.height - dot_size, - }, - background: Background::Color(style.dot_color), - border_radius: dot_size / 2.0, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }; - - vec![radio, radio_circle, label] - } else { - vec![radio, label] - }, - }, - if is_mouse_over { - mouse::Interaction::Pointer - } else { - mouse::Interaction::default() - }, - ) - } } diff --git a/graphics/src/widget/row.rs b/graphics/src/widget/row.rs index 397d80bf..55960c04 100644 --- a/graphics/src/widget/row.rs +++ b/graphics/src/widget/row.rs @@ -1,49 +1,5 @@ -use crate::{Backend, Primitive, Renderer}; -use iced_native::mouse; -use iced_native::row; -use iced_native::{Element, Layout, Point, Rectangle}; +use crate::Renderer; /// A container that distributes its contents horizontally. pub type Row<'a, Message, Backend> = iced_native::Row<'a, Message, Renderer>; - -impl row::Renderer for Renderer -where - B: Backend, -{ - fn draw( - &mut self, - defaults: &Self::Defaults, - content: &[Element<'_, Message, Self>], - layout: Layout<'_>, - cursor_position: Point, - viewport: &Rectangle, - ) -> Self::Output { - let mut mouse_interaction = mouse::Interaction::default(); - - ( - Primitive::Group { - primitives: content - .iter() - .zip(layout.children()) - .map(|(child, layout)| { - let (primitive, new_mouse_interaction) = child.draw( - self, - defaults, - layout, - cursor_position, - viewport, - ); - - if new_mouse_interaction > mouse_interaction { - mouse_interaction = new_mouse_interaction; - } - - primitive - }) - .collect(), - }, - mouse_interaction, - ) - } -} diff --git a/graphics/src/widget/rule.rs b/graphics/src/widget/rule.rs index 835ebed8..62766f82 100644 --- a/graphics/src/widget/rule.rs +++ b/graphics/src/widget/rule.rs @@ -1,9 +1,7 @@ //! Display a horizontal or vertical rule for dividing content. -use crate::{Backend, Primitive, Renderer}; -use iced_native::mouse; +use crate::{Backend, Renderer}; use iced_native::rule; -use iced_native::{Background, Color, Rectangle}; pub use iced_style::rule::{FillMode, Style, StyleSheet}; @@ -17,57 +15,4 @@ where B: Backend, { type Style = Box; - - fn draw( - &mut self, - bounds: Rectangle, - style_sheet: &Self::Style, - is_horizontal: bool, - ) -> Self::Output { - let style = style_sheet.style(); - - let line = if is_horizontal { - let line_y = (bounds.y + (bounds.height / 2.0) - - (style.width as f32 / 2.0)) - .round(); - - let (offset, line_width) = style.fill_mode.fill(bounds.width); - let line_x = bounds.x + offset; - - Primitive::Quad { - bounds: Rectangle { - x: line_x, - y: line_y, - width: line_width, - height: style.width as f32, - }, - background: Background::Color(style.color), - border_radius: style.radius, - border_width: 0.0, - border_color: Color::TRANSPARENT, - } - } else { - let line_x = (bounds.x + (bounds.width / 2.0) - - (style.width as f32 / 2.0)) - .round(); - - let (offset, line_height) = style.fill_mode.fill(bounds.height); - let line_y = bounds.y + offset; - - Primitive::Quad { - bounds: Rectangle { - x: line_x, - y: line_y, - width: style.width as f32, - height: line_height, - }, - background: Background::Color(style.color), - border_radius: style.radius, - border_width: 0.0, - border_color: Color::TRANSPARENT, - } - }; - - (line, mouse::Interaction::default()) - } } diff --git a/graphics/src/widget/scrollable.rs b/graphics/src/widget/scrollable.rs index 2220e4b8..f1fe0d2d 100644 --- a/graphics/src/widget/scrollable.rs +++ b/graphics/src/widget/scrollable.rs @@ -1,8 +1,7 @@ //! Navigate an endless amount of content with a scrollbar. -use crate::{Backend, Primitive, Renderer}; -use iced_native::mouse; +use crate::{Backend, Renderer}; use iced_native::scrollable; -use iced_native::{Background, Color, Rectangle, Vector}; +use iced_native::Rectangle; pub use iced_native::scrollable::State; pub use iced_style::scrollable::{Scrollbar, Scroller, StyleSheet}; @@ -73,86 +72,4 @@ where None } } - - fn draw( - &mut self, - state: &scrollable::State, - bounds: Rectangle, - _content_bounds: Rectangle, - is_mouse_over: bool, - is_mouse_over_scrollbar: bool, - scrollbar: Option, - offset: u32, - style_sheet: &Self::Style, - (content, mouse_interaction): Self::Output, - ) -> Self::Output { - ( - if let Some(scrollbar) = scrollbar { - let clip = Primitive::Clip { - bounds, - offset: Vector::new(0, offset), - content: Box::new(content), - }; - - let style = if state.is_scroller_grabbed() { - style_sheet.dragging() - } else if is_mouse_over_scrollbar { - style_sheet.hovered() - } else { - style_sheet.active() - }; - - let is_scrollbar_visible = - style.background.is_some() || style.border_width > 0.0; - - let scroller = if is_mouse_over - || state.is_scroller_grabbed() - || is_scrollbar_visible - { - Primitive::Quad { - bounds: scrollbar.scroller.bounds, - background: Background::Color(style.scroller.color), - border_radius: style.scroller.border_radius, - border_width: style.scroller.border_width, - border_color: style.scroller.border_color, - } - } else { - Primitive::None - }; - - let scrollbar = if is_scrollbar_visible { - Primitive::Quad { - bounds: scrollbar.bounds, - background: style - .background - .unwrap_or(Background::Color(Color::TRANSPARENT)), - border_radius: style.border_radius, - border_width: style.border_width, - border_color: style.border_color, - } - } else { - Primitive::None - }; - - let scroll = Primitive::Clip { - bounds, - offset: Vector::new(0, 0), - content: Box::new(Primitive::Group { - primitives: vec![scrollbar, scroller], - }), - }; - - Primitive::Group { - primitives: vec![clip, scroll], - } - } else { - content - }, - if is_mouse_over_scrollbar || state.is_scroller_grabbed() { - mouse::Interaction::Idle - } else { - mouse_interaction - }, - ) - } } diff --git a/graphics/src/widget/slider.rs b/graphics/src/widget/slider.rs index aeceec3f..5125d66c 100644 --- a/graphics/src/widget/slider.rs +++ b/graphics/src/widget/slider.rs @@ -1,10 +1,8 @@ //! Display an interactive selector of a single value from a range of values. //! //! A [`Slider`] has some local [`State`]. -use crate::{Backend, Primitive, Renderer}; -use iced_native::mouse; +use crate::{Backend, Renderer}; use iced_native::slider; -use iced_native::{Background, Color, Point, Rectangle}; pub use iced_native::slider::State; pub use iced_style::slider::{Handle, HandleShape, Style, StyleSheet}; @@ -23,101 +21,4 @@ where type Style = Box; const DEFAULT_HEIGHT: u16 = 22; - - fn draw( - &mut self, - bounds: Rectangle, - cursor_position: Point, - range: std::ops::RangeInclusive, - value: f32, - is_dragging: bool, - style_sheet: &Self::Style, - ) -> Self::Output { - let is_mouse_over = bounds.contains(cursor_position); - - let style = if is_dragging { - style_sheet.dragging() - } else if is_mouse_over { - style_sheet.hovered() - } else { - style_sheet.active() - }; - - let rail_y = bounds.y + (bounds.height / 2.0).round(); - - let (rail_top, rail_bottom) = ( - Primitive::Quad { - bounds: Rectangle { - x: bounds.x, - y: rail_y, - width: bounds.width, - height: 2.0, - }, - background: Background::Color(style.rail_colors.0), - border_radius: 0.0, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }, - Primitive::Quad { - bounds: Rectangle { - x: bounds.x, - y: rail_y + 2.0, - width: bounds.width, - height: 2.0, - }, - background: Background::Color(style.rail_colors.1), - border_radius: 0.0, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }, - ); - - let (handle_width, handle_height, handle_border_radius) = match style - .handle - .shape - { - HandleShape::Circle { radius } => { - (radius * 2.0, radius * 2.0, radius) - } - HandleShape::Rectangle { - width, - border_radius, - } => (f32::from(width), f32::from(bounds.height), border_radius), - }; - - let (range_start, range_end) = range.into_inner(); - - let handle_offset = if range_start >= range_end { - 0.0 - } else { - (bounds.width - handle_width) * (value - range_start) - / (range_end - range_start) - }; - - let handle = Primitive::Quad { - bounds: Rectangle { - x: bounds.x + handle_offset.round(), - y: rail_y - handle_height / 2.0, - width: handle_width, - height: handle_height, - }, - background: Background::Color(style.handle.color), - border_radius: handle_border_radius, - border_width: style.handle.border_width, - border_color: style.handle.border_color, - }; - - ( - Primitive::Group { - primitives: vec![rail_top, rail_bottom, handle], - }, - if is_dragging { - mouse::Interaction::Grabbing - } else if is_mouse_over { - mouse::Interaction::Grab - } else { - mouse::Interaction::default() - }, - ) - } } diff --git a/graphics/src/widget/space.rs b/graphics/src/widget/space.rs index 1f31eabe..a4d60d4b 100644 --- a/graphics/src/widget/space.rs +++ b/graphics/src/widget/space.rs @@ -1,15 +1 @@ -use crate::{Backend, Primitive, Renderer}; -use iced_native::mouse; -use iced_native::space; -use iced_native::Rectangle; - pub use iced_native::Space; - -impl space::Renderer for Renderer -where - B: Backend, -{ - fn draw(&mut self, _bounds: Rectangle) -> Self::Output { - (Primitive::None, mouse::Interaction::default()) - } -} diff --git a/graphics/src/widget/svg.rs b/graphics/src/widget/svg.rs index 8b5ed66a..b74a0abb 100644 --- a/graphics/src/widget/svg.rs +++ b/graphics/src/widget/svg.rs @@ -1,7 +1,7 @@ //! Display vector graphics in your application. use crate::backend::{self, Backend}; -use crate::{Primitive, Renderer}; -use iced_native::{mouse, svg, Layout}; +use crate::Renderer; +use iced_native::svg; pub use iced_native::svg::{Handle, Svg}; @@ -12,18 +12,4 @@ where fn dimensions(&self, handle: &svg::Handle) -> (u32, u32) { self.backend().viewport_dimensions(handle) } - - fn draw( - &mut self, - handle: svg::Handle, - layout: Layout<'_>, - ) -> Self::Output { - ( - Primitive::Svg { - handle, - bounds: layout.bounds(), - }, - mouse::Interaction::default(), - ) - } } diff --git a/graphics/src/widget/text.rs b/graphics/src/widget/text.rs index d6d446d3..2ccd18a1 100644 --- a/graphics/src/widget/text.rs +++ b/graphics/src/widget/text.rs @@ -1,10 +1,8 @@ //! Write some text for your users to read. use crate::backend::{self, Backend}; -use crate::{Primitive, Renderer}; -use iced_native::alignment; -use iced_native::mouse; +use crate::Renderer; use iced_native::text; -use iced_native::{Color, Font, Point, Rectangle, Size}; +use iced_native::{Font, Point, Size}; /// A paragraph of text. /// @@ -52,41 +50,4 @@ where nearest_only, ) } - - fn draw( - &mut self, - defaults: &Self::Defaults, - bounds: Rectangle, - content: &str, - size: u16, - font: Font, - color: Option, - horizontal_alignment: alignment::Horizontal, - vertical_alignment: alignment::Vertical, - ) -> Self::Output { - let x = match horizontal_alignment { - alignment::Horizontal::Left => bounds.x, - alignment::Horizontal::Center => bounds.center_x(), - alignment::Horizontal::Right => bounds.x + bounds.width, - }; - - let y = match vertical_alignment { - alignment::Vertical::Top => bounds.y, - alignment::Vertical::Center => bounds.center_y(), - alignment::Vertical::Bottom => bounds.y + bounds.height, - }; - - ( - Primitive::Text { - content: content.to_string(), - size: f32::from(size), - bounds: Rectangle { x, y, ..bounds }, - color: color.unwrap_or(defaults.text.color), - font, - horizontal_alignment, - vertical_alignment, - }, - mouse::Interaction::default(), - ) - } } diff --git a/graphics/src/widget/text_input.rs b/graphics/src/widget/text_input.rs index 1516b007..e9dbf056 100644 --- a/graphics/src/widget/text_input.rs +++ b/graphics/src/widget/text_input.rs @@ -1,14 +1,9 @@ //! Display fields that can be filled with text. //! //! A [`TextInput`] has some local [`State`]. -use crate::alignment; use crate::backend::{self, Backend}; -use crate::{ - Background, Color, Font, Point, Primitive, Rectangle, Renderer, Size, - Vector, -}; +use crate::{Font, Rectangle, Renderer, Size}; -use iced_native::mouse; use iced_native::text_input::{self, cursor}; use std::f32; @@ -66,181 +61,6 @@ where 0.0 } } - - fn draw( - &mut self, - bounds: Rectangle, - text_bounds: Rectangle, - cursor_position: Point, - font: Font, - size: u16, - placeholder: &str, - value: &text_input::Value, - state: &text_input::State, - style_sheet: &Self::Style, - ) -> Self::Output { - let is_mouse_over = bounds.contains(cursor_position); - - let style = if state.is_focused() { - style_sheet.focused() - } else if is_mouse_over { - style_sheet.hovered() - } else { - style_sheet.active() - }; - - let input = Primitive::Quad { - bounds, - background: style.background, - border_radius: style.border_radius, - border_width: style.border_width, - border_color: style.border_color, - }; - - let text = value.to_string(); - - let text_value = Primitive::Text { - content: if text.is_empty() { - placeholder.to_string() - } else { - text.clone() - }, - color: if text.is_empty() { - style_sheet.placeholder_color() - } else { - style_sheet.value_color() - }, - font, - bounds: Rectangle { - y: text_bounds.center_y(), - width: f32::INFINITY, - ..text_bounds - }, - size: f32::from(size), - horizontal_alignment: alignment::Horizontal::Left, - vertical_alignment: alignment::Vertical::Center, - }; - - let (contents_primitive, offset) = if state.is_focused() { - let cursor = state.cursor(); - - let (cursor_primitive, offset) = match cursor.state(value) { - cursor::State::Index(position) => { - let (text_value_width, offset) = - measure_cursor_and_scroll_offset( - self, - text_bounds, - value, - size, - position, - font, - ); - - ( - Primitive::Quad { - bounds: Rectangle { - x: text_bounds.x + text_value_width, - y: text_bounds.y, - width: 1.0, - height: text_bounds.height, - }, - background: Background::Color( - style_sheet.value_color(), - ), - border_radius: 0.0, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }, - offset, - ) - } - cursor::State::Selection { start, end } => { - let left = start.min(end); - let right = end.max(start); - - let (left_position, left_offset) = - measure_cursor_and_scroll_offset( - self, - text_bounds, - value, - size, - left, - font, - ); - - let (right_position, right_offset) = - measure_cursor_and_scroll_offset( - self, - text_bounds, - value, - size, - right, - font, - ); - - let width = right_position - left_position; - - ( - Primitive::Quad { - bounds: Rectangle { - x: text_bounds.x + left_position, - y: text_bounds.y, - width, - height: text_bounds.height, - }, - background: Background::Color( - style_sheet.selection_color(), - ), - border_radius: 0.0, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }, - if end == right { - right_offset - } else { - left_offset - }, - ) - } - }; - - ( - Primitive::Group { - primitives: vec![cursor_primitive, text_value], - }, - Vector::new(offset as u32, 0), - ) - } else { - (text_value, Vector::new(0, 0)) - }; - - let text_width = self.measure_value( - if text.is_empty() { placeholder } else { &text }, - size, - font, - ); - - let contents = if text_width > text_bounds.width { - Primitive::Clip { - bounds: text_bounds, - offset, - content: Box::new(contents_primitive), - } - } else { - contents_primitive - }; - - ( - Primitive::Group { - primitives: vec![input, contents], - }, - if is_mouse_over { - mouse::Interaction::Text - } else { - mouse::Interaction::default() - }, - ) - } } fn measure_cursor_and_scroll_offset( diff --git a/graphics/src/widget/toggler.rs b/graphics/src/widget/toggler.rs index 852d18ee..cd072f7c 100644 --- a/graphics/src/widget/toggler.rs +++ b/graphics/src/widget/toggler.rs @@ -1,19 +1,10 @@ //! Show toggle controls using togglers. use crate::backend::{self, Backend}; -use crate::{Primitive, Renderer}; -use iced_native::mouse; +use crate::Renderer; use iced_native::toggler; -use iced_native::Rectangle; pub use iced_style::toggler::{Style, StyleSheet}; -/// Makes sure that the border radius of the toggler looks good at every size. -const BORDER_RADIUS_RATIO: f32 = 32.0 / 13.0; - -/// The space ratio between the background Quad and the Toggler bounds, and -/// between the background Quad and foreground Quad. -const SPACE_RATIO: f32 = 0.05; - /// A toggler that can be toggled. /// /// This is an alias of an `iced_native` toggler with an `iced_wgpu::Renderer`. @@ -27,73 +18,4 @@ where type Style = Box; const DEFAULT_SIZE: u16 = 20; - - fn draw( - &mut self, - bounds: Rectangle, - is_active: bool, - is_mouse_over: bool, - label: Option, - style_sheet: &Self::Style, - ) -> Self::Output { - let style = if is_mouse_over { - style_sheet.hovered(is_active) - } else { - style_sheet.active(is_active) - }; - - let border_radius = bounds.height as f32 / BORDER_RADIUS_RATIO; - let space = SPACE_RATIO * bounds.height as f32; - - let toggler_background_bounds = Rectangle { - x: bounds.x + space, - y: bounds.y + space, - width: bounds.width - (2.0 * space), - height: bounds.height - (2.0 * space), - }; - - let toggler_background = Primitive::Quad { - bounds: toggler_background_bounds, - background: style.background.into(), - border_radius, - border_width: 1.0, - border_color: style.background_border.unwrap_or(style.background), - }; - - let toggler_foreground_bounds = Rectangle { - x: bounds.x - + if is_active { - bounds.width - 2.0 * space - (bounds.height - (4.0 * space)) - } else { - 2.0 * space - }, - y: bounds.y + (2.0 * space), - width: bounds.height - (4.0 * space), - height: bounds.height - (4.0 * space), - }; - - let toggler_foreground = Primitive::Quad { - bounds: toggler_foreground_bounds, - background: style.foreground.into(), - border_radius, - border_width: 1.0, - border_color: style.foreground_border.unwrap_or(style.foreground), - }; - - ( - Primitive::Group { - primitives: match label { - Some((l, _)) => { - vec![l, toggler_background, toggler_foreground] - } - None => vec![toggler_background, toggler_foreground], - }, - }, - if is_mouse_over { - mouse::Interaction::Pointer - } else { - mouse::Interaction::default() - }, - ) - } } diff --git a/graphics/src/widget/tooltip.rs b/graphics/src/widget/tooltip.rs index 493a6389..d55d61b2 100644 --- a/graphics/src/widget/tooltip.rs +++ b/graphics/src/widget/tooltip.rs @@ -1,11 +1,6 @@ //! Decorate content and apply alignment. use crate::backend::{self, Backend}; -use crate::defaults::{self, Defaults}; -use crate::{Primitive, Renderer, Vector}; - -use iced_native::container; -use iced_native::layout::{self, Layout}; -use iced_native::{Element, Padding, Point, Rectangle, Size, Text}; +use crate::Renderer; /// An element decorating some content. /// @@ -21,148 +16,4 @@ where B: Backend + backend::Text, { const DEFAULT_PADDING: u16 = 5; - - fn draw( - &mut self, - defaults: &Defaults, - cursor_position: Point, - content_layout: Layout<'_>, - viewport: &Rectangle, - content: &Element<'_, Message, Self>, - tooltip: &Text, - position: Position, - style_sheet: &::Style, - gap: u16, - padding: u16, - ) -> Self::Output { - let (content, mouse_interaction) = content.draw( - self, - &defaults, - content_layout, - cursor_position, - viewport, - ); - - let bounds = content_layout.bounds(); - - if bounds.contains(cursor_position) { - use iced_native::Widget; - - let gap = f32::from(gap); - let style = style_sheet.style(); - - let defaults = Defaults { - text: defaults::Text { - color: style.text_color.unwrap_or(defaults.text.color), - }, - }; - - let text_layout = Widget::<(), Self>::layout( - tooltip, - self, - &layout::Limits::new(Size::ZERO, viewport.size()) - .pad(Padding::new(padding)), - ); - - let padding = f32::from(padding); - let text_bounds = text_layout.bounds(); - let x_center = bounds.x + (bounds.width - text_bounds.width) / 2.0; - let y_center = - bounds.y + (bounds.height - text_bounds.height) / 2.0; - - let mut tooltip_bounds = { - let offset = match position { - Position::Top => Vector::new( - x_center, - bounds.y - text_bounds.height - gap - padding, - ), - Position::Bottom => Vector::new( - x_center, - bounds.y + bounds.height + gap + padding, - ), - Position::Left => Vector::new( - bounds.x - text_bounds.width - gap - padding, - y_center, - ), - Position::Right => Vector::new( - bounds.x + bounds.width + gap + padding, - y_center, - ), - Position::FollowCursor => Vector::new( - cursor_position.x, - cursor_position.y - text_bounds.height, - ), - }; - - Rectangle { - x: offset.x - padding, - y: offset.y - padding, - width: text_bounds.width + padding * 2.0, - height: text_bounds.height + padding * 2.0, - } - }; - - if tooltip_bounds.x < viewport.x { - tooltip_bounds.x = viewport.x; - } else if viewport.x + viewport.width - < tooltip_bounds.x + tooltip_bounds.width - { - tooltip_bounds.x = - viewport.x + viewport.width - tooltip_bounds.width; - } - - if tooltip_bounds.y < viewport.y { - tooltip_bounds.y = viewport.y; - } else if viewport.y + viewport.height - < tooltip_bounds.y + tooltip_bounds.height - { - tooltip_bounds.y = - viewport.y + viewport.height - tooltip_bounds.height; - } - - let (tooltip, _) = Widget::<(), Self>::draw( - tooltip, - self, - &defaults, - Layout::with_offset( - Vector::new( - tooltip_bounds.x + padding, - tooltip_bounds.y + padding, - ), - &text_layout, - ), - cursor_position, - viewport, - ); - - ( - Primitive::Group { - primitives: vec![ - content, - Primitive::Clip { - bounds: *viewport, - offset: Vector::new(0, 0), - content: Box::new( - if let Some(background) = - crate::container::background( - tooltip_bounds, - &style, - ) - { - Primitive::Group { - primitives: vec![background, tooltip], - } - } else { - tooltip - }, - ), - }, - ], - }, - mouse_interaction, - ) - } else { - (content, mouse_interaction) - } - } } diff --git a/graphics/src/window/compositor.rs b/graphics/src/window/compositor.rs index 37edef1d..9ea040cd 100644 --- a/graphics/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -1,7 +1,5 @@ use crate::{Color, Error, Viewport}; -use iced_native::mouse; - use raw_window_handle::HasRawWindowHandle; use thiserror::Error; @@ -30,9 +28,8 @@ pub trait Compositor: Sized { window: &W, ) -> Self::Surface; - /// Crates a new [`SwapChain`] for the given [`Surface`]. + /// Configures a new [`Surface`] with the given dimensions. /// - /// [`SwapChain`]: Self::SwapChain /// [`Surface`]: Self::Surface fn configure_surface( &mut self, @@ -41,18 +38,17 @@ pub trait Compositor: Sized { height: u32, ); - /// Draws the output primitives to the next frame of the given [`SwapChain`]. + /// Presents the [`Renderer`] primitives to the next frame of the given [`Surface`]. /// /// [`SwapChain`]: Self::SwapChain - fn draw>( + fn present>( &mut self, renderer: &mut Self::Renderer, surface: &mut Self::Surface, viewport: &Viewport, background_color: Color, - output: &::Output, overlay: &[T], - ) -> Result; + ) -> Result<(), SurfaceError>; } /// Result of an unsuccessful call to [`Compositor::draw`]. @@ -63,13 +59,13 @@ pub enum SurfaceError { "A timeout was encountered while trying to acquire the next frame" )] Timeout, - /// The underlying surface has changed, and therefore the swap chain must be updated. + /// The underlying surface has changed, and therefore the surface must be updated. #[error( - "The underlying surface has changed, and therefore the swap chain must be updated." + "The underlying surface has changed, and therefore the surface must be updated." )] Outdated, /// The swap chain has been lost and needs to be recreated. - #[error("The swap chain has been lost and needs to be recreated")] + #[error("The surface has been lost and needs to be recreated")] Lost, /// There is no more memory left to allocate a new frame. #[error("There is no more memory left to allocate a new frame")] diff --git a/graphics/src/window/gl_compositor.rs b/graphics/src/window/gl_compositor.rs index 34d70be3..b1b995f1 100644 --- a/graphics/src/window/gl_compositor.rs +++ b/graphics/src/window/gl_compositor.rs @@ -1,5 +1,4 @@ use crate::{Color, Error, Size, Viewport}; -use iced_native::mouse; use core::ffi::c_void; @@ -49,15 +48,15 @@ pub trait GLCompositor: Sized { /// Resizes the viewport of the [`GLCompositor`]. fn resize_viewport(&mut self, physical_size: Size); - /// Draws the provided output with the given [`Renderer`]. + /// Presents the primitives of the [`Renderer`] to the next frame of the + /// [`GLCompositor`]. /// /// [`Renderer`]: crate::Renderer - fn draw>( + fn present>( &mut self, renderer: &mut Self::Renderer, viewport: &Viewport, background_color: Color, - output: &::Output, overlay: &[T], - ) -> mouse::Interaction; + ); } -- cgit From 3a0c503db99eb3d45ac971132904df419ee566b6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 14 Oct 2021 16:59:19 +0700 Subject: Implement `Widget::draw` for `Text` --- graphics/src/backend.rs | 1 - graphics/src/layer.rs | 16 +++++++------ graphics/src/renderer.rs | 55 +++++++++++++++++++++++++++++++++++++++------ graphics/src/widget/text.rs | 2 -- 4 files changed, 57 insertions(+), 17 deletions(-) (limited to 'graphics') diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index 7e0af2cc..b8ff5d21 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -16,7 +16,6 @@ pub trait Backend { fn trim_measurements(&mut self) {} } -/// A graphics backend that supports text rendering. pub trait Text { /// The icon font of the backend. const ICON_FONT: Font; diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index 9653a2e4..e5cb64c3 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -74,7 +74,7 @@ impl<'a> Layer<'a> { /// Distributes the given [`Primitive`] and generates a list of layers based /// on its contents. pub fn generate( - primitive: &'a Primitive, + primitives: &'a [Primitive], viewport: &Viewport, ) -> Vec { let first_layer = @@ -82,12 +82,14 @@ impl<'a> Layer<'a> { let mut layers = vec![first_layer]; - Self::process_primitive( - &mut layers, - Vector::new(0.0, 0.0), - primitive, - 0, - ); + for primitive in primitives { + Self::process_primitive( + &mut layers, + Vector::new(0.0, 0.0), + primitive, + 0, + ); + } layers } diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index cedffe7e..a708cb67 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -1,12 +1,14 @@ -use crate::{Backend, Defaults, Primitive}; +use crate::backend::{self, Backend}; +use crate::{Defaults, Primitive, Vector}; use iced_native::layout; -use iced_native::{Element, Rectangle}; +use iced_native::renderer; +use iced_native::{Color, Element, Font, Rectangle}; /// A backend-agnostic renderer that supports all the built-in widgets. #[derive(Debug)] pub struct Renderer { backend: B, - primitive: Primitive, + primitives: Vec, } impl Renderer { @@ -14,7 +16,7 @@ impl Renderer { pub fn new(backend: B) -> Self { Self { backend, - primitive: Primitive::None, + primitives: Vec::new(), } } @@ -22,8 +24,8 @@ impl Renderer { &self.backend } - pub fn present(&mut self, f: impl FnOnce(&mut B, &Primitive)) { - f(&mut self.backend, &self.primitive); + pub fn present(&mut self, f: impl FnOnce(&mut B, &[Primitive])) { + f(&mut self.backend, &self.primitives); } } @@ -45,5 +47,44 @@ where layout } - fn with_layer(&mut self, _bounds: Rectangle, _f: impl FnOnce(&mut Self)) {} + fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)) { + let current_primitives = + std::mem::replace(&mut self.primitives, Vec::new()); + + f(self); + + let layer_primitives = + std::mem::replace(&mut self.primitives, current_primitives); + + self.primitives.push(Primitive::Clip { + bounds, + offset: Vector::new(0, 0), + content: Box::new(Primitive::Group { + primitives: layer_primitives, + }), + }); + } + + fn clear(&mut self) { + self.primitives.clear(); + } +} + +impl renderer::Text for Renderer +where + B: Backend + backend::Text, +{ + type Font = Font; + + fn fill_text(&mut self, text: renderer::text::Section<'_, Self::Font>) { + self.primitives.push(Primitive::Text { + content: text.content.to_string(), + bounds: text.bounds, + size: text.size.unwrap_or(f32::from(self.backend.default_size())), + color: text.color.unwrap_or(Color::BLACK), + font: text.font, + horizontal_alignment: text.horizontal_alignment, + vertical_alignment: text.vertical_alignment, + }); + } } diff --git a/graphics/src/widget/text.rs b/graphics/src/widget/text.rs index 2ccd18a1..4ee2c616 100644 --- a/graphics/src/widget/text.rs +++ b/graphics/src/widget/text.rs @@ -15,8 +15,6 @@ impl text::Renderer for Renderer where B: Backend + backend::Text, { - type Font = Font; - fn default_size(&self) -> u16 { self.backend().default_size() } -- cgit From dfceee99aad9462f09ca61081e68e1decb2fed92 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 14 Oct 2021 17:15:29 +0700 Subject: Implement `Widget::draw` for `Scrollable` Rendering the scroller is still WIP --- graphics/src/renderer.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'graphics') diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index a708cb67..91f6b550 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -47,7 +47,12 @@ where layout } - fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)) { + fn with_layer( + &mut self, + bounds: Rectangle, + offset: Vector, + f: impl FnOnce(&mut Self), + ) { let current_primitives = std::mem::replace(&mut self.primitives, Vec::new()); @@ -58,7 +63,7 @@ where self.primitives.push(Primitive::Clip { bounds, - offset: Vector::new(0, 0), + offset, content: Box::new(Primitive::Group { primitives: layer_primitives, }), @@ -77,6 +82,8 @@ where type Font = Font; fn fill_text(&mut self, text: renderer::text::Section<'_, Self::Font>) { + dbg!(text); + self.primitives.push(Primitive::Text { content: text.content.to_string(), bounds: text.bounds, -- cgit From a4f4d831615899046d36c96e6a580d5386aa25bf Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 14:47:49 +0700 Subject: Introduce `fill_rectangle` to `Renderer` trait --- graphics/src/renderer.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'graphics') diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index 91f6b550..7ed06151 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -70,6 +70,16 @@ where }); } + fn fill_rectangle(&mut self, quad: renderer::Quad) { + self.primitives.push(Primitive::Quad { + bounds: quad.bounds, + background: quad.background, + border_radius: quad.border_radius, + border_width: quad.border_width, + border_color: quad.border_color, + }); + } + fn clear(&mut self) { self.primitives.clear(); } -- cgit From 54a9a232f8110364972a8eef966b7b7477573f4f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 14:48:33 +0700 Subject: Draw scrollbar in `Widget::draw` for `Scrollable` --- graphics/src/widget/scrollable.rs | 64 +-------------------------------------- 1 file changed, 1 insertion(+), 63 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/scrollable.rs b/graphics/src/widget/scrollable.rs index f1fe0d2d..61eae587 100644 --- a/graphics/src/widget/scrollable.rs +++ b/graphics/src/widget/scrollable.rs @@ -1,7 +1,5 @@ //! Navigate an endless amount of content with a scrollbar. -use crate::{Backend, Renderer}; -use iced_native::scrollable; -use iced_native::Rectangle; +use crate::Renderer; pub use iced_native::scrollable::State; pub use iced_style::scrollable::{Scrollbar, Scroller, StyleSheet}; @@ -13,63 +11,3 @@ pub use iced_style::scrollable::{Scrollbar, Scroller, StyleSheet}; /// `Renderer`. pub type Scrollable<'a, Message, Backend> = iced_native::Scrollable<'a, Message, Renderer>; - -impl scrollable::Renderer for Renderer -where - B: Backend, -{ - type Style = Box; - - fn scrollbar( - &self, - bounds: Rectangle, - content_bounds: Rectangle, - offset: u32, - scrollbar_width: u16, - scrollbar_margin: u16, - scroller_width: u16, - ) -> Option { - if content_bounds.height > bounds.height { - let outer_width = - scrollbar_width.max(scroller_width) + 2 * scrollbar_margin; - - let outer_bounds = Rectangle { - x: bounds.x + bounds.width - outer_width as f32, - y: bounds.y, - width: outer_width as f32, - height: bounds.height, - }; - - let scrollbar_bounds = Rectangle { - x: bounds.x + bounds.width - - f32::from(outer_width / 2 + scrollbar_width / 2), - y: bounds.y, - width: scrollbar_width as f32, - height: bounds.height, - }; - - let ratio = bounds.height / content_bounds.height; - let scroller_height = bounds.height * ratio; - let y_offset = offset as f32 * ratio; - - let scroller_bounds = Rectangle { - x: bounds.x + bounds.width - - f32::from(outer_width / 2 + scroller_width / 2), - y: scrollbar_bounds.y + y_offset, - width: scroller_width as f32, - height: scroller_height, - }; - - Some(scrollable::Scrollbar { - outer_bounds, - bounds: scrollbar_bounds, - margin: scrollbar_margin, - scroller: scrollable::Scroller { - bounds: scroller_bounds, - }, - }) - } else { - None - } - } -} -- cgit From edea093350e1b576e2b7db50c525e7fa5c3bea9f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 15:19:04 +0700 Subject: Move `Defaults` from `iced_graphics` to `iced_native` --- graphics/src/defaults.rs | 32 -------------------------------- graphics/src/lib.rs | 4 +--- graphics/src/renderer.rs | 12 ++++++------ graphics/src/widget/canvas.rs | 6 ++++-- graphics/src/widget/qr_code.rs | 9 +++++---- 5 files changed, 16 insertions(+), 47 deletions(-) delete mode 100644 graphics/src/defaults.rs (limited to 'graphics') diff --git a/graphics/src/defaults.rs b/graphics/src/defaults.rs deleted file mode 100644 index 11718a87..00000000 --- a/graphics/src/defaults.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! Use default styling attributes to inherit styles. -use iced_native::Color; - -/// Some default styling attributes. -#[derive(Debug, Clone, Copy)] -pub struct Defaults { - /// Text styling - pub text: Text, -} - -impl Default for Defaults { - fn default() -> Defaults { - Defaults { - text: Text::default(), - } - } -} - -/// Some default text styling attributes. -#[derive(Debug, Clone, Copy)] -pub struct Text { - /// The default color of text - pub color: Color, -} - -impl Default for Text { - fn default() -> Text { - Text { - color: Color::BLACK, - } - } -} diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index 9c113da6..dbd94e99 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -13,15 +13,14 @@ mod antialiasing; mod error; mod primitive; -mod renderer; mod transformation; mod viewport; pub mod backend; -pub mod defaults; pub mod font; pub mod layer; pub mod overlay; +pub mod renderer; pub mod triangle; pub mod widget; pub mod window; @@ -31,7 +30,6 @@ pub use widget::*; pub use antialiasing::Antialiasing; pub use backend::Backend; -pub use defaults::Defaults; pub use error::Error; pub use layer::Layer; pub use primitive::Primitive; diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index 7ed06151..8d623868 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -1,8 +1,10 @@ use crate::backend::{self, Backend}; -use crate::{Defaults, Primitive, Vector}; +use crate::{Primitive, Vector}; use iced_native::layout; use iced_native::renderer; -use iced_native::{Color, Element, Font, Rectangle}; +use iced_native::{Element, Font, Rectangle}; + +pub use iced_native::renderer::Style; /// A backend-agnostic renderer that supports all the built-in widgets. #[derive(Debug)] @@ -33,8 +35,6 @@ impl iced_native::Renderer for Renderer where B: Backend, { - type Defaults = Defaults; - fn layout<'a, Message>( &mut self, element: &Element<'a, Message, Self>, @@ -97,8 +97,8 @@ where self.primitives.push(Primitive::Text { content: text.content.to_string(), bounds: text.bounds, - size: text.size.unwrap_or(f32::from(self.backend.default_size())), - color: text.color.unwrap_or(Color::BLACK), + size: text.size, + color: text.color, font: text.font, horizontal_alignment: text.horizontal_alignment, vertical_alignment: text.vertical_alignment, diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index 7bf00ca5..3990c2b9 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -3,7 +3,9 @@ //! A [`Canvas`] widget can be used to draw different kinds of 2D shapes in a //! [`Frame`]. It can be used for animation, data visualization, game graphics, //! and more! -use crate::{Backend, Defaults, Renderer}; +use crate::renderer::{self, Renderer}; +use crate::Backend; + use iced_native::layout; use iced_native::{ Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, @@ -187,7 +189,7 @@ where fn draw( &self, _renderer: &mut Renderer, - _defaults: &Defaults, + _style: &renderer::Style, _layout: Layout<'_>, _cursor_position: Point, _viewport: &Rectangle, diff --git a/graphics/src/widget/qr_code.rs b/graphics/src/widget/qr_code.rs index a809d99f..364b636b 100644 --- a/graphics/src/widget/qr_code.rs +++ b/graphics/src/widget/qr_code.rs @@ -1,10 +1,11 @@ //! Encode and display information in a QR code. use crate::canvas; -use crate::{Backend, Defaults, Renderer}; +use crate::renderer::{self, Renderer}; +use crate::Backend; +use iced_native::layout; use iced_native::{ - layout, Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, - Widget, + Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; use thiserror::Error; @@ -81,7 +82,7 @@ where fn draw( &self, _renderer: &mut Renderer, - _defaults: &Defaults, + _style: &renderer::Style, _layout: Layout<'_>, _cursor_position: Point, _viewport: &Rectangle, -- cgit From d61cb58d92b6fcd520f665deb093f3747ffd5e5c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 15:36:32 +0700 Subject: Wire up `container` styling to `iced_native` --- graphics/src/widget/container.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/container.rs b/graphics/src/widget/container.rs index 811a0c7f..c4c4e5ba 100644 --- a/graphics/src/widget/container.rs +++ b/graphics/src/widget/container.rs @@ -1,6 +1,5 @@ //! Decorate content and apply alignment. -use crate::container; -use crate::{Backend, Renderer}; +use crate::Renderer; pub use iced_style::container::{Style, StyleSheet}; @@ -10,10 +9,3 @@ pub use iced_style::container::{Style, StyleSheet}; /// `Renderer`. pub type Container<'a, Message, Backend> = iced_native::Container<'a, Message, Renderer>; - -impl iced_native::container::Renderer for Renderer -where - B: Backend, -{ - type Style = Box; -} -- cgit From 3140cdc4babcefc444f1c1d30eb0f5f4ed1df054 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 16:02:30 +0700 Subject: Wire up styling to `Button` in `iced_native` --- graphics/src/widget/button.rs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/button.rs b/graphics/src/widget/button.rs index 990aac9f..3d3e6675 100644 --- a/graphics/src/widget/button.rs +++ b/graphics/src/widget/button.rs @@ -1,23 +1,12 @@ //! Allow your users to perform actions by pressing a button. //! //! A [`Button`] has some local [`State`]. -use crate::{Backend, Renderer}; -use iced_native::Padding; +use crate::Renderer; -pub use iced_native::button::State; -pub use iced_style::button::{Style, StyleSheet}; +pub use iced_native::button::{State, Style, StyleSheet}; /// A widget that produces a message when clicked. /// /// This is an alias of an `iced_native` button with an `iced_wgpu::Renderer`. pub type Button<'a, Message, Backend> = iced_native::Button<'a, Message, Renderer>; - -impl iced_native::button::Renderer for Renderer -where - B: Backend, -{ - const DEFAULT_PADDING: Padding = Padding::new(5); - - type Style = Box; -} -- cgit From e00a2e9b2d97147be5912a97362d1bf1705b7c4e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 16:59:59 +0700 Subject: Remove `dbg!` leftover in `Renderer::fill_text` --- graphics/src/renderer.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'graphics') diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index 8d623868..0dca685f 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -92,8 +92,6 @@ where type Font = Font; fn fill_text(&mut self, text: renderer::text::Section<'_, Self::Font>) { - dbg!(text); - self.primitives.push(Primitive::Text { content: text.content.to_string(), bounds: text.bounds, -- cgit From 11bcb1342796a6fabc7c5b89a15c22c754b014ce Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Oct 2021 15:50:42 +0700 Subject: Wire up styling to `Slider` in `iced_native` --- graphics/src/widget/slider.rs | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/slider.rs b/graphics/src/widget/slider.rs index 5125d66c..766ecbb6 100644 --- a/graphics/src/widget/slider.rs +++ b/graphics/src/widget/slider.rs @@ -1,24 +1,5 @@ //! Display an interactive selector of a single value from a range of values. //! //! A [`Slider`] has some local [`State`]. -use crate::{Backend, Renderer}; -use iced_native::slider; - -pub use iced_native::slider::State; +pub use iced_native::slider::{Slider, State}; pub use iced_style::slider::{Handle, HandleShape, Style, StyleSheet}; - -/// An horizontal bar and a handle that selects a single value from a range of -/// values. -/// -/// This is an alias of an `iced_native` slider with an `iced_wgpu::Renderer`. -pub type Slider<'a, T, Message, Backend> = - iced_native::Slider<'a, T, Message, Renderer>; - -impl slider::Renderer for Renderer -where - B: Backend, -{ - type Style = Box; - - const DEFAULT_HEIGHT: u16 = 22; -} -- cgit From e914888f57394e4b67b40e42f1ad9df4ae8147e6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Oct 2021 18:40:39 +0700 Subject: Implement `Widget::draw` for `TextInput` --- graphics/src/renderer.rs | 36 ++++++++++++++++++- graphics/src/widget/text.rs | 44 ----------------------- graphics/src/widget/text_input.rs | 75 +-------------------------------------- 3 files changed, 36 insertions(+), 119 deletions(-) (limited to 'graphics') diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index 0dca685f..f8c67047 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -2,7 +2,7 @@ use crate::backend::{self, Backend}; use crate::{Primitive, Vector}; use iced_native::layout; use iced_native::renderer; -use iced_native::{Element, Font, Rectangle}; +use iced_native::{Element, Font, Point, Rectangle, Size}; pub use iced_native::renderer::Style; @@ -91,6 +91,40 @@ where { type Font = Font; + fn default_size(&self) -> u16 { + self.backend().default_size() + } + + fn measure( + &self, + content: &str, + size: u16, + font: Font, + bounds: Size, + ) -> (f32, f32) { + self.backend() + .measure(content, f32::from(size), font, bounds) + } + + fn hit_test( + &self, + content: &str, + size: f32, + font: Font, + bounds: Size, + point: Point, + nearest_only: bool, + ) -> Option { + self.backend().hit_test( + content, + size, + font, + bounds, + point, + nearest_only, + ) + } + fn fill_text(&mut self, text: renderer::text::Section<'_, Self::Font>) { self.primitives.push(Primitive::Text { content: text.content.to_string(), diff --git a/graphics/src/widget/text.rs b/graphics/src/widget/text.rs index 4ee2c616..ec0349f9 100644 --- a/graphics/src/widget/text.rs +++ b/graphics/src/widget/text.rs @@ -1,51 +1,7 @@ //! Write some text for your users to read. -use crate::backend::{self, Backend}; use crate::Renderer; -use iced_native::text; -use iced_native::{Font, Point, Size}; /// A paragraph of text. /// /// This is an alias of an `iced_native` text with an `iced_wgpu::Renderer`. pub type Text = iced_native::Text>; - -use std::f32; - -impl text::Renderer for Renderer -where - B: Backend + backend::Text, -{ - fn default_size(&self) -> u16 { - self.backend().default_size() - } - - fn measure( - &self, - content: &str, - size: u16, - font: Font, - bounds: Size, - ) -> (f32, f32) { - self.backend() - .measure(content, f32::from(size), font, bounds) - } - - fn hit_test( - &self, - content: &str, - size: f32, - font: Font, - bounds: Size, - point: Point, - nearest_only: bool, - ) -> Option { - self.backend().hit_test( - content, - size, - font, - bounds, - point, - nearest_only, - ) - } -} diff --git a/graphics/src/widget/text_input.rs b/graphics/src/widget/text_input.rs index e9dbf056..ebb9138f 100644 --- a/graphics/src/widget/text_input.rs +++ b/graphics/src/widget/text_input.rs @@ -1,11 +1,7 @@ //! Display fields that can be filled with text. //! //! A [`TextInput`] has some local [`State`]. -use crate::backend::{self, Backend}; -use crate::{Font, Rectangle, Renderer, Size}; - -use iced_native::text_input::{self, cursor}; -use std::f32; +use crate::Renderer; pub use iced_native::text_input::State; pub use iced_style::text_input::{Style, StyleSheet}; @@ -15,72 +11,3 @@ pub use iced_style::text_input::{Style, StyleSheet}; /// This is an alias of an `iced_native` text input with an `iced_wgpu::Renderer`. pub type TextInput<'a, Message, Backend> = iced_native::TextInput<'a, Message, Renderer>; - -impl text_input::Renderer for Renderer -where - B: Backend + backend::Text, -{ - type Style = Box; - - fn measure_value(&self, value: &str, size: u16, font: Font) -> f32 { - let backend = self.backend(); - - let (width, _) = - backend.measure(value, f32::from(size), font, Size::INFINITY); - - width - } - - fn offset( - &self, - text_bounds: Rectangle, - font: Font, - size: u16, - value: &text_input::Value, - state: &text_input::State, - ) -> f32 { - if state.is_focused() { - let cursor = state.cursor(); - - let focus_position = match cursor.state(value) { - cursor::State::Index(i) => i, - cursor::State::Selection { end, .. } => end, - }; - - let (_, offset) = measure_cursor_and_scroll_offset( - self, - text_bounds, - value, - size, - focus_position, - font, - ); - - offset - } else { - 0.0 - } - } -} - -fn measure_cursor_and_scroll_offset( - renderer: &Renderer, - text_bounds: Rectangle, - value: &text_input::Value, - size: u16, - cursor_index: usize, - font: Font, -) -> (f32, f32) -where - B: Backend + backend::Text, -{ - use iced_native::text_input::Renderer; - - let text_before_cursor = value.until(cursor_index).to_string(); - - let text_value_width = - renderer.measure_value(&text_before_cursor, size, font); - let offset = ((text_value_width + 5.0) - text_bounds.width).max(0.0); - - (text_value_width, offset) -} -- cgit From d39ad717ed0ab85acbe935d7ab883166b36e7bc7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 20 Oct 2021 19:06:53 +0700 Subject: Wire up styling to `Radio` in `iced_native` --- graphics/src/widget/radio.rs | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/radio.rs b/graphics/src/widget/radio.rs index cd83f2ff..4185bd61 100644 --- a/graphics/src/widget/radio.rs +++ b/graphics/src/widget/radio.rs @@ -1,6 +1,5 @@ //! Create choices using radio buttons. -use crate::{Backend, Renderer}; -use iced_native::radio; +use crate::Renderer; pub use iced_style::radio::{Style, StyleSheet}; @@ -8,15 +7,5 @@ pub use iced_style::radio::{Style, StyleSheet}; /// /// This is an alias of an `iced_native` radio button with an /// `iced_wgpu::Renderer`. -pub type Radio = - iced_native::Radio>; - -impl radio::Renderer for Renderer -where - B: Backend, -{ - type Style = Box; - - const DEFAULT_SIZE: u16 = 28; - const DEFAULT_SPACING: u16 = 15; -} +pub type Radio<'a, Message, Backend> = + iced_native::Radio<'a, Message, Renderer>; -- cgit From 7c08c6bd138207b862933ee479752a4f1d18c4f2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 21 Oct 2021 18:50:27 +0700 Subject: Remove `Renderer` trait for `Checkbox` --- graphics/src/widget/checkbox.rs | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/checkbox.rs b/graphics/src/widget/checkbox.rs index 3b756525..a458d85c 100644 --- a/graphics/src/widget/checkbox.rs +++ b/graphics/src/widget/checkbox.rs @@ -1,23 +1,10 @@ //! Show toggle controls using checkboxes. -use crate::backend::{self, Backend}; use crate::Renderer; -use iced_native::checkbox; - pub use iced_style::checkbox::{Style, StyleSheet}; /// A box that can be checked. /// /// This is an alias of an `iced_native` checkbox with an `iced_wgpu::Renderer`. -pub type Checkbox = - iced_native::Checkbox>; - -impl checkbox::Renderer for Renderer -where - B: Backend + backend::Text, -{ - type Style = Box; - - const DEFAULT_SIZE: u16 = 20; - const DEFAULT_SPACING: u16 = 15; -} +pub type Checkbox<'a, Message, Backend> = + iced_native::Checkbox<'a, Message, Renderer>; -- cgit From 1397be38ca2caaf5c49ca51e164a28b63f9e461b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 21 Oct 2021 19:06:22 +0700 Subject: Implement `Widget::draw` for `Checkbox` --- graphics/src/renderer.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'graphics') diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index f8c67047..5e74019f 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -91,6 +91,10 @@ where { type Font = Font; + const ICON_FONT: Font = B::ICON_FONT; + const CHECKMARK_ICON: char = B::CHECKMARK_ICON; + const ARROW_DOWN_ICON: char = B::ARROW_DOWN_ICON; + fn default_size(&self) -> u16 { self.backend().default_size() } -- cgit From 1afbc98544327b5454b862bec938dc76c4d38fa0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 25 Oct 2021 15:03:57 +0700 Subject: Implement `Widget::draw` for `Image` --- graphics/src/renderer.rs | 4 ++++ graphics/src/widget/image.rs | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index 5e74019f..d90a3f1e 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -26,6 +26,10 @@ impl Renderer { &self.backend } + pub fn draw_primitive(&mut self, primitive: Primitive) { + self.primitives.push(primitive); + } + pub fn present(&mut self, f: impl FnOnce(&mut B, &[Primitive])) { f(&mut self.backend, &self.primitives); } diff --git a/graphics/src/widget/image.rs b/graphics/src/widget/image.rs index b55ba32f..dc2d1657 100644 --- a/graphics/src/widget/image.rs +++ b/graphics/src/widget/image.rs @@ -3,8 +3,9 @@ pub mod viewer; use crate::backend::{self, Backend}; -use crate::Renderer; +use crate::{Primitive, Renderer}; use iced_native::image; +use iced_native::Layout; pub use iced_native::image::{Handle, Image, Viewer}; @@ -15,4 +16,11 @@ where fn dimensions(&self, handle: &image::Handle) -> (u32, u32) { self.backend().dimensions(handle) } + + fn draw(&mut self, handle: image::Handle, layout: Layout<'_>) { + self.draw_primitive(Primitive::Image { + handle, + bounds: layout.bounds(), + }) + } } -- cgit From 41394b4e90a81a43c796c070e706e6aa4d8652bc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 25 Oct 2021 15:37:28 +0700 Subject: Implement `Widget::draw` for `PaneGrid` --- graphics/src/widget/pane_grid.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index 8c6b0f82..4dc42e41 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -7,8 +7,7 @@ //! drag and drop, and hotkey support. //! //! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.3/examples/pane_grid -use crate::{Backend, Renderer}; -use iced_native::pane_grid; +use crate::Renderer; pub use iced_native::pane_grid::{ Axis, Configuration, Content, Direction, DragEvent, Node, Pane, @@ -25,10 +24,3 @@ pub use iced_style::pane_grid::{Line, StyleSheet}; /// This is an alias of an `iced_native` pane grid with an `iced_wgpu::Renderer`. pub type PaneGrid<'a, Message, Backend> = iced_native::PaneGrid<'a, Message, Renderer>; - -impl pane_grid::Renderer for Renderer -where - B: Backend, -{ - type Style = Box; -} -- cgit From 4a11cbd99445338619dfaf1f327dbc25b2983cb7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 25 Oct 2021 16:16:35 +0700 Subject: Implement `Widget::mouse_interaction` for `PaneGrid` ... and fix rendering of drag interaction in `PaneGrid` by introducing an explicit `with_translation` method to `Renderer` and simplifying the `with_layer` and `Clip` primitive. --- graphics/src/layer.rs | 9 ++------- graphics/src/primitive.rs | 2 -- graphics/src/renderer.rs | 27 +++++++++++++++++++++------ 3 files changed, 23 insertions(+), 15 deletions(-) (limited to 'graphics') diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index e5cb64c3..7a32c850 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -175,11 +175,7 @@ impl<'a> Layer<'a> { }); } } - Primitive::Clip { - bounds, - offset, - content, - } => { + Primitive::Clip { bounds, content } => { let layer = &mut layers[current_layer]; let translated_bounds = *bounds + translation; @@ -192,8 +188,7 @@ impl<'a> Layer<'a> { Self::process_primitive( layers, - translation - - Vector::new(offset.x as f32, offset.y as f32), + translation, content, layers.len() - 1, ); diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index 32f8383d..b984feaa 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -66,8 +66,6 @@ pub enum Primitive { Clip { /// The bounds of the clip bounds: Rectangle, - /// The offset transformation of the clip - offset: Vector, /// The content of the clip content: Box, }, diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index d90a3f1e..8b6c2217 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -51,10 +51,26 @@ where layout } - fn with_layer( + fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)) { + let current_primitives = + std::mem::replace(&mut self.primitives, Vec::new()); + + f(self); + + let layer_primitives = + std::mem::replace(&mut self.primitives, current_primitives); + + self.primitives.push(Primitive::Clip { + bounds, + content: Box::new(Primitive::Group { + primitives: layer_primitives, + }), + }); + } + + fn with_translation( &mut self, - bounds: Rectangle, - offset: Vector, + translation: Vector, f: impl FnOnce(&mut Self), ) { let current_primitives = @@ -65,9 +81,8 @@ where let layer_primitives = std::mem::replace(&mut self.primitives, current_primitives); - self.primitives.push(Primitive::Clip { - bounds, - offset, + self.primitives.push(Primitive::Translate { + translation, content: Box::new(Primitive::Group { primitives: layer_primitives, }), -- cgit From fe4dfeafdbc8f427bd351f394d27f606a3843b44 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 28 Oct 2021 15:41:12 +0700 Subject: Wire up style to `PickList` and `overlay::Menu` --- graphics/src/overlay/menu.rs | 11 ----------- graphics/src/widget/pick_list.rs | 17 ----------------- 2 files changed, 28 deletions(-) (limited to 'graphics') diff --git a/graphics/src/overlay/menu.rs b/graphics/src/overlay/menu.rs index 7dfb48b9..c5ff093d 100644 --- a/graphics/src/overlay/menu.rs +++ b/graphics/src/overlay/menu.rs @@ -1,14 +1,3 @@ //! Build and show dropdown menus. -use crate::backend::{self, Backend}; -use crate::Renderer; - -use iced_native::overlay; pub use iced_style::menu::Style; - -impl overlay::menu::Renderer for Renderer -where - B: Backend + backend::Text, -{ - type Style = Style; -} diff --git a/graphics/src/widget/pick_list.rs b/graphics/src/widget/pick_list.rs index 54f42cde..b5cb0a44 100644 --- a/graphics/src/widget/pick_list.rs +++ b/graphics/src/widget/pick_list.rs @@ -1,26 +1,9 @@ //! Display a dropdown list of selectable values. -use crate::backend::{self, Backend}; use crate::Renderer; -use iced_native::Padding; -use iced_style::menu; - pub use iced_native::pick_list::State; pub use iced_style::pick_list::{Style, StyleSheet}; /// A widget allowing the selection of a single value from a list of options. pub type PickList<'a, T, Message, Backend> = iced_native::PickList<'a, T, Message, Renderer>; - -impl iced_native::pick_list::Renderer for Renderer -where - B: Backend + backend::Text, -{ - type Style = Box; - - const DEFAULT_PADDING: Padding = Padding::new(5); - - fn menu_style(style: &Box) -> menu::Style { - style.menu() - } -} -- cgit From 8b1587421822d9a0e7fa1c2224447da7007dbd30 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 28 Oct 2021 17:01:23 +0700 Subject: Implement `Widget::draw` for `image::Viewer` --- graphics/src/widget/image.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/image.rs b/graphics/src/widget/image.rs index dc2d1657..242d36d7 100644 --- a/graphics/src/widget/image.rs +++ b/graphics/src/widget/image.rs @@ -2,10 +2,9 @@ pub mod viewer; use crate::backend::{self, Backend}; +use crate::{Primitive, Rectangle, Renderer}; -use crate::{Primitive, Renderer}; use iced_native::image; -use iced_native::Layout; pub use iced_native::image::{Handle, Image, Viewer}; @@ -17,10 +16,7 @@ where self.backend().dimensions(handle) } - fn draw(&mut self, handle: image::Handle, layout: Layout<'_>) { - self.draw_primitive(Primitive::Image { - handle, - bounds: layout.bounds(), - }) + fn draw(&mut self, handle: image::Handle, bounds: Rectangle) { + self.draw_primitive(Primitive::Image { handle, bounds }) } } -- cgit From e42e1e2f57ddb455ceff0017e215ddacca978d37 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 28 Oct 2021 17:36:36 +0700 Subject: Implement `Widget::draw` for `ProgressBar` --- graphics/src/widget/progress_bar.rs | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/progress_bar.rs b/graphics/src/widget/progress_bar.rs index 3d21b7b9..df4f5ec6 100644 --- a/graphics/src/widget/progress_bar.rs +++ b/graphics/src/widget/progress_bar.rs @@ -2,22 +2,4 @@ //! //! A [`ProgressBar`] has a range of possible values and a current value, //! as well as a length, height and style. -use crate::{Backend, Renderer}; -use iced_native::progress_bar; - -pub use iced_style::progress_bar::{Style, StyleSheet}; - -/// A bar that displays progress. -/// -/// This is an alias of an `iced_native` progress bar with an -/// `iced_wgpu::Renderer`. -pub type ProgressBar = iced_native::ProgressBar>; - -impl progress_bar::Renderer for Renderer -where - B: Backend, -{ - type Style = Box; - - const DEFAULT_HEIGHT: u16 = 30; -} +pub use iced_native::progress_bar::*; -- cgit From f6257973926233e7bb18ae3b4dee4385bfc6ab61 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 28 Oct 2021 18:03:24 +0700 Subject: Implement `Widget::draw` for `Rule` --- graphics/src/widget/rule.rs | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/rule.rs b/graphics/src/widget/rule.rs index 62766f82..57f4a530 100644 --- a/graphics/src/widget/rule.rs +++ b/graphics/src/widget/rule.rs @@ -1,18 +1,3 @@ //! Display a horizontal or vertical rule for dividing content. -use crate::{Backend, Renderer}; -use iced_native::rule; - -pub use iced_style::rule::{FillMode, Style, StyleSheet}; - -/// Display a horizontal or vertical rule for dividing content. -/// -/// This is an alias of an `iced_native` rule with an `iced_graphics::Renderer`. -pub type Rule = iced_native::Rule>; - -impl rule::Renderer for Renderer -where - B: Backend, -{ - type Style = Box; -} +pub use iced_native::rule::*; -- cgit From 1c2792c0a0dc565f9dd9183ca8948331ec467590 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 28 Oct 2021 18:17:47 +0700 Subject: Implement `Widget::draw` for `Toggler` --- graphics/src/widget/toggler.rs | 9 --------- 1 file changed, 9 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/toggler.rs b/graphics/src/widget/toggler.rs index cd072f7c..166f3877 100644 --- a/graphics/src/widget/toggler.rs +++ b/graphics/src/widget/toggler.rs @@ -10,12 +10,3 @@ pub use iced_style::toggler::{Style, StyleSheet}; /// This is an alias of an `iced_native` toggler with an `iced_wgpu::Renderer`. pub type Toggler = iced_native::Toggler>; - -impl toggler::Renderer for Renderer -where - B: Backend + backend::Text, -{ - type Style = Box; - - const DEFAULT_SIZE: u16 = 20; -} -- cgit From 4889a95e592033a4ef3ae05210220a6679711832 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 28 Oct 2021 20:17:49 +0700 Subject: Remove unused imports for `toggler` in `iced_graphics` --- graphics/src/widget/toggler.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/toggler.rs b/graphics/src/widget/toggler.rs index 166f3877..60d8a347 100644 --- a/graphics/src/widget/toggler.rs +++ b/graphics/src/widget/toggler.rs @@ -1,7 +1,5 @@ //! Show toggle controls using togglers. -use crate::backend::{self, Backend}; use crate::Renderer; -use iced_native::toggler; pub use iced_style::toggler::{Style, StyleSheet}; -- cgit From 5fee1e33d4a873c5e37e6c158257bcf54fd17452 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 28 Oct 2021 20:30:25 +0700 Subject: Implement `Widget::draw` for `Tooltip` --- graphics/src/widget/tooltip.rs | 8 -------- 1 file changed, 8 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/tooltip.rs b/graphics/src/widget/tooltip.rs index d55d61b2..867f0437 100644 --- a/graphics/src/widget/tooltip.rs +++ b/graphics/src/widget/tooltip.rs @@ -1,5 +1,4 @@ //! Decorate content and apply alignment. -use crate::backend::{self, Backend}; use crate::Renderer; /// An element decorating some content. @@ -10,10 +9,3 @@ pub type Tooltip<'a, Message, Backend> = iced_native::Tooltip<'a, Message, Renderer>; pub use iced_native::tooltip::Position; - -impl iced_native::tooltip::Renderer for Renderer -where - B: Backend + backend::Text, -{ - const DEFAULT_PADDING: u16 = 5; -} -- cgit From e6156fb0c53d78ae75a1d85d4bfd81f6b7e8817e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 28 Oct 2021 22:13:26 +0700 Subject: Implement `Widget::draw` for `Svg` --- graphics/src/widget/svg.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/widget/svg.rs b/graphics/src/widget/svg.rs index b74a0abb..621f1a85 100644 --- a/graphics/src/widget/svg.rs +++ b/graphics/src/widget/svg.rs @@ -1,6 +1,6 @@ //! Display vector graphics in your application. use crate::backend::{self, Backend}; -use crate::Renderer; +use crate::{Primitive, Rectangle, Renderer}; use iced_native::svg; pub use iced_native::svg::{Handle, Svg}; @@ -12,4 +12,8 @@ where fn dimensions(&self, handle: &svg::Handle) -> (u32, u32) { self.backend().viewport_dimensions(handle) } + + fn draw(&mut self, handle: svg::Handle, bounds: Rectangle) { + self.draw_primitive(Primitive::Svg { handle, bounds }) + } } -- cgit From de728737fd69265f812f717ca70d3e0bd40838e5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 29 Oct 2021 16:45:47 +0700 Subject: Implement `Widget::draw` for `Canvas` in `iced_graphics` --- graphics/src/widget/canvas.rs | 46 +++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index 3990c2b9..1b0d49d8 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -4,11 +4,12 @@ //! [`Frame`]. It can be used for animation, data visualization, game graphics, //! and more! use crate::renderer::{self, Renderer}; -use crate::Backend; +use crate::{Backend, Primitive}; use iced_native::layout; use iced_native::{ - Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, + Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector, + Widget, }; use std::hash::Hash; use std::marker::PhantomData; @@ -188,31 +189,28 @@ where fn draw( &self, - _renderer: &mut Renderer, + renderer: &mut Renderer, _style: &renderer::Style, - _layout: Layout<'_>, - _cursor_position: Point, + layout: Layout<'_>, + cursor_position: Point, _viewport: &Rectangle, ) { - // let bounds = layout.bounds(); - // let translation = Vector::new(bounds.x, bounds.y); - // let cursor = Cursor::from_window_position(cursor_position); - - // ( - // Primitive::Translate { - // translation, - // content: Box::new(Primitive::Group { - // primitives: self - // .program - // .draw(bounds, cursor) - // .into_iter() - // .map(Geometry::into_primitive) - // .collect(), - // }), - // }, - // self.program.mouse_interaction(bounds, cursor), - // ) - // TODO + use iced_native::Renderer as _; + + let bounds = layout.bounds(); + let translation = Vector::new(bounds.x, bounds.y); + let cursor = Cursor::from_window_position(cursor_position); + + renderer.with_translation(translation, |renderer| { + renderer.draw_primitive(Primitive::Group { + primitives: self + .program + .draw(bounds, cursor) + .into_iter() + .map(Geometry::into_primitive) + .collect(), + }); + }); } fn hash_layout(&self, state: &mut Hasher) { -- cgit From 0b66095f9ffddad0a6bc6f4e8aa4e2311e89292a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 29 Oct 2021 16:47:30 +0700 Subject: Implement `Widget::mouse_interaction` for `Canvas` in `iced_graphics` --- graphics/src/widget/canvas.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'graphics') diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index 1b0d49d8..ad9704d0 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -7,6 +7,7 @@ use crate::renderer::{self, Renderer}; use crate::{Backend, Primitive}; use iced_native::layout; +use iced_native::mouse; use iced_native::{ Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector, Widget, @@ -187,6 +188,18 @@ where event::Status::Ignored } + fn mouse_interaction( + &self, + layout: Layout<'_>, + _viewport: &Rectangle, + cursor_position: Point, + ) -> mouse::Interaction { + let bounds = layout.bounds(); + let cursor = Cursor::from_window_position(cursor_position); + + self.program.mouse_interaction(bounds, cursor) + } + fn draw( &self, renderer: &mut Renderer, -- cgit From 77bab6beeecb148225ee6081e087aa30c0e80c26 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 30 Oct 2021 20:38:12 +0700 Subject: Implement `Widget::draw` for `QRCode` in `iced_graphics` --- graphics/src/widget/qr_code.rs | 94 +++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 47 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/qr_code.rs b/graphics/src/widget/qr_code.rs index 364b636b..285b8622 100644 --- a/graphics/src/widget/qr_code.rs +++ b/graphics/src/widget/qr_code.rs @@ -5,7 +5,8 @@ use crate::Backend; use iced_native::layout; use iced_native::{ - Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, + Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector, + Widget, }; use thiserror::Error; @@ -81,56 +82,55 @@ where fn draw( &self, - _renderer: &mut Renderer, + renderer: &mut Renderer, _style: &renderer::Style, - _layout: Layout<'_>, + layout: Layout<'_>, _cursor_position: Point, _viewport: &Rectangle, ) { - // let bounds = layout.bounds(); - // let side_length = self.state.width + 2 * QUIET_ZONE; - - // // Reuse cache if possible - // let geometry = self.state.cache.draw(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, - // ); - // }); - // }); - - // ( - // Primitive::Translate { - // translation: Vector::new(bounds.x, bounds.y), - // content: Box::new(geometry.into_primitive()), - // }, - // mouse::Interaction::default(), - // ) - // TODO + 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(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.into_primitive()); + }); } } -- cgit From 0aafcde0ef1533c9eeba0379de8c0082e30c7504 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 15:35:12 +0700 Subject: Remove `widget` module re-exports in `iced_native` --- graphics/src/backend.rs | 6 +++--- graphics/src/primitive.rs | 6 +++--- graphics/src/widget/button.rs | 4 ++-- graphics/src/widget/checkbox.rs | 2 +- graphics/src/widget/column.rs | 2 +- graphics/src/widget/container.rs | 2 +- graphics/src/widget/image.rs | 4 ++-- graphics/src/widget/image/viewer.rs | 2 +- graphics/src/widget/pane_grid.rs | 4 ++-- graphics/src/widget/pick_list.rs | 4 ++-- graphics/src/widget/progress_bar.rs | 2 +- graphics/src/widget/radio.rs | 2 +- graphics/src/widget/row.rs | 2 +- graphics/src/widget/rule.rs | 2 +- graphics/src/widget/scrollable.rs | 4 ++-- graphics/src/widget/slider.rs | 2 +- graphics/src/widget/space.rs | 2 +- graphics/src/widget/svg.rs | 4 ++-- graphics/src/widget/text.rs | 2 +- graphics/src/widget/text_input.rs | 4 ++-- graphics/src/widget/toggler.rs | 2 +- graphics/src/widget/tooltip.rs | 4 ++-- 22 files changed, 34 insertions(+), 34 deletions(-) (limited to 'graphics') diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index b8ff5d21..4692ced1 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -1,7 +1,7 @@ //! Write a graphics backend. -use iced_native::image; -use iced_native::svg; -use iced_native::text; +use iced_native::widget::image; +use iced_native::widget::svg; +use iced_native::widget::text; use iced_native::{Font, Point, Size}; /// The graphics backend of a [`Renderer`]. diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index b984feaa..16046387 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -1,6 +1,6 @@ -use iced_native::{ - image, svg, Background, Color, Font, Rectangle, Size, Vector, -}; +use iced_native::widget::image; +use iced_native::widget::svg; +use iced_native::{Background, Color, Font, Rectangle, Size, Vector}; use crate::alignment; use crate::triangle; diff --git a/graphics/src/widget/button.rs b/graphics/src/widget/button.rs index 3d3e6675..7b40c47b 100644 --- a/graphics/src/widget/button.rs +++ b/graphics/src/widget/button.rs @@ -3,10 +3,10 @@ //! A [`Button`] has some local [`State`]. use crate::Renderer; -pub use iced_native::button::{State, Style, StyleSheet}; +pub use iced_native::widget::button::{State, Style, StyleSheet}; /// A widget that produces a message when clicked. /// /// This is an alias of an `iced_native` button with an `iced_wgpu::Renderer`. pub type Button<'a, Message, Backend> = - iced_native::Button<'a, Message, Renderer>; + iced_native::widget::Button<'a, Message, Renderer>; diff --git a/graphics/src/widget/checkbox.rs b/graphics/src/widget/checkbox.rs index a458d85c..0d2e93f9 100644 --- a/graphics/src/widget/checkbox.rs +++ b/graphics/src/widget/checkbox.rs @@ -7,4 +7,4 @@ pub use iced_style::checkbox::{Style, StyleSheet}; /// /// This is an alias of an `iced_native` checkbox with an `iced_wgpu::Renderer`. pub type Checkbox<'a, Message, Backend> = - iced_native::Checkbox<'a, Message, Renderer>; + iced_native::widget::Checkbox<'a, Message, Renderer>; diff --git a/graphics/src/widget/column.rs b/graphics/src/widget/column.rs index 567529e5..561681d5 100644 --- a/graphics/src/widget/column.rs +++ b/graphics/src/widget/column.rs @@ -2,4 +2,4 @@ use crate::Renderer; /// A container that distributes its contents vertically. pub type Column<'a, Message, Backend> = - iced_native::Column<'a, Message, Renderer>; + iced_native::widget::Column<'a, Message, Renderer>; diff --git a/graphics/src/widget/container.rs b/graphics/src/widget/container.rs index c4c4e5ba..99996f3b 100644 --- a/graphics/src/widget/container.rs +++ b/graphics/src/widget/container.rs @@ -8,4 +8,4 @@ pub use iced_style::container::{Style, StyleSheet}; /// This is an alias of an `iced_native` container with a default /// `Renderer`. pub type Container<'a, Message, Backend> = - iced_native::Container<'a, Message, Renderer>; + iced_native::widget::Container<'a, Message, Renderer>; diff --git a/graphics/src/widget/image.rs b/graphics/src/widget/image.rs index 242d36d7..0131dd82 100644 --- a/graphics/src/widget/image.rs +++ b/graphics/src/widget/image.rs @@ -4,9 +4,9 @@ pub mod viewer; use crate::backend::{self, Backend}; use crate::{Primitive, Rectangle, Renderer}; -use iced_native::image; +use iced_native::widget::image; -pub use iced_native::image::{Handle, Image, Viewer}; +pub use iced_native::widget::image::{Handle, Image, Viewer}; impl image::Renderer for Renderer where diff --git a/graphics/src/widget/image/viewer.rs b/graphics/src/widget/image/viewer.rs index ea7d8591..9260990a 100644 --- a/graphics/src/widget/image/viewer.rs +++ b/graphics/src/widget/image/viewer.rs @@ -1,2 +1,2 @@ //! Zoom and pan on an image. -pub use iced_native::image::Viewer; +pub use iced_native::widget::image::Viewer; diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index 4dc42e41..95189920 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -9,7 +9,7 @@ //! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.3/examples/pane_grid use crate::Renderer; -pub use iced_native::pane_grid::{ +pub use iced_native::widget::pane_grid::{ Axis, Configuration, Content, Direction, DragEvent, Node, Pane, ResizeEvent, Split, State, TitleBar, }; @@ -23,4 +23,4 @@ pub use iced_style::pane_grid::{Line, StyleSheet}; /// /// This is an alias of an `iced_native` pane grid with an `iced_wgpu::Renderer`. pub type PaneGrid<'a, Message, Backend> = - iced_native::PaneGrid<'a, Message, Renderer>; + iced_native::widget::PaneGrid<'a, Message, Renderer>; diff --git a/graphics/src/widget/pick_list.rs b/graphics/src/widget/pick_list.rs index b5cb0a44..f3ac12b8 100644 --- a/graphics/src/widget/pick_list.rs +++ b/graphics/src/widget/pick_list.rs @@ -1,9 +1,9 @@ //! Display a dropdown list of selectable values. use crate::Renderer; -pub use iced_native::pick_list::State; +pub use iced_native::widget::pick_list::State; pub use iced_style::pick_list::{Style, StyleSheet}; /// A widget allowing the selection of a single value from a list of options. pub type PickList<'a, T, Message, Backend> = - iced_native::PickList<'a, T, Message, Renderer>; + iced_native::widget::PickList<'a, T, Message, Renderer>; diff --git a/graphics/src/widget/progress_bar.rs b/graphics/src/widget/progress_bar.rs index df4f5ec6..3666ecfd 100644 --- a/graphics/src/widget/progress_bar.rs +++ b/graphics/src/widget/progress_bar.rs @@ -2,4 +2,4 @@ //! //! A [`ProgressBar`] has a range of possible values and a current value, //! as well as a length, height and style. -pub use iced_native::progress_bar::*; +pub use iced_native::widget::progress_bar::*; diff --git a/graphics/src/widget/radio.rs b/graphics/src/widget/radio.rs index 4185bd61..20d72747 100644 --- a/graphics/src/widget/radio.rs +++ b/graphics/src/widget/radio.rs @@ -8,4 +8,4 @@ pub use iced_style::radio::{Style, StyleSheet}; /// This is an alias of an `iced_native` radio button with an /// `iced_wgpu::Renderer`. pub type Radio<'a, Message, Backend> = - iced_native::Radio<'a, Message, Renderer>; + iced_native::widget::Radio<'a, Message, Renderer>; diff --git a/graphics/src/widget/row.rs b/graphics/src/widget/row.rs index 55960c04..5bee3fd5 100644 --- a/graphics/src/widget/row.rs +++ b/graphics/src/widget/row.rs @@ -2,4 +2,4 @@ use crate::Renderer; /// A container that distributes its contents horizontally. pub type Row<'a, Message, Backend> = - iced_native::Row<'a, Message, Renderer>; + iced_native::widget::Row<'a, Message, Renderer>; diff --git a/graphics/src/widget/rule.rs b/graphics/src/widget/rule.rs index 57f4a530..b96924fa 100644 --- a/graphics/src/widget/rule.rs +++ b/graphics/src/widget/rule.rs @@ -1,3 +1,3 @@ //! Display a horizontal or vertical rule for dividing content. -pub use iced_native::rule::*; +pub use iced_native::widget::rule::*; diff --git a/graphics/src/widget/scrollable.rs b/graphics/src/widget/scrollable.rs index 61eae587..3fdaf668 100644 --- a/graphics/src/widget/scrollable.rs +++ b/graphics/src/widget/scrollable.rs @@ -1,7 +1,7 @@ //! Navigate an endless amount of content with a scrollbar. use crate::Renderer; -pub use iced_native::scrollable::State; +pub use iced_native::widget::scrollable::State; pub use iced_style::scrollable::{Scrollbar, Scroller, StyleSheet}; /// A widget that can vertically display an infinite amount of content @@ -10,4 +10,4 @@ pub use iced_style::scrollable::{Scrollbar, Scroller, StyleSheet}; /// This is an alias of an `iced_native` scrollable with a default /// `Renderer`. pub type Scrollable<'a, Message, Backend> = - iced_native::Scrollable<'a, Message, Renderer>; + iced_native::widget::Scrollable<'a, Message, Renderer>; diff --git a/graphics/src/widget/slider.rs b/graphics/src/widget/slider.rs index 766ecbb6..96dc6ec4 100644 --- a/graphics/src/widget/slider.rs +++ b/graphics/src/widget/slider.rs @@ -1,5 +1,5 @@ //! Display an interactive selector of a single value from a range of values. //! //! A [`Slider`] has some local [`State`]. -pub use iced_native::slider::{Slider, State}; +pub use iced_native::widget::slider::{Slider, State}; pub use iced_style::slider::{Handle, HandleShape, Style, StyleSheet}; diff --git a/graphics/src/widget/space.rs b/graphics/src/widget/space.rs index a4d60d4b..77e93dbb 100644 --- a/graphics/src/widget/space.rs +++ b/graphics/src/widget/space.rs @@ -1 +1 @@ -pub use iced_native::Space; +pub use iced_native::widget::Space; diff --git a/graphics/src/widget/svg.rs b/graphics/src/widget/svg.rs index 621f1a85..64a2b461 100644 --- a/graphics/src/widget/svg.rs +++ b/graphics/src/widget/svg.rs @@ -1,9 +1,9 @@ //! Display vector graphics in your application. use crate::backend::{self, Backend}; use crate::{Primitive, Rectangle, Renderer}; -use iced_native::svg; +use iced_native::widget::svg; -pub use iced_native::svg::{Handle, Svg}; +pub use iced_native::widget::svg::{Handle, Svg}; impl svg::Renderer for Renderer where diff --git a/graphics/src/widget/text.rs b/graphics/src/widget/text.rs index ec0349f9..43516fca 100644 --- a/graphics/src/widget/text.rs +++ b/graphics/src/widget/text.rs @@ -4,4 +4,4 @@ use crate::Renderer; /// A paragraph of text. /// /// This is an alias of an `iced_native` text with an `iced_wgpu::Renderer`. -pub type Text = iced_native::Text>; +pub type Text = iced_native::widget::Text>; diff --git a/graphics/src/widget/text_input.rs b/graphics/src/widget/text_input.rs index ebb9138f..87384d7e 100644 --- a/graphics/src/widget/text_input.rs +++ b/graphics/src/widget/text_input.rs @@ -3,11 +3,11 @@ //! A [`TextInput`] has some local [`State`]. use crate::Renderer; -pub use iced_native::text_input::State; +pub use iced_native::widget::text_input::State; pub use iced_style::text_input::{Style, StyleSheet}; /// A field that can be filled with text. /// /// This is an alias of an `iced_native` text input with an `iced_wgpu::Renderer`. pub type TextInput<'a, Message, Backend> = - iced_native::TextInput<'a, Message, Renderer>; + iced_native::widget::TextInput<'a, Message, Renderer>; diff --git a/graphics/src/widget/toggler.rs b/graphics/src/widget/toggler.rs index 60d8a347..0cabd027 100644 --- a/graphics/src/widget/toggler.rs +++ b/graphics/src/widget/toggler.rs @@ -7,4 +7,4 @@ pub use iced_style::toggler::{Style, StyleSheet}; /// /// This is an alias of an `iced_native` toggler with an `iced_wgpu::Renderer`. pub type Toggler = - iced_native::Toggler>; + iced_native::widget::Toggler>; diff --git a/graphics/src/widget/tooltip.rs b/graphics/src/widget/tooltip.rs index 867f0437..7dc12ed4 100644 --- a/graphics/src/widget/tooltip.rs +++ b/graphics/src/widget/tooltip.rs @@ -6,6 +6,6 @@ use crate::Renderer; /// This is an alias of an `iced_native` tooltip with a default /// `Renderer`. pub type Tooltip<'a, Message, Backend> = - iced_native::Tooltip<'a, Message, Renderer>; + iced_native::widget::Tooltip<'a, Message, Renderer>; -pub use iced_native::tooltip::Position; +pub use iced_native::widget::tooltip::Position; -- cgit From b3a01973c6c726e6539be959659f4306ef3234c6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 16:13:03 +0700 Subject: Introduce first-class `text` module in `iced_native` --- graphics/src/backend.rs | 2 +- graphics/src/renderer.rs | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'graphics') diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index 4692ced1..533ac15f 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -1,7 +1,7 @@ //! Write a graphics backend. +use iced_native::text; use iced_native::widget::image; use iced_native::widget::svg; -use iced_native::widget::text; use iced_native::{Font, Point, Size}; /// The graphics backend of a [`Renderer`]. diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index 8b6c2217..125962ba 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -2,6 +2,7 @@ use crate::backend::{self, Backend}; use crate::{Primitive, Vector}; use iced_native::layout; use iced_native::renderer; +use iced_native::text::{self, Text}; use iced_native::{Element, Font, Point, Rectangle, Size}; pub use iced_native::renderer::Style; @@ -104,7 +105,7 @@ where } } -impl renderer::Text for Renderer +impl text::Renderer for Renderer where B: Backend + backend::Text, { @@ -137,7 +138,7 @@ where bounds: Size, point: Point, nearest_only: bool, - ) -> Option { + ) -> Option { self.backend().hit_test( content, size, @@ -148,7 +149,7 @@ where ) } - fn fill_text(&mut self, text: renderer::text::Section<'_, Self::Font>) { + fn fill_text(&mut self, text: Text<'_, Self::Font>) { self.primitives.push(Primitive::Text { content: text.content.to_string(), bounds: text.bounds, -- cgit From c4186a71b746b603984e5fe1926a8cef6e8dcfcb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 16:20:50 +0700 Subject: Introduce first-class `image` module in `iced_native` --- graphics/src/backend.rs | 2 +- graphics/src/primitive.rs | 2 +- graphics/src/widget/image.rs | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) (limited to 'graphics') diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index 533ac15f..e9f3801a 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -1,6 +1,6 @@ //! Write a graphics backend. +use iced_native::image; use iced_native::text; -use iced_native::widget::image; use iced_native::widget::svg; use iced_native::{Font, Point, Size}; diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index 16046387..c46c212e 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -1,4 +1,4 @@ -use iced_native::widget::image; +use iced_native::image; use iced_native::widget::svg; use iced_native::{Background, Color, Font, Rectangle, Size, Vector}; diff --git a/graphics/src/widget/image.rs b/graphics/src/widget/image.rs index 0131dd82..ad8159de 100644 --- a/graphics/src/widget/image.rs +++ b/graphics/src/widget/image.rs @@ -4,9 +4,10 @@ pub mod viewer; use crate::backend::{self, Backend}; use crate::{Primitive, Rectangle, Renderer}; -use iced_native::widget::image; +use iced_native::image; -pub use iced_native::widget::image::{Handle, Image, Viewer}; +pub use iced_native::widget::image::{Image, Viewer}; +pub use image::Handle; impl image::Renderer for Renderer where -- cgit From 9a3c81f336b8e29c64471026860f3c9d8b56348c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 16:24:31 +0700 Subject: Introduce first-class `svg` module in `iced_native` --- graphics/src/backend.rs | 2 +- graphics/src/primitive.rs | 2 +- graphics/src/widget/svg.rs | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) (limited to 'graphics') diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index e9f3801a..b8ff5d21 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -1,7 +1,7 @@ //! Write a graphics backend. use iced_native::image; +use iced_native::svg; use iced_native::text; -use iced_native::widget::svg; use iced_native::{Font, Point, Size}; /// The graphics backend of a [`Renderer`]. diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index c46c212e..5f7a344d 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -1,5 +1,5 @@ use iced_native::image; -use iced_native::widget::svg; +use iced_native::svg; use iced_native::{Background, Color, Font, Rectangle, Size, Vector}; use crate::alignment; diff --git a/graphics/src/widget/svg.rs b/graphics/src/widget/svg.rs index 64a2b461..5817a552 100644 --- a/graphics/src/widget/svg.rs +++ b/graphics/src/widget/svg.rs @@ -1,9 +1,10 @@ //! Display vector graphics in your application. use crate::backend::{self, Backend}; use crate::{Primitive, Rectangle, Renderer}; -use iced_native::widget::svg; +use iced_native::svg; -pub use iced_native::widget::svg::{Handle, Svg}; +pub use iced_native::widget::svg::Svg; +pub use svg::Handle; impl svg::Renderer for Renderer where -- cgit From c9ed15782c3a62fcbfe56a141837b384ada82aaa Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 17:48:23 +0700 Subject: Introduce state lifetime for `style_sheet` in `Toggler` --- graphics/src/widget/toggler.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/toggler.rs b/graphics/src/widget/toggler.rs index 0cabd027..9053e6ed 100644 --- a/graphics/src/widget/toggler.rs +++ b/graphics/src/widget/toggler.rs @@ -6,5 +6,5 @@ pub use iced_style::toggler::{Style, StyleSheet}; /// A toggler that can be toggled. /// /// This is an alias of an `iced_native` toggler with an `iced_wgpu::Renderer`. -pub type Toggler = - iced_native::widget::Toggler>; +pub type Toggler<'a, Message, Backend> = + iced_native::widget::Toggler<'a, Message, Renderer>; -- cgit From 631e95ee0be01dc7f5e5183e1429972aee37787f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 2 Nov 2021 15:03:29 +0700 Subject: Move `viewport` argument to last position in `mouse_interaction` methods This keeps the order of the arguments consistent with `draw`. --- graphics/src/widget/canvas.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index ad9704d0..639c2a9b 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -191,8 +191,8 @@ where fn mouse_interaction( &self, layout: Layout<'_>, - _viewport: &Rectangle, cursor_position: Point, + _viewport: &Rectangle, ) -> mouse::Interaction { let bounds = layout.bounds(); let cursor = Cursor::from_window_position(cursor_position); -- cgit From ef5a731e4bd17f763b1697c7f0ac595928e91e58 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 4 Nov 2021 18:23:18 +0700 Subject: Use `mem::take` instead of `mem::replace` in `iced_graphics::Renderer` Thanks to @tarkah for pointing this out! --- graphics/src/renderer.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'graphics') diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index 125962ba..df9f1ac0 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -53,8 +53,7 @@ where } fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)) { - let current_primitives = - std::mem::replace(&mut self.primitives, Vec::new()); + let current_primitives = std::mem::take(&mut self.primitives); f(self); @@ -74,8 +73,7 @@ where translation: Vector, f: impl FnOnce(&mut Self), ) { - let current_primitives = - std::mem::replace(&mut self.primitives, Vec::new()); + let current_primitives = std::mem::take(&mut self.primitives); f(self); -- cgit From 023aded2772f0cd6abd716fe5c8624d5d22e21fa Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 4 Nov 2021 19:22:29 +0700 Subject: Rename `fill_rectangle` to `fill_quad` in `Renderer` --- graphics/src/renderer.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'graphics') diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index df9f1ac0..30f9d40e 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -3,7 +3,7 @@ use crate::{Primitive, Vector}; use iced_native::layout; use iced_native::renderer; use iced_native::text::{self, Text}; -use iced_native::{Element, Font, Point, Rectangle, Size}; +use iced_native::{Background, Element, Font, Point, Rectangle, Size}; pub use iced_native::renderer::Style; @@ -88,10 +88,14 @@ where }); } - fn fill_rectangle(&mut self, quad: renderer::Quad) { + fn fill_quad( + &mut self, + quad: renderer::Quad, + background: impl Into, + ) { self.primitives.push(Primitive::Quad { bounds: quad.bounds, - background: quad.background, + background: background.into(), border_radius: quad.border_radius, border_width: quad.border_width, border_color: quad.border_color, -- cgit From 9fe65ed729c75a8401765cf373345aaba93352ca Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 5 Nov 2021 15:38:27 +0700 Subject: Rename `Renderer::present` to `with_primitives` --- graphics/src/renderer.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index 30f9d40e..69ed8775 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -31,7 +31,9 @@ impl Renderer { self.primitives.push(primitive); } - pub fn present(&mut self, f: impl FnOnce(&mut B, &[Primitive])) { + /// Runs the given closure with the [`Backend`] and the recorded primitives + /// of the [`Renderer`]. + pub fn with_primitives(&mut self, f: impl FnOnce(&mut B, &[Primitive])) { f(&mut self.backend, &self.primitives); } } -- cgit From b64e8752056e526b95ad3c0ff2385000c1213698 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 5 Nov 2021 15:38:40 +0700 Subject: Write missing documentation in `iced_graphics` --- graphics/src/backend.rs | 1 + graphics/src/lib.rs | 2 +- graphics/src/renderer.rs | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index b8ff5d21..7e0af2cc 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -16,6 +16,7 @@ pub trait Backend { fn trim_measurements(&mut self) {} } +/// A graphics backend that supports text rendering. pub trait Text { /// The icon font of the backend. const ICON_FONT: Font; diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index dbd94e99..5b8defc5 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -4,7 +4,7 @@ //! ![The native path of the Iced ecosystem](https://github.com/hecrj/iced/blob/0525d76ff94e828b7b21634fa94a747022001c83/docs/graphs/native.png?raw=true) //! //! [`iced`]: https://github.com/hecrj/iced -//#![deny(missing_docs)] +#![deny(missing_docs)] #![deny(missing_debug_implementations)] #![deny(unused_results)] #![deny(unsafe_code)] diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index 69ed8775..c32eb471 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -1,3 +1,4 @@ +//! Create a renderer from a [`Backend`]. use crate::backend::{self, Backend}; use crate::{Primitive, Vector}; use iced_native::layout; @@ -23,10 +24,12 @@ impl Renderer { } } + /// Returns the [`Backend`] of the [`Renderer`]. pub fn backend(&self) -> &B { &self.backend } + /// Enqueues the given [`Primitive`] in the [`Renderer`] for drawing. pub fn draw_primitive(&mut self, primitive: Primitive) { self.primitives.push(primitive); } -- cgit