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

Prevent drag and drop for hidden widgets #110

Merged
merged 1 commit into from
Feb 28, 2024

Conversation

mat007
Copy link
Contributor

@mat007 mat007 commented Dec 26, 2023

Hi!

This pull request fixes drag and drop callbacks being triggered for a widget with widget.Visibility_Hide.

The following diff adds basic drag&drop to the _examples/widget_demos/visibility demo to highlight the problem:

diff --git a/_examples/widget_demos/visibility/main.go b/_examples/widget_demos/visibility/main.go
index d46c46d..c7c30a2 100644
--- a/_examples/widget_demos/visibility/main.go
+++ b/_examples/widget_demos/visibility/main.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"fmt"
 	"image/color"
 	"log"
 
@@ -18,6 +19,14 @@ type game struct {
 	ui *ebitenui.UI
 }
 
+type drag struct {
+}
+
+func (d drag) Create(hasWidget widget.HasWidget) (*widget.Container, any) {
+	fmt.Println("create drag from yellow container")
+	return widget.NewContainer(), nil
+}
+
 func main() {
 	// load images for button states: idle, hover, and pressed
 	buttonImage, _ := loadButtonImage()
@@ -53,7 +62,11 @@ func main() {
 			widget.WidgetOpts.LayoutData(widget.RowLayoutData{
 				Stretch: true,
 			}),
-		),
+			widget.WidgetOpts.EnableDragAndDrop(
+				widget.NewDragAndDrop(
+					widget.DragAndDropOpts.ContentsCreater(&drag{}),
+				),
+		)),
 	)
 
 	// construct a new container for middle middle of window
@@ -92,6 +105,13 @@ func main() {
 				Stretch: true,
 			}),
 			widget.WidgetOpts.MinSize(100, 100),
+			widget.WidgetOpts.CanDrop(func(args *widget.DragAndDropDroppedEventArgs) bool {
+				fmt.Println("can drop on blue container")
+				return true
+			}),
+			widget.WidgetOpts.Dropped(func(args *widget.DragAndDropDroppedEventArgs) {
+				fmt.Println("dropped on blue container")
+			}),
 		),
 	)
 

Running go run ./_examples/widget_demos/visibility/, and dragging from the yellow to the blue container prints the expected outputs.
Then clicking on the button to hide the blue container, and dragging again from the yellow container to where the blue one was, still incorrectly shows the same outputs: it shouldn’t because the blue container is now hidden.

drag-hidden-widget

We’re filtering out hidden target elements here instead of when collecting available drop targets, because a widget visibility can be changed while the drag and drop is in progress. If we only checked the visibility once when creating the drag and drop state, we would not be able to handle that use case.
@mcarpenter622 mcarpenter622 merged commit 6e5c17d into ebitenui:master Feb 28, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants