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

TreeNode: allow pinning EventHandler to TreeNode #918

Merged
merged 3 commits into from
Nov 27, 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
19 changes: 15 additions & 4 deletions ClickableWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,8 @@ type TreeNodeWidget struct {
label string
flags TreeNodeFlags
layout Layout
eventHandler func()
event func()
eventHandler *EventHandler
}

// TreeNode creates a new tree node widget.
Expand All @@ -531,10 +532,16 @@ func (t *TreeNodeWidget) Flags(flags TreeNodeFlags) *TreeNodeWidget {
return t
}

// Event create TreeNode with eventHandler
// Event create TreeNode with event handling function.
// You could detect events (e.g. IsItemClicked IsMouseDoubleClicked etc...) and handle them for TreeNode inside eventHandler.
// Deprecated: Use EventHandler instead!
// Deprecated: Use (*TreeNodeWidget).EventHandler instead!
func (t *TreeNodeWidget) Event(handler func()) *TreeNodeWidget {
t.event = handler
return t
}

// EventHandler allows to set *EventHandler instance for the actual TreeNode.
func (t *TreeNodeWidget) EventHandler(handler *EventHandler) *TreeNodeWidget {
t.eventHandler = handler
return t
}
Expand All @@ -549,8 +556,12 @@ func (t *TreeNodeWidget) Layout(widgets ...Widget) *TreeNodeWidget {
func (t *TreeNodeWidget) Build() {
open := imgui.TreeNodeExStrV(t.label, imgui.TreeNodeFlags(t.flags))

if t.event != nil {
t.event()
}

if t.eventHandler != nil {
t.eventHandler()
t.eventHandler.Build()
}

if open {
Expand Down
12 changes: 7 additions & 5 deletions examples/widgets/widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,13 @@ func loop() {
g.TreeNode("TreeNode with event handler").Layout(
g.Selectable("Selectable 1").OnClick(func() { fmt.Println(1) }),
g.Selectable("Selectable 2").OnClick(func() { fmt.Println(2) }),
).Event(func() {
if g.IsItemClicked(g.MouseButtonLeft) {
fmt.Println("Clicked")
}
}),
).EventHandler(g.Event().OnClick(g.MouseButtonLeft, func() {
fmt.Println("Clicked")
}).OnDClick(g.MouseButtonLeft, func() {
fmt.Println("Double-Clicked")
}).OnClick(g.MouseButtonRight, func() {
fmt.Println("Right-Clicked")
})),
),
g.TabItem("TreeTable").Layout(
g.TreeTable().
Expand Down
Loading