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

context: use ID as type of id arg in Set/GetState #866

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions ClickableWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,11 @@ func (b *ImageButtonWithRgbaWidget) FramePadding(padding int) *ImageButtonWithRg

// Build implements Widget interface.
func (b *ImageButtonWithRgbaWidget) Build() {
if state := GetState[imageState](Context, b.id.String()); state == nil {
SetState(Context, b.id.String(), &imageState{})
if state := GetState[imageState](Context, b.id); state == nil {
SetState(Context, b.id, &imageState{})

NewTextureFromRgba(b.rgba, func(tex *Texture) {
SetState(Context, b.id.String(), &imageState{texture: tex})
SetState(Context, b.id, &imageState{texture: tex})
})
} else {
b.ImageButtonWidget.texture = state.texture
Expand Down
4 changes: 2 additions & 2 deletions CodeEditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ func (ce *CodeEditorWidget) Build() {
}

func (ce *CodeEditorWidget) getState() (state *codeEditorState) {
if state = GetState[codeEditorState](Context, ce.title.String()); state == nil {
if state = GetState[codeEditorState](Context, ce.title); state == nil {
state = &codeEditorState{
// editor: imgui.NewTextEditor(),
}

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

return state
Expand Down
8 changes: 4 additions & 4 deletions Context.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@ func (c *context) Backend() backend.Backend[glfwbackend.GLFWWindowFlags] {
}

// SetState is a generic version of Context.SetState.
func SetState[T any, PT genericDisposable[T]](c *context, id string, data PT) {
func SetState[T any, PT genericDisposable[T]](c *context, id ID, 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) {
func (c *context) SetState(id ID, data Disposable) {
c.state.Store(id, &state{valid: true, data: data})
}

// GetState is a generic version of Context.GetState.
func GetState[T any, PT genericDisposable[T]](c *context, id string) PT {
func GetState[T any, PT genericDisposable[T]](c *context, id ID) PT {
if s, ok := c.load(id); ok {
c.m.Lock()
s.valid = true
Expand All @@ -165,7 +165,7 @@ func GetState[T any, PT genericDisposable[T]](c *context, id string) PT {
}

// GetState returns previously stored state by id.
func (c *context) GetState(id string) any {
func (c *context) GetState(id ID) any {
if s, ok := c.load(id); ok {
c.m.Lock()
s.valid = true
Expand Down
17 changes: 9 additions & 8 deletions Context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func (t *teststate2) Dispose() {

func Test_SetGetState(t *testing.T) {
tests := []struct {
id string
id ID
data *teststate
}{
{"nil", nil},
{"pointer", &teststate{}},
}

for _, tc := range tests {
t.Run(tc.id, func(t *testing.T) {
t.Run(tc.id.String(), func(t *testing.T) {
ctx := CreateContext(nil)
SetState(ctx, tc.id, tc.data)
restored := GetState[teststate](ctx, tc.id)
Expand All @@ -39,15 +39,15 @@ func Test_SetGetState(t *testing.T) {

func Test_SetGetStateGeneric(t *testing.T) {
tests := []struct {
id string
id ID
data *teststate
}{
{"nil", nil},
{"pointer", &teststate{}},
}

for _, tc := range tests {
t.Run(tc.id, func(t *testing.T) {
t.Run(tc.id.String(), func(t *testing.T) {
ctx := CreateContext(nil)
SetState(ctx, tc.id, tc.data)
restored := GetState[teststate](ctx, tc.id)
Expand All @@ -57,7 +57,7 @@ func Test_SetGetStateGeneric(t *testing.T) {
}

func Test_SetGetWrongStateGeneric(t *testing.T) {
id := "id"
id := ID("id")
data := &teststate{}
ctx := context{}

Expand All @@ -66,16 +66,17 @@ func Test_SetGetWrongStateGeneric(t *testing.T) {
t.Errorf("expected code to assert to panic, but it didn't")
}
}()

SetState(&ctx, id, data)
GetState[teststate2](&ctx, id)
}

func Test_invalidState(t *testing.T) {
ctx := CreateContext(nil)

state1ID := "state1"
state2ID := "state2"
states := map[string]*teststate{
state1ID := ID("state1")
state2ID := ID("state2")
states := map[ID]*teststate{
state1ID: {},
state2ID: {},
}
Expand Down
4 changes: 2 additions & 2 deletions EventHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ func (eh *EventHandler) Build() {
var state *eventHandlerState

stateID := GenAutoID("eventHandlerState")
if state = GetState[eventHandlerState](Context, stateID.String()); state == nil {
if state = GetState[eventHandlerState](Context, stateID); state == nil {
state = &eventHandlerState{}
SetState(Context, stateID.String(), state)
SetState(Context, stateID, state)
}

if eh.onActivate != nil && isActive && !state.isActive {
Expand Down
4 changes: 2 additions & 2 deletions ExtraWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ func (l *ListBoxWidget) Build() {
selectedIndex := l.selectedIndex
if selectedIndex == nil {
var state *listBoxState
if state = GetState[listBoxState](Context, l.id.String()); state == nil {
if state = GetState[listBoxState](Context, l.id); state == nil {
state = &listBoxState{selectedIndex: 0}
SetState(Context, l.id.String(), state)
SetState(Context, l.id, state)
}

selectedIndex = &state.selectedIndex
Expand Down
14 changes: 7 additions & 7 deletions ImageWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ func (i *ImageWithRgbaWidget) OnClick(cb func()) *ImageWithRgbaWidget {
func (i *ImageWithRgbaWidget) Build() {
if i.rgba != nil {
var imgState *imageState
if imgState = GetState[imageState](Context, i.id.String()); imgState == nil || !reflect.DeepEqual(i.rgba, imgState.img) {
if imgState = GetState[imageState](Context, i.id); imgState == nil || !reflect.DeepEqual(i.rgba, imgState.img) {
imgState = &imageState{}
SetState(Context, i.id.String(), imgState)
SetState(Context, i.id, imgState)

NewTextureFromRgba(i.rgba, func(tex *Texture) {
imgState.texture = tex
Expand All @@ -200,7 +200,7 @@ var _ Widget = &ImageWithFileWidget{}
// because files are not included in executable binaries!
// You may want to use "embed" package and ImageWithRgba instead.
type ImageWithFileWidget struct {
id string
id ID
imgPath string
img *ImageWidget
}
Expand All @@ -210,14 +210,14 @@ type ImageWithFileWidget struct {
// to set a specific size, use .Size(width, height).
func ImageWithFile(imgPath string) *ImageWithFileWidget {
return &ImageWithFileWidget{
id: fmt.Sprintf("ImageWithFile_%s", imgPath),
id: ID(fmt.Sprintf("ImageWithFile_%s", imgPath)),
imgPath: imgPath,
img: Image(nil),
}
}

// ID sets the interval id of ImageWithFile widgets.
func (i *ImageWithFileWidget) ID(id string) *ImageWithFileWidget {
func (i *ImageWithFileWidget) ID(id ID) *ImageWithFileWidget {
i.id = id
return i
}
Expand Down Expand Up @@ -259,7 +259,7 @@ var _ Widget = &ImageWithURLWidget{}
// ImageWithURLWidget allows to display an image using
// an URL as image source.
type ImageWithURLWidget struct {
id string
id ID
imgURL string
downloadTimeout time.Duration
whenLoading Layout
Expand All @@ -274,7 +274,7 @@ type ImageWithURLWidget struct {
// to set a specific size, use .Size(width, height).
func ImageWithURL(url string) *ImageWithURLWidget {
return &ImageWithURLWidget{
id: fmt.Sprintf("ImageWithURL_%s", url),
id: ID(fmt.Sprintf("ImageWithURL_%s", url)),
imgURL: url,
downloadTimeout: 10 * time.Second,
whenLoading: Layout{Dummy(100, 100)},
Expand Down
4 changes: 2 additions & 2 deletions Msgbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func buildMsgboxButtons(buttons MsgboxButtons, callback DialogResultCallback) La
}
}

const msgboxID string = "###Msgbox"
const msgboxID ID = "###Msgbox"

// PrepareMsgbox should be invoked in function in the same layout level where you call g.Msgbox.
// BUG: calling this more than 1 time per frame causes unexpected
Expand All @@ -122,7 +122,7 @@ func PrepareMsgbox() Layout {

state.m.Lock()
if state.open {
OpenPopup(msgboxID)
OpenPopup(msgboxID.String())
state.open = false
}

Expand Down
4 changes: 2 additions & 2 deletions ProgressIndicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var _ Widget = &ProgressIndicatorWidget{}
// ProgressIndicatorWidget represents progress indicator widget
// see examples/extrawidgets/.
type ProgressIndicatorWidget struct {
internalID string
internalID ID
width float32
height float32
radius float32
Expand All @@ -65,7 +65,7 @@ type ProgressIndicatorWidget struct {
// ProgressIndicator creates a new ProgressIndicatorWidget.
func ProgressIndicator(label string, width, height, radius float32) *ProgressIndicatorWidget {
return &ProgressIndicatorWidget{
internalID: "###giu-progress-indicator",
internalID: GenAutoID("###giu-progress-indicator"),
width: width,
height: height,
radius: radius,
Expand Down
4 changes: 2 additions & 2 deletions SplitLayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ func (s *SplitLayoutWidget) buildChild(width, height float32, layout Widget) Wid
}

func (s *SplitLayoutWidget) getState() (state *splitLayoutState) {
if state = GetState[splitLayoutState](Context, s.id.String()); state == nil {
if state = GetState[splitLayoutState](Context, s.id); state == nil {
state = &splitLayoutState{delta: 0.0}
SetState(Context, s.id.String(), state)
SetState(Context, s.id, state)
}

return state
Expand Down
4 changes: 2 additions & 2 deletions TextWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ func (i *InputTextWidget) OnChange(onChange func()) *InputTextWidget {
func (i *InputTextWidget) Build() {
// Get state
var state *inputTextState
if state = GetState[inputTextState](Context, i.label.String()); state == nil {
if state = GetState[inputTextState](Context, i.label); state == nil {
state = &inputTextState{}
SetState(Context, i.label.String(), state)
SetState(Context, i.label, state)
}

if i.width != 0 {
Expand Down
4 changes: 2 additions & 2 deletions Window.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ func (w *WindowWidget) RegisterKeyboardShortcuts(s ...WindowShortcut) *WindowWid
return w
}

func (w *WindowWidget) getStateID() string {
return fmt.Sprintf("%s_windowState", w.title)
func (w *WindowWidget) getStateID() ID {
return ID(fmt.Sprintf("%s_windowState", w.title))
}

// returns window state.
Expand Down
Loading