Skip to content

Commit

Permalink
Align Container and debug borders to pixels.
Browse files Browse the repository at this point in the history
  • Loading branch information
xStrom committed Mar 16, 2020
1 parent 0cb0216 commit 33dacdc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
12 changes: 9 additions & 3 deletions druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::collections::VecDeque;
use log;

use crate::bloom::Bloom;
use crate::kurbo::{Affine, Insets, Point, Rect, Shape, Size};
use crate::kurbo::{Affine, Insets, Rect, Shape, Size};
use crate::piet::RenderContext;
use crate::{
BoxConstraints, Command, Data, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx,
Expand Down Expand Up @@ -268,10 +268,16 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
paint_ctx.z_ops.append(&mut ctx.z_ops);

if env.get(Env::DEBUG_PAINT) {
let rect = Rect::from_origin_size(Point::ORIGIN, ctx.size());
const BORDER_WIDTH: f64 = 1.0;
const BORDER_MIDPOINT: f64 = BORDER_WIDTH / 2.0;
let size = ctx.size();
let rect = Rect::from_origin_size(
(BORDER_MIDPOINT, BORDER_MIDPOINT),
(size.width - BORDER_WIDTH, size.height - BORDER_WIDTH),
);
let id = self.id().to_raw();
let color = env.get_debug_color(id);
ctx.stroke(rect, &color, 1.0);
ctx.stroke(rect, &color, BORDER_WIDTH);
}

self.state.needs_inval = false;
Expand Down
42 changes: 24 additions & 18 deletions druid/src/widget/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,35 @@ impl<T: Data> Widget<T> for Container<T> {
}

fn paint(&mut self, paint_ctx: &mut PaintCtx, data: &T, env: &Env) {
let panel = RoundedRect::from_origin_size(
Point::ORIGIN,
paint_ctx.size().to_vec2(),
self.corner_radius,
);

if let Err(e) = paint_ctx.save() {
log::error!("{}", e);
return;
}

paint_ctx.clip(panel);

if let Some(background) = self.background.as_mut() {
let bg_rect = RoundedRect::from_origin_size(
Point::ORIGIN,
paint_ctx.size().to_vec2(),
self.corner_radius,
);

if let Err(e) = paint_ctx.save() {
log::error!("{}", e);
return;
}

paint_ctx.clip(bg_rect);
background.paint(paint_ctx, data, env);
}

if let Err(e) = paint_ctx.restore() {
log::error!("{}", e);
}
if let Err(e) = paint_ctx.restore() {
log::error!("{}", e);
}
};

if let Some(border) = &self.border {
paint_ctx.stroke(panel, &border.color.resolve(env), border.width.resolve(env));
let border_width = border.width.resolve(env);
let border_midpoint = border_width / 2.0;
let border_rect = RoundedRect::from_origin_size(
Point::new(border_midpoint, border_midpoint),
paint_ctx.size().to_vec2() - (border_width, border_width).into(),
self.corner_radius,
);
paint_ctx.stroke(border_rect, &border.color.resolve(env), border_width);
};

self.inner.paint(paint_ctx, data, env);
Expand Down

0 comments on commit 33dacdc

Please # to comment.