-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathPopups.go
More file actions
136 lines (111 loc) · 2.91 KB
/
Popups.go
File metadata and controls
136 lines (111 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package giu
import (
"fmt"
"github.com/AllenDang/cimgui-go/imgui"
)
// OpenPopup opens a popup with specified id.
func OpenPopup(name string) {
SetState[popupState](Context, popupStateID(name), &popupState{open: true})
}
// CloseCurrentPopup closes currently opened popup.
// If no popups opened, no action will be taken.
func CloseCurrentPopup() {
imgui.CloseCurrentPopup()
}
func popupStateID(name string) ID {
return ID(fmt.Sprintf("%s##state", name))
}
func applyPopupState(name string) {
var state *popupState
if state = GetState[popupState](Context, popupStateID(name)); state == nil {
state = &popupState{open: false}
}
if state.open {
imgui.OpenPopupStr(name)
SetState[popupState](Context, popupStateID(name), &popupState{open: false})
}
}
var _ Disposable = &popupState{}
type popupState struct {
open bool
}
func (s *popupState) Dispose() {
// noop
}
var _ Widget = &PopupWidget{}
// PopupWidget is a window which appears next to the mouse cursor.
// For instance it is used to display color palette in ColorSelectWidget.
type PopupWidget struct {
name string
flags WindowFlags
layout Layout
}
// Popup creates new popup widget.
func Popup(name string) *PopupWidget {
return &PopupWidget{
name: name,
flags: 0,
layout: nil,
}
}
// Flags sets popup's flags.
func (p *PopupWidget) Flags(flags WindowFlags) *PopupWidget {
p.flags = flags
return p
}
// Layout sets popup's layout.
func (p *PopupWidget) Layout(widgets ...Widget) *PopupWidget {
p.layout = Layout(widgets)
return p
}
// Build implements Widget interface.
func (p *PopupWidget) Build() {
applyPopupState(p.name)
if imgui.BeginPopupV(p.name, imgui.WindowFlags(p.flags)) {
p.layout.Build()
imgui.EndPopup()
}
}
var _ Widget = &PopupModalWidget{}
// PopupModalWidget is a popup window that block every interactions behind it, cannot be closed by
// user, adds a dimming background, has a title bar.
type PopupModalWidget struct {
name string
open *bool
flags WindowFlags
layout Layout
}
// PopupModal creates new popup modal widget.
func PopupModal(name string) *PopupModalWidget {
return &PopupModalWidget{
name: name,
open: nil,
flags: WindowFlagsNoResize,
layout: nil,
}
}
// IsOpen allows to control popup's state
// NOTE: changing opens' value will not result in changing popup's state
// if OpenPopup(...) wasn't called!
func (p *PopupModalWidget) IsOpen(open *bool) *PopupModalWidget {
p.open = open
return p
}
// Flags allows to specify popup's flags.
func (p *PopupModalWidget) Flags(flags WindowFlags) *PopupModalWidget {
p.flags = flags
return p
}
// Layout sets layout.
func (p *PopupModalWidget) Layout(widgets ...Widget) *PopupModalWidget {
p.layout = Layout(widgets)
return p
}
// Build implements Widget interface.
func (p *PopupModalWidget) Build() {
applyPopupState(p.name)
if imgui.BeginPopupModalV(p.name, p.open, imgui.WindowFlags(p.flags)) {
p.layout.Build()
imgui.EndPopup()
}
}