Rename fill_rectangle to fill_quad in Renderer

This commit is contained in:
Héctor Ramón Jiménez 2021-11-04 19:22:29 +07:00
parent 343f9b7e2e
commit 023aded277
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
19 changed files with 320 additions and 245 deletions

View file

@ -12,8 +12,7 @@ mod circle {
use iced_native::layout::{self, Layout};
use iced_native::renderer;
use iced_native::{
Background, Color, Element, Hasher, Length, Point, Rectangle, Size,
Widget,
Color, Element, Hasher, Length, Point, Rectangle, Size, Widget,
};
pub struct Circle {
@ -60,13 +59,15 @@ mod circle {
_cursor_position: Point,
_viewport: &Rectangle,
) {
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds: layout.bounds(),
background: Background::Color(Color::BLACK),
border_radius: self.radius,
border_width: 0.0,
border_color: Color::TRANSPARENT,
});
},
Color::BLACK,
);
}
}

View file

@ -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<Background>,
) {
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,

View file

@ -4,8 +4,7 @@ use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::{
Background, Clipboard, Color, Hasher, Layout, Length, Point, Rectangle,
Widget,
Clipboard, Color, Hasher, Layout, Length, Point, Rectangle, Widget,
};
/// A generic [`Widget`].
@ -254,6 +253,7 @@ where
.draw(renderer, style, layout, cursor_position, viewport)
}
/// Returns the current [`mouse::Interaction`] of the [`Element`].
pub fn mouse_interaction(
&self,
layout: Layout<'_>,
@ -452,13 +452,15 @@ where
color: Color,
layout: Layout<'_>,
) {
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds: layout.bounds(),
border_color: color,
border_width: 1.0,
border_radius: 0.0,
background: Background::Color(Color::TRANSPARENT),
});
},
Color::TRANSPARENT,
);
for child in layout.children() {
explain_layout(renderer, color, child);

View file

@ -253,13 +253,15 @@ where
) {
let bounds = layout.bounds();
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds,
background: self.style.background,
border_color: self.style.border_color,
border_width: self.style.border_width,
border_radius: 0.0,
});
},
self.style.background,
);
self.container
.draw(renderer, style, layout, cursor_position, &bounds);
@ -432,13 +434,15 @@ where
};
if is_selected {
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds,
background: self.style.selected_background,
border_color: Color::TRANSPARENT,
border_width: 0.0,
border_radius: 0.0,
});
},
self.style.selected_background,
);
}
renderer.fill_text(Text {

View file

@ -42,23 +42,28 @@ pub trait Renderer: Sized {
element.layout(self, limits)
}
/// Draws the primitives recorded in the given closure in a new layer.
///
/// The layer will clip its contents to the provided `bounds`.
fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self));
/// Applies a `translation` to the primitives recorded in the given closure.
fn with_translation(
&mut self,
translation: Vector,
f: impl FnOnce(&mut Self),
);
/// Clears all of the recorded primitives in the [`Renderer`].
fn clear(&mut self);
fn fill_rectangle(&mut self, quad: Quad);
/// Fills a [`Quad`] with the provided [`Background`].
fn fill_quad(&mut self, quad: Quad, background: impl Into<Background>);
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Quad {
pub bounds: Rectangle,
pub background: Background,
pub border_radius: f32,
pub border_width: f32,
pub border_color: Color,

View file

@ -1,6 +1,6 @@
use crate::renderer::{self, Renderer};
use crate::text::{self, Text};
use crate::{Font, Point, Rectangle, Size, Vector};
use crate::{Background, Font, Point, Rectangle, Size, Vector};
/// A renderer that does nothing.
///
@ -27,7 +27,12 @@ impl Renderer for Null {
fn clear(&mut self) {}
fn fill_rectangle(&mut self, _quad: renderer::Quad) {}
fn fill_quad(
&mut self,
_quad: renderer::Quad,
_background: impl Into<Background>,
) {
}
}
impl text::Renderer for Null {

View file

@ -293,28 +293,32 @@ where
if styling.background.is_some() || styling.border_width > 0.0 {
if styling.shadow_offset != Vector::default() {
// TODO: Implement proper shadow support
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::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,
});
},
Background::Color([0.0, 0.0, 0.0, 0.5].into()),
);
}
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::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,
});
},
styling
.background
.unwrap_or(Background::Color(Color::TRANSPARENT)),
);
}
self.content.draw(

View file

@ -226,13 +226,15 @@ where
self.style_sheet.active(self.is_checked)
};
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds,
background: style.background,
border_radius: style.border_radius,
border_width: style.border_width,
border_color: style.border_color,
});
},
style.background,
);
if self.is_checked {
renderer.fill_text(text::Text {

View file

@ -238,6 +238,7 @@ where
}
}
/// Draws the background of a [`Container`] given its [`Style`] and its `bounds`.
pub fn draw_background<Renderer>(
renderer: &mut Renderer,
style: &Style,
@ -246,15 +247,17 @@ pub fn draw_background<Renderer>(
Renderer: crate::Renderer,
{
if style.background.is_some() || style.border_width > 0.0 {
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::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,
});
},
style
.background
.unwrap_or(Background::Color(Color::TRANSPARENT)),
);
}
}

