@@ -208,6 +208,22 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
208208 end
209209 end
210210
211+ -- Pointer to the last node encountered
212+ local last_node = nil
213+ local max_x = nil
214+ local max_y = nil
215+ local win_x , win_y = ImGui .GetWindowPos (ctx )
216+ local start_x , start_y = win_x , win_y
217+
218+ -- Helper function to track max dimensions
219+ local function track_max_dimensions ()
220+ local imax_x , imax_y = ImGui .GetItemRectMax (ctx )
221+ imax_x = imax_x - start_x
222+ imax_y = imax_y - start_y
223+ if not max_x or imax_x > max_x then max_x = imax_x end
224+ if not max_y or imax_y > max_y then max_y = imax_y end
225+ end
226+
211227 local function draw_word (node , word )
212228 local x , y = ImGui .GetCursorScreenPos (ctx )
213229
@@ -218,6 +234,9 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
218234 local color = base_txt_color
219235 ImGui .TextColored (ctx , color , word )
220236
237+ -- Track max dimensions after each word
238+ track_max_dimensions ()
239+
221240 if in_link then
222241 if ImGui .IsItemHovered (ctx ) then
223242 ImGui .SetMouseCursor (ctx , ImGui .MouseCursor_Hand )
@@ -503,13 +522,6 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
503522 ImGui .SetCursorPosX (ctx , ImGui .GetCursorPosX (ctx ) + left_indent (node_style , level ))
504523 end
505524
506- -- Pointer to the last node encountered
507- local last_node = nil
508- local max_x = nil
509- local max_y = nil
510- local win_x , win_y = ImGui .GetWindowPos (ctx )
511- local start_x , start_y = win_x , win_y
512-
513525 local function render_node (node , level )
514526 level = level or 1 -- Default to level 1 if not provided
515527 if node .type == " Document" then
@@ -550,6 +562,9 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
550562 ImGui .PopStyleVar (ctx )
551563 ImGui .EndGroup (ctx )
552564
565+ -- Track after group
566+ track_max_dimensions ()
567+
553568 -- Add some vertical padding after
554569 if not (rendered_last and skip_last_whitespace ) then
555570 ImGuiVDummy (ctx , nstyle .padding_bottom )
@@ -580,6 +595,9 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
580595 ImGui .PopStyleVar (ctx )
581596 ImGui .EndGroup (ctx )
582597
598+ -- Track after group
599+ track_max_dimensions ()
600+
583601 -- Add some vertical padding after
584602 -- If we're in a blockquote, force symmetry
585603 if not (rendered_last and skip_last_whitespace ) then
@@ -596,6 +614,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
596614 elseif node .type == " Separator" then
597615 ImGuiVDummy (ctx , style .separator .padding_top )
598616 ImGui .Separator (ctx )
617+ track_max_dimensions ()
599618
600619 if not (rendered_last and skip_last_whitespace ) then
601620 ImGuiVDummy (ctx , style .separator .padding_bottom )
@@ -609,6 +628,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
609628
610629 push_style (node )
611630 ImGui .TextColored (ctx , base_txt_color , node .value )
631+ track_max_dimensions ()
612632 pop_style ()
613633
614634 ImGui .SameLine (ctx )
@@ -639,6 +659,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
639659 ImGui .Text (ctx , " " )
640660 ImGui .SameLine (ctx )
641661 ImGui .EndGroup (ctx )
662+ track_max_dimensions ()
642663 ImGui .SameLine (ctx )
643664
644665 -- Since we used AstToPlainText, last_node detection is affected
@@ -688,6 +709,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
688709 ImGui .BeginGroup (ctx )
689710 render_children (node .children , level )
690711 ImGui .EndGroup (ctx )
712+ track_max_dimensions ()
691713
692714 -- Avoid line return, the next list item will perform new line
693715 if level > 1 then
@@ -728,6 +750,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
728750 render_node (child , level + 1 )
729751 end
730752 ImGui .EndGroup (ctx )
753+ track_max_dimensions ()
731754 ImGui .PopStyleVar (ctx )
732755 pop_style ()
733756
@@ -758,6 +781,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
758781 pop_style ()
759782 ImGui .PopStyleVar (ctx )
760783 ImGui .EndGroup (ctx )
784+ track_max_dimensions ()
761785
762786 local x2 , y2 = ImGui .GetCursorScreenPos (ctx )
763787 local draw_list = ImGui .GetWindowDrawList (ctx )
@@ -792,6 +816,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
792816 pop_style ()
793817 ImGui .PopStyleVar (ctx )
794818 ImGui .EndGroup (ctx )
819+ track_max_dimensions ()
795820
796821 if not (rendered_last and skip_last_whitespace ) then
797822 ImGuiVDummy (ctx , nstyle .padding_bottom )
@@ -803,6 +828,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
803828
804829 push_style (node )
805830 ImGui .Text (ctx , " Images are not supported yet." )
831+ track_max_dimensions ()
806832 pop_style ()
807833
808834 elseif node .type == " Table" then
@@ -844,6 +870,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
844870 ImGui .EndTable (ctx )
845871 end
846872 ImGui .EndGroup (ctx )
873+ track_max_dimensions ()
847874 pop_style ()
848875
849876 if not (rendered_last and skip_last_whitespace ) then
@@ -867,6 +894,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
867894 ImGui .PopFont (ctx )
868895 pop_style ()
869896 ImGui .EndGroup (ctx )
897+ track_max_dimensions ()
870898
871899 if b then
872900 local old_value = node .attributes .checked
@@ -889,12 +917,6 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
889917 error (" Unhandle node type " .. node .type )
890918 end
891919
892- local imax_x , imax_y = ImGui .GetItemRectMax (ctx )
893- imax_x = imax_x - start_x
894- imax_y = imax_y - start_y
895- if not max_x or imax_x > max_x then max_x = imax_x end
896- if not max_y or imax_y > max_y then max_y = imax_y end
897-
898920 if (not rendered_last ) and (last_node == node ) then
899921 rendered_last = true
900922 end
0 commit comments