Misc code cleanup

This commit is contained in:
Emil Ernerfeldt 2020-12-16 21:59:33 +01:00
parent 56502405f5
commit e4afba3bb8
3 changed files with 98 additions and 87 deletions

View file

@ -63,18 +63,15 @@ impl State {
add_contents: impl FnOnce(&mut Ui) -> R, add_contents: impl FnOnce(&mut Ui) -> R,
) -> Option<(R, Response)> { ) -> Option<(R, Response)> {
let openness = self.openness(ui.ctx(), id); let openness = self.openness(ui.ctx(), id);
let animate = 0.0 < openness && openness < 1.0; if openness <= 0.0 {
if animate { None
} else if openness < 1.0 {
Some(ui.wrap(|child_ui| { Some(ui.wrap(|child_ui| {
let max_height = if self.open { let max_height = if self.open && self.open_height.is_none() {
if let Some(full_height) = self.open_height {
remap_clamp(openness, 0.0..=1.0, 0.0..=full_height)
} else {
// First frame of expansion. // First frame of expansion.
// We don't know full height yet, but we will next frame. // We don't know full height yet, but we will next frame.
// Just use a placeholder value that shows some movement: // Just use a placeholder value that shows some movement:
10.0 10.0
}
} else { } else {
let full_height = self.open_height.unwrap_or_default(); let full_height = self.open_height.unwrap_or_default();
remap_clamp(openness, 0.0..=1.0, 0.0..=full_height) remap_clamp(openness, 0.0..=1.0, 0.0..=full_height)
@ -84,23 +81,21 @@ impl State {
clip_rect.max.y = clip_rect.max.y.min(child_ui.max_rect().top() + max_height); clip_rect.max.y = clip_rect.max.y.min(child_ui.max_rect().top() + max_height);
child_ui.set_clip_rect(clip_rect); child_ui.set_clip_rect(clip_rect);
let r = add_contents(child_ui); let ret = add_contents(child_ui);
self.open_height = Some(child_ui.min_size().y);
// Pretend children took up less space:
let mut min_rect = child_ui.min_rect(); let mut min_rect = child_ui.min_rect();
min_rect.max.y = min_rect.max.y.min(min_rect.top() + max_height); self.open_height = Some(min_rect.height());
// Pretend children took up at most `max_height` space:
min_rect.max.y = min_rect.max.y.at_most(min_rect.top() + max_height);
child_ui.force_set_min_rect(min_rect); child_ui.force_set_min_rect(min_rect);
r ret
})) }))
} else if self.open || ui.memory().all_collpasing_are_open { } else {
let (ret, response) = ui.wrap(add_contents); let (ret, response) = ui.wrap(add_contents);
let full_size = response.rect.size(); let full_size = response.rect.size();
self.open_height = Some(full_size.y); self.open_height = Some(full_size.y);
Some((ret, response)) Some((ret, response))
} else {
None
} }
} }
} }

View file

