Skip to content

Commit

Permalink
Add dragged event to the main API
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Apr 26, 2019
1 parent f7e12fc commit f247a77
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
6 changes: 6 additions & 0 deletions canvasobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ type Scrollable interface {
Scrolled(*ScrollEvent)
}

// Draggable indicates that a CanvasObject can be dragged.
// This is used for any item that the user has indicated should be moved across the screen.
type Draggable interface {
Dragged(*DragEvent)
}

// Focusable describes any CanvasObject that can respond to being focused.
// It will receive the FocusGained and FocusLost events appropriately.
// When focused it will also have TypedRune called as text is input and
Expand Down
37 changes: 25 additions & 12 deletions driver/gl/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ type window struct {
padded bool
visible bool

mousePos fyne.Position
mouseButton desktop.MouseButton
mouseOver desktop.Hoverable
onClosed func()
mousePos fyne.Position
mouseDragPos fyne.Position
mouseButton desktop.MouseButton
mouseOver desktop.Hoverable
onClosed func()

xpos, ypos int
ignoreResize bool
Expand Down Expand Up @@ -504,8 +505,9 @@ func (w *window) mouseMoved(viewport *glfw.Window, xpos float64, ypos float64) {
cursor = hyperlinkCursor
}

_, drag := object.(fyne.Draggable)
_, hover := object.(desktop.Hoverable)
return hover
return drag || hover
})

runOnMainAsync(func() {
Expand All @@ -529,13 +531,18 @@ func (w *window) mouseMoved(viewport *glfw.Window, xpos float64, ypos float64) {
w.mouseOver = obj
}
}
// TODO figure how far we dragged...
// TODO only if we have a mouse down
//if obj, ok := drag.(fyne.Dragable); ok {
// ev := new(fyne.DragEvent)
// ev.Position = fyne.NewPos(x, y)
// obj.Dragged(ev)
//}

if w.mouseButton > 0 {
if obj, ok := drag.(fyne.Draggable); ok {
ev := new(fyne.DragEvent)
ev.Position = fyne.NewPos(x, y)
ev.DraggedX = x - w.mouseDragPos.X
ev.DraggedY = y - w.mouseDragPos.Y
obj.Dragged(ev)

w.mouseDragPos = ev.Position
}
}
} else if w.mouseOver != nil {
w.mouseOver.MouseOut()
w.mouseOver = nil
Expand All @@ -548,6 +555,8 @@ func (w *window) mouseClicked(viewport *glfw.Window, button glfw.MouseButton, ac
return true
} else if _, ok := object.(fyne.Focusable); ok {
return true
} else if _, ok := object.(fyne.Draggable); ok {
return true
} else if _, ok := object.(desktop.Mouseable); ok {
return true
} else if _, ok := object.(desktop.Hoverable); ok {
Expand Down Expand Up @@ -596,6 +605,10 @@ func (w *window) mouseClicked(viewport *glfw.Window, button glfw.MouseButton, ac
go wid.Tapped(ev)
}
}
case fyne.Draggable:
if action == glfw.Press {
w.mouseDragPos = ev.Position
}
case fyne.Focusable:
if needsfocus == true {
w.canvas.Focus(wid)
Expand Down
7 changes: 7 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ type ScrollEvent struct {
PointEvent
DeltaX, DeltaY int
}

// DragEvent defines the parameters of a pointer or other drag event.
// The DraggedX and DraggedY fields show how far the item was dragged since the last event.
type DragEvent struct {
PointEvent
DraggedX, DraggedY int
}

0 comments on commit f247a77

Please sign in to comment.