From bd566dda7bfcd6b68e6f8178ed27ca383ff3c1e2 Mon Sep 17 00:00:00 2001 From: Roland Singer Date: Sun, 20 Jan 2019 15:04:36 +0100 Subject: [PATCH] further progress --- app.go | 38 +++++++++++++++++- gml.go | 78 +++++++++++++------------------------ samples/hello_world/main.go | 30 +------------- 3 files changed, 63 insertions(+), 83 deletions(-) diff --git a/app.go b/app.go index f63c8df..35cc652 100644 --- a/app.go +++ b/app.go @@ -62,6 +62,7 @@ type app struct { argv **C.char mutex sync.Mutex + initFuncs []func() error ctxPropMap map[string]interface{} imgProvMap map[string]*ImageProvider } @@ -168,6 +169,14 @@ func (a *app) getDp() (dp float64, err error) { dp = dpi / 140.0*/ } +// OnInit calls the function during application initialization. +// The qml data is already loaded, but the application does not run yet. +func (a *app) OnInit(f func() error) { + a.mutex.Lock() + a.initFuncs = append(a.initFuncs, f) + a.mutex.Unlock() +} + // RunMain runs the function on the applications main thread. func (a *app) RunMain(f func()) { // Check if already running on the main thread. @@ -211,6 +220,7 @@ func (a *app) Load(url string) error { return nil } +// TODO: Remove? Currently not used. // LoadData loads the QML given in data. // Hint: Must be called within main thread. func (a *app) LoadData(data string) error { @@ -227,6 +237,7 @@ func (a *app) LoadData(data string) error { return nil } +// TODO: Remove? Currently not used. // AddImportPath adds the given import path to the app engine. // Hint: Must be called within main thread. func (a *app) AddImportPath(path string) { @@ -261,10 +272,33 @@ func (a *app) AddImageProvider(id string, ip *ImageProvider) error { return nil } -// Exec executes the application and returns the exit code. +// Exec load sthe root QML file located at url, +// executes the application and returns the exit code. // This method is blocking. // Hint: Must be called within main thread. -func (a *app) Exec() (retCode int, err error) { +func (a *app) Exec(url string) (retCode int, err error) { + // Load the main qml. + err = a.Load(url) + if err != nil { + return + } + + // Call all init functions. + var initFuncs []func() error + a.mutex.Lock() + initFuncs = a.initFuncs + a.initFuncs = nil + a.mutex.Unlock() + + for _, f := range initFuncs { + err = f() + if err != nil { + err = fmt.Errorf("gml: onInit: %v", err) + return + } + } + + // Execute the app. apiErr := errorPool.Get() defer errorPool.Put(apiErr) diff --git a/gml.go b/gml.go index 8422dd9..7da7b86 100644 --- a/gml.go +++ b/gml.go @@ -51,39 +51,34 @@ func init() { } } +// OnInit calls the function during application initialization. +// The qml data is already loaded, but the application does not run yet. +func OnInit(f func() error) { + gapp.OnInit(f) +} + // RunMain runs the function on the applications main thread. func RunMain(f func()) { gapp.RunMain(f) } -// Load the root QML file located at url. -// Hint: Must be called within main thread. -func Load(url string) error { - return gapp.Load(url) -} - -// LoadData loads the QML given in data. -// Hint: Must be called within main thread. -func LoadData(data string) error { - return gapp.LoadData(data) -} - -// AddImportPath adds the given import path to the app engine. -// Hint: Must be called within main thread. -func AddImportPath(path string) { - gapp.AddImportPath(path) -} - -// AddImageProvider adds the image provider to the app engine for the given id. -func AddImageProvider(id string, ip *ImageProvider) error { - return gapp.AddImageProvider(id, ip) -} - -// Exec executes the application and returns the exit code. +// Exec load sthe root QML file located at url, +// executes the application and returns the exit code. // This method is blocking. // Hint: Must be called within main thread. -func Exec() (retCode int, err error) { - return gapp.Exec() +func Exec(url string) (retCode int, err error) { + return gapp.Exec(url) +} + +// ExecExit load sthe root QML file located at url, +// executes the app, prints errors and exits +// the application with the specific exit code. +func ExecExit(url string) { + ret, err := gapp.Exec(url) + if err != nil { + fmt.Println(err) + } + os.Exit(ret) } // Quit the application. @@ -91,10 +86,6 @@ func Quit() { gapp.Quit() } -func SetContextProperty(name string, v interface{}) (err error) { - return gapp.SetContextProperty(name, v) -} - func SetApplicationName(name string) { gapp.SetApplicationName(name) } @@ -103,28 +94,11 @@ func SetOrganizationName(name string) { gapp.SetOrganizationName(name) } -// ExecExit executes the app, prints errors and exits -// the application with the specific exit code. -func ExecExit() { - ret, err := gapp.Exec() - if err != nil { - fmt.Println(err) - } - os.Exit(ret) +// AddImageProvider adds the image provider to the app engine for the given id. +func AddImageProvider(id string, ip *ImageProvider) error { + return gapp.AddImageProvider(id, ip) } -// LoadExecExit loads the qml url, executes the app, prints errors and exits -// the application with the specific exit code. -func LoadExecExit(url string) { - err := gapp.Load(url) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - ret, err := gapp.Exec() - if err != nil { - fmt.Println(err) - } - os.Exit(ret) +func SetContextProperty(name string, v interface{}) (err error) { + return gapp.SetContextProperty(name, v) } diff --git a/samples/hello_world/main.go b/samples/hello_world/main.go index 6ebc60e..8c73f95 100644 --- a/samples/hello_world/main.go +++ b/samples/hello_world/main.go @@ -28,37 +28,9 @@ package main import ( - "log" - "github.com/desertbit/gml" ) func main() { - err := gml.LoadData(qmlData) - if err != nil { - log.Fatalln(err) - } - - gml.ExecExit() + gml.ExecExit("qrc:/qml/app.qml") } - -const qmlData = ` -import QtQuick 2.11 -import QtQuick.Layouts 1.3 -import QtQuick.Controls 2.4 - -ApplicationWindow { - id: window - width: 300 - height: 300 - visible: true - - Text { - text: "Hello world!" - anchors.horizontalCenter: parent.horizontalCenter - anchors.verticalCenter: parent.verticalCenter - font.pointSize: 24 - font.bold: true - } -} -`