Skip to content

Commit 352ada5

Browse files
author
Pierre Champion
authored
Merge pull request #4 from james-lawrence/importable-library
rework package to be importable (proof of concept)
2 parents f10c59d + 5ef90fd commit 352ada5

File tree

106 files changed

+134
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+134
-66
lines changed

README.md

+13-21

example/stocks/.build/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
temp.zip
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

example/stocks/main.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"image"
5+
_ "image/png"
6+
"log"
7+
"os"
8+
"os/signal"
9+
10+
gutter "github.com/Drakirus/go-flutter-desktop-embedder"
11+
12+
"github.com/go-gl/glfw/v3.2/glfw"
13+
)
14+
15+
// #cgo linux LDFLAGS: -L${SRCDIR}/flutter/library/linux
16+
import "C"
17+
18+
func main() {
19+
var (
20+
err error
21+
)
22+
23+
options := []gutter.Option{
24+
gutter.OptionAssetPath("flutter_project/stocks/build/flutter_assets"),
25+
gutter.OptionICUDataPath("../../flutter/library/icudtl.dat"),
26+
gutter.OptionWindowInitializer(setIcon),
27+
}
28+
29+
if err = gutter.Run(options...); err != nil {
30+
log.Fatalln(err)
31+
}
32+
33+
signals := make(chan os.Signal, 1)
34+
signal.Notify(signals, os.Kill, os.Interrupt)
35+
select {
36+
case <-signals:
37+
}
38+
}
39+
40+
func setIcon(window *glfw.Window) error {
41+
imgFile, err := os.Open("assets/icon.png")
42+
if err != nil {
43+
return err
44+
}
45+
img, _, err := image.Decode(imgFile)
46+
if err != nil {
47+
return err
48+
}
49+
window.SetIcon([]image.Image{img})
50+
return nil
51+
}

flutter/build.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ package flutter
44
// Linux Build Tags
55
// ----------------
66
#cgo linux CFLAGS: -I${SRCDIR}/library
7-
#cgo linux LDFLAGS: -L${SRCDIR}/library/linux -lflutter_engine -Wl,-rpath,$ORIGIN/flutter/library/linux
7+
#cgo linux LDFLAGS: -lflutter_engine -Wl,-rpath,flutter/library/linux
88
99
*/
1010
import "C"
11+
12+
import (
13+
// prevents dep from stripping out the c source files in flutter library.
14+
_ "github.com/Drakirus/go-flutter-desktop-embedder/flutter/library"
15+
)

flutter/library/library.go

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package library
2+
3+
// this file along with its _ import in build.go prevent this package
4+
// from being pruned by dep. see https://github.com/golang/dep/issues/1847

flutter/library/linux/.gitkeep

Whitespace-only changes.

main.go renamed to gutter.go

+56-43
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,77 @@
1-
package main
1+
package gutter
22

33
import (
44
"encoding/json"
5-
"flutter_desktop_go_embedding/flutter"
6-
"image"
7-
_ "image/png"
85
"log"
9-
"os"
10-
"runtime"
116
"time"
127
"unsafe"
138

9+
"github.com/Drakirus/go-flutter-desktop-embedder/flutter"
1410
"github.com/go-gl/glfw/v3.2/glfw"
1511
)
1612

17-
// TextInput model
18-
var state = textModel{}
13+
// Option for gutter
14+
type Option func(*config)
1915

