From e93f07fbbb2603e596316ead496d3754e42c4730 Mon Sep 17 00:00:00 2001 From: Ben Arthur Date: Tue, 1 Oct 2024 09:53:08 -0400 Subject: [PATCH] refactor per jkrumbiegel's review --- src/makielayout/blocks/textbox.jl | 50 ++++++++++++++----------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/makielayout/blocks/textbox.jl b/src/makielayout/blocks/textbox.jl index fbb93e846b9..fc9f65d0c40 100644 --- a/src/makielayout/blocks/textbox.jl +++ b/src/makielayout/blocks/textbox.jl @@ -90,32 +90,36 @@ function initialize_block!(tbox::Textbox) return end - points = if 0 < ci < length(bbs) + if 0 < ci < length(bbs) [leftline(bbs[ci+1])...] elseif ci == 0 [leftline(bbs[1])...] else [leftline(bbs[ci])...] .+ Point2f(hadvances[ci], 0) end + end - textpos = textplot.converted[1][][1] - offset = if points[1][1] > tbox.layoutobservables.computedbbox[].widths[1] - points[1][1] - tbox.layoutobservables.computedbbox[].widths[1] - elseif points[1][1] < 0 - points[1][1] - else + cursor = linesegments!(scene, cursorpoints, color = tbox.cursorcolor, linewidth = 1, inspectable = false) + + on(cursorpoints) do cpts + typeof(tbox.width[]) <: Number || return + + # translate scene to keep cursor within box + rel_cursor_pos = cpts[1][1] + scene.transformation.translation[][1] + offset = if rel_cursor_pos <= 0 + -rel_cursor_pos + elseif rel_cursor_pos < tbox.width[] 0 + else + tbox.width[] - rel_cursor_pos end - if offset != 0 - textplot.converted[1][] = [Point3f(textpos[1]-offset, textpos[2:3]...)] - points = [Point2f(p[1]-offset, p[2]) for p in points] - end + translate!(Accum, scene, offset, 0, 0) - points + # don't let right side of box be empty if length of text exceeds box width + offset = tbox.width[] - right(displayed_charbbs[][end]) + scene.transformation.translation[][1] < offset < 0 && translate!(scene, offset, 0, 0) end - cursor = linesegments!(scene, cursorpoints, color = tbox.cursorcolor, linewidth = 1, inspectable = false) - tbox.cursoranimtask = nothing on(topscene, t.layoutobservables.reporteddimensions) do dims @@ -139,7 +143,11 @@ function initialize_block!(tbox::Textbox) return Consume(true) end - pos = state.data + if typeof(tbox.width[]) <: Number + pos = state.data .- scene.transformation.translation[][1:2] + else + pos = state.data + end closest_charindex = argmin( [sum((pos .- center(bb)).^2) for bb in displayed_charbbs[]] ) @@ -176,12 +184,8 @@ function initialize_block!(tbox::Textbox) empty!(displayed_chars[]) index = 1 end - textplot = t.blockscene.plots[1] - oldval = textplot.converted[1][][1] newchars = [displayed_chars[][1:index-1]; c; displayed_chars[][index:end]] tbox.displayed_string[] = join(newchars) - offset = displayed_charbbs[][index].widths[1] / 2 - textplot.converted[1][] = [Point3f(oldval[1]+offset, oldval[2:3]...)] cursorindex[] = index end @@ -190,7 +194,6 @@ function initialize_block!(tbox::Textbox) end function removechar!(index) - index==0 && return newchars = [displayed_chars[][1:index-1]; displayed_chars[][index+1:end]] if isempty(newchars) @@ -201,14 +204,7 @@ function initialize_block!(tbox::Textbox) cursorindex[] = max(0, cursorindex[] - 1) end - textplot = t.blockscene.plots[1] - oldval = textplot.converted[1][][1] - offset = displayed_charbbs[][index].widths[1] / 2 - if displayed_charbbs[][1].origin[1] < 0 - offset *= -1 - end tbox.displayed_string[] = join(newchars) - textplot.converted[1][] = [Point3f(oldval[1]-offset, oldval[2:3]...)] end on(topscene, events(scene).unicode_input; priority=60) do char