Skip to content

Commit 7641a88

Browse files
author
jld3103
authored
Implement flutter/mousecursor (#445)
1 parent ee4acea commit 7641a88

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

application.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func NewApplication(opt ...Option) *Application {
5454
opt = append(opt, AddPlugin(defaultLifecyclePlugin))
5555
opt = append(opt, AddPlugin(defaultKeyeventsPlugin))
5656
opt = append(opt, AddPlugin(defaultAccessibilityPlugin))
57+
opt = append(opt, AddPlugin(defaultMousecursorPlugin))
5758

5859
// apply all configs
5960
for _, o := range opt {

mousecursor.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package flutter
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/go-flutter-desktop/go-flutter/plugin"
7+
"github.com/go-gl/glfw/v3.3/glfw"
8+
)
9+
10+
const mousecursorChannelName = "flutter/mousecursor"
11+
12+
// mousecursorPlugin implements flutter.Plugin and handles method calls to the
13+
// flutter/mousecursor channel.
14+
type mousecursorPlugin struct {
15+
window *glfw.Window
16+
lastCursor *glfw.Cursor
17+
}
18+
19+
var defaultMousecursorPlugin = &mousecursorPlugin{}
20+
21+
func (p *mousecursorPlugin) InitPlugin(messenger plugin.BinaryMessenger) error {
22+
channel := plugin.NewMethodChannel(messenger, mousecursorChannelName, plugin.StandardMethodCodec{})
23+
channel.HandleFuncSync("activateSystemCursor", p.handleActivateSystemCursor)
24+
return nil
25+
}
26+
func (p *mousecursorPlugin) InitPluginGLFW(window *glfw.Window) error {
27+
p.window = window
28+
return nil
29+
}
30+
31+
func (p *mousecursorPlugin) handleActivateSystemCursor(arguments interface{}) (reply interface{}, err error) {
32+
args := arguments.(map[interface{}]interface{})
33+
var cursor *glfw.Cursor
34+
if args["kind"] == "none" {
35+
p.window.SetInputMode(glfw.CursorMode, glfw.CursorHidden)
36+
} else {
37+
p.window.SetInputMode(glfw.CursorMode, glfw.CursorNormal)
38+
}
39+
switch kind := args["kind"]; {
40+
case kind == "none":
41+
case kind == "basic" || kind == "forbidden" || kind == "grab" || kind == "grabbing":
42+
// go-gl GLFW currently (latest tagged v3.3 version) has no cursors for "forbidden", "grab" and "grabbing"
43+
// TODO: Wait for https://github.com/glfw/glfw/commit/7dbdd2e6a5f01d2a4b377a197618948617517b0e to appear in go-gl GLFW and implement the "forbidden" cursor
44+
cursor = glfw.CreateStandardCursor(glfw.ArrowCursor)
45+
case kind == "click":
46+
cursor = glfw.CreateStandardCursor(glfw.HandCursor)
47+
case kind == "text":
48+
cursor = glfw.CreateStandardCursor(glfw.IBeamCursor)
49+
default:
50+
return nil, errors.New(fmt.Sprintf("cursor kind %s not implemented", args["kind"]))
51+
}
52+
if p.lastCursor != nil {
53+
p.lastCursor.Destroy()
54+
}
55+
if cursor != nil {
56+
p.window.SetCursor(cursor)
57+
p.lastCursor = cursor
58+
}
59+
return nil, nil
60+
}

0 commit comments

Comments
 (0)