Skip to content

Commit 6e1b3f4

Browse files
author
Drakirus
committed
✨ Merge branch 'plugin', Add support for the JSON MethodChannel Plugins
2 parents 0278c3b + 9b06731 commit 6e1b3f4

File tree

14 files changed

+398
-221
lines changed

14 files changed

+398
-221
lines changed

README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ go build
168168
</details>
169169

170170

171+
171172
## Flutter Demos Projects
172173

173174
The examples are available [here](./example/).
@@ -179,16 +180,20 @@ The examples are available [here](./example/).
179180
- [x] Linux :penguin:
180181
- [x] Windows :checkered_flag:
181182
- [x] MacOS :apple:
182-
- [x] Text input
183-
- [ ] Plugins
184183
- [x] Importable go library
185-
- [x] Clipboard (through shortcuts)
186-
- [ ] Clipboard (through the click)
187-
- [x] Keyboard shortcuts
188-
- [x] <kbd>ctrl-c</kbd> <kbd>ctrl-v</kbd> <kbd>ctrl-x</kbd> <kbd>ctrl-a</kbd>
189-
- [x] <kbd>Home</kbd> <kbd>End</kbd> <kbd>shift-Home</kbd> <kbd>shift-End</kbd>
190-
- [x] <kbd>Left</kbd> <kbd>ctrl-Left</kbd> <kbd>ctrl-shift-Left</kbd>
191-
- [x] <kbd>Right</kbd> <kbd>ctrl-Right</kbd> <kbd>ctrl-shift-Right</kbd>
192-
- [x] <kbd>Backspace</kbd> <kbd>ctrl-Backspace</kbd> <kbd>Delete</kbd>
193-
- [ ] <kbd>ctrl-Delete</kbd>
194-
- [ ] Key events
184+
- [ ] Plugins [Medium article on how the the Flutter's messaging works](https://medium.com/flutter-io/flutter-platform-channels-ce7f540a104e)
185+
- [x] JSON MethodChannel
186+
- [ ] StandardMethodCodec, ...
187+
- [ ] System plugins [Platform channels used by the Flutter system](https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/services/system_channels.dart)
188+
- [x] Window Title
189+
- [x] Text input
190+
- [x] Clipboard (through shortcuts)
191+
- [ ] Clipboard (through the click)
192+
- [x] Keyboard shortcuts
193+
- [x] <kbd>ctrl-c</kbd> <kbd>ctrl-v</kbd> <kbd>ctrl-x</kbd> <kbd>ctrl-a</kbd>
194+
- [x] <kbd>Home</kbd> <kbd>End</kbd> <kbd>shift-Home</kbd> <kbd>shift-End</kbd>
195+
- [x] <kbd>Left</kbd> <kbd>ctrl-Left</kbd> <kbd>ctrl-shift-Left</kbd>
196+
- [x] <kbd>Right</kbd> <kbd>ctrl-Right</kbd> <kbd>ctrl-shift-Right</kbd>
197+
- [x] <kbd>Backspace</kbd> <kbd>ctrl-Backspace</kbd> <kbd>Delete</kbd>
198+
- [ ] <kbd>ctrl-Delete</kbd>
199+
- [ ] Key events

example/simpleDemo/flutter_project/demo/lib/main.dart

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import 'package:flutter/material.dart';
33
import 'package:flutter/foundation.dart'
44
show debugDefaultTargetPlatformOverride;
55

6+
import 'package:flutter/services.dart';
7+
import 'dart:async';
8+
69
void main() {
710
// Desktop platforms aren't a valid platform.
811
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
@@ -31,11 +34,26 @@ class MyHomePage extends StatefulWidget {
3134
final String title;
3235

3336
@override
34-
_MyHomePageState createState() => new _MyHomePageState();
37+
_MyHomePageState createState() {
38+
return new _MyHomePageState();
39+
}
3540
}
3641

3742
class _MyHomePageState extends State<MyHomePage> {
43+
44+
45+
static MethodChannel _channel = new MethodChannel('plugin_demo', new JSONMethodCodec());
46+
Future GetVersion() async {
47+
var res = await _channel.invokeMethod('getNumber');
48+
print(res);
49+
setState(() {
50+
_counter = res;
51+
});
52+
}
53+
54+
3855
int _counter = 0;
56+
bool _ok = false;
3957

4058
void _incrementCounter() {
4159
setState(() {
@@ -45,6 +63,10 @@ class _MyHomePageState extends State<MyHomePage> {
4563

4664
@override
4765
Widget build(BuildContext context) {
66+
if (!_ok) {
67+
GetVersion();
68+
_ok = true;
69+
}
4870
return new Scaffold(
4971
appBar: new AppBar(
5072
title: new Text(widget.title),

example/simpleDemo/flutter_project/demo/pubspec.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
name: analyzer
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "0.32.5"
10+
version: "0.33.0"
1111
args:
1212
dependency: transitive
1313
description:
@@ -63,7 +63,7 @@ packages:
6363
name: csslib
6464
url: "https://pub.dartlang.org"
6565
source: hosted
66-
version: "0.14.5"
66+
version: "0.14.6"
6767
cupertino_icons:
6868
dependency: "direct main"
6969
description:
@@ -87,7 +87,7 @@ packages:
8787
name: front_end
8888
url: "https://pub.dartlang.org"
8989
source: hosted
90-
version: "0.1.4+1"
90+
version: "0.1.6"
9191
glob:
9292
dependency: transitive
9393
description:
@@ -108,7 +108,7 @@ packages:
108108
name: http
109109
url: "https://pub.dartlang.org"
110110
source: hosted
111-
version: "0.11.3+17"
111+
version: "0.12.0"
112112
http_multi_server:
113113
dependency: transitive
114114
description:
@@ -150,7 +150,7 @@ packages:
150150
name: kernel
151151
url: "https://pub.dartlang.org"
152152
source: hosted
153-
version: "0.3.4+1"
153+
version: "0.3.6"
154154
logging:
155155
dependency: transitive
156156
description:
@@ -206,7 +206,7 @@ packages:
206206
name: package_resolver
207207
url: "https://pub.dartlang.org"
208208
source: hosted
209-
version: "1.0.4"
209+
version: "1.0.6"
210210
path:
211211
dependency: transitive
212212
description:
@@ -288,7 +288,7 @@ packages:
288288
name: source_maps
289289
url: "https://pub.dartlang.org"
290290
source: hosted
291-
version: "0.10.7"
291+
version: "0.10.8"
292292
source_span:
293293
dependency: transitive
294294
description:
@@ -330,7 +330,7 @@ packages:
330330
name: test
331331
url: "https://pub.dartlang.org"
332332
source: hosted
333-
version: "1.3.0"
333+
version: "1.3.4"
334334
typed_data:
335335
dependency: transitive
336336
description:
@@ -381,4 +381,4 @@ packages:
381381
source: hosted
382382
version: "2.1.15"
383383
sdks:
384-
dart: ">=2.0.0-dev.68.0 <3.0.0"
384+
dart: ">=2.0.0 <3.0.0"

example/simpleDemo/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bufio"
45
"image"
56
_ "image/png"
67
"log"
@@ -9,6 +10,7 @@ import (
910
"runtime"
1011

1112
gutter "github.com/Drakirus/go-flutter-desktop-embedder"
13+
"github.com/Drakirus/go-flutter-desktop-embedder/flutter"
1214

1315
"github.com/go-gl/glfw/v3.2/glfw"
1416
)
@@ -29,6 +31,7 @@ func main() {
2931
gutter.OptionWindowInitializer(setIcon),
3032
gutter.OptionPixelRatio(1.2),
3133
gutter.OptionVMArguments([]string{"--dart-non-checked-mode", "--observatory-port=50300"}),
34+
gutter.OptionAddPluginReceiver(ownPlugin, "plugin_demo"),
3235
}
3336

3437
if err = gutter.Run(options...); err != nil {
@@ -51,3 +54,26 @@ func setIcon(window *glfw.Window) error {
5154
window.SetIcon([]image.Image{img})
5255
return nil
5356
}
57+
58+
// Plugin that read the stdin and send the number to the dart side
59+
func ownPlugin(
60+
platMessage flutter.PlatformMessage,
61+
flutterEngine *flutter.EngineOpenGL,
62+
window *glfw.Window,
63+
) bool {
64+
if platMessage.Message.Method != "getNumber" {
65+
log.Printf("Unhandled platform method: %#v from channel %#v\n",
66+
platMessage.Message.Method, platMessage.Channel)
67+
return false
68+
}
69+
70+
go func() {
71+
reader := bufio.NewReader(os.Stdin)
72+
print("Reading (A number): ")
73+
s, _ := reader.ReadString('\n')
74+
flutterEngine.SendPlatformMessageResponse(platMessage, []byte("[ "+s+" ]"))
75+
}()
76+
77+
return true
78+
79+
}

example/stocks/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func main() {
2222
// gutter.OptionICUDataPath("./FlutterEmbedder.framework/Resources/icudtl.dat"), // OSX
2323
gutter.OptionWindowDimension(800, 600),
2424
gutter.OptionWindowInitializer(setIcon),
25-
gutter.OptionPixelRatio(1.9),
25+
gutter.OptionPixelRatio(1.2),
2626
gutter.OptionVMArguments([]string{"--dart-non-checked-mode", "--observatory-port=50300"}),
2727
}
2828

flutter/flutter.go

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@ import (
1212
)
1313

1414
// the current FlutterEngine running (associated with his callback)
15-
var globalFlutterOpenGL EngineOpenGL
15+
var flutterEngines []*EngineOpenGL
16+
17+
// SelectEngine return a EngineOpenGL from an index
18+
func SelectEngine(index int) *EngineOpenGL {
19+
return flutterEngines[index]
20+
}
21+
22+
// NumberOfEngines return the number of engine registered into this embedder
23+
func NumberOfEngines() C.int {
24+
return C.int(len(flutterEngines))
25+
}
1626

1727
// Result corresponds to the C.enum retuned by the shared flutter library
1828
// whenever we call it.
@@ -42,20 +52,20 @@ type EngineOpenGL struct {
4252

4353
// Engine arguments
4454
PixelRatio float64
45-
AssetsPath unsafe.Pointer
46-
IcuDataPath unsafe.Pointer
55+
AssetsPath string
56+
IcuDataPath string
4757
}
4858

4959
// Run launches the Flutter Engine in a background thread.
5060
func (flu *EngineOpenGL) Run(window uintptr, vmArgs []string) Result {
5161

52-
globalFlutterOpenGL = *flu
62+
flutterEngines = append(flutterEngines, flu)
5363

5464
args := C.FlutterProjectArgs{
55-
assets_path: (*C.char)(flu.AssetsPath),
65+
assets_path: C.CString(flu.AssetsPath),
5666
main_path: C.CString(""),
5767
packages_path: C.CString(""),
58-
icu_data_path: (*C.char)(flu.IcuDataPath),
68+
icu_data_path: C.CString(flu.IcuDataPath),
5969
}
6070

6171
args.struct_size = C.size_t(unsafe.Sizeof(args))
@@ -136,17 +146,24 @@ func (flu *EngineOpenGL) EngineSendWindowMetricsEvent(Metric WindowMetricsEvent)
136146
return (Result)(res)
137147
}
138148

139-
type platformMessageResponseHandle C.FlutterPlatformMessageResponseHandle
140-
141-
// PlatformMessage represents a message from or to the Flutter Engine (and thus the dart code)
149+
// PlatformMessage represents a `MethodChannel` serialized with the `JSONMethodCodec`
150+
// TODO Support for `StandardMethodCodec`
142151
type PlatformMessage struct {
143152
Channel string
144153
Message Message
145-
ResponseHandle *platformMessageResponseHandle
154+
ResponseHandle *C.FlutterPlatformMessageResponseHandle
155+
}
156+
157+
// Message is the json content of a PlatformMessage
158+
type Message struct {
159+
// Describe the method
160+
Method string `json:"method"`
161+
// Actual datas
162+
Args json.RawMessage `json:"args"`
146163
}
147164

148-
// EngineSendPlatformMessage is used to send a PlatformMessage to the Flutter engine.
149-
func (flu *EngineOpenGL) EngineSendPlatformMessage(Message PlatformMessage) Result {
165+
// SendPlatformMessage is used to send a PlatformMessage to the Flutter engine.
166+
func (flu *EngineOpenGL) SendPlatformMessage(Message PlatformMessage) Result {
150167

151168
marshalled, err := json.Marshal(Message.Message)
152169
if err != nil {
@@ -170,6 +187,22 @@ func (flu *EngineOpenGL) EngineSendPlatformMessage(Message PlatformMessage) Resu
170187
return (Result)(res)
171188
}
172189

190+
// SendPlatformMessageResponse is used to send a message to the Flutter side using the correct ResponseHandle!
191+
func (flu *EngineOpenGL) SendPlatformMessageResponse(
192+
responseTo PlatformMessage,
193+
data []byte,
194+
) Result {
195+
196+
res := C.FlutterEngineSendPlatformMessageResponse(
197+
flu.Engine,
198+
(*C.FlutterPlatformMessageResponseHandle)(responseTo.ResponseHandle),
199+
(*C.uint8_t)(unsafe.Pointer(C.CBytes(data))),
200+
(C.ulong)(len(data)))
201+
202+
return (Result)(res)
203+
204+
}
205+
173206
// EngineFlushPendingTasksNow flush tasks on a message loop not controlled by the Flutter engine.
174207
// deprecated soon.
175208
func EngineFlushPendingTasksNow() {

flutter/flutter_helper.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ bool proxy_on_platform_message(FlutterPlatformMessage *message,
1515

1616
// C helper
1717
FlutterResult runFlutter(uintptr_t window, FlutterEngine *engine, FlutterProjectArgs * Args,
18-
const char *const * vmArgs, int nVmAgrs) {
18+
const char *const * vmArgs, int nVmAgrs) {
1919

2020
FlutterRendererConfig config = {};
2121
config.type = kOpenGL;
2222

2323
config.open_gl.struct_size = sizeof(FlutterOpenGLRendererConfig);
2424

25-
config.open_gl.make_current = proxy_make_current;
26-
config.open_gl.clear_current = proxy_clear_current;
27-
config.open_gl.present = proxy_present;
28-
config.open_gl.fbo_callback = proxy_fbo_callback;
29-
config.open_gl.make_resource_current = proxy_make_resource_current;
25+
config.open_gl.make_current = proxy_make_current;
26+
config.open_gl.clear_current = proxy_clear_current;
27+
config.open_gl.present = proxy_present;
28+
config.open_gl.fbo_callback = proxy_fbo_callback;
29+
config.open_gl.make_resource_current = proxy_make_resource_current;
3030

3131
Args->command_line_argc = nVmAgrs;
3232
Args->command_line_argv = vmArgs;

0 commit comments

Comments
 (0)