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

Bugfix: 922 #945

Merged
merged 3 commits into from May 7, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 17 additions & 30 deletions internal/driver/glfw/window.go
Expand Up @@ -472,15 +472,8 @@ func (w *window) refresh(viewport *glfw.Window) {
w.canvas.setDirty(true)
}

func (w *window) findObjectAtPositionMatching(canvas *glCanvas, mouse fyne.Position,
matches func(object fyne.CanvasObject) bool) (fyne.CanvasObject, fyne.Position) {
roots := []fyne.CanvasObject{canvas.content}

if canvas.menu != nil {
roots = []fyne.CanvasObject{canvas.menu, canvas.content}
}

return driver.FindObjectAtPositionMatching(mouse, matches, canvas.Overlays().Top(), roots...)
func (w *window) findObjectAtPositionMatching(canvas *glCanvas, mouse fyne.Position, matches func(object fyne.CanvasObject) bool) (fyne.CanvasObject, fyne.Position, int) {
return driver.FindObjectAtPositionMatching(mouse, matches, canvas.Overlays().Top(), canvas.menu, canvas.content)
}

func fyneToNativeCursor(cursor desktop.Cursor) *glfw.Cursor {
Expand All @@ -495,7 +488,7 @@ func (w *window) mouseMoved(viewport *glfw.Window, xpos float64, ypos float64) {
w.mousePos = fyne.NewPos(internal.UnscaleInt(w.canvas, int(xpos)), internal.UnscaleInt(w.canvas, int(ypos)))

cursor := cursorMap[desktop.DefaultCursor]
obj, pos := w.findObjectAtPositionMatching(w.canvas, w.mousePos, func(object fyne.CanvasObject) bool {
obj, pos, _ := w.findObjectAtPositionMatching(w.canvas, w.mousePos, func(object fyne.CanvasObject) bool {
if cursorable, ok := object.(desktop.Cursorable); ok {
fyneCursor := cursorable.Cursor()
cursor = fyneToNativeCursor(fyneCursor)
Expand Down Expand Up @@ -567,18 +560,9 @@ func (w *window) mouseOut() {
}

func (w *window) mouseClicked(_ *glfw.Window, btn glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
co, pos := w.findObjectAtPositionMatching(w.canvas, w.mousePos, func(object fyne.CanvasObject) bool {
if _, ok := object.(fyne.Tappable); ok {
return true
} else if _, ok := object.(fyne.SecondaryTappable); ok {
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 {
co, pos, layer := w.findObjectAtPositionMatching(w.canvas, w.mousePos, func(object fyne.CanvasObject) bool {
switch object.(type) {
case fyne.Tappable, fyne.SecondaryTappable, fyne.Focusable, fyne.Draggable, desktop.Mouseable, desktop.Hoverable:
return true
}

Expand Down Expand Up @@ -609,13 +593,16 @@ func (w *window) mouseClicked(_ *glfw.Window, btn glfw.MouseButton, action glfw.
}
}

needsfocus := true
wid := w.canvas.Focused()
if wid != nil {
if wid.(fyne.CanvasObject) != co {
w.canvas.Unfocus()
} else {
needsfocus = false
needsfocus := false
if layer != 1 { // 0 - overlay, 1 - menu, 2 - content
needsfocus = true

if wid := w.canvas.Focused(); wid != nil {
if wid.(fyne.CanvasObject) != co {
w.canvas.Unfocus()
} else {
needsfocus = false
}
}
}

Expand Down Expand Up @@ -684,7 +671,7 @@ func (w *window) mouseClicked(_ *glfw.Window, btn glfw.MouseButton, action glfw.
}

func (w *window) mouseScrolled(viewport *glfw.Window, xoff float64, yoff float64) {
co, _ := w.findObjectAtPositionMatching(w.canvas, w.mousePos, func(object fyne.CanvasObject) bool {
co, _, _ := w.findObjectAtPositionMatching(w.canvas, w.mousePos, func(object fyne.CanvasObject) bool {
_, ok := object.(fyne.Scrollable)
return ok
})
Expand Down
28 changes: 15 additions & 13 deletions internal/driver/gomobile/canvas.go
Expand Up @@ -225,9 +225,9 @@ func (c *mobileCanvas) walkTree(
}
}

func (c *mobileCanvas) findObjectAtPositionMatching(pos fyne.Position, test func(object fyne.CanvasObject) bool) (fyne.CanvasObject, fyne.Position) {
if c.menu != nil && c.overlays.Top() == nil {
return driver.FindObjectAtPositionMatching(pos, test, c.menu)
func (c *mobileCanvas) findObjectAtPositionMatching(pos fyne.Position, test func(object fyne.CanvasObject) bool) (fyne.CanvasObject, fyne.Position, int) {
if c.menu != nil {
return driver.FindObjectAtPositionMatching(pos, test, c.overlays.Top(), c.menu)
}

return driver.FindObjectAtPositionMatching(pos, test, c.overlays.Top(), c.windowHead, c.content)
Expand All @@ -238,7 +238,7 @@ func (c *mobileCanvas) tapDown(pos fyne.Position, tapID int) {
c.lastTapDownPos[tapID] = pos
c.dragging = nil

co, objPos := c.findObjectAtPositionMatching(pos, func(object fyne.CanvasObject) bool {
co, objPos, layer := c.findObjectAtPositionMatching(pos, func(object fyne.CanvasObject) bool {
if _, ok := object.(fyne.Tappable); ok {
return true
} else if _, ok := object.(mobile.Touchable); ok {
Expand All @@ -258,13 +258,15 @@ func (c *mobileCanvas) tapDown(pos fyne.Position, tapID int) {
c.touched[tapID] = wid
}

needsFocus := true
wid := c.Focused()
if wid != nil {
if wid.(fyne.CanvasObject) != co {
c.Unfocus()
} else {
needsFocus = false
needsFocus := false
if layer != 1 { // 0 - overlay, 1 - window head / menu, 2 - content
needsFocus = true
if wid := c.Focused(); wid != nil {
if wid.(fyne.CanvasObject) != co {
c.Unfocus()
} else {
needsFocus = false
}
}
}
if wid, ok := co.(fyne.Focusable); ok && needsFocus {
Expand All @@ -284,7 +286,7 @@ func (c *mobileCanvas) tapMove(pos fyne.Position, tapID int,
}
c.lastTapDownPos[tapID] = pos

co, objPos := c.findObjectAtPositionMatching(pos, func(object fyne.CanvasObject) bool {
co, objPos, _ := c.findObjectAtPositionMatching(pos, func(object fyne.CanvasObject) bool {
if _, ok := object.(fyne.Draggable); ok {
return true
} else if _, ok := object.(mobile.Touchable); ok {
Expand Down Expand Up @@ -339,7 +341,7 @@ func (c *mobileCanvas) tapUp(pos fyne.Position, tapID int,
return
}

co, objPos := c.findObjectAtPositionMatching(pos, func(object fyne.CanvasObject) bool {
co, objPos, _ := c.findObjectAtPositionMatching(pos, func(object fyne.CanvasObject) bool {
if _, ok := object.(fyne.Tappable); ok {
return true
} else if _, ok := object.(fyne.SecondaryTappable); ok {
Expand Down
7 changes: 4 additions & 3 deletions internal/driver/util.go
Expand Up @@ -93,8 +93,7 @@ func walkObjectTree(
// FindObjectAtPositionMatching is used to find an object in a canvas at the specified position.
// The matches function determines of the type of object that is found at this position is of a suitable type.
// The various canvas roots and overlays that can be searched are also passed in.
func FindObjectAtPositionMatching(mouse fyne.Position, matches func(object fyne.CanvasObject) bool,
overlay fyne.CanvasObject, roots ...fyne.CanvasObject) (fyne.CanvasObject, fyne.Position) {
func FindObjectAtPositionMatching(mouse fyne.Position, matches func(object fyne.CanvasObject) bool, overlay fyne.CanvasObject, roots ...fyne.CanvasObject) (fyne.CanvasObject, fyne.Position, int) {
var found fyne.CanvasObject
var foundPos fyne.Position

Expand Down Expand Up @@ -126,10 +125,12 @@ func FindObjectAtPositionMatching(mouse fyne.Position, matches func(object fyne.
return false
}

layer := 0
if overlay != nil {
WalkVisibleObjectTree(overlay, findFunc, nil)
} else {
for _, root := range roots {
layer++
if root == nil {
continue
}
Expand All @@ -140,7 +141,7 @@ func FindObjectAtPositionMatching(mouse fyne.Position, matches func(object fyne.
}
}

return found, foundPos
return found, foundPos, layer
}

// AbsolutePositionForObject returns the absolute position of an object in a set of object trees.
Expand Down