20-
func init() {
21-
runtime.LockOSThread()
16+
// OptionAssetPath specify the flutter asset directory.
17+
func OptionAssetPath(p string) Option {
18+
return func(c *config) {
19+
c.AssetPath = p
20+
}
2221
}
2322

24-
func main() {
25-
err := glfw.Init()
26-
if err != nil {
27-
panic(err)
23+
// OptionICUDataPath specify the path to the ICUData.
24+
func OptionICUDataPath(p string) Option {
25+
return func(c *config) {
26+
c.ICUDataPath = p
2827
}
29-
defer glfw.Terminate()
28+
}
3029

31-
window, err := glfw.CreateWindow(800, 600, "Loading..", nil, nil)
32-
if err != nil {
33-
panic(err)
30+
// OptionWindowInitializer allow initializing the window.
31+
func OptionWindowInitializer(ini func(*glfw.Window) error) Option {
32+
return func(c *config) {
33+
c.WindowInitializer = ini
3434
}
35+
}
3536

36-
defer window.Destroy()
37+
type config struct {
38+
AssetPath string
39+
ICUDataPath string
40+
WindowInitializer func(*glfw.Window) error
41+
}
3742

38-
// set icon
39-
if err := setIcon(window); err != nil {
40-
log.Printf("unable to set window icon: %v\n", err)
43+
func (t config) merge(options ...Option) config {
44+
for _, opt := range options {
45+
opt(&t)
4146
}
4247

43-
assetsPath := "./flutter_project/stocks/build/flutter_assets"
44-
icuDataPath := "./flutter/library/icudtl.dat"
48+
return t
49+
}
50+
51+
// Run executes a flutter application with the provided options.
52+
// given limitations this method must be called by the main function directly.
53+
func Run(options ...Option) (err error) {
54+
var (
55+
window *glfw.Window
56+
c config
57+
)
58+
c = c.merge(options...)
4559

46-
engine := runFlutter(window, assetsPath, icuDataPath)
60+
if err = glfw.Init(); err != nil {
61+
return err
62+
}
63+
defer glfw.Terminate()
64+
65+
if window, err = glfw.CreateWindow(800, 600, "Loading..", nil, nil); err != nil {
66+
return err
67+
}
68+
defer window.Destroy()
69+
70+
if err = c.WindowInitializer(window); err != nil {
71+
return err
72+
}
73+
74+
engine := runFlutter(window, c.AssetPath, c.ICUDataPath)
4775

4876
defer engine.Shutdown()
4977

@@ -53,10 +81,10 @@ func main() {
5381
flutter.EngineFlushPendingTasksNow()
5482
}
5583

84+
return nil
5685
}
5786

5887
// GLFW callbacks to the Flutter Engine
59-
6088
func glfwCursorPositionCallbackAtPhase(
6189
window *glfw.Window, phase flutter.PointerPhase,
6290
x float64, y float64,
@@ -91,6 +119,8 @@ func glfwMouseButtonCallback(window *glfw.Window, key glfw.MouseButton, action g
91119

92120
}
93121

122+
var state = textModel{}
123+
94124
func glfwKeyCallback(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
95125

96126
if key == glfw.KeyEscape && action == glfw.Press {
@@ -146,16 +176,13 @@ func glfwKeyCallback(w *glfw.Window, key glfw.Key, scancode int, action glfw.Act
146176
state.addChar([]rune(clpString))
147177
}
148178
}
149-
150179
}
151-
152180
}
153181
}
154182

155183
}
156184

157185
func glfwWindowSizeCallback(window *glfw.Window, width int, height int) {
158-
159186
event := flutter.WindowMetricsEvent{
160187
Width: width,
161188
Height: height,
@@ -173,7 +200,6 @@ func glfwCharCallback(w *glfw.Window, char rune) {
173200
}
174201

175202
// Flutter Engine
176-
177203
func runFlutter(window *glfw.Window, assetsPath string, icuDataPath string) *flutter.EngineOpenGL {
178204

179205
flutterOGL := flutter.EngineOpenGL{
@@ -301,16 +327,3 @@ func updateEditingState(window *glfw.Window) {
301327
flutterOGL := *(*flutter.EngineOpenGL)(window.GetUserPointer())
302328
flutterOGL.EngineSendPlatformMessage(mess)
303329
}
304-
305-
func setIcon(window *glfw.Window) error {
306-
imgFile, err := os.Open("assets/icon.png")
307-
if err != nil {
308-
return err
309-
}
310-
img, _, err := image.Decode(imgFile)
311-
if err != nil {
312-
return err
313-
}
314-
window.SetIcon([]image.Image{img})
315-
return nil
316-
}

textModel.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package gutter
22

33
import (
44
"sort"

0 commit comments

Comments
 (0)