Skip to content

Commit

Permalink
Merge pull request #918 from gucio321/event-tree-node
Browse files Browse the repository at this point in the history
TreeNode: allow pinning EventHandler to TreeNode
  • Loading branch information
gucio321 authored Nov 27, 2024
2 parents 127b189 + 6e13828 commit 9fe305a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
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

0 comments on commit 9fe305a

Please sign in to comment.