Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(views): Reimplement views using slices #330

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions views.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,42 @@
package main

import (
"fmt"
"sync"
"log"

"github.com/aarzilli/nucular"
)

type ViewFunc func(ctx *ntcontext, w *nucular.Window)

type ViewStack struct {
stack [100]ViewFunc
sp int8
mu sync.Mutex
items []ViewFunc
}

func NewViewStack() *ViewStack {
return &ViewStack{sp: -1}
return &ViewStack{make([]ViewFunc, 0)}
}

func (v *ViewStack) Push(f ViewFunc) {
v.mu.Lock()
defer v.mu.Unlock()

v.stack[v.sp+1] = f
v.sp++
v.items = append(v.items, f)
}

func (v *ViewStack) Pop() (ViewFunc, error) {
v.mu.Lock()

if v.sp <= 0 {
return nil, fmt.Errorf("Cannot pop root element from ViewStack")
func (v *ViewStack) Pop() ViewFunc {
if len(v.items) == 0 {
log.Fatal("Tried to Pop an empty ViewStack")
}

defer (func() {
v.stack[v.sp] = nil
v.sp--
v.mu.Unlock()
})()
item := v.items[len(v.items)-1]

// The last item gets removed
v.items = v.items[:len(v.items)-1]

return v.stack[v.sp], nil
return item
}

func (v *ViewStack) Peek() ViewFunc {
v.mu.Lock()
defer v.mu.Unlock()
return v.stack[v.sp]
if len(v.items) == 0 {
log.Fatal("Tried to Peek an empty ViewStack")
}

return v.items[len(v.items)-1]
}