diff --git a/ClickableWidgets.go b/ClickableWidgets.go index 55124b37..e2bf6923 100644 --- a/ClickableWidgets.go +++ b/ClickableWidgets.go @@ -336,11 +336,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 diff --git a/CodeEditor.go b/CodeEditor.go index 0abcae4b..33b12ade 100644 --- a/CodeEditor.go +++ b/CodeEditor.go @@ -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 diff --git a/Context.go b/Context.go index 17d280f6..8baabd23 100644 --- a/Context.go +++ b/Context.go @@ -142,17 +142,17 @@ func (c *GIUContext) Backend() backend.Backend[glfwbackend.GLFWWindowFlags] { } // SetState is a generic version of Context.SetState. -func SetState[T any, PT genericDisposable[T]](c *GIUContext, id string, data PT) { +func SetState[T any, PT genericDisposable[T]](c *GIUContext, id ID, data PT) { c.state.Store(id, &state{valid: true, data: data}) } // SetState stores data in context by id. -func (c *GIUContext) SetState(id string, data Disposable) { +func (c *GIUContext) 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 *GIUContext, id string) PT { +func GetState[T any, PT genericDisposable[T]](c *GIUContext, id ID) PT { if s, ok := c.load(id); ok { c.m.Lock() s.valid = true @@ -168,7 +168,7 @@ func GetState[T any, PT genericDisposable[T]](c *GIUContext, id string) PT { } // GetState returns previously stored state by id. -func (c *GIUContext) GetState(id string) any { +func (c *GIUContext) GetState(id ID) any { if s, ok := c.load(id); ok { c.m.Lock() s.valid = true diff --git a/Context_test.go b/Context_test.go index 38a692d3..1e12ca9d 100644 --- a/Context_test.go +++ b/Context_test.go @@ -20,7 +20,7 @@ func (t *teststate2) Dispose() { func Test_SetGetState(t *testing.T) { tests := []struct { - id string + id ID data *teststate }{ {"nil", nil}, @@ -28,7 +28,7 @@ func Test_SetGetState(t *testing.T) { } 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) @@ -39,7 +39,7 @@ func Test_SetGetState(t *testing.T) { func Test_SetGetStateGeneric(t *testing.T) { tests := []struct { - id string + id ID data *teststate }{ {"nil", nil}, @@ -47,7 +47,7 @@ func Test_SetGetStateGeneric(t *testing.T) { } 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) @@ -57,7 +57,7 @@ func Test_SetGetStateGeneric(t *testing.T) { } func Test_SetGetWrongStateGeneric(t *testing.T) { - id := "id" + id := ID("id") data := &teststate{} ctx := GIUContext{} @@ -66,6 +66,7 @@ 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) } @@ -73,9 +74,9 @@ func Test_SetGetWrongStateGeneric(t *testing.T) { 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: {}, } diff --git a/EventHandler.go b/EventHandler.go index 417eed56..5327f99a 100644 --- a/EventHandler.go +++ b/EventHandler.go @@ -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 { diff --git a/ExtraWidgets.go b/ExtraWidgets.go index 7464262b..e6f7134c 100644 --- a/ExtraWidgets.go +++ b/ExtraWidgets.go @@ -314,9 +314,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 diff --git a/ImageWidgets.go b/ImageWidgets.go index 33262c64..4d6bd67c 100644 --- a/ImageWidgets.go +++ b/ImageWidgets.go @@ -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 @@ -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 } @@ -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 } @@ -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 @@ -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)}, diff --git a/Msgbox.go b/Msgbox.go index 7585da1f..c6abdd37 100644 --- a/Msgbox.go +++ b/Msgbox.go @@ -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 @@ -122,7 +122,7 @@ func PrepareMsgbox() Layout { state.m.Lock() if state.open { - OpenPopup(msgboxID) + OpenPopup(msgboxID.String()) state.open = false } diff --git a/ProgressIndicator.go b/ProgressIndicator.go index 75aea552..6669a2b5 100644 --- a/ProgressIndicator.go +++ b/ProgressIndicator.go @@ -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 @@ -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, diff --git a/SplitLayout.go b/SplitLayout.go index 290fa4fc..5bb4a68d 100644 --- a/SplitLayout.go +++ b/SplitLayout.go @@ -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 diff --git a/TextWidgets.go b/TextWidgets.go index 57cc2c19..99571a20 100644 --- a/TextWidgets.go +++ b/TextWidgets.go @@ -244,9 +244,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 { diff --git a/Window.go b/Window.go index 861dc119..6430a08c 100644 --- a/Window.go +++ b/Window.go @@ -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.