Skip to content

Commit

Permalink
reenable markdown widget
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Sep 25, 2023
1 parent 65bce2a commit 34755c9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 91 deletions.
189 changes: 101 additions & 88 deletions Markdown.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package giu

import (
imgui "github.com/AllenDang/cimgui-go"
"github.com/faiface/mainthread"
"image"
"image/color"
"net/http"
"strings"
"time"
)

// MarkdownWidget implements DearImGui markdown extension
// https://github.com/juliettef/imgui_markdown
// It is like LabelWidget but with md formatting.
// TODO: re-implement this.
type MarkdownWidget struct {
md *string
linkCb func(url string)
// headers []imgui.MarkdownHeaderData
md *string
linkCb func(url string)
headers []imgui.MarkdownHeadingFormat
}

// Markdown creates new markdown widget.
Expand All @@ -28,98 +37,102 @@ func (m *MarkdownWidget) OnLink(cb func(url string)) *MarkdownWidget {
// Header sets header formatting
// NOTE: level (counting from 0!) is header level. (for instance, header `# H1` will have level 0).
func (m *MarkdownWidget) Header(level int, font *FontInfo, separator bool) *MarkdownWidget {
// ensure if header data are at least as long as level
// if m.headers == nil {
// m.headers = make([]imgui.MarkdownHeaderData, level)
//}
//ensure if header data are at least as long as level
if m.headers == nil {
m.headers = make([]imgui.MarkdownHeadingFormat, level)
}

// if level <= len(m.headers) {
// m.headers = append(m.headers, make([]imgui.MarkdownHeaderData, len(m.headers)-level+1)...)
//}
if level <= len(m.headers) {
m.headers = append(m.headers, make([]imgui.MarkdownHeadingFormat, len(m.headers)-level+1)...)
}

// if font != nil {
// if f, ok := Context.FontAtlas.extraFontMap[font.String()]; ok {
// m.headers[level].Font = *f
// }
//}
if font != nil {
if f, ok := Context.FontAtlas.extraFontMap[font.String()]; ok {
m.headers[level].FieldFont = f
}
}

// m.headers[level].HasSeparator = separator
m.headers[level].FieldSeparator = separator

return m
}

// Build implements Widget interface.
func (m *MarkdownWidget) Build() {
// imgui.Markdown(Context.FontAtlas.RegisterStringPointer(m.md), m.linkCb, loadImage, m.headers)
cfg := imgui.MarkdownConfig{}
imgui.Markdown(
*Context.FontAtlas.RegisterStringPointer(m.md),
uint64(len(*m.md)),
cfg, // TODO: implement callbacks
)
}

//func loadImage(path string) imgui.MarkdownImageData {
// var img *image.RGBA
//
// var err error
//
// switch {
// case strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://"):
// Load image from url
//client := &http.Client{Timeout: 5 * time.Second}
//resp, respErr := client.Get(path)
//
//if respErr != nil {
// return imgui.MarkdownImageData{}
//}
//
//defer func() {
// closeErr := resp.Body.Close()
// Assert((closeErr == nil), "MarkdownWidget", "loadImage", "Could not close http request!")
//}()
//
//rgba, _, imgErr := image.Decode(resp.Body)
//if imgErr != nil {
// return imgui.MarkdownImageData{}
//}
//
//img = ImageToRgba(rgba)
//default:
// img, err = LoadImage(path)
// if err != nil {
// return imgui.MarkdownImageData{}
// }
//}
//
//size := img.Bounds()
//
//nolint:gocritic // TODO/BUG: figure out, why it doesn't work as expected and consider
//if current workaround is save
///*
// tex := &Texture{}
// NewTextureFromRgba(img, func(t *Texture) {
// fmt.Println("creating texture")
// tex.id = t.id
// })
//*/
//
//var id imgui.TextureID
//
//mainthread.Call(func() {
// var err error
// id, err = Context.renderer.LoadImage(img)
// if err != nil {
// return
// }
//})
//
//return imgui.MarkdownImageData{
// TextureID: &id,
// Scale: true,
// Size: imgui.Vec2{
// X: float32(size.Dx()),
// Y: float32(size.Dy()),
// },
// UseLinkCallback: true,
// default values
//Uv0: ToVec2(image.Point{0, 0}),
//Uv1: ToVec2(image.Point{1, 1}),
//TintColor: ToVec4Color(color.RGBA{255, 255, 255, 255}),
//BorderColor: ToVec4Color(color.RGBA{0, 0, 0, 0}),
//}
//}
func loadImage(path string) imgui.MarkdownImageData {
var img *image.RGBA

var err error

switch {
case strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://"):
//Load image from url
client := &http.Client{Timeout: 5 * time.Second}
resp, respErr := client.Get(path)

if respErr != nil {
return imgui.MarkdownImageData{}
}

defer func() {
closeErr := resp.Body.Close()
Assert((closeErr == nil), "MarkdownWidget", "loadImage", "Could not close http request!")
}()

rgba, _, imgErr := image.Decode(resp.Body)
if imgErr != nil {
return imgui.MarkdownImageData{}
}

img = ImageToRgba(rgba)
default:
img, err = LoadImage(path)
if err != nil {
return imgui.MarkdownImageData{}
}
}

size := img.Bounds()

//nolint:gocritic // TODO/BUG: figure out, why it doesn't work as expected and consider
//if current workaround is save
/*
tex := &Texture{}
NewTextureFromRgba(img, func(t *Texture) {
fmt.Println("creating texture")
tex.id = t.id
})
*/

var id imgui.TextureID

mainthread.Call(func() {
id = Context.backend.CreateTextureRgba(img, img.Bounds().Dx(), img.Bounds().Dy())
if id == nil {
return
}
})

return imgui.MarkdownImageData{
FieldUser_texture_id: id,
FieldSize: imgui.Vec2{
X: float32(size.Dx()),
Y: float32(size.Dy()),
},
FieldUseLinkCallback: true,
//default values
FieldUv0: ToVec2(image.Point{0, 0}),
FieldUv1: ToVec2(image.Point{1, 1}),
FieldTint_col: ToVec4Color(color.RGBA{255, 255, 255, 255}),
FieldBorder_col: ToVec4Color(color.RGBA{0, 0, 0, 0}),
FieldIsValid: true,
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/AllenDang/giu
go 1.21

require (
github.com/AllenDang/cimgui-go v0.0.0-20230918114712-675654f053d9
github.com/AllenDang/cimgui-go v0.0.0-20230925201818-e415b694ce3c
github.com/AllenDang/go-findfont v0.0.0-20200702051237-9f180485aeb8
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3
github.com/mazznoer/csscolorparser v0.1.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/AllenDang/cimgui-go v0.0.0-20230918114712-675654f053d9 h1:tITzow7xji/fjyoGZcji6+XazqAtTUiO5T5+YJBbqh8=
github.com/AllenDang/cimgui-go v0.0.0-20230918114712-675654f053d9/go.mod h1:UH0IZCbsJKA1hAVQQU6AIDfamqTk0/d3VucaN8lp1Gs=
github.com/AllenDang/cimgui-go v0.0.0-20230925201818-e415b694ce3c h1:kzTPxbg6eyFb59tpX9mGqGCVUgy5Lywkp2Vz3apC/yE=
github.com/AllenDang/cimgui-go v0.0.0-20230925201818-e415b694ce3c/go.mod h1:UH0IZCbsJKA1hAVQQU6AIDfamqTk0/d3VucaN8lp1Gs=
github.com/AllenDang/go-findfont v0.0.0-20200702051237-9f180485aeb8 h1:dKZMqib/yUDoCFigmz2agG8geZ/e3iRq304/KJXqKyw=
github.com/AllenDang/go-findfont v0.0.0-20200702051237-9f180485aeb8/go.mod h1:b4uuDd0s6KRIPa84cEEchdQ9ICh7K0OryZHbSzMca9k=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down

0 comments on commit 34755c9

Please sign in to comment.