From 7e9ac697825c24cdd8aecb891898413aa4604a74 Mon Sep 17 00:00:00 2001 From: gucio321 <73652197+gucio321@users.noreply.github.com> Date: Mon, 11 Nov 2024 21:39:39 +0100 Subject: [PATCH] markdown: add link callback support --- Markdown.go | 21 ++++++++++++++------- MasterWindow.go | 5 +++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Markdown.go b/Markdown.go index 50837774..6b2d6b29 100644 --- a/Markdown.go +++ b/Markdown.go @@ -62,27 +62,34 @@ func (m *MarkdownWidget) newState() *markdownState { type MarkdownWidget struct { md string id ID - linkCb func(url string) headers [3]immarkdown.MarkdownHeadingFormat } // Markdown creates new markdown widget. func Markdown(md string) *MarkdownWidget { - return &MarkdownWidget{ - md: md, - linkCb: OpenURL, - id: GenAutoID("MarkdownWidget"), + return (&MarkdownWidget{ + md: md, + id: GenAutoID("MarkdownWidget"), headers: [3]immarkdown.MarkdownHeadingFormat{ *immarkdown.NewEmptyMarkdownHeadingFormat(), *immarkdown.NewEmptyMarkdownHeadingFormat(), *immarkdown.NewEmptyMarkdownHeadingFormat(), }, - } + }).OnLink(OpenURL) } // OnLink sets another than default link callback. +// NOTE: due to cimgui-go's limitation https://github.com/AllenDang/cimgui-go?tab=readme-ov-file#callbacks +// we clear MarkdownLinkCallback pool every frame. No further action from you should be required (just feel informed). +// ref (*MasterWindow).beforeRender func (m *MarkdownWidget) OnLink(cb func(url string)) *MarkdownWidget { - m.linkCb = cb + igCb := immarkdown.MarkdownLinkCallback(func(data immarkdown.MarkdownLinkCallbackData) { + link := data.Link()[:data.LinkLength()] + cb(link) + }) + + m.getState().cfg.SetLinkCallback(&igCb) + return m } diff --git a/MasterWindow.go b/MasterWindow.go index 95089b7e..bd43c138 100644 --- a/MasterWindow.go +++ b/MasterWindow.go @@ -10,6 +10,7 @@ import ( "github.com/AllenDang/cimgui-go/backend/glfwbackend" "github.com/AllenDang/cimgui-go/imgui" "github.com/AllenDang/cimgui-go/imguizmo" + "github.com/AllenDang/cimgui-go/immarkdown" "github.com/AllenDang/cimgui-go/imnodes" "github.com/AllenDang/cimgui-go/implot" "golang.org/x/image/colornames" @@ -201,6 +202,10 @@ func (w *MasterWindow) sizeChange(_, _ int) { } func (w *MasterWindow) beforeRender() { + // Clean callbacks + // see https://github.com/AllenDang/cimgui-go?tab=readme-ov-file#callbacks + immarkdown.ClearMarkdownLinkCallbackPool() + Context.FontAtlas.rebuildFontAtlas() // process texture load requests