From 2660ed4c9bb7cccbb7e10206a5b050c929f4c998 Mon Sep 17 00:00:00 2001 From: Andy Maloney Date: Sat, 26 Nov 2022 16:53:05 -0500 Subject: [PATCH] Linting fixes - mainly spacing --- Alignment.go | 5 +++-- CSS.go | 4 ++++ ClickableWidgets.go | 1 + CodeEditor.go | 1 + Context.go | 6 +++++- EventHandler.go | 4 ++++ ExtraWidgets.go | 14 ++++++++++++++ FontAtlasProsessor.go | 1 + ImageWidgets.go | 6 ++++++ ListClipper.go | 1 + Markdown.go | 3 +++ MasterWindow.go | 2 ++ Msgbox.go | 3 +++ Plot.go | 5 +++++ ProgressIndicator.go | 4 ++++ SliderWidgets.go | 2 ++ SplitLayout.go | 2 ++ StackWidget.go | 2 ++ TableWidgets.go | 2 ++ TextWidgets.go | 4 ++++ Texture.go | 2 ++ Utils.go | 2 ++ Widgets.go | 2 ++ Window.go | 3 +++ 24 files changed, 78 insertions(+), 3 deletions(-) diff --git a/Alignment.go b/Alignment.go index 23e9ac19..e9c0610a 100644 --- a/Alignment.go +++ b/Alignment.go @@ -10,7 +10,7 @@ import ( // These constants holds information about where GetWidgetWidth should proceed their // measurements. // -// It should be far away from our working space, -1000 seems to be good choice for now +// It should be far away from our working space, -1000 seems to be good choice for now. const ( getWidgetWidthTestingSpaceX, getWidgetWidthTestingSpaceY = -1000, -1000 ) @@ -82,7 +82,7 @@ var _ Widget = &AlignmentSetter{} // usage: see examples/align // // list of known bugs: -// - BUG: there is some bug with SelectableWidget +// - BUG: there is some bug with SelectableWidget. type AlignmentSetter struct { alignType AlignmentType layout Layout @@ -195,6 +195,7 @@ func GetWidgetWidth(w Widget) (result float32) { // save widget's width // check cursor position imgui.SameLine() + spacingW, _ := GetItemSpacing() result = float32(GetCursorPos().X-startPos.X) - spacingW diff --git a/CSS.go b/CSS.go index 3520b096..2ee601ee 100644 --- a/CSS.go +++ b/CSS.go @@ -31,9 +31,11 @@ func ParseCSSStyleSheet(data []byte) error { for rule, style := range stylesheet { setter := Style() + for styleVarName, styleVarValue := range style { // convert style variable name to giu style variable name var styleVarID StyleVarID + err := panicToErr(func() { styleVarID = StyleVarIDFromString(styleVarName) }) @@ -74,6 +76,7 @@ func ParseCSSStyleSheet(data []byte) error { } var styleColorID StyleColorID + err = panicToErr(func() { styleColorID = StyleColorIDFromString(styleVarName) }) @@ -104,6 +107,7 @@ func panicToErr(f func()) (err error) { }() f() + return err } diff --git a/ClickableWidgets.go b/ClickableWidgets.go index 9fc9b9ec..24f8a32c 100644 --- a/ClickableWidgets.go +++ b/ClickableWidgets.go @@ -542,6 +542,7 @@ func (t *TreeNodeWidget) Build() { if open { t.layout.Build() + if (t.flags & imgui.TreeNodeFlagsNoTreePushOnOpen) == 0 { imgui.TreePop() } diff --git a/CodeEditor.go b/CodeEditor.go index acc75332..efb93622 100644 --- a/CodeEditor.go +++ b/CodeEditor.go @@ -211,5 +211,6 @@ func (ce *CodeEditorWidget) getState() (state *codeEditorState) { SetState(&Context, ce.title, state) } + return state } diff --git a/Context.go b/Context.go index 7887fc5f..f986bd2b 100644 --- a/Context.go +++ b/Context.go @@ -68,7 +68,6 @@ func CreateContext(p imgui.Platform, r imgui.Renderer) context { r.SetFontTexture(fontAtlas) } else { result.FontAtlas.shouldRebuildFontAtlas = true - // result.FontAtlas.rebuildFontAtlas() } return result @@ -123,8 +122,10 @@ func GetState[T any, PT genericDisposable[T]](c context, id string) PT { s.valid = true data, isOk := s.data.(PT) Assert(isOk, "Context", "GetState", fmt.Sprintf("got state of unexpected type: expected %T, instead found %T", new(T), s.data)) + return data } + return nil } @@ -133,6 +134,7 @@ func (c *context) GetState(id string) any { s.valid = true return s.data } + return nil } @@ -142,6 +144,7 @@ func (c *context) load(id any) (*state, bool) { return s, true } } + return nil, false } @@ -149,5 +152,6 @@ func (c *context) load(id any) (*state, bool) { func (c *context) GetWidgetIndex() int { i := c.widgetIndexCounter c.widgetIndexCounter++ + return i } diff --git a/EventHandler.go b/EventHandler.go index af4e0cbf..4b154c85 100644 --- a/EventHandler.go +++ b/EventHandler.go @@ -115,12 +115,14 @@ func (eh *EventHandler) OnMouseReleased(mouseButton MouseButton, callback func() } // Build implements Widget interface +// //nolint:gocognit,gocyclo // will fix later func (eh *EventHandler) Build() { isActive := IsItemActive() if eh.onActivate != nil || eh.onDeactivate != nil { var state *eventHandlerState + stateID := GenAutoID("eventHandlerState") if state = GetState[eventHandlerState](Context, stateID); state == nil { state = &eventHandlerState{} @@ -129,11 +131,13 @@ func (eh *EventHandler) Build() { if eh.onActivate != nil && isActive && !state.isActive { state.isActive = true + eh.onActivate() } if eh.onDeactivate != nil && !isActive && state.isActive { state.isActive = false + eh.onDeactivate() } } diff --git a/ExtraWidgets.go b/ExtraWidgets.go index beb3e6be..3a700c4c 100644 --- a/ExtraWidgets.go +++ b/ExtraWidgets.go @@ -50,6 +50,7 @@ func (h *HSplitterWidget) ID(id string) *HSplitterWidget { } // Build implements Widget interface +// //nolint:dupl // will fix later func (h *HSplitterWidget) Build() { // Calc line position. @@ -69,11 +70,13 @@ func (h *HSplitterWidget) Build() { // Place a invisible button to capture event. imgui.InvisibleButton(h.id, imgui.Vec2{X: h.width, Y: h.height}) + if imgui.IsItemActive() { *(h.delta) = imgui.CurrentIO().GetMouseDelta().Y } else { *(h.delta) = 0 } + if imgui.IsItemHovered() { imgui.SetMouseCursor(imgui.MouseCursorResizeNS) c = Vec4ToRGBA(style.GetColor(imgui.StyleColorScrollbarGrabActive)) @@ -126,6 +129,7 @@ func (v *VSplitterWidget) ID(id string) *VSplitterWidget { } // Build implements Widget interface +// //nolint:dupl // will fix later func (v *VSplitterWidget) Build() { // Calc line position. @@ -145,6 +149,7 @@ func (v *VSplitterWidget) Build() { // Place a invisible button to capture event. imgui.InvisibleButton(v.id, imgui.Vec2{X: v.width, Y: v.height}) + if imgui.IsItemActive() { *(v.delta) = imgui.CurrentIO().GetMouseDelta().X } else { @@ -244,6 +249,7 @@ func TreeTable() *TreeTableWidget { func (tt *TreeTableWidget) Freeze(col, row int) *TreeTableWidget { tt.freezeColumn = col tt.freezeRow = row + return tt } @@ -287,6 +293,7 @@ func (tt *TreeTableWidget) Build() { for _, col := range tt.columns { col.BuildTableColumn() } + imgui.TableHeadersRow() } @@ -445,6 +452,7 @@ func (l *ListBoxWidget) OnMenu(onMenu func(selectedIndex int, menu string)) *Lis } // Build implements Widget interface +// //nolint:gocognit // will fix later func (l *ListBoxWidget) Build() { var state *ListBoxState @@ -532,6 +540,7 @@ func (d *DatePickerWidget) OnChange(onChange func()) *DatePickerWidget { if onChange != nil { d.onChange = onChange } + return d } @@ -549,6 +558,7 @@ func (d *DatePickerWidget) getFormat() string { if d.format == "" { return "2006-01-02" // default } + return d.format } @@ -556,6 +566,7 @@ func (d *DatePickerWidget) offsetDay(offset int) time.Weekday { day := (int(d.startOfWeek) + offset) % 7 // offset may be negative, thus day can be negative day = (day + 7) % 7 + return time.Weekday(day) } @@ -570,6 +581,7 @@ func (d *DatePickerWidget) Build() { if d.width > 0 { PushItemWidth(d.width) + defer PopItemWidth() } @@ -652,6 +664,7 @@ func (d *DatePickerWidget) getDaysGroups() (days [][]int) { monthDay := 1 emptyDaysInFirstWeek := (int(firstDay.Weekday()) - int(d.startOfWeek) + 7) % 7 + for i := emptyDaysInFirstWeek; i < 7; i++ { days[0][i] = monthDay monthDay++ @@ -680,6 +693,7 @@ func (d *DatePickerWidget) getDaysGroups() (days [][]int) { func (d *DatePickerWidget) calendarField(day int) Widget { today := time.Now() highlightColor := imgui.CurrentStyle().GetColor(imgui.StyleColorPlotHistogram) + return Custom(func() { isToday := d.date.Year() == today.Year() && d.date.Month() == today.Month() && day == today.Day() if isToday { diff --git a/FontAtlasProsessor.go b/FontAtlasProsessor.go index b6aa4a1f..ad847d72 100644 --- a/FontAtlasProsessor.go +++ b/FontAtlasProsessor.go @@ -191,6 +191,7 @@ func (a *FontAtlas) registerDefaultFont(fontName string, size float32) { func (a *FontAtlas) registerDefaultFonts(fontInfos []FontInfo) { var firstFoundFont *FontInfo + for _, fi := range fontInfos { fontPath, err := findfont.Find(fi.fontName) if err == nil { diff --git a/ImageWidgets.go b/ImageWidgets.go index ccbf5d85..98948c8d 100644 --- a/ImageWidgets.go +++ b/ImageWidgets.go @@ -68,6 +68,7 @@ func (i *ImageWidget) Size(width, height float32) *ImageWidget { // Size image with DPI scaling factor := Context.GetPlatform().GetContentScale() i.width, i.height = width*factor, height*factor + return i } @@ -75,9 +76,11 @@ func (i *ImageWidget) Size(width, height float32) *ImageWidget { func (i *ImageWidget) Build() { size := imgui.Vec2{X: i.width, Y: i.height} rect := imgui.ContentRegionAvail() + if size.X == -1 { size.X = rect.X } + if size.Y == -1 { size.Y = rect.Y } @@ -92,6 +95,7 @@ func (i *ImageWidget) Build() { cursorPos := GetCursorScreenPos() mousePos := GetMousePos() mousePos.Add(cursorPos) + if cursorPos.X <= mousePos.X && cursorPos.Y <= mousePos.Y && cursorPos.X+int(i.width) >= mousePos.X && cursorPos.Y+int(i.height) >= mousePos.Y { i.onClick() @@ -313,6 +317,7 @@ func (i *ImageWithURLWidget) Build() { // Prevent multiple invocation to download image. downloadContext, cancelFunc := ctx.WithCancel(ctx.Background()) + SetState(&Context, i.id, &imageState{loading: true, cancel: cancelFunc}) errorFn := func(err error) { @@ -328,6 +333,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) + if err != nil { errorFn(err) return diff --git a/ListClipper.go b/ListClipper.go index caf1f3e7..760b8a58 100644 --- a/ListClipper.go +++ b/ListClipper.go @@ -28,6 +28,7 @@ func (l *ListClipperWrapper) Layout(layout ...Widget) *ListClipperWrapper { func (l *ListClipperWrapper) Build() { // read all the layout widgets and (eventually) split nested layouts var layout Layout + l.layout.Range(func(w Widget) { layout = append(layout, w) }) diff --git a/Markdown.go b/Markdown.go index 68c3d36b..df7dc11b 100644 --- a/Markdown.go +++ b/Markdown.go @@ -64,6 +64,7 @@ func (m *MarkdownWidget) Build() { func loadImage(path string) imgui.MarkdownImageData { var img *image.RGBA + var err error switch { @@ -71,6 +72,7 @@ func loadImage(path string) imgui.MarkdownImageData { // Load image from url client := &http.Client{Timeout: 5 * time.Second} resp, respErr := client.Get(path) + if respErr != nil { return imgui.MarkdownImageData{} } @@ -106,6 +108,7 @@ func loadImage(path string) imgui.MarkdownImageData { */ var id imgui.TextureID + mainthread.Call(func() { var err error id, err = Context.renderer.LoadImage(img) diff --git a/MasterWindow.go b/MasterWindow.go index deff2045..ad098e34 100644 --- a/MasterWindow.go +++ b/MasterWindow.go @@ -218,6 +218,7 @@ func (w *MasterWindow) run() { ticker := time.NewTicker(time.Second / time.Duration(p.GetTPS())) shouldQuit := false + for !shouldQuit { mainthread.Call(func() { // process texture load requests @@ -386,6 +387,7 @@ func (w *MasterWindow) SetShouldClose(v bool) { // see InputHandler.go. func (w *MasterWindow) SetInputHandler(handler InputHandler) { Context.InputHandler = handler + w.platform.SetInputCallback(func(key glfw.Key, modifier glfw.ModifierKey, action glfw.Action) { k, m, a := Key(key), Modifier(modifier), Action(action) handler.Handle(k, m, a) diff --git a/Msgbox.go b/Msgbox.go index 79ae6bb3..69858bf6 100644 --- a/Msgbox.go +++ b/Msgbox.go @@ -48,6 +48,7 @@ func (ms *msgboxState) Dispose() { func msgboxInvokeCallback(result DialogResult, callback DialogResultCallback) { CloseCurrentPopup() + if callback != nil { callback(result) } @@ -159,6 +160,7 @@ func Msgbox(title, content string) *MsgboxWidget { func (m *MsgboxWidget) Buttons(buttons MsgboxButtons) *MsgboxWidget { s := m.getState() s.buttons = buttons + return m } @@ -166,5 +168,6 @@ func (m *MsgboxWidget) Buttons(buttons MsgboxButtons) *MsgboxWidget { func (m *MsgboxWidget) ResultCallback(cb DialogResultCallback) *MsgboxWidget { s := m.getState() s.resultCallback = cb + return m } diff --git a/Plot.go b/Plot.go index ef97ee38..35fa90c3 100644 --- a/Plot.go +++ b/Plot.go @@ -104,6 +104,7 @@ func (p *PlotCanvasWidget) XTicks(ticks []PlotTicker, showDefault bool) *PlotCan p.xTicksValue = values p.xTicksLabel = labels p.xTicksShowDefault = showDefault + return p } @@ -126,6 +127,7 @@ func (p *PlotCanvasWidget) YTicks(ticks []PlotTicker, showDefault bool, yAxis Im p.yTicksLabel = labels p.yTicksShowDefault = showDefault p.yTicksYAxis = yAxis + return p } @@ -146,6 +148,7 @@ func (p *PlotCanvasWidget) YAxeFlags(yFlags, y2Flags, y3Flags PlotAxisFlags) *Pl p.yFlags = yFlags p.y2Flags = y2Flags p.y3Flags = y3Flags + return p } @@ -159,6 +162,7 @@ func (p *PlotCanvasWidget) Plots(plots ...PlotWidget) *PlotCanvasWidget { func (p *PlotCanvasWidget) Size(width, height int) *PlotCanvasWidget { p.width = width p.height = height + return p } @@ -185,6 +189,7 @@ func (p *PlotCanvasWidget) Build() { for _, plot := range p.plots { plot.Plot() } + imgui.ImPlotEnd() } } diff --git a/ProgressIndicator.go b/ProgressIndicator.go index 7b2200d6..ed833ccb 100644 --- a/ProgressIndicator.go +++ b/ProgressIndicator.go @@ -17,10 +17,12 @@ type progressIndicatorState struct { func (ps *progressIndicatorState) update() { ticker := time.NewTicker(time.Second / 60) + for !ps.stop { if ps.angle > 6.2 { ps.angle = 0 } + ps.angle += 0.1 Update() @@ -65,7 +67,9 @@ func (p *ProgressIndicatorWidget) Build() { if state := GetState[progressIndicatorState](Context, p.internalID); state == nil { // Register state and start go routine ps := progressIndicatorState{angle: 0.0, stop: false} + SetState(&Context, p.internalID, &ps) + go ps.update() } else { child := Child().Border(false).Size(p.width, p.height).Layout(Layout{ diff --git a/SliderWidgets.go b/SliderWidgets.go index aa20c1dc..ee11f2d5 100644 --- a/SliderWidgets.go +++ b/SliderWidgets.go @@ -68,6 +68,7 @@ func (s *SliderIntWidget) Labelf(format string, args ...any) *SliderIntWidget { func (s *SliderIntWidget) Build() { if s.width != 0 { PushItemWidth(s.width) + defer PopItemWidth() } @@ -217,6 +218,7 @@ func (sf *SliderFloatWidget) Labelf(format string, args ...any) *SliderFloatWidg func (sf *SliderFloatWidget) Build() { if sf.width != 0 { PushItemWidth(sf.width) + defer PopItemWidth() } diff --git a/SplitLayout.go b/SplitLayout.go index fde31193..fab7ead6 100644 --- a/SplitLayout.go +++ b/SplitLayout.go @@ -98,6 +98,7 @@ func (s *SplitLayoutWidget) Build() { if *s.sashPos >= availableW { *s.sashPos = availableW } + layout = Layout{ Row( s.buildChild(*s.sashPos, 0, s.layout1), @@ -163,5 +164,6 @@ func (s *SplitLayoutWidget) getState() (state *splitLayoutState) { state = &splitLayoutState{delta: 0.0} SetState(&Context, s.id, state) } + return state } diff --git a/StackWidget.go b/StackWidget.go index d76db976..99d8c327 100644 --- a/StackWidget.go +++ b/StackWidget.go @@ -40,9 +40,11 @@ func (s *StackWidget) Build() { // build invisible layouts with 0 alpha imgui.PushStyleVarFloat(imgui.StyleVarAlpha, 0) + for _, l := range layouts { SetCursorScreenPos(visiblePos) l.Build() } + imgui.PopStyleVar() } diff --git a/TableWidgets.go b/TableWidgets.go index ac18ff37..a02d59e3 100644 --- a/TableWidgets.go +++ b/TableWidgets.go @@ -136,6 +136,7 @@ func (t *TableWidget) FastMode(b bool) *TableWidget { func (t *TableWidget) Freeze(col, row int) *TableWidget { t.freezeColumn = col t.freezeRow = row + return t } @@ -184,6 +185,7 @@ func (t *TableWidget) Build() { for _, col := range t.columns { col.BuildTableColumn() } + imgui.TableHeadersRow() } diff --git a/TextWidgets.go b/TextWidgets.go index 589a3769..9e203eca 100644 --- a/TextWidgets.go +++ b/TextWidgets.go @@ -235,6 +235,7 @@ func (i *InputTextWidget) Build() { if i.width != 0 { PushItemWidth(i.width) + defer PopItemWidth() } @@ -349,6 +350,7 @@ func (i *InputIntWidget) OnChange(onChange func()) *InputIntWidget { func (i *InputIntWidget) Build() { if i.width != 0 { PushItemWidth(i.width) + defer PopItemWidth() } @@ -434,6 +436,7 @@ func (i *InputFloatWidget) StepSizeFast(stepFast float32) *InputFloatWidget { func (i *InputFloatWidget) Build() { if i.width != 0 { PushItemWidth(i.width) + defer PopItemWidth() } @@ -480,6 +483,7 @@ func (l *LabelWidget) Font(font *FontInfo) *LabelWidget { func (l *LabelWidget) Build() { if l.wrapped { PushTextWrapPos() + defer PopTextWrapPos() } diff --git a/Texture.go b/Texture.go index b3ab2cc4..db9f7e54 100644 --- a/Texture.go +++ b/Texture.go @@ -41,12 +41,14 @@ func NewTextureFromRgba(rgba image.Image, loadCallback func(*Texture)) { func loadTexture(rgba image.Image, loadCallback func(*Texture)) { go func() { Update() + result := mainthread.CallVal(func() any { texID, err := Context.renderer.LoadImage(ImageToRgba(rgba)) return &loadImageResult{id: texID, err: err} }) tid, ok := result.(*loadImageResult) + switch { case !ok: panic("giu: NewTextureFromRgba: unexpected error occurred") diff --git a/Utils.go b/Utils.go index 676dfef3..8260b602 100644 --- a/Utils.go +++ b/Utils.go @@ -44,6 +44,7 @@ func ImageToRgba(img image.Image) *image.RGBA { default: rgba := image.NewRGBA(trueImg.Bounds()) draw.Draw(rgba, trueImg.Bounds(), trueImg, image.Pt(0, 0), draw.Src) + return rgba } } @@ -53,6 +54,7 @@ func ToVec4Color(col color.Color) imgui.Vec4 { const mask = 0xffff r, g, b, a := col.RGBA() + return imgui.Vec4{ X: float32(r) / mask, Y: float32(g) / mask, diff --git a/Widgets.go b/Widgets.go index c5c87c58..2691ebb2 100644 --- a/Widgets.go +++ b/Widgets.go @@ -30,6 +30,7 @@ func Row(widgets ...Widget) *RowWidget { // Build implements Widget interface. func (l *RowWidget) Build() { isFirst := true + l.widgets.Range(func(w Widget) { switch w.(type) { case *TooltipWidget, @@ -630,6 +631,7 @@ func (t *TabBarWidget) Build() { for _, ti := range t.tabItems { ti.BuildTabItem() } + imgui.EndTabBar() } } diff --git a/Window.go b/Window.go index 3903a7d9..2a71633c 100644 --- a/Window.go +++ b/Window.go @@ -12,6 +12,7 @@ import ( func SingleWindow() *WindowWidget { size := Context.platform.DisplaySize() title := fmt.Sprintf("SingleWindow_%d", Context.GetWidgetIndex()) + return Window(title). Flags( imgui.WindowFlagsNoTitleBar| @@ -26,6 +27,7 @@ func SingleWindow() *WindowWidget { func SingleWindowWithMenuBar() *WindowWidget { size := Context.platform.DisplaySize() title := fmt.Sprintf("SingleWindow_%d", Context.GetWidgetIndex()) + return Window(title). Flags( imgui.WindowFlagsNoTitleBar| @@ -117,6 +119,7 @@ func (w *WindowWidget) Layout(widgets ...Widget) { if w.bringToFront { imgui.SetNextWindowFocus() + w.bringToFront = false }