From 7835e3c344c9a54db19b2a1721cc74084907ed9f Mon Sep 17 00:00:00 2001 From: gucio321 Date: Wed, 27 Nov 2024 11:09:29 +0100 Subject: [PATCH 1/3] tree node: allow to pin Eventhandler to TreeNode This adds (*TreeNodeWidget).EventHandler(...) as an alternative to (*TreeNodeWidget).Event (which is marked depecated btw). fix #917 --- ClickableWidgets.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/ClickableWidgets.go b/ClickableWidgets.go index 64215600..9c992db0 100644 --- a/ClickableWidgets.go +++ b/ClickableWidgets.go @@ -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. @@ -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 } @@ -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 { From 5a422911f8193b8e8e18860e7baa1f8beb6e1c15 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Wed, 27 Nov 2024 11:09:50 +0100 Subject: [PATCH 2/3] examples/widgets: do not use depracted api for TreeNode events --- examples/widgets/widgets.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/widgets/widgets.go b/examples/widgets/widgets.go index 2b187e93..37b9bd46 100644 --- a/examples/widgets/widgets.go +++ b/examples/widgets/widgets.go @@ -6,6 +6,7 @@ import ( "image/color" "time" + "github.com/AllenDang/giu" g "github.com/AllenDang/giu" ) @@ -179,11 +180,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(giu.Event().OnClick(giu.MouseButtonLeft, func() { + fmt.Println("Clicked") + }).OnDClick(giu.MouseButtonLeft, func() { + fmt.Println("Double-Clicked") + }).OnClick(giu.MouseButtonRight, func() { + fmt.Println("Right-Clicked") + })), ), g.TabItem("TreeTable").Layout( g.TreeTable(). From 6e1382839ff6175cfa6c1bcae56cfb6db2dc9879 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Wed, 27 Nov 2024 11:13:35 +0100 Subject: [PATCH 3/3] fix lint error --- examples/widgets/widgets.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/widgets/widgets.go b/examples/widgets/widgets.go index 37b9bd46..67c191db 100644 --- a/examples/widgets/widgets.go +++ b/examples/widgets/widgets.go @@ -6,7 +6,6 @@ import ( "image/color" "time" - "github.com/AllenDang/giu" g "github.com/AllenDang/giu" ) @@ -180,11 +179,11 @@ 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) }), - ).EventHandler(giu.Event().OnClick(giu.MouseButtonLeft, func() { + ).EventHandler(g.Event().OnClick(g.MouseButtonLeft, func() { fmt.Println("Clicked") - }).OnDClick(giu.MouseButtonLeft, func() { + }).OnDClick(g.MouseButtonLeft, func() { fmt.Println("Double-Clicked") - }).OnClick(giu.MouseButtonRight, func() { + }).OnClick(g.MouseButtonRight, func() { fmt.Println("Right-Clicked") })), ),