more dpi fixes:

- margin and padding
- Widget::draw_batch
- trip timeline
- custom buttons

and finally enable automatically setting DPI based on the monitor!!!
[rebuild]
This commit is contained in:
Dustin Carlino 2020-05-17 17:02:33 -07:00
parent 2f1b274276
commit fa1273bd34
5 changed files with 65 additions and 13 deletions

View File

@ -189,6 +189,14 @@ impl GeomBatch {
self.fancy_push(color, poly.translate(dx, dy));
}
}
/// Scales the batch by some factor.
pub fn scale(mut self, factor: f64) -> GeomBatch {
for (_, poly) in &mut self.list {
*poly = poly.scale(factor);
}
self
}
}
pub enum RewriteColor {

View File

@ -223,7 +223,12 @@ impl Widget {
// TODO These are literally just convenient APIs to avoid importing JustDraw. Do we want this
// or not?
pub fn draw_batch(ctx: &EventCtx, batch: GeomBatch) -> Widget {
JustDraw::wrap(ctx, batch)
let scale = ctx.get_scale_factor();
if scale == 1.0 {
JustDraw::wrap(ctx, batch)
} else {
JustDraw::wrap(ctx, batch.scale(scale))
}
}
pub fn draw_svg<I: Into<String>>(ctx: &EventCtx, filename: I) -> Widget {
JustDraw::svg(ctx, filename.into())
@ -287,7 +292,13 @@ impl Widget {
}
// Populate a flattened list of Nodes, matching the traversal order
fn get_flexbox(&self, parent: Node, stretch: &mut Stretch, nodes: &mut Vec<Node>) {
fn get_flexbox(
&self,
parent: Node,
scale_factor: f32,
stretch: &mut Stretch,
nodes: &mut Vec<Node>,
) {
if let Some(container) = self.widget.downcast_ref::<Container>() {
let mut style = self.layout.style.clone();
style.flex_direction = if container.is_row {
@ -298,7 +309,7 @@ impl Widget {
let node = stretch.new_node(style, Vec::new()).unwrap();
nodes.push(node);
for widget in &container.members {
widget.get_flexbox(node, stretch, nodes);
widget.get_flexbox(node, scale_factor, stretch, nodes);
}
stretch.add_child(parent, node).unwrap();
return;
@ -308,6 +319,32 @@ impl Widget {
width: Dimension::Points(self.widget.get_dims().width as f32),
height: Dimension::Points(self.widget.get_dims().height as f32),
};
if scale_factor != 1.0 {
if let Dimension::Points(ref mut px) = style.padding.start {
*px *= scale_factor;
}
if let Dimension::Points(ref mut px) = style.padding.end {
*px *= scale_factor;
}
if let Dimension::Points(ref mut px) = style.padding.top {
*px *= scale_factor;
}
if let Dimension::Points(ref mut px) = style.padding.bottom {
*px *= scale_factor;
}
if let Dimension::Points(ref mut px) = style.margin.start {
*px *= scale_factor;
}
if let Dimension::Points(ref mut px) = style.margin.end {
*px *= scale_factor;
}
if let Dimension::Points(ref mut px) = style.margin.top {
*px *= scale_factor;
}
if let Dimension::Points(ref mut px) = style.margin.bottom {
*px *= scale_factor;
}
}
let node = stretch.new_node(style, Vec::new()).unwrap();
stretch.add_child(parent, node).unwrap();
nodes.push(node);
@ -509,7 +546,12 @@ impl Composite {
.unwrap();
let mut nodes = vec![];
self.top_level.get_flexbox(root, &mut stretch, &mut nodes);
self.top_level.get_flexbox(
root,
ctx.get_scale_factor() as f32,
&mut stretch,
&mut nodes,
);
nodes.reverse();
// TODO Express more simply. Constraining this seems useless.

View File

@ -164,7 +164,7 @@ pub struct Settings {
profiling_enabled: bool,
default_font_size: usize,
dump_raw_events: bool,
scale_factor: f64,
scale_factor: Option<f64>,
window_icon: Option<String>,
}
@ -176,7 +176,7 @@ impl Settings {
profiling_enabled: false,
default_font_size: text::DEFAULT_FONT_SIZE,
dump_raw_events: false,
scale_factor: 1.0,
scale_factor: None,
window_icon: None,
}
}
@ -196,7 +196,7 @@ impl Settings {
}
pub fn scale_factor(&mut self, scale_factor: f64) {
self.scale_factor = scale_factor;
self.scale_factor = Some(scale_factor);
}
pub fn window_icon(&mut self, path: &str) {
@ -224,7 +224,9 @@ pub fn run<G: 'static + GUI, F: FnOnce(&mut EventCtx) -> G>(settings: Settings,
assets: Assets::new(
settings.default_font_size,
settings.font_dir,
settings.scale_factor,
settings
.scale_factor
.unwrap_or_else(|| prerender_innards.monitor_scale_factor()),
),
num_uploads: Cell::new(0),
inner: prerender_innards,

View File

@ -402,12 +402,12 @@ impl BtnBuilder {
}
BtnBuilder::Custom(normal, hovered, hitbox, maybe_t) => Button::new(
ctx,
normal,
hovered,
normal.scale(ctx.get_scale_factor()),
hovered.scale(ctx.get_scale_factor()),
key,
&action_tooltip.into(),
maybe_t,
hitbox,
hitbox.scale(ctx.get_scale_factor()),
),
}
}

View File

@ -423,7 +423,7 @@ fn make_timeline(
normal.add_svg(
ctx.prerender,
"../data/system/assets/timeline/current_pos.svg",
Pt2D::new(p * phase_width, 7.5),
Pt2D::new(p * phase_width, 7.5 * ctx.get_scale_factor()),
1.0,
Angle::ZERO,
RewriteColor::NoOp,
@ -450,7 +450,7 @@ fn make_timeline(
TripPhaseType::Remote => "../data/system/assets/timeline/delayed_start.svg",
},
// TODO Hardcoded layouting...
Pt2D::new(0.5 * phase_width, -20.0),
Pt2D::new(0.5 * phase_width, -20.0 * ctx.get_scale_factor()),
1.0,
Angle::ZERO,
RewriteColor::NoOp,