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

Close ephemeral on widget removal #121

Merged
merged 4 commits into from
Mar 6, 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Ebiten UI

**A user interface engine and widget library for [Ebitengine](https://ebitengine.org/)**

**>> Note: This library is separate from [Ebitengine](https://ebitengine.org/). Please reach out to the linked Discord server for questions or suggestions**

Ebiten UI is an extension to Ebitengine that provides the ability to render a complete user interface,
with widgets such as buttons, lists, combo boxes, and so on. It uses the [retained mode] model.
All graphics used by Ebiten UI can be fully customized, so you can really make your UI your own.
Expand Down
17 changes: 9 additions & 8 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,8 @@ func (u *UI) render(screen *ebiten.Image) {
widget.RenderWithDeferred(screen, u.renderers)
}

// AddWindow adds window w to u for rendering. It returns a function to remove w from u.
// AddWindow adds window w to ui for rendering. It returns a function to remove w from ui.
func (u *UI) AddWindow(w *widget.Window) widget.RemoveWindowFunc {
closeFunc := func() {
u.removeWindow(w)
}

if u.addWindow(w) {
w.GetContainer().GetWidget().ContextMenuEvent.AddHandler(u.handleContextMenu)
w.GetContainer().GetWidget().FocusEvent.AddHandler(u.handleFocusEvent)
Expand All @@ -265,22 +261,27 @@ func (u *UI) AddWindow(w *widget.Window) widget.RemoveWindowFunc {
if w.Modal && u.focusedWidget != nil {
u.focusedWidget.(widget.Focuser).Focus(false)
}

w.SetCloseFunction(closeFunc)
}

return closeFunc
return w.GetCloseFunction()
}

func (u *UI) addWindow(w *widget.Window) bool {
if u.IsWindowOpen(w) {
return false
}

closeFunc := func() {
u.removeWindow(w)
}
w.SetCloseFunction(closeFunc)

u.windows = append(u.windows, w)

sort.SliceStable(u.windows, func(i, j int) bool {
return u.windows[i].DrawLayer < u.windows[j].DrawLayer
})

return true
}

Expand Down
26 changes: 25 additions & 1 deletion widget/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,36 @@ func (c *Container) RemoveChild(child PreferredSizeLocateableWidget) {

child.GetWidget().parent = nil

if child.GetWidget().ToolTip != nil && child.GetWidget().ToolTip.window != nil {
child.GetWidget().ToolTip.window.Close()
}

if child.GetWidget().DragAndDrop != nil && child.GetWidget().DragAndDrop.window != nil {
child.GetWidget().DragAndDrop.window.Close()
}

if child.GetWidget().ContextMenuWindow != nil {
child.GetWidget().ContextMenuWindow.Close()
}
c.RequestRelayout()
}

func (c *Container) RemoveChildren() {
for i := range c.children {
c.children[i].GetWidget().parent = nil
childWidget := c.children[i].GetWidget()
childWidget.parent = nil

if childWidget.ToolTip != nil && childWidget.ToolTip.window != nil {
childWidget.ToolTip.window.Close()
}

if childWidget.DragAndDrop != nil && childWidget.DragAndDrop.window != nil {
childWidget.DragAndDrop.window.Close()
}

if childWidget.ContextMenuWindow != nil {
childWidget.ContextMenuWindow.Close()
}
}
c.children = nil

Expand Down
5 changes: 5 additions & 0 deletions widget/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ func (w *Window) SetCloseFunction(close RemoveWindowFunc) {
w.closeFunc = close
}

// Typically used internally.
func (w *Window) GetCloseFunction() RemoveWindowFunc {
return w.closeFunc
}

// Typically used internally
func (w *Window) RequestRelayout() {
w.container.RequestRelayout()
Expand Down
Loading