Skip to content

Commit

Permalink
markdown: add link callback support
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Nov 11, 2024
1 parent 4c38b33 commit 7e9ac69
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
21 changes: 14 additions & 7 deletions Markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 84 in Markdown.go

View workflow job for this annotation

GitHub Actions / Lint

Comment should end in a period (godot)
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
}

Expand Down
5 changes: 5 additions & 0 deletions MasterWindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7e9ac69

Please sign in to comment.