From ea95ea64ce33f203bd6e049fcbbee746b05bcd49 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Mon, 30 Sep 2024 19:04:37 +0200 Subject: [PATCH 01/12] linter: do not skip examples directory --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index bd44ca3e..46e183ff 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -81,6 +81,7 @@ run: timeout: 5m issues: + exclude-dirs-use-default: false skip-dirs: - .github - build From 6ec7af539441386ef6af29008dfa9a89c96cb2d3 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Mon, 30 Sep 2024 19:05:20 +0200 Subject: [PATCH 02/12] run golangci-lint run --fix ./... --- examples/dragdrop/dragdrop.go | 1 + examples/imguidemo/imguidemo.go | 1 + examples/transparent/transparent.go | 1 + 3 files changed, 3 insertions(+) diff --git a/examples/dragdrop/dragdrop.go b/examples/dragdrop/dragdrop.go index c8922fac..4b7bf762 100644 --- a/examples/dragdrop/dragdrop.go +++ b/examples/dragdrop/dragdrop.go @@ -5,6 +5,7 @@ import ( "unsafe" "github.com/AllenDang/cimgui-go/imgui" + g "github.com/AllenDang/giu" ) diff --git a/examples/imguidemo/imguidemo.go b/examples/imguidemo/imguidemo.go index dbdf72bb..2ccad205 100644 --- a/examples/imguidemo/imguidemo.go +++ b/examples/imguidemo/imguidemo.go @@ -2,6 +2,7 @@ package main import ( "github.com/AllenDang/cimgui-go/imgui" + g "github.com/AllenDang/giu" ) diff --git a/examples/transparent/transparent.go b/examples/transparent/transparent.go index 7cf6a176..2ae1d956 100644 --- a/examples/transparent/transparent.go +++ b/examples/transparent/transparent.go @@ -5,6 +5,7 @@ import ( "image/color" "github.com/AllenDang/cimgui-go/imgui" + g "github.com/AllenDang/giu" ) From 2ae57de27e265a64ec08ad32bc7e6f772232c721 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Mon, 30 Sep 2024 21:56:52 +0200 Subject: [PATCH 03/12] lint examples --- examples/CSS-styling/main.go | 2 ++ examples/canvas/canvas.go | 6 +++--- examples/codeeditor/codeeditor.go | 18 +++++++++--------- examples/customwidget/customwidget.go | 2 ++ examples/discussion-648/main.go | 2 ++ examples/dragdrop/dragdrop.go | 5 ++++- examples/fps/main.go | 3 ++- examples/gifdecode/gifdecode.go | 6 ++---- examples/issue-501/main.go | 1 + examples/memoryeditor/memoryeditor.go | 1 + examples/ondrop/ondrop.go | 5 ++--- examples/plot/main.go | 2 ++ examples/pushfont/main.go | 1 + examples/rangebuilder/rangebuilder.go | 3 +-- examples/texturefiltering/texturefiltering.go | 1 + examples/transparent/transparent.go | 18 ++++++++++-------- examples/update/update.go | 5 ++--- 17 files changed, 47 insertions(+), 34 deletions(-) diff --git a/examples/CSS-styling/main.go b/examples/CSS-styling/main.go index eea4a9fa..cf1f77ba 100644 --- a/examples/CSS-styling/main.go +++ b/examples/CSS-styling/main.go @@ -22,8 +22,10 @@ func loop() { func main() { wnd := giu.NewMasterWindow("CSS Style [example]", 640, 480, 0) + if err := giu.ParseCSSStyleSheet(cssStyle); err != nil { panic(err) } + wnd.Run(loop) } diff --git a/examples/canvas/canvas.go b/examples/canvas/canvas.go index 5f50a7c1..b81c85d2 100644 --- a/examples/canvas/canvas.go +++ b/examples/canvas/canvas.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "image" "image/color" @@ -11,7 +10,6 @@ import ( var texture *g.Texture func loop() { - fmt.Println(texture.ID()) g.SingleWindow().Layout( g.Label("Canvas demo"), g.Custom(func() { @@ -32,7 +30,8 @@ func loop() { p2 := pos.Add(image.Pt(120, 210)) p3 := pos.Add(image.Pt(210, 210)) p4 := pos.Add(image.Pt(210, 150)) - // canvas.AddTriangle(p1, p2, p3, col, 2) + + canvas.AddTriangle(p1, p2, p3, col, 2) canvas.AddQuad(p1, p2, p3, p4, col, 1) p1 = p1.Add(image.Pt(120, 60)) @@ -41,6 +40,7 @@ func loop() { p1 = pos.Add(image.Pt(10, 400)) p2 = pos.Add(image.Pt(50, 440)) p3 = pos.Add(image.Pt(200, 500)) + canvas.PathLineTo(p1) canvas.PathLineTo(p2) canvas.PathBezierCubicCurveTo(p2.Add(image.Pt(40, 0)), p3.Add(image.Pt(-50, 0)), p3, 0) diff --git a/examples/codeeditor/codeeditor.go b/examples/codeeditor/codeeditor.go index 6d642b76..7fe337c0 100644 --- a/examples/codeeditor/codeeditor.go +++ b/examples/codeeditor/codeeditor.go @@ -1,20 +1,20 @@ +//nolint:wsl,gocritic,staticcheck // this is disabled now. package main import ( "fmt" "github.com/AllenDang/giu" - g "github.com/AllenDang/giu" ) -var editor *g.CodeEditorWidget +var editor *giu.CodeEditorWidget // errMarkers imgui.ErrorMarkers func loop() { - g.SingleWindow().Layout( - g.Row( - g.Button("Get Text").OnClick(func() { + giu.SingleWindow().Layout( + giu.Row( + giu.Button("Get Text").OnClick(func() { if editor.HasSelection() { fmt.Println(editor.GetSelectedText()) } else { @@ -29,10 +29,10 @@ func loop() { fmt.Println("Current line is", editor.GetCurrentLineText()) }), - g.Button("Set Text").OnClick(func() { + giu.Button("Set Text").OnClick(func() { editor.Text("Set text") }), - g.Button("Set Error Marker").OnClick(func() { + giu.Button("Set Error Marker").OnClick(func() { panic("implement me!") // errMarkers.Clear() // errMarkers.Insert(1, "Error message") @@ -46,11 +46,11 @@ func loop() { } func main() { - wnd := g.NewMasterWindow("Code Editor", 800, 600, 0) + wnd := giu.NewMasterWindow("Code Editor", 800, 600, 0) // errMarkers = imgui.NewErrorMarkers() - editor = g.CodeEditor(). + editor = giu.CodeEditor(). ShowWhitespaces(false). TabSize(2). Text("select * from greeting\nwhere date > current_timestamp\norder by date"). diff --git a/examples/customwidget/customwidget.go b/examples/customwidget/customwidget.go index a4faad1d..abc6a32a 100644 --- a/examples/customwidget/customwidget.go +++ b/examples/customwidget/customwidget.go @@ -22,6 +22,7 @@ func CircleButton(id string, clicked func()) *CircleButtonWidget { func (c *CircleButtonWidget) Build() { width, height := g.CalcTextSize(c.id) + var padding float32 = 8.0 // You may want to use GetCursorPos here depending on you use case. @@ -45,6 +46,7 @@ func (c *CircleButtonWidget) Build() { if drawActive { canvas.AddCircleFilled(center, float32(radius), color.RGBA{12, 12, 200, 255}) } + canvas.AddCircle(center, float32(radius), color.RGBA{200, 12, 12, 255}, int32(radius), 2) // Draw text diff --git a/examples/discussion-648/main.go b/examples/discussion-648/main.go index b1ef4af1..7a043150 100644 --- a/examples/discussion-648/main.go +++ b/examples/discussion-648/main.go @@ -6,8 +6,10 @@ func loop() { giu.Window("wnd").Layout( giu.Custom(func() { const footerPercentage = 0.2 + _, availableH := giu.GetAvailableRegion() _, itemSpacingH := giu.GetItemSpacing() + giu.Layout{ giu.Child().Layout(giu.Label("your layout")).Size(-1, (availableH-itemSpacingH)*(1-footerPercentage)), giu.Child().Layout(giu.Label("footer")).Size(-1, (availableH-itemSpacingH)*footerPercentage), diff --git a/examples/dragdrop/dragdrop.go b/examples/dragdrop/dragdrop.go index 4b7bf762..ee042aa6 100644 --- a/examples/dragdrop/dragdrop.go +++ b/examples/dragdrop/dragdrop.go @@ -16,6 +16,7 @@ func loop() { g.Row( g.Custom(func() { g.Button("Drag me: 9").Build() + if imgui.BeginDragDropSource() { data := int(9) imgui.SetDragDropPayload( @@ -29,6 +30,7 @@ func loop() { }), g.Custom(func() { g.Button("Drag me: 10").Build() + if imgui.BeginDragDropSource() { data := 10 imgui.SetDragDropPayload( @@ -46,8 +48,9 @@ func loop() { if imgui.BeginDragDropTarget() { payload := imgui.AcceptDragDropPayload("DND_DEMO") if payload != nil && payload.CData != nil { - dropTarget = fmt.Sprintf("Dropped value: %d", *(*int)(unsafe.Pointer(payload.Data()))) + dropTarget = fmt.Sprintf("Dropped value: %d", *(*int)(unsafe.Pointer(payload.Data()))) //nolint:govet // its cimgui-go design } + imgui.EndDragDropTarget() } }), diff --git a/examples/fps/main.go b/examples/fps/main.go index 23ad0be4..9b2a6bf3 100644 --- a/examples/fps/main.go +++ b/examples/fps/main.go @@ -19,8 +19,9 @@ func loop() { giu.SingleWindow().Layout( giu.Custom(func() { frames++ - timeDelta = time.Now().Sub(currentTime) + timeDelta = time.Since(currentTime) currentTime = time.Now() + fpsTime = fpsTime.Add(timeDelta) if fpsTime.Second() >= 1 { currentFPS = frames diff --git a/examples/gifdecode/gifdecode.go b/examples/gifdecode/gifdecode.go index 6b2f1c4d..f1bf0f2c 100644 --- a/examples/gifdecode/gifdecode.go +++ b/examples/gifdecode/gifdecode.go @@ -13,8 +13,6 @@ import ( //go:embed golang.gif var gifFileData []byte -const gifFilepath = "./golang.gif" - var ( frames []*giu.Texture gifImg *gif.GIF @@ -25,8 +23,6 @@ func loop() { // load textures if frames[0] == nil { for i, frame := range gifImg.Image { - // lol, this is the most magic thing in go i've ever seen :D - i := i giu.NewTextureFromRgba(giu.ImageToRgba(frame), func(t *giu.Texture) { frames[i] = t }) @@ -53,7 +49,9 @@ func main() { go func() { for { time.Sleep(time.Duration(gifImg.Delay[currentFrame]*10) * time.Millisecond) + giu.Update() + currentFrame++ if currentFrame == len(frames) { currentFrame = 0 diff --git a/examples/issue-501/main.go b/examples/issue-501/main.go index a9ff8412..0f09812b 100644 --- a/examples/issue-501/main.go +++ b/examples/issue-501/main.go @@ -14,6 +14,7 @@ func loop() { giu.Custom(func() { if shouldFocus { shouldFocus = false + giu.SetKeyboardFocusHere() } }), diff --git a/examples/memoryeditor/memoryeditor.go b/examples/memoryeditor/memoryeditor.go index 3fc3795f..539bffa9 100644 --- a/examples/memoryeditor/memoryeditor.go +++ b/examples/memoryeditor/memoryeditor.go @@ -1,3 +1,4 @@ +//nolint:wsl,gocritic // this is disabled in cimgui-go package main // import ( diff --git a/examples/ondrop/ondrop.go b/examples/ondrop/ondrop.go index 30244c6c..383661ca 100644 --- a/examples/ondrop/ondrop.go +++ b/examples/ondrop/ondrop.go @@ -7,9 +7,7 @@ import ( g "github.com/AllenDang/giu" ) -var ( - dropInFiles string -) +var dropInFiles string func loop() { g.SingleWindow().Layout( @@ -25,6 +23,7 @@ func onDrop(names []string) { } dropInFiles = sb.String() + g.Update() } diff --git a/examples/plot/main.go b/examples/plot/main.go index 6c44dd0c..4cdc4ff1 100644 --- a/examples/plot/main.go +++ b/examples/plot/main.go @@ -60,6 +60,7 @@ func loop() { func main() { delta := 0.1 + for x := 0.0; x < 10; x += delta { linedata = append(linedata, math.Sin(x)) linedata2 = append(linedata2, math.Cos(x)) @@ -71,6 +72,7 @@ func main() { } delta = 1 + for x := 0.0; x < 10; x += delta { bardata = append(bardata, math.Sin(x)) bardata2 = append(bardata2, math.Sin(x)-0.2) diff --git a/examples/pushfont/main.go b/examples/pushfont/main.go index b7ecd85a..3354ad59 100644 --- a/examples/pushfont/main.go +++ b/examples/pushfont/main.go @@ -10,6 +10,7 @@ var font *giu.FontInfo func loop() { fontPushed := false + giu.Window("example").Layout( giu.Custom(func() { fontPushed = giu.PushFont(font) diff --git a/examples/rangebuilder/rangebuilder.go b/examples/rangebuilder/rangebuilder.go index 978bff61..be0e52b0 100644 --- a/examples/rangebuilder/rangebuilder.go +++ b/examples/rangebuilder/rangebuilder.go @@ -21,8 +21,7 @@ func loop() { ), g.Dummy(0, 8), g.Label("Below buttons are generated by RangeBuilder"), - g.RangeBuilder("Buttons", []interface{}{"Button1", "Button2", "Button3"}, func(i int, v interface{}) g.Widget { - str := v.(string) + g.RangeBuilder("Buttons", []string{"Button1", "Button2", "Button3"}, func(i int, str string) g.Widget { return g.Button(str).OnClick(func() { fmt.Println(str, "is clicked") }) diff --git a/examples/texturefiltering/texturefiltering.go b/examples/texturefiltering/texturefiltering.go index 72e19e29..2236f4bd 100644 --- a/examples/texturefiltering/texturefiltering.go +++ b/examples/texturefiltering/texturefiltering.go @@ -1,3 +1,4 @@ +//nolint:gocritic,wsl // this is disabled now package main // import ( diff --git a/examples/transparent/transparent.go b/examples/transparent/transparent.go index 2ae1d956..59f9fc89 100644 --- a/examples/transparent/transparent.go +++ b/examples/transparent/transparent.go @@ -17,26 +17,28 @@ func loop() { g.Custom(func() { canvas := g.GetCanvas() pos := g.GetCursorScreenPos() - color := color.RGBA{200, 75, 75, 255} - canvas.AddLine(pos, pos.Add(image.Pt(100, 100)), color, 1) - canvas.AddRect(pos.Add(image.Pt(110, 0)), pos.Add(image.Pt(200, 100)), color, 5, g.DrawFlagsRoundCornersAll, 1) - canvas.AddRectFilled(pos.Add(image.Pt(220, 0)), pos.Add(image.Pt(320, 100)), color, 0, 0) + col := color.RGBA{200, 75, 75, 255} + + canvas.AddLine(pos, pos.Add(image.Pt(100, 100)), col, 1) + canvas.AddRect(pos.Add(image.Pt(110, 0)), pos.Add(image.Pt(200, 100)), col, 5, g.DrawFlagsRoundCornersAll, 1) + canvas.AddRectFilled(pos.Add(image.Pt(220, 0)), pos.Add(image.Pt(320, 100)), col, 0, 0) pos0 := pos.Add(image.Pt(0, 110)) cp0 := pos.Add(image.Pt(80, 110)) cp1 := pos.Add(image.Pt(50, 210)) pos1 := pos.Add(image.Pt(120, 210)) - canvas.AddBezierCubic(pos0, cp0, cp1, pos1, color, 1, 0) + canvas.AddBezierCubic(pos0, cp0, cp1, pos1, col, 1, 0) p1 := pos.Add(image.Pt(160, 110)) p2 := pos.Add(image.Pt(120, 210)) p3 := pos.Add(image.Pt(210, 210)) p4 := pos.Add(image.Pt(210, 150)) - // canvas.AddTriangle(p1, p2, p3, color, 2) - canvas.AddQuad(p1, p2, p3, p4, color, 1) + + canvas.AddTriangle(p1, p2, p3, col, 2) + canvas.AddQuad(p1, p2, p3, p4, col, 1) p1 = p1.Add(image.Pt(120, 60)) - canvas.AddCircleFilled(p1, 50, color) + canvas.AddCircleFilled(p1, 50, col) }), ) g.PopStyleColor() diff --git a/examples/update/update.go b/examples/update/update.go index 01a39f6c..00883de8 100644 --- a/examples/update/update.go +++ b/examples/update/update.go @@ -8,15 +8,14 @@ import ( "github.com/AllenDang/giu" ) -var ( - counter int -) +var counter int func refresh() { ticker := time.NewTicker(time.Second * 1) for { counter = rand.Intn(100) + giu.Update() <-ticker.C From bee005efecadb18de88b4b7b05a66836da5f4410 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Mon, 30 Sep 2024 21:58:28 +0200 Subject: [PATCH 04/12] enable gofumpt linter --- .golangci.yml | 2 +- CSS.go | 1 - examples/hugelist/hugelist.go | 4 +--- examples/keyboardShortcuts/keyboardShortcuts.go | 3 ++- examples/markdown/markdown.go | 1 + 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 46e183ff..255525e8 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -21,7 +21,7 @@ linters: - gocyclo - err113 - gofmt - #- gofumpt + - gofumpt - goheader - goimports #- gomnd diff --git a/CSS.go b/CSS.go index f53aa434..68d5e9d0 100644 --- a/CSS.go +++ b/CSS.go @@ -99,7 +99,6 @@ func ParseCSSStyleSheet(data []byte) error { err = panicToErr(func() { styleColorID = StyleColorIDFromString(styleVarName) }) - if err != nil { return ErrCSSParse{What: "style variable ID", Value: styleVarName} } diff --git a/examples/hugelist/hugelist.go b/examples/hugelist/hugelist.go index ef058c93..51c94737 100644 --- a/examples/hugelist/hugelist.go +++ b/examples/hugelist/hugelist.go @@ -7,9 +7,7 @@ import ( g "github.com/AllenDang/giu" ) -var ( - names []string -) +var names []string func buildRows() []*g.TableRowWidget { rows := make([]*g.TableRowWidget, len(names)) diff --git a/examples/keyboardShortcuts/keyboardShortcuts.go b/examples/keyboardShortcuts/keyboardShortcuts.go index ec821a4b..464cacd5 100644 --- a/examples/keyboardShortcuts/keyboardShortcuts.go +++ b/examples/keyboardShortcuts/keyboardShortcuts.go @@ -29,7 +29,8 @@ func main() { giu.WindowShortcut{ Key: giu.KeyC, Modifier: giu.ModControl, - Callback: func() { checkbox1 = !checkbox1 }}, + Callback: func() { checkbox1 = !checkbox1 }, + }, ) wnd.Run(loop) diff --git a/examples/markdown/markdown.go b/examples/markdown/markdown.go index b0cf0b17..aae88401 100644 --- a/examples/markdown/markdown.go +++ b/examples/markdown/markdown.go @@ -72,6 +72,7 @@ func loop() { ), ) } + func main() { wnd := giu.NewMasterWindow("ImGui Markdown [Demo]", 640, 480, 0) wnd.Run(loop) From 5006942cdb227c52058e6d67001bd34b21c7cfb7 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Mon, 30 Sep 2024 22:02:14 +0200 Subject: [PATCH 05/12] enable usestdlibvars --- .golangci.yml | 1 + ImageWidgets.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 255525e8..2e2cc547 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -47,6 +47,7 @@ linters: - unconvert - unparam - unused + - usestdlibvars - wastedassign - whitespace - wrapcheck diff --git a/ImageWidgets.go b/ImageWidgets.go index 7c76e48c..33262c64 100644 --- a/ImageWidgets.go +++ b/ImageWidgets.go @@ -350,7 +350,7 @@ func (i *ImageWithURLWidget) Build() { // Load image from url client := &http.Client{Timeout: i.downloadTimeout} - req, err := http.NewRequestWithContext(downloadContext, "GET", i.imgURL, http.NoBody) + req, err := http.NewRequestWithContext(downloadContext, http.MethodGet, i.imgURL, http.NoBody) if err != nil { errorFn(err) return From c883b7e9cdeb894b4a1453b280ff11f8c4edb1b6 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Mon, 30 Sep 2024 22:06:20 +0200 Subject: [PATCH 06/12] enable some more linters --- .golangci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 2e2cc547..063d17c3 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -39,10 +39,13 @@ linters: - prealloc - predeclared - promlinter + - reassign #- revive - rowserrcheck - staticcheck - stylecheck + - tagliatelle + - testifylint - typecheck - unconvert - unparam From 278013a9315fc18c7debf8e3a12b1ba00c9576a1 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Mon, 30 Sep 2024 22:12:00 +0200 Subject: [PATCH 07/12] widgets: add missing docs I'm going to reenable revive but idk if that'll happen here --- Widgets.go | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/Widgets.go b/Widgets.go index 15cde5f0..78de70d9 100644 --- a/Widgets.go +++ b/Widgets.go @@ -56,6 +56,8 @@ func SameLine() { var _ Widget = &ChildWidget{} +// ChildWidget is a container widget. It will have a separated scroll bar. +// Use Child if you want to create a layout of e specific size. type ChildWidget struct { id ID width float32 @@ -65,36 +67,38 @@ type ChildWidget struct { layout Layout } -// Build implements Widget interface. -func (c *ChildWidget) Build() { - if imgui.BeginChildStrV(c.id.String(), imgui.Vec2{X: c.width, Y: c.height}, func() imgui.ChildFlags { - if c.border { - return imgui.ChildFlagsBorders - } - - return 0 - }(), imgui.WindowFlags(c.flags)) { - c.layout.Build() +// Child creates a new ChildWidget +func Child() *ChildWidget { + return &ChildWidget{ + id: GenAutoID("Child"), + width: 0, + height: 0, + border: true, + flags: 0, + layout: nil, } - - imgui.EndChild() } +// Border sets whether child should have border +// You can use imgui.ChildFlagsBorders as well func (c *ChildWidget) Border(border bool) *ChildWidget { c.border = border return c } +// Size sets child size. func (c *ChildWidget) Size(width, height float32) *ChildWidget { c.width, c.height = width, height return c } +// Flags allows to specify Child flags. func (c *ChildWidget) Flags(flags WindowFlags) *ChildWidget { c.flags = flags return c } +// Layout sets widgets that will be rendered inside of the Child. func (c *ChildWidget) Layout(widgets ...Widget) *ChildWidget { c.layout = Layout(widgets) return c @@ -106,15 +110,19 @@ func (c *ChildWidget) ID(id ID) *ChildWidget { return c } -func Child() *ChildWidget { - return &ChildWidget{ - id: GenAutoID("Child"), - width: 0, - height: 0, - border: true, - flags: 0, - layout: nil, +// Build makes a Child. +func (c *ChildWidget) Build() { + if imgui.BeginChildStrV(c.id.String(), imgui.Vec2{X: c.width, Y: c.height}, func() imgui.ChildFlags { + if c.border { + return imgui.ChildFlagsBorders + } + + return 0 + }(), imgui.WindowFlags(c.flags)) { + c.layout.Build() } + + imgui.EndChild() } var _ Widget = &ComboCustomWidget{} From 55e00426551efd40e61918013cd1b785f7ead29d Mon Sep 17 00:00:00 2001 From: gucio321 Date: Mon, 30 Sep 2024 22:17:38 +0200 Subject: [PATCH 08/12] table and mw: add missing docs" --- MasterWindow.go | 2 +- TableWidgets.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/MasterWindow.go b/MasterWindow.go index f0fa0eda..346e9757 100644 --- a/MasterWindow.go +++ b/MasterWindow.go @@ -394,7 +394,7 @@ func (w *MasterWindow) SetShouldClose(v bool) { func (w *MasterWindow) SetInputHandler(handler InputHandler) { Context.InputHandler = handler - w.backend.SetKeyCallback(func(key, scanCode, action, modifier int) { + w.backend.SetKeyCallback(func(key, _, action, modifier int) { k, m, a := keyFromGLFWKey(glfwbackend.GLFWKey(key)), Modifier(modifier), Action(action) handler.Handle(k, m, a) diff --git a/TableWidgets.go b/TableWidgets.go index 09a991a8..6bcd37f0 100644 --- a/TableWidgets.go +++ b/TableWidgets.go @@ -6,6 +6,7 @@ import ( "github.com/AllenDang/cimgui-go/imgui" ) +// TableRowWidget represents a row in a table. type TableRowWidget struct { flags TableRowFlags minRowHeight float64 @@ -13,6 +14,9 @@ type TableRowWidget struct { bgColor color.Color } +// TableRow creates a TbleRowWidget. +// Each widget will be rendered in a separated column. +// NOTE: if you want to put multiple widgets in one cell, enclose them in Layout{}. func TableRow(widgets ...Widget) *TableRowWidget { return &TableRowWidget{ flags: 0, @@ -22,16 +26,19 @@ func TableRow(widgets ...Widget) *TableRowWidget { } } +// BgColor sets the background color of the row. func (r *TableRowWidget) BgColor(c color.Color) *TableRowWidget { r.bgColor = c return r } +// Flags sets the flags of the row. func (r *TableRowWidget) Flags(flags TableRowFlags) *TableRowWidget { r.flags = flags return r } +// MinHeight sets the minimum height of the row. func (r *TableRowWidget) MinHeight(height float64) *TableRowWidget { r.minRowHeight = height return r @@ -58,6 +65,7 @@ func (r *TableRowWidget) BuildTableRow() { } } +// TableColumnWidget allows to configure table columns headers. type TableColumnWidget struct { label string flags TableColumnFlags @@ -65,6 +73,7 @@ type TableColumnWidget struct { userID uint32 } +// TableColumn creates a new TableColumnWidget. func TableColumn(label string) *TableColumnWidget { return &TableColumnWidget{ label: Context.FontAtlas.RegisterString(label), @@ -74,16 +83,19 @@ func TableColumn(label string) *TableColumnWidget { } } +// Flags sets the flags of the column. func (c *TableColumnWidget) Flags(flags TableColumnFlags) *TableColumnWidget { c.flags = flags return c } +// InnerWidthOrWeight sets the inner width or weight of the column. func (c *TableColumnWidget) InnerWidthOrWeight(w float32) *TableColumnWidget { c.innerWidthOrWeight = w return c } +// UserID sets the user id of the column. func (c *TableColumnWidget) UserID(id uint32) *TableColumnWidget { c.userID = id return c @@ -96,6 +108,10 @@ func (c *TableColumnWidget) BuildTableColumn() { var _ Widget = &TableWidget{} +// TableWidget is a table widget. +// - Call Table to create new +// - Then use Rows method to add content +// - Use Columns method to configure columns (optional) type TableWidget struct { id ID flags TableFlags @@ -109,6 +125,7 @@ type TableWidget struct { noHeader bool } +// Table creates new TableWidget. func Table() *TableWidget { return &TableWidget{ id: GenAutoID("Table"), @@ -158,21 +175,25 @@ func (t *TableWidget) Columns(cols ...*TableColumnWidget) *TableWidget { return t } +// Rows sets the rows of the table. func (t *TableWidget) Rows(rows ...*TableRowWidget) *TableWidget { t.rows = rows return t } +// Size sets the size of the table. func (t *TableWidget) Size(width, height float32) *TableWidget { t.size = imgui.Vec2{X: width, Y: height} return t } +// InnerWidth sets the inner width of the table. func (t *TableWidget) InnerWidth(width float64) *TableWidget { t.innerWidth = width return t } +// Flags sets the flags of the table. func (t *TableWidget) Flags(flags TableFlags) *TableWidget { t.flags = flags return t From 342f33704f5c7116435c5b0bb7f61222d1ccb5c8 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Mon, 30 Sep 2024 22:20:43 +0200 Subject: [PATCH 09/12] Context: add missing doc --- Context.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Context.go b/Context.go index 6c081aae..b205d73d 100644 --- a/Context.go +++ b/Context.go @@ -66,6 +66,7 @@ type context struct { m *sync.Mutex } +// CreateContext creates a new giu context. func CreateContext(b backend.Backend[glfwbackend.GLFWWindowFlags]) *context { result := context{ cssStylesheet: make(cssStylesheet), @@ -91,10 +92,13 @@ func CreateContext(b backend.Backend[glfwbackend.GLFWWindowFlags]) *context { return &result } +// IO returns the imgui.IO object. func (c *context) IO() *imgui.IO { return imgui.CurrentIO() } +// invalidAllState should be called before rendering. +// it ensures all states are marked as invalid for that moment. func (c *context) invalidAllState() { c.state.Range(func(k, v any) bool { if s, ok := v.(*state); ok { @@ -107,6 +111,8 @@ func (c *context) invalidAllState() { }) } +// cleanState removes all states that were not marked as valid during rendering. +// should be called after rendering func (c *context) cleanState() { c.state.Range(func(k, v any) bool { if s, ok := v.(*state); ok { @@ -127,18 +133,22 @@ func (c *context) cleanState() { c.widgetIndex = make(map[string]int) } +// Backend returns the imgui.backend used by the context. func (c *context) Backend() backend.Backend[glfwbackend.GLFWWindowFlags] { return c.backend } +// SetState is a generic version of Context.SetState func SetState[T any, PT genericDisposable[T]](c *context, id string, data PT) { c.state.Store(id, &state{valid: true, data: data}) } +// SetState stores data in context by id. func (c *context) SetState(id string, data Disposable) { c.state.Store(id, &state{valid: true, data: data}) } +// Get state is a generic version of Context.GetState func GetState[T any, PT genericDisposable[T]](c *context, id string) PT { if s, ok := c.load(id); ok { c.m.Lock() @@ -154,6 +164,7 @@ func GetState[T any, PT genericDisposable[T]](c *context, id string) PT { return nil } +// GetState returns previously stored state by id. func (c *context) GetState(id string) any { if s, ok := c.load(id); ok { c.m.Lock() From 750a4623adf97ca835644957b733ae32ebe02244 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Mon, 30 Sep 2024 22:21:18 +0200 Subject: [PATCH 10/12] fix godot --- TableWidgets.go | 2 +- Widgets.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/TableWidgets.go b/TableWidgets.go index 6bcd37f0..dfd4b44f 100644 --- a/TableWidgets.go +++ b/TableWidgets.go @@ -111,7 +111,7 @@ var _ Widget = &TableWidget{} // TableWidget is a table widget. // - Call Table to create new // - Then use Rows method to add content -// - Use Columns method to configure columns (optional) +// - Use Columns method to configure columns (optional). type TableWidget struct { id ID flags TableFlags diff --git a/Widgets.go b/Widgets.go index 78de70d9..9c68fa60 100644 --- a/Widgets.go +++ b/Widgets.go @@ -67,7 +67,7 @@ type ChildWidget struct { layout Layout } -// Child creates a new ChildWidget +// Child creates a new ChildWidget. func Child() *ChildWidget { return &ChildWidget{ id: GenAutoID("Child"), @@ -80,7 +80,7 @@ func Child() *ChildWidget { } // Border sets whether child should have border -// You can use imgui.ChildFlagsBorders as well +// You can use imgui.ChildFlagsBorders as well. func (c *ChildWidget) Border(border bool) *ChildWidget { c.border = border return c From 9debdd037b8a338d8ec70207d4ffe1e6477d4ea0 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Mon, 30 Sep 2024 22:35:13 +0200 Subject: [PATCH 11/12] autofix in Context.go --- Context.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Context.go b/Context.go index b205d73d..c7874fbc 100644 --- a/Context.go +++ b/Context.go @@ -112,7 +112,7 @@ func (c *context) invalidAllState() { } // cleanState removes all states that were not marked as valid during rendering. -// should be called after rendering +// should be called after rendering. func (c *context) cleanState() { c.state.Range(func(k, v any) bool { if s, ok := v.(*state); ok { @@ -138,7 +138,7 @@ func (c *context) Backend() backend.Backend[glfwbackend.GLFWWindowFlags] { return c.backend } -// SetState is a generic version of Context.SetState +// SetState is a generic version of Context.SetState. func SetState[T any, PT genericDisposable[T]](c *context, id string, data PT) { c.state.Store(id, &state{valid: true, data: data}) } @@ -148,7 +148,7 @@ func (c *context) SetState(id string, data Disposable) { c.state.Store(id, &state{valid: true, data: data}) } -// Get state is a generic version of Context.GetState +// Get state is a generic version of Context.GetState. func GetState[T any, PT genericDisposable[T]](c *context, id string) PT { if s, ok := c.load(id); ok { c.m.Lock() From 8ba0d013c35f5e79e1f218297c9ce6f06a5906bb Mon Sep 17 00:00:00 2001 From: gucio321 Date: Mon, 30 Sep 2024 22:39:23 +0200 Subject: [PATCH 12/12] fix comment on GetState --- Context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Context.go b/Context.go index c7874fbc..d6dc9125 100644 --- a/Context.go +++ b/Context.go @@ -148,7 +148,7 @@ func (c *context) SetState(id string, data Disposable) { c.state.Store(id, &state{valid: true, data: data}) } -// Get state is a generic version of Context.GetState. +// GetState is a generic version of Context.GetState. func GetState[T any, PT genericDisposable[T]](c *context, id string) PT { if s, ok := c.load(id); ok { c.m.Lock()