Skip to content

Commit

Permalink
add custom MouseBack/Forward APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Mar 13, 2024
1 parent 8f22cd7 commit 5ecd928
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
4 changes: 4 additions & 0 deletions canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type Canvas interface {
AddShortcut(shortcut Shortcut, handler func(shortcut Shortcut))
RemoveShortcut(shortcut Shortcut)

// Supersonic-specific extensions
SetOnMouseBack(func())
SetOnMouseForward(func())

Capture() image.Image

// PixelCoordinateForPosition returns the x and y pixel coordinate for a given position on this canvas.
Expand Down
10 changes: 10 additions & 0 deletions driver/desktop/mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ const (
// Since: 2.0
MouseButtonTertiary

// MouseButtonBack is the back mouse button
//
// Since: <Supersonic specific extension>
MouseButtonBack

// MouseButtonForward is the back mouse button
//
// Since: <Supersonic specific extension>
MouseButtonForward

// LeftMouseButton is the most common mouse button - on some systems the only one.
//
// Deprecated: use MouseButtonPrimary which will adapt to mouse configuration.
Expand Down
18 changes: 14 additions & 4 deletions internal/driver/glfw/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ type glCanvas struct {
padded, debug bool
size fyne.Size

onTypedRune func(rune)
onTypedKey func(*fyne.KeyEvent)
onKeyDown func(*fyne.KeyEvent)
onKeyUp func(*fyne.KeyEvent)
onTypedRune func(rune)
onTypedKey func(*fyne.KeyEvent)
onKeyDown func(*fyne.KeyEvent)
onKeyUp func(*fyne.KeyEvent)
onMouseBack func()
onMouseForward func()
// shortcut fyne.ShortcutHandler

scale, detectedScale, texScale float32
Expand Down Expand Up @@ -178,6 +180,14 @@ func (c *glCanvas) SetOnTypedRune(typed func(rune)) {
c.onTypedRune = typed
}

func (c *glCanvas) SetOnMouseBack(fn func()) {
c.onMouseBack = fn
}

func (c *glCanvas) SetOnMouseForward(fn func()) {
c.onMouseForward = fn
}

func (c *glCanvas) SetPadded(padded bool) {
c.Lock()
content := c.content
Expand Down
9 changes: 9 additions & 0 deletions internal/driver/glfw/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,15 @@ func (w *window) processMouseClicked(button desktop.MouseButton, action action,
w.mouseLock.Unlock()
}

if button == desktop.MouseButtonBack || button == desktop.MouseButtonForward {
if w.canvas.onMouseBack != nil && button == desktop.MouseButtonBack {
w.canvas.onMouseBack()
} else if w.canvas.onMouseForward != nil && button == desktop.MouseButtonForward {
w.canvas.onMouseForward()
}
return
}

co, pos, _ := w.findObjectAtPositionMatching(w.canvas, mousePos, func(object fyne.CanvasObject) bool {
switch object.(type) {
case fyne.Tappable, fyne.SecondaryTappable, fyne.DoubleTappable, fyne.Focusable, desktop.Mouseable, desktop.Hoverable:
Expand Down
4 changes: 4 additions & 0 deletions internal/driver/glfw/window_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ func convertMouseButton(btn glfw.MouseButton, mods glfw.ModifierKey) (desktop.Mo
button = desktop.MouseButtonSecondary
case glfw.MouseButton3:
button = desktop.MouseButtonTertiary
case glfw.MouseButton4:
button = desktop.MouseButtonBack
case glfw.MouseButton5:
button = desktop.MouseButtonForward
}
return button, modifier
}
Expand Down
4 changes: 4 additions & 0 deletions internal/driver/mobile/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func (c *mobileCanvas) SetOnTypedRune(typed func(rune)) {
c.onTypedRune = typed
}

func (*mobileCanvas) SetOnMouseBack(func()) {}

func (*mobileCanvas) SetOnMouseForward(func()) {}

func (c *mobileCanvas) Size() fyne.Size {
return c.size
}
Expand Down

0 comments on commit 5ecd928

Please sign in to comment.