|
| 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