View file

@ -609,7 +609,8 @@ where
};
if let Some(highlight) = highlight {
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds: match axis {
Axis::Horizontal => Rectangle {
x: split_region.x,
@ -622,18 +623,20 @@ where
},
Axis::Vertical => Rectangle {
x: (split_region.x
+ (split_region.width - highlight.width) / 2.0)
+ (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,
});
},
highlight.color,
);
}
}
}

View file

@ -360,13 +360,15 @@ where
self.style_sheet.active()
};
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds,
background: style.background,
border_color: style.border_color,
border_width: style.border_width,
border_radius: style.border_radius,
});
},
style.background,
);
renderer.fill_text(Text {
content: &Renderer::ARROW_DOWN_ICON.to_string(),

View file

@ -116,25 +116,29 @@ where
let style = self.style_sheet.style();
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds: Rectangle { ..bounds },
background: style.background,
border_radius: style.border_radius,
border_width: 0.0,
border_color: Color::TRANSPARENT,
});
},
style.background,
);
if active_progress_width > 0.0 {
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
width: active_progress_width,
..bounds
},
background: style.bar,
border_radius: style.border_radius,
border_width: 0.0,
border_color: Color::TRANSPARENT,
});
},
style.bar,
);
}
}

View file

@ -10,8 +10,8 @@ use crate::text;
use crate::touch;
use crate::widget::{self, Row, Text};
use crate::{
Alignment, Background, Clipboard, Color, Element, Hasher, Layout, Length,
Point, Rectangle, Widget,
Alignment, Clipboard, Color, Element, Hasher, Layout, Length, Point,
Rectangle, Widget,
};
pub use iced_style::radio::{Style, StyleSheet};
@ -243,27 +243,31 @@ where
self.style_sheet.active()
};
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds,
background: style.background,
border_radius: size / 2.0,
border_width: style.border_width,
border_color: style.border_color,
});
},
style.background,
);
if self.is_selected {
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::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,
});
},
style.dot_color,
);
}
}

View file

@ -2,8 +2,7 @@
use crate::layout;
use crate::renderer;
use crate::{
Background, Color, Element, Hasher, Layout, Length, Point, Rectangle, Size,
Widget,
Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
};
use std::hash::Hash;
@ -113,13 +112,15 @@ where
}
};
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds,
background: Background::Color(style.color),
border_radius: style.radius,
border_width: 0.0,
border_color: Color::TRANSPARENT,
});
},
style.color,
);
}
fn hash_layout(&self, state: &mut Hasher) {

View file

@ -526,28 +526,32 @@ where
},
|renderer| {
if is_scrollbar_visible {
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::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,
});
},
style.background.unwrap_or(Background::Color(
Color::TRANSPARENT,
)),
);
}
if is_mouse_over
|| self.state.is_scroller_grabbed()
|| is_scrollbar_visible
{
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::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,
});
},
style.scroller.color,
);
}
},
);

View file

@ -58,6 +58,7 @@ where
T: Copy + From<u8> + std::cmp::PartialOrd,
Message: Clone,
{
/// The default height of a [`Slider`].
pub const DEFAULT_HEIGHT: u16 = 22;
/// Creates a new [`Slider`].
@ -272,31 +273,35 @@ where
let rail_y = bounds.y + (bounds.height / 2.0).round();
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::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,
});
},
style.rail_colors.0,
);
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::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,
});
},
Background::Color(style.rail_colors.1),
);
let (handle_width, handle_height, handle_border_radius) = match style
.handle
@ -325,18 +330,20 @@ where
/ (range_end - range_start)
};
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::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,
});
},
style.handle.color,
);
}
fn mouse_interaction(

View file

@ -163,6 +163,16 @@ where
}
}
/// Draws text using the same logic as the [`Text`] widget.
///
/// Specifically:
///
/// * If no `size` is provided, the default text size of the `Renderer` will be
/// used.
/// * If no `color` is provided, the [`renderer::Style::text_color`] will be
/// used.
/// * The alignment attributes do not affect the position of the bounds of the
/// [`Layout`].
pub fn draw<Renderer>(
renderer: &mut Renderer,
style: &renderer::Style,

View file

@ -20,8 +20,8 @@ use crate::renderer;
use crate::text::{self, Text};
use crate::touch;
use crate::{
Background, Clipboard, Color, Element, Hasher, Layout, Length, Padding,
Point, Rectangle, Size, Vector, Widget,
Clipboard, Color, Element, Hasher, Layout, Length, Padding, Point,
Rectangle, Size, Vector, Widget,
};
use std::u32;
@ -196,13 +196,15 @@ where
self.style_sheet.active()
};
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds,
background: style.background,
border_radius: style.border_radius,
border_width: style.border_width,
border_color: style.border_color,
});
},
style.background,
);
let text = value.to_string();
let size = self.size.unwrap_or(renderer.default_size());
@ -221,20 +223,20 @@ where
);
(
Some(renderer::Quad {
Some((
renderer::Quad {
bounds: Rectangle {
x: text_bounds.x + text_value_width,
y: text_bounds.y,
width: 1.0,
height: text_bounds.height,
},
background: Background::Color(
self.style_sheet.value_color(),
),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
}),
},
self.style_sheet.value_color(),
)),
offset,
)
}
@ -265,20 +267,20 @@ where
let width = right_position - left_position;
(
Some(renderer::Quad {
Some((
renderer::Quad {
bounds: Rectangle {
x: text_bounds.x + left_position,
y: text_bounds.y,
width,
height: text_bounds.height,
},
background: Background::Color(
self.style_sheet.selection_color(),
),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
}),
},
self.style_sheet.selection_color(),
)),
if end == right {
right_offset
} else {
@ -302,8 +304,8 @@ where
);
let render = |renderer: &mut Renderer| {
if let Some(cursor) = cursor {
renderer.fill_rectangle(cursor);
if let Some((cursor, color)) = cursor {
renderer.fill_quad(cursor, color);
}
renderer.fill_text(Text {

View file

@ -258,13 +258,17 @@ where
height: bounds.height - (2.0 * space),
};
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds: toggler_background_bounds,
background: style.background.into(),
border_radius,
border_width: 1.0,
border_color: style.background_border.unwrap_or(style.background),
});
border_color: style
.background_border
.unwrap_or(style.background),
},
style.background,
);
let toggler_foreground_bounds = Rectangle {
x: bounds.x
@ -278,13 +282,17 @@ where
height: bounds.height - (4.0 * space),
};
renderer.fill_rectangle(renderer::Quad {
renderer.fill_quad(
renderer::Quad {
bounds: toggler_foreground_bounds,
background: style.foreground.into(),
border_radius,
border_width: 1.0,
border_color: style.foreground_border.unwrap_or(style.foreground),
});
border_color: style
.foreground_border
.unwrap_or(style.foreground),
},
style.foreground,
);
}
fn hash_layout(&self, state: &mut Hasher) {