Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Development/talagan_ReaImGui Markdown.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
--[[
@description ReaImGui Markdown : A Markdown rendering library for ReaImGui
@version 0.1.16
@version 0.1.17
@author Ben 'Talagan' Babut
@license MIT
@donation https://www.paypal.com/donate/?business=3YEZMY9D6U8NC&no_recurring=1&currency_code=EUR
@links
Forum Thread https://forum.cockos.com/showthread.php?t=301055
@changelog
- [Bug Fix] Crash if widget's first text is set to ""
- [Bug Fix] Better calculation of max boundaries
@metapackage
@provides
[nomain] talagan_ReaImGui Markdown/reaimgui_markdown/**/*.lua
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,22 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
end
end

-- Pointer to the last node encountered
local last_node = nil
local max_x = nil
local max_y = nil
local win_x, win_y = ImGui.GetWindowPos(ctx)
local start_x, start_y = win_x, win_y

-- Helper function to track max dimensions
local function track_max_dimensions()
local imax_x, imax_y = ImGui.GetItemRectMax(ctx)
imax_x = imax_x - start_x
imax_y = imax_y - start_y
if not max_x or imax_x > max_x then max_x = imax_x end
if not max_y or imax_y > max_y then max_y = imax_y end
end

local function draw_word (node, word)
local x, y = ImGui.GetCursorScreenPos(ctx)

Expand All @@ -218,6 +234,9 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
local color = base_txt_color
ImGui.TextColored(ctx, color, word)

-- Track max dimensions after each word
track_max_dimensions()

if in_link then
if ImGui.IsItemHovered(ctx) then
ImGui.SetMouseCursor(ctx, ImGui.MouseCursor_Hand)
Expand Down Expand Up @@ -503,13 +522,6 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
ImGui.SetCursorPosX(ctx, ImGui.GetCursorPosX(ctx) + left_indent(node_style, level))
end

-- Pointer to the last node encountered
local last_node = nil
local max_x = nil
local max_y = nil
local win_x, win_y = ImGui.GetWindowPos(ctx)
local start_x, start_y = win_x, win_y

local function render_node(node, level)
level = level or 1 -- Default to level 1 if not provided
if node.type == "Document" then
Expand Down Expand Up @@ -550,6 +562,9 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
ImGui.PopStyleVar(ctx)
ImGui.EndGroup(ctx)

-- Track after group
track_max_dimensions()

-- Add some vertical padding after
if not (rendered_last and skip_last_whitespace) then
ImGuiVDummy(ctx, nstyle.padding_bottom)
Expand Down Expand Up @@ -580,6 +595,9 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
ImGui.PopStyleVar(ctx)
ImGui.EndGroup(ctx)

-- Track after group
track_max_dimensions()

-- Add some vertical padding after
-- If we're in a blockquote, force symmetry
if not (rendered_last and skip_last_whitespace) then
Expand All @@ -596,6 +614,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
elseif node.type == "Separator" then
ImGuiVDummy(ctx, style.separator.padding_top)
ImGui.Separator(ctx)
track_max_dimensions()

if not (rendered_last and skip_last_whitespace) then
ImGuiVDummy(ctx, style.separator.padding_bottom)
Expand All @@ -609,6 +628,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)

push_style(node)
ImGui.TextColored(ctx, base_txt_color, node.value)
track_max_dimensions()
pop_style()

ImGui.SameLine(ctx)
Expand Down Expand Up @@ -639,6 +659,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
ImGui.Text(ctx, " ")
ImGui.SameLine(ctx)
ImGui.EndGroup(ctx)
track_max_dimensions()
ImGui.SameLine(ctx)

-- Since we used AstToPlainText, last_node detection is affected
Expand Down Expand Up @@ -688,6 +709,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
ImGui.BeginGroup(ctx)
render_children(node.children, level)
ImGui.EndGroup(ctx)
track_max_dimensions()

-- Avoid line return, the next list item will perform new line
if level > 1 then
Expand Down Expand Up @@ -728,6 +750,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
render_node(child, level + 1)
end
ImGui.EndGroup(ctx)
track_max_dimensions()
ImGui.PopStyleVar(ctx)
pop_style()

Expand Down Expand Up @@ -758,6 +781,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
pop_style()
ImGui.PopStyleVar(ctx)
ImGui.EndGroup(ctx)
track_max_dimensions()

local x2, y2 = ImGui.GetCursorScreenPos(ctx)
local draw_list = ImGui.GetWindowDrawList(ctx)
Expand Down Expand Up @@ -792,6 +816,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
pop_style()
ImGui.PopStyleVar(ctx)
ImGui.EndGroup(ctx)
track_max_dimensions()

if not (rendered_last and skip_last_whitespace) then
ImGuiVDummy(ctx, nstyle.padding_bottom)
Expand All @@ -803,6 +828,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)

push_style(node)
ImGui.Text(ctx, "Images are not supported yet.")
track_max_dimensions()
pop_style()

elseif node.type == "Table" then
Expand Down Expand Up @@ -844,6 +870,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
ImGui.EndTable(ctx)
end
ImGui.EndGroup(ctx)
track_max_dimensions()
pop_style()

if not (rendered_last and skip_last_whitespace) then
Expand All @@ -867,6 +894,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
ImGui.PopFont(ctx)
pop_style()
ImGui.EndGroup(ctx)
track_max_dimensions()

if b then
local old_value = node.attributes.checked
Expand All @@ -889,12 +917,6 @@ local function ASTToImgui(ctx, ast, fonts, style, options)
error("Unhandle node type " .. node.type)
end

local imax_x, imax_y = ImGui.GetItemRectMax(ctx)
imax_x = imax_x - start_x
imax_y = imax_y - start_y
if not max_x or imax_x > max_x then max_x = imax_x end
if not max_y or imax_y > max_y then max_y = imax_y end

if (not rendered_last) and (last_node == node) then
rendered_last = true
end
Expand Down