From efbea105322eb3446d5efb54b9e414a1e76cba1e Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Mon, 23 Jun 2025 10:12:57 +0200 Subject: [PATCH] Shrink handwriting when not focused [wip] --- src/handwriting/mod.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/handwriting/mod.rs b/src/handwriting/mod.rs index 62fcaa8..142a753 100644 --- a/src/handwriting/mod.rs +++ b/src/handwriting/mod.rs @@ -78,6 +78,9 @@ struct Ephemeral { canvas_rasterizer: CanvasRasterizer, + /// Whether the handwriting element is being interacted with. + is_focused: bool, + /// The stroke that is currently being drawed. current_stroke: Vec, @@ -128,6 +131,7 @@ impl Default for Ephemeral { Self { id: random_id(), canvas_rasterizer: Default::default(), + is_focused: false, current_stroke: Default::default(), tessellator: None, mesh: Default::default(), @@ -214,9 +218,16 @@ impl Handwriting { // Was the user in the process of drawing a stroke last frame? let was_drawing = !self.e.current_stroke.is_empty(); - // Is the user in the process of drawing a stroke now? + // Is the user in the process of drawing a stroke now?; let is_drawing = response.interact_pointer_pos().is_some(); + if is_drawing { + self.e.is_focused = true; + } else if ui.ctx().input(|i| i.pointer.any_down()) { + // if the user starts interactive with something else, unfocus the handwriting area. + self.e.is_focused = false; + } + if !is_drawing { if was_drawing { // commit current line @@ -224,12 +235,18 @@ impl Handwriting { response.mark_changed(); } + let padding = if self.e.is_focused { + HANDWRITING_BOTTOM_PADDING + } else { + 0.0 + }; + // recalculate how tall the widget should be let lines_max_y = self .strokes .iter() .flatten() - .map(|p| p.y + HANDWRITING_BOTTOM_PADDING) + .map(|p| p.y + padding) .fold(HANDWRITING_MIN_HEIGHT, |max, y| max.max(y)); // Change the height of the handwriting item.