Skip to content

Popups workaround #565

Closed
Closed

Description

@gucio321

highlihgts:

  • remember to open popup in the same level it is defined
  • IsOpen should generally be only getter (changing respective variable's value will not cause popup open/close)
  • there is a bypass code below: Popups workaround #565 (comment)

tl;dr:

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?

Activity

  1. gucio321 commented on Oct 16, 2022

    @gucio321
    CollaboratorAuthor

    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?

  2. gucio321 commented on Oct 16, 2022

    @gucio321
    CollaboratorAuthor

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

  3. gucio321 commented on Oct 16, 2022

    @gucio321
    CollaboratorAuthor

    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)
    	}
    }
  4. changed the title [-]POPUPS! they don't work![/-] [+]Popups workaround[/+] on May 9, 2023
  5. gucio321 commented on Jul 10, 2024

    @gucio321
    CollaboratorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址