@ -25,7 +25,7 @@ pub struct Window<'open> {
} }
impl<'open> Window<'open> { impl<'open> Window<'open> {
// TODO: Into<Label> /// The winodw title must be unique, and should not change.
pub fn new(title: impl Into<String>) -> Self { pub fn new(title: impl Into<String>) -> Self {
let title = title.into(); let title = title.into();
let area = Area::new(&title); let area = Area::new(&title);
@ -40,7 +40,7 @@ impl<'open> Window<'open> {
resize: Resize::default() resize: Resize::default()
.with_stroke(false) .with_stroke(false)
.min_size([96.0, 32.0]) .min_size([96.0, 32.0])
.default_size([420.0, 420.0]), .default_size([420.0, 420.0]), // Default inner size of a winodw
scroll: None, scroll: None,
collapsible: true, collapsible: true,
} }
@ -219,10 +219,6 @@ impl<'open> Window<'open> {
// First interact (move etc) to avoid frame delay: // First interact (move etc) to avoid frame delay:
let last_frame_outer_rect = area.state().rect(); let last_frame_outer_rect = area.state().rect();
let interaction = if possible.movable || possible.resizable { let interaction = if possible.movable || possible.resizable {
let title_bar_height = title_label.font_height(ctx.fonts(), &ctx.style())
+ 1.0 * ctx.style().spacing.item_spacing.y; // this could be better
let margins = 2.0 * frame.margin + vec2(0.0, title_bar_height);
window_interaction( window_interaction(
ctx, ctx,
possible, possible,
@ -231,6 +227,11 @@ impl<'open> Window<'open> {
last_frame_outer_rect, last_frame_outer_rect,
) )
.and_then(|window_interaction| { .and_then(|window_interaction| {
// Calculate roughly how much larger the window size is compared to the inner rect
let title_bar_height = title_label.font_height(ctx.fonts(), &ctx.style())
+ 1.0 * ctx.style().spacing.item_spacing.y; // this could be better
let margins = 2.0 * frame.margin + vec2(0.0, title_bar_height);
interact( interact(
window_interaction, window_interaction,
ctx, ctx,
@ -276,9 +277,9 @@ impl<'open> Window<'open> {
ui.allocate_space(ui.style().spacing.item_spacing); ui.allocate_space(ui.style().spacing.item_spacing);
if let Some(scroll) = scroll { if let Some(scroll) = scroll {
scroll.show(ui, add_contents) scroll.show(ui, add_contents);
} else { } else {
add_contents(ui) add_contents(ui);
} }
}) })
}) })
@ -685,6 +686,7 @@ impl TitleBar {
let left = outer_rect.left(); let left = outer_rect.left();
let right = outer_rect.right(); let right = outer_rect.right();
let y = content_response.rect.top() + ui.style().spacing.item_spacing.y * 0.5; let y = content_response.rect.top() + ui.style().spacing.item_spacing.y * 0.5;
// let y = lerp(self.rect.bottom()..=content_response.rect.top(), 0.5);
ui.painter().line_segment( ui.painter().line_segment(
[pos2(left, y), pos2(right, y)], [pos2(left, y), pos2(right, y)],
ui.style().visuals.widgets.inactive.bg_stroke, ui.style().visuals.widgets.inactive.bg_stroke,

View file

@ -673,10 +673,10 @@ fn mul_color(color: Srgba, factor: f32) -> Srgba {
/// * `scratchpad_path`: if you plan to run `tessellate_paint_command` /// * `scratchpad_path`: if you plan to run `tessellate_paint_command`
/// many times, pass it a reference to the same `Path` to avoid excessive allocations. /// many times, pass it a reference to the same `Path` to avoid excessive allocations.
fn tessellate_paint_command( fn tessellate_paint_command(
clip_rect: Rect,
command: PaintCmd,
options: TesselationOptions, options: TesselationOptions,
fonts: &Fonts, fonts: &Fonts,
clip_rect: Rect,
command: PaintCmd,
out: &mut Triangles, out: &mut Triangles,
scratchpad_points: &mut Vec<Pos2>, scratchpad_points: &mut Vec<Pos2>,
scratchpad_path: &mut Path, scratchpad_path: &mut Path,
@ -773,6 +773,24 @@ fn tessellate_paint_command(
text_style, text_style,
color, color,
} => { } => {
tesselate_text(
options, fonts, clip_rect, pos, &galley, text_style, color, out,
);
}
}
}
#[allow(clippy::too_many_arguments)]
fn tesselate_text(
options: TesselationOptions,
fonts: &Fonts,
clip_rect: Rect,
pos: Pos2,
galley: &super::Galley,
text_style: super::TextStyle,
color: Srgba,
out: &mut Triangles,
) {
if color == TRANSPARENT { if color == TRANSPARENT {
return; return;
} }
@ -794,8 +812,7 @@ fn tessellate_paint_command(
for line in &galley.rows { for line in &galley.rows {
let line_min_y = pos.y + line.y_min + text_offset.x; let line_min_y = pos.y + line.y_min + text_offset.x;
let line_max_y = line_min_y + font.row_height(); let line_max_y = line_min_y + font.row_height();
let is_line_visible = let is_line_visible = line_max_y >= clip_rect.min.y && line_min_y <= clip_rect.max.y;
line_max_y >= clip_rect.min.y && line_min_y <= clip_rect.max.y;
for x_offset in line.x_offsets.iter().take(line.x_offsets.len() - 1) { for x_offset in line.x_offsets.iter().take(line.x_offsets.len() - 1) {
let c = chars.next().unwrap(); let c = chars.next().unwrap();
@ -807,8 +824,7 @@ fn tessellate_paint_command(
} }
if let Some(glyph) = font.uv_rect(c) { if let Some(glyph) = font.uv_rect(c) {
let mut left_top = let mut left_top = pos + glyph.offset + vec2(*x_offset, line.y_min) + text_offset;
pos + glyph.offset + vec2(*x_offset, line.y_min) + text_offset;
left_top.x = font.round_to_pixel(left_top.x); // Pixel-perfection. left_top.x = font.round_to_pixel(left_top.x); // Pixel-perfection.
left_top.y = font.round_to_pixel(left_top.y); // Pixel-perfection. left_top.y = font.round_to_pixel(left_top.y); // Pixel-perfection.
@ -826,8 +842,6 @@ fn tessellate_paint_command(
} }
} }
assert_eq!(chars.next(), None); assert_eq!(chars.next(), None);
}
}
} }
/// Turns `PaintCmd`:s into sets of triangles. /// Turns `PaintCmd`:s into sets of triangles.
@ -862,10 +876,10 @@ pub fn tessellate_paint_commands(
let out = &mut jobs.last_mut().unwrap().1; let out = &mut jobs.last_mut().unwrap().1;
tessellate_paint_command( tessellate_paint_command(
clip_rect,
cmd,
options, options,
fonts, fonts,
clip_rect,
cmd,
out, out,
&mut scratchpad_points, &mut scratchpad_points,
&mut scratchpad_path, &mut scratchpad_path,
@ -875,6 +889,8 @@ pub fn tessellate_paint_commands(
if options.debug_paint_clip_rects { if options.debug_paint_clip_rects {
for (clip_rect, triangles) in &mut jobs { for (clip_rect, triangles) in &mut jobs {
tessellate_paint_command( tessellate_paint_command(
options,
fonts,
Rect::everything(), Rect::everything(),
PaintCmd::Rect { PaintCmd::Rect {
rect: *clip_rect, rect: *clip_rect,
@ -882,8 +898,6 @@ pub fn tessellate_paint_commands(
fill: Default::default(), fill: Default::default(),
stroke: Stroke::new(2.0, srgba(150, 255, 150, 255)), stroke: Stroke::new(2.0, srgba(150, 255, 150, 255)),
}, },
options,
fonts,
triangles, triangles,
&mut scratchpad_points, &mut scratchpad_points,
&mut scratchpad_path, &mut scratchpad_path,