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

Popups workaround #565

Closed
gucio321 opened this issue Oct 6, 2022 · 4 comments
Closed

Popups workaround #565

gucio321 opened this issue Oct 6, 2022 · 4 comments
Labels
documentation Improvements or additions to documentation

Comments

@gucio321
Copy link
Collaborator

gucio321 commented Oct 6, 2022

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?

@gucio321
Copy link
Collaborator Author

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

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?

@gucio321
Copy link
Collaborator Author

and one more thing: popupModals cannot be definied inside MenuWidget, why?

@gucio321
Copy link
Collaborator Author

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

@gucio321 gucio321 changed the title POPUPS! they don't work! Popups workaround May 9, 2023
@gucio321 gucio321 added the documentation Improvements or additions to documentation label May 9, 2023
@gucio321
Copy link
Collaborator Author

will be added on wiki in FAQ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant