Skip to content

Commit

Permalink
Linting fixes - mainly spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaloney committed Nov 26, 2022
1 parent 93764f0 commit 2660ed4
Show file tree
Hide file tree
Showing 24 changed files with 78 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Alignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions CSS.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -74,6 +76,7 @@ func ParseCSSStyleSheet(data []byte) error {
}

var styleColorID StyleColorID

err = panicToErr(func() {
styleColorID = StyleColorIDFromString(styleVarName)
})
Expand Down Expand Up @@ -104,6 +107,7 @@ func panicToErr(f func()) (err error) {
}()

f()

return err
}

Expand Down
1 change: 1 addition & 0 deletions ClickableWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ func (t *TreeNodeWidget) Build() {

if open {
t.layout.Build()

if (t.flags & imgui.TreeNodeFlagsNoTreePushOnOpen) == 0 {
imgui.TreePop()
}
Expand Down
1 change: 1 addition & 0 deletions CodeEditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,6 @@ func (ce *CodeEditorWidget) getState() (state *codeEditorState) {

SetState(&Context, ce.title, state)
}

return state
}
6 changes: 5 additions & 1 deletion Context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -133,6 +134,7 @@ func (c *context) GetState(id string) any {
s.valid = true
return s.data
}

return nil
}

Expand All @@ -142,12 +144,14 @@ func (c *context) load(id any) (*state, bool) {
return s, true
}
}

return nil, false
}

// Get widget index for current layout.
func (c *context) GetWidgetIndex() int {
i := c.widgetIndexCounter
c.widgetIndexCounter++

return i
}
4 changes: 4 additions & 0 deletions EventHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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()
}
}
Expand Down
14 changes: 14 additions & 0 deletions ExtraWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -244,6 +249,7 @@ func TreeTable() *TreeTableWidget {
func (tt *TreeTableWidget) Freeze(col, row int) *TreeTableWidget {
tt.freezeColumn = col
tt.freezeRow = row

return tt
}

Expand Down Expand Up @@ -287,6 +293,7 @@ func (tt *TreeTableWidget) Build() {
for _, col := range tt.columns {
col.BuildTableColumn()
}

imgui.TableHeadersRow()
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -532,6 +540,7 @@ func (d *DatePickerWidget) OnChange(onChange func()) *DatePickerWidget {
if onChange != nil {
d.onChange = onChange
}

return d
}

Expand All @@ -549,13 +558,15 @@ func (d *DatePickerWidget) getFormat() string {
if d.format == "" {
return "2006-01-02" // default
}

return d.format
}

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)
}

Expand All @@ -570,6 +581,7 @@ func (d *DatePickerWidget) Build() {

if d.width > 0 {
PushItemWidth(d.width)

defer PopItemWidth()
}

Expand Down Expand Up @@ -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++
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions FontAtlasProsessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions ImageWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,19 @@ 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
}

// Build implements Widget interface.
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
}
Expand All @@ -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()
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions ListClipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
3 changes: 3 additions & 0 deletions Markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ func (m *MarkdownWidget) Build() {

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{}
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions MasterWindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions Msgbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (ms *msgboxState) Dispose() {

func msgboxInvokeCallback(result DialogResult, callback DialogResultCallback) {
CloseCurrentPopup()

if callback != nil {
callback(result)
}
Expand Down Expand Up @@ -159,12 +160,14 @@ func Msgbox(title, content string) *MsgboxWidget {
func (m *MsgboxWidget) Buttons(buttons MsgboxButtons) *MsgboxWidget {
s := m.getState()
s.buttons = buttons

return m
}

// ResultCallback sets result callback.
func (m *MsgboxWidget) ResultCallback(cb DialogResultCallback) *MsgboxWidget {
s := m.getState()
s.resultCallback = cb

return m
}
Loading

0 comments on commit 2660ed4

Please sign in to comment.