giu
giu copied to clipboard
Popups workaround
Hi there! I'm trying to make a PopupModal widget and... (again) I met an issue cpde:
package main
import "github.com/AllenDang/giu"
type settings struct {
id string
}
func newSettings() *settings {
return &settings{
id: "settings",
}
}
func (s *settings) Open() {
giu.OpenPopup(s.id)
}
func (s *settings) Build() {
giu.PopupModal(s.id).Layout(
giu.Button("close").OnClick(giu.CloseCurrentPopup),
).Build()
}
var settingsInstance *settings = newSettings()
func loop() {
giu.MainMenuBar().Layout(
giu.Menu("File").Layout(
settingsInstance,
giu.MenuItem("settings").OnClick(settingsInstance.Open),
),
).Build()
}
func main() {
wnd := giu.NewMasterWindow("popups!", 640, 480, 0)
wnd.Run(loop)
}
what is wrong?
okey, while working on constitutor I finally found it:
THE giu.OpenPopup(...) NEEDS TO BE CALLED IN THE SAME LAYOUT LEVEL AS POPUP DEFINITION
(I think it was already said somewhere but I didn't understand it :smile:)
here is my wrapper for PopupModal:
package app
import "github.com/AllenDang/giu"
type PopupModal struct {
id string
popup *giu.PopupModalWidget
isOpen bool
shouldOpen bool
}
func NewPopupModal(id string) *PopupModal {
return &PopupModal{
id: id,
popup: giu.PopupModal(id),
isOpen: false,
shouldOpen: false,
}
}
func (p *PopupModal) Open() {
if !p.isOpen {
p.shouldOpen = true
}
}
func (p *PopupModal) Close() {
if p.isOpen {
p.isOpen = false
p.shouldOpen = false
giu.CloseCurrentPopup()
}
}
func (p *PopupModal) Layout(l ...giu.Widget) *PopupModal {
p.popup.Layout(l...)
return p
}
func (p *PopupModal) Build() {
if p.shouldOpen {
p.isOpen = true
p.shouldOpen = false
giu.OpenPopup(p.id)
}
p.popup.Build()
}
you just call popup.Open() to open it
@AllenDang can I add it to giu?
and one more thing: popupModals cannot be definied inside MenuWidget, why?
ok, another imporvement - I baypassed imgui at all ;-)
package app
import (
"github.com/AllenDang/giu"
)
type PopupModal struct {
id string
popup *giu.PopupModalWidget
isOpenInGiu bool
isOpen bool
}
func NewPopupModal(id string) *PopupModal {
return &PopupModal{
id: id,
popup: giu.PopupModal(id),
isOpen: false,
}
}
func (p *PopupModal) Open() {
p.isOpen = true
}
func (p *PopupModal) Close() {
p.isOpen = false
}
func (p *PopupModal) Layout(l ...giu.Widget) *PopupModal {
p.popup.Layout(l...)
return p
}
func (p *PopupModal) Build() {
if !p.isOpen {
return
}
p.popup.IsOpen(&p.isOpen).Build()
if !p.isOpenInGiu {
giu.OpenPopup(p.id)
}
}
will be added on wiki in FAQ