Compare commits

...

5 Commits

Author SHA1 Message Date
Héctor Ramón Jiménez 836a41c53e Fix `Widget::draw` for `Rule` 2020-09-08 00:53:03 +02:00
Héctor Ramón Jiménez 577fe8774c Merge branch 'feature/error-handling' into improvement/viewport-aware-drawing 2020-09-08 00:52:01 +02:00
Héctor Ramón Jiménez 3a0eea0dcd Merge branch 'master' into improvement/viewport-aware-drawing 2020-08-19 02:05:41 +02:00
Héctor Ramón Jiménez 9cab88b986 Draw only visible options in `overlay::Menu` 2020-08-18 04:12:23 +02:00
Héctor Ramón Jiménez 4bd44e4ed2 Introduce `viewport` to `Widget::draw`
This should eventually allow us to only generate primitives that are
visible.
2020-08-18 03:37:32 +02:00
33 changed files with 138 additions and 38 deletions

View File

@ -12,7 +12,7 @@ mod circle {
use iced_graphics::{Backend, Defaults, Primitive, Renderer};
use iced_native::{
layout, mouse, Background, Color, Element, Hasher, Layout, Length,
Point, Size, Widget,
Point, Rectangle, Size, Widget,
};
pub struct Circle {
@ -60,6 +60,7 @@ mod circle {
_defaults: &Defaults,
layout: Layout<'_>,
_cursor_position: Point,
_viewport: &Rectangle,
) -> (Primitive, mouse::Interaction) {
(
Primitive::Quad {

View File

@ -15,8 +15,8 @@ mod rainbow {
Backend, Defaults, Primitive, Renderer,
};
use iced_native::{
layout, mouse, Element, Hasher, Layout, Length, Point, Size, Vector,
Widget,
layout, mouse, Element, Hasher, Layout, Length, Point, Rectangle, Size,
Vector, Widget,
};
pub struct Rainbow;
@ -57,6 +57,7 @@ mod rainbow {
_defaults: &Defaults,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
) -> (Primitive, mouse::Interaction) {
let b = layout.bounds();

View File

@ -42,6 +42,7 @@ where
&mut self,
bounds: Rectangle,
cursor_position: Point,
viewport: &Rectangle,
options: &[T],
hovered_option: Option<usize>,
padding: u16,
@ -52,16 +53,24 @@ where
use std::f32;
let is_mouse_over = bounds.contains(cursor_position);
let option_height = text_size as usize + padding as usize * 2;
let mut primitives = Vec::new();
for (i, option) in options.iter().enumerate() {
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
+ ((text_size as usize + padding as usize * 2) * i) as f32,
y: bounds.y + (option_height * i) as f32,
width: bounds.width,
height: f32::from(text_size + padding * 2),
};

View File

@ -96,10 +96,11 @@ where
widget: &dyn Widget<Message, Self>,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
color: Color,
) -> Self::Output {
let (primitive, cursor) =
widget.draw(self, defaults, layout, cursor_position);
widget.draw(self, defaults, layout, cursor_position, viewport);
let mut primitives = Vec::new();

View File

@ -62,6 +62,7 @@ where
},
content_layout,
cursor_position,
&bounds,
);
(

View File

@ -8,8 +8,8 @@
//! [`Frame`]: struct.Frame.html
use crate::{Backend, Defaults, Primitive, Renderer};
use iced_native::{
layout, mouse, Clipboard, Element, Hasher, Layout, Length, Point, Size,
Vector, Widget,
layout, mouse, Clipboard, Element, Hasher, Layout, Length, Point,
Rectangle, Size, Vector, Widget,
};
use std::hash::Hash;
use std::marker::PhantomData;
@ -196,6 +196,7 @@ where
_defaults: &Defaults,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
) -> (Primitive, mouse::Interaction) {
let bounds = layout.bounds();
let translation = Vector::new(bounds.x, bounds.y);

View File

@ -1,7 +1,7 @@
use crate::{Backend, Primitive, Renderer};
use iced_native::column;
use iced_native::mouse;
use iced_native::{Element, Layout, Point};
use iced_native::{Element, Layout, Point, Rectangle};
/// A container that distributes its contents vertically.
pub type Column<'a, Message, Backend> =
@ -17,6 +17,7 @@ where
content: &[Element<'_, Message, Self>],
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) -> Self::Output {
let mut mouse_interaction = mouse::Interaction::default();
@ -26,8 +27,13 @@ where
.iter()
.zip(layout.children())
.map(|(child, layout)| {
let (primitive, new_mouse_interaction) =
child.draw(self, defaults, layout, cursor_position);
let (primitive, new_mouse_interaction) = child.draw(
self,
defaults,
layout,
cursor_position,
viewport,
);
if new_mouse_interaction > mouse_interaction {
mouse_interaction = new_mouse_interaction;

View File

@ -24,6 +24,7 @@ where
defaults: &Defaults,
bounds: Rectangle,
cursor_position: Point,
viewport: &Rectangle,
style_sheet: &Self::Style,
content: &Element<'_, Message, Self>,
content_layout: Layout<'_>,
@ -36,8 +37,13 @@ where
},
};
let (content, mouse_interaction) =
content.draw(self, &defaults, content_layout, cursor_position);
let (content, mouse_interaction) = content.draw(
self,
&defaults,
content_layout,
cursor_position,
viewport,
);
if let Some(background) = background(bounds, &style) {
(

View File

@ -137,7 +137,7 @@ where
let (body, body_layout) = body;
let (body_primitive, body_interaction) =
body.draw(self, defaults, body_layout, cursor_position);
body.draw(self, defaults, body_layout, cursor_position, &bounds);
let background = crate::widget::container::background(bounds, &style);
@ -224,6 +224,7 @@ where
&defaults,
controls_layout,
cursor_position,
&bounds,
);
(

View File

@ -1,7 +1,7 @@
use crate::{Backend, Primitive, Renderer};
use iced_native::mouse;
use iced_native::row;
use iced_native::{Element, Layout, Point};
use iced_native::{Element, Layout, Point, Rectangle};
/// A container that distributes its contents horizontally.
pub type Row<'a, Message, Backend> =
@ -17,6 +17,7 @@ where
content: &[Element<'_, Message, Self>],
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) -> Self::Output {
let mut mouse_interaction = mouse::Interaction::default();
@ -26,8 +27,13 @@ where
.iter()
.zip(layout.children())
.map(|(child, layout)| {
let (primitive, new_mouse_interaction) =
child.draw(self, defaults, layout, cursor_position);
let (primitive, new_mouse_interaction) = child.draw(
self,
defaults,
layout,
cursor_position,
viewport,
);
if new_mouse_interaction > mouse_interaction {
mouse_interaction = new_mouse_interaction;

View File

@ -1,6 +1,7 @@
use crate::layout;
use crate::overlay;
use crate::{
layout, overlay, Clipboard, Color, Event, Hasher, Layout, Length, Point,
Widget,
Clipboard, Color, Event, Hasher, Layout, Length, Point, Rectangle, Widget,
};
/// A generic [`Widget`].
@ -260,9 +261,10 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) -> Renderer::Output {
self.widget
.draw(renderer, defaults, layout, cursor_position)
.draw(renderer, defaults, layout, cursor_position, viewport)
}
/// Computes the _layout_ hash of the [`Element`].
@ -356,9 +358,10 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) -> Renderer::Output {
self.widget
.draw(renderer, defaults, layout, cursor_position)
.draw(renderer, defaults, layout, cursor_position, viewport)
}
fn hash_layout(&self, state: &mut Hasher) {
@ -437,12 +440,14 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) -> Renderer::Output {
renderer.explain(
defaults,
self.element.widget.as_ref(),
layout,
cursor_position,
viewport,
self.color,
)
}

View File

@ -1,4 +1,4 @@
use crate::{Color, Layout, Point, Renderer, Widget};
use crate::{Color, Layout, Point, Rectangle, Renderer, Widget};
/// A renderer able to graphically explain a [`Layout`].
///
@ -21,6 +21,7 @@ pub trait Debugger: Renderer {
widget: &dyn Widget<Message, Self>,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
color: Color,
) -> Self::Output;
}

View File

@ -253,9 +253,13 @@ where
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
let primitives =
self.container
.draw(renderer, defaults, layout, cursor_position);
let primitives = self.container.draw(
renderer,
defaults,
layout,
cursor_position,
&layout.bounds(),
);
renderer.decorate(
layout.bounds(),
@ -368,11 +372,13 @@ where
_defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) -> Renderer::Output {
self::Renderer::draw(
renderer,
layout.bounds(),
cursor_position,
viewport,
self.options,
*self.hovered_option,
self.padding,
@ -418,6 +424,7 @@ pub trait Renderer:
&mut self,
bounds: Rectangle,
cursor_position: Point,
viewport: &Rectangle,
options: &[T],
hovered_option: Option<usize>,
padding: u16,

View File

@ -35,6 +35,7 @@ impl column::Renderer for Null {
_content: &[Element<'_, Message, Self>],
_layout: Layout<'_>,
_cursor_position: Point,
_viewport: &Rectangle,
) {
}
}
@ -46,6 +47,7 @@ impl row::Renderer for Null {
_content: &[Element<'_, Message, Self>],
_layout: Layout<'_>,
_cursor_position: Point,
_viewport: &Rectangle,
) {
}
}
@ -234,6 +236,7 @@ impl container::Renderer for Null {
_defaults: &Self::Defaults,
_bounds: Rectangle,
_cursor_position: Point,
_viewport: &Rectangle,
_style: &Self::Style,
_content: &Element<'_, Message, Self>,
_content_layout: Layout<'_>,

View File

@ -1,4 +1,6 @@
use crate::{layout, overlay, Clipboard, Element, Event, Layout, Point, Size};
use crate::layout;
use crate::overlay;
use crate::{Clipboard, Element, Event, Layout, Point, Rectangle, Size};
use std::hash::Hasher;
@ -327,6 +329,8 @@ where
renderer: &mut Renderer,
cursor_position: Point,
) -> Renderer::Output {
let viewport = Rectangle::with_size(self.bounds);
let overlay = if let Some(mut overlay) =
self.root.overlay(Layout::new(&self.base.layout))
{
@ -365,6 +369,7 @@ where
&Renderer::Defaults::default(),
Layout::new(&self.base.layout),
base_cursor,
&viewport,
);
renderer.overlay(
@ -378,6 +383,7 @@ where
&Renderer::Defaults::default(),
Layout::new(&self.base.layout),
cursor_position,
&viewport,
)
}
}

View File

@ -73,7 +73,9 @@ pub use text::Text;
#[doc(no_inline)]
pub use text_input::TextInput;
use crate::{layout, overlay, Clipboard, Event, Hasher, Layout, Length, Point};
use crate::{
layout, overlay, Clipboard, Event, Hasher, Layout, Length, Point, Rectangle,
};
/// A component that displays information and allows interaction.
///
@ -137,6 +139,7 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) -> Renderer::Output;
/// Computes the _layout_ hash of the [`Widget`].

View File

@ -215,6 +215,7 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
renderer.draw(
defaults,

View File

@ -180,6 +180,7 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
let bounds = layout.bounds();
let mut children = layout.children();

View File

@ -1,9 +1,11 @@
//! Distribute content vertically.
use std::hash::Hash;
use crate::layout;
use crate::overlay;
use crate::{
layout, overlay, Align, Clipboard, Element, Event, Hasher, Layout, Length,
Point, Widget,
Align, Clipboard, Element, Event, Hasher, Layout, Length, Point, Rectangle,
Widget,
};
use std::u32;
@ -185,8 +187,15 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) -> Renderer::Output {
renderer.draw(defaults, &self.children, layout, cursor_position)
renderer.draw(
defaults,
&self.children,
layout,
cursor_position,
viewport,
)
}
fn hash_layout(&self, state: &mut Hasher) {
@ -240,6 +249,7 @@ pub trait Renderer: crate::Renderer + Sized {
content: &[Element<'_, Message, Self>],
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) -> Self::Output;
}

View File

@ -191,11 +191,13 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) -> Renderer::Output {
renderer.draw(
defaults,
layout.bounds(),
cursor_position,
viewport,
&self.style,
&self.content,
layout.children().next().unwrap(),
@ -242,6 +244,7 @@ pub trait Renderer: crate::Renderer {
defaults: &Self::Defaults,
bounds: Rectangle,
cursor_position: Point,
viewport: &Rectangle,
style: &Self::Style,
content: &Element<'_, Message, Self>,
content_layout: Layout<'_>,

View File

@ -1,5 +1,6 @@
//! Display images in your user interface.
use crate::{layout, Element, Hasher, Layout, Length, Point, Size, Widget};
use crate::layout;
use crate::{Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget};
use std::{
hash::{Hash, Hasher as _},
@ -97,6 +98,7 @@ where
_defaults: &Renderer::Defaults,
layout: Layout<'_>,
_cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
renderer.draw(self.handle.clone(), layout)
}

View File

@ -588,6 +588,7 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
let picked_split = self
.state

View File

@ -256,6 +256,7 @@ where
_defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
self::Renderer::draw(
renderer,

View File

@ -104,6 +104,7 @@ where
_defaults: &Renderer::Defaults,
layout: Layout<'_>,
_cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
renderer.draw(
layout.bounds(),

View File

@ -181,6 +181,7 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
let bounds = layout.bounds();
let mut children = layout.children();

View File

@ -1,11 +1,12 @@
//! Distribute content horizontally.
use std::hash::Hash;
use crate::layout;
use crate::overlay;
use crate::{
layout, overlay, Align, Clipboard, Element, Event, Hasher, Layout, Length,
Point, Widget,
Align, Clipboard, Element, Event, Hasher, Layout, Length, Point, Rectangle,
Widget,
};
use std::hash::Hash;
use std::u32;
/// A container that distributes its contents horizontally.
@ -186,8 +187,15 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) -> Renderer::Output {
renderer.draw(defaults, &self.children, layout, cursor_position)
renderer.draw(
defaults,
&self.children,
layout,
cursor_position,
viewport,
)
}
fn hash_layout(&self, state: &mut Hasher) {
@ -242,6 +250,7 @@ pub trait Renderer: crate::Renderer + Sized {
children: &[Element<'_, Message, Self>],
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) -> Self::Output;
}

View File

@ -77,6 +77,7 @@ where
_defaults: &Renderer::Defaults,
layout: Layout<'_>,
_cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
renderer.draw(layout.bounds(), &self.style, self.is_horizontal)
}

View File

@ -264,6 +264,7 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
let bounds = layout.bounds();
let content_layout = layout.children().next().unwrap();
@ -289,6 +290,10 @@ where
defaults,
content_layout,
cursor_position,
&Rectangle {
y: bounds.y + offset as f32,
..bounds
},
)
};

View File

@ -257,6 +257,7 @@ where
_defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
let start = *self.range.start();
let end = *self.range.end();

View File

@ -71,6 +71,7 @@ where
_defaults: &Renderer::Defaults,
layout: Layout<'_>,
_cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
renderer.draw(layout.bounds())
}

View File

@ -1,5 +1,6 @@
//! Display vector graphics in your application.
use crate::{layout, Element, Hasher, Layout, Length, Point, Size, Widget};
use crate::layout;
use crate::{Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget};
use std::{
hash::{Hash, Hasher as _},
@ -103,6 +104,7 @@ where
_defaults: &Renderer::Defaults,
layout: Layout<'_>,
_cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
renderer.draw(self.handle.clone(), layout)
}

View File

@ -149,6 +149,7 @@ where
defaults: &Renderer::Defaults,
layout: Layout<'_>,
_cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
renderer.draw(
defaults,

View File

@ -493,6 +493,7 @@ where
_defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
) -> Renderer::Output {
let bounds = layout.bounds();
let text_bounds = layout.children().next().unwrap().bounds();