Remove trait-specific draw logic in iced_native

This commit is contained in:
Héctor Ramón Jiménez 2021-10-14 16:07:22 +07:00
parent 3aae45c191
commit 03b3493138
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
71 changed files with 641 additions and 3126 deletions

View file

@ -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<B: Backend> {
backend: B,
primitive: Primitive,
}
impl<B: Backend> Renderer<B> {
/// 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<B> iced_native::Renderer for Renderer<B>
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<B> layout::Debugger for Renderer<B>
where
B: Backend,
{
fn explain<Message>(
&mut self,
defaults: &Defaults,
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, 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<Primitive>,
) {
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)) {}
}