Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove draw_rect() shortcut in ItemRenderer trait #7106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions internal/core/item_rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,24 @@ pub trait RenderBorderRectangle {
fn border_color(self: Pin<&Self>) -> Brush;
}

impl RenderBorderRectangle for Brush {
fn background(self: Pin<&Self>) -> Brush {
self.get_ref().clone()
}

fn border_width(self: Pin<&Self>) -> LogicalLength {
Default::default()
}

fn border_radius(self: Pin<&Self>) -> LogicalBorderRadius {
Default::default()
}

fn border_color(self: Pin<&Self>) -> Brush {
Default::default()
}
}

/// Trait for an item that represents an Image towards the renderer
#[allow(missing_docs)]
pub trait RenderImage {
Expand Down Expand Up @@ -451,12 +469,6 @@ pub trait ItemRenderer {

fn draw_image_direct(&mut self, image: crate::graphics::Image);

/// Fills a rectangle at (0,0) with the given size. This is used for example by the Skia renderer to
/// handle window backgrounds with a brush (gradient).
fn draw_rect(&mut self, _size: LogicalSize, _brush: Brush) {
unimplemented!()
}

/// This is called before it is being rendered (before the draw_* function).
/// Returns
/// - if the item needs to be drawn (false means it is clipped or doesn't need to be drawn)
Expand Down Expand Up @@ -910,10 +922,6 @@ impl<'a, T: ItemRenderer> ItemRenderer for PartialRenderer<'a, T> {
self.actual_renderer.draw_image_direct(image)
}

fn draw_rect(&mut self, size: LogicalSize, brush: Brush) {
self.actual_renderer.draw_rect(size, brush);
}

fn window(&self) -> &crate::window::WindowInner {
self.actual_renderer.window()
}
Expand Down
12 changes: 12 additions & 0 deletions internal/core/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,18 @@ impl WindowInner {
})
}

/// Returns the window item that is the first item in the component.
pub fn window_item_rc(&self) -> Option<ItemRc> {
self.try_component().and_then(|component_rc| {
let window_item_rc = ItemRc::new(component_rc, 0);
if window_item_rc.downcast::<crate::items::WindowItem>().is_some() {
Some(window_item_rc)
} else {
None
}
})
}

/// Sets the size of the window item. This method is typically called in response to receiving a
/// window resize event from the windowing system.
pub(crate) fn set_window_item_geometry(&self, size: crate::lengths::LogicalSize) {
Expand Down
12 changes: 4 additions & 8 deletions internal/renderers/femtovg/itemrenderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,18 @@ impl<'a> GLItemRenderer<'a> {
pub fn global_alpha_transparent(&self) -> bool {
self.state.last().unwrap().global_alpha == 0.0
}
}

/// Draws a `Rectangle` using the `GLItemRenderer`.
pub fn draw_rect(&mut self, size: LogicalSize, brush: Brush) {
impl<'a> ItemRenderer for GLItemRenderer<'a> {
fn draw_rectangle(&mut self, rect: Pin<&items::Rectangle>, _: &ItemRc, size: LogicalSize) {
let geometry = PhysicalRect::from(size * self.scale_factor);
if geometry.is_empty() {
return;
}
if self.global_alpha_transparent() {
return;
}
let brush = rect.background();
// TODO: cache path in item to avoid re-tesselation
let path = rect_to_path(geometry);
let paint = match self.brush_to_paint(brush, &path) {
Expand All @@ -197,12 +199,6 @@ impl<'a> GLItemRenderer<'a> {
.with_anti_alias(false);
self.canvas.borrow_mut().fill_path(&path, &paint);
}
}

impl<'a> ItemRenderer for GLItemRenderer<'a> {
fn draw_rectangle(&mut self, rect: Pin<&items::Rectangle>, _: &ItemRc, size: LogicalSize) {
self.draw_rect(size, rect.background());
}

fn draw_border_rectangle(
&mut self,
Expand Down
10 changes: 8 additions & 2 deletions internal/renderers/femtovg/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,17 @@ impl FemtoVGRenderer {
match window_background_brush {
Some(Brush::SolidColor(..)) | None => {}
Some(brush) => {
item_renderer.draw_rect(
let window_item_rc = window_inner.window_item_rc().unwrap();
let window_item =
window_item_rc.downcast::<i_slint_core::items::WindowItem>().unwrap();
let pinned_brush = std::pin::pin!(brush);
item_renderer.draw_border_rectangle(
pinned_brush,
&window_item_rc,
i_slint_core::lengths::logical_size_from_api(
window.size().to_logical(window_inner.scale_factor()),
),
brush,
&window_item.cached_rendering_data,
);
}
}
Expand Down
10 changes: 8 additions & 2 deletions internal/renderers/skia/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,17 @@ impl SkiaRenderer {
}
None => {}
Some(brush @ _) => {
item_renderer.draw_rect(
let window_item_rc = window_inner.window_item_rc().unwrap();
let window_item =
window_item_rc.downcast::<i_slint_core::items::WindowItem>().unwrap();
let pinned_brush = std::pin::pin!(brush);
item_renderer.draw_border_rectangle(
pinned_brush,
&window_item_rc,
i_slint_core::lengths::logical_size_from_api(
window.size().to_logical(window_inner.scale_factor()),
),
brush,
&window_item.cached_rendering_data,
);
}
}
Expand Down
Loading