go & multi platform gui trials and errors

48
Go & multi platform GUI Trials and Errors Go Conference 2016 sprint @ 4/23/2016 @shibukawa (Twitter) github.com/shibukawa Yoshiki Shibukawa

Upload: yoshiki-shibukawa

Post on 07-Jan-2017

3.065 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Go & multi platform GUI Trials and Errors

Go & multi platform GUITrials and Errors

Go Conference 2016 sprint @ 4/23/2016@shibukawa (Twitter)github.com/shibukawa

Yoshiki Shibukawa

Page 2: Go & multi platform GUI Trials and Errors

Who Are You?• Programmer in Mobile Game Department

– http://www.slideshare.net/dena_study/final-fantasy-record-keeper

Yoshiki Shibukawa

Languages C++/JavaScript/Python/Golang etc Japanese/English/Español (studying with Duolingo)

Books Translations: Expert Python/Pomodoro Technique/The Art of

community Original: Mithril, つまみぐい勉強 etc

Page 3: Go & multi platform GUI Trials and Errors

Before talking my presentation…

• Machine readable docs for web service API

Page 4: Go & multi platform GUI Trials and Errors

Before talking my presentation…

• Machine readable docs for web service API

YES!

Page 5: Go & multi platform GUI Trials and Errors

• https://shibukawa.github.io/curl_as_dsl/• I will add net/http, fasthttp server code generate in future• Welcome Ruby/Perl/emacs lisp pull request

Page 6: Go & multi platform GUI Trials and Errors

Conclusion

• This presentation doesn’t show the best solution for multi platform GUI

• I am exploring possibility about Go and GUI and creating experimental codes.

• GUI programming is not easy• C++ is required

Page 7: Go & multi platform GUI Trials and Errors

Solutions I triedPros Cons

Toolkit Wrapper The most intuitive solution Maintaining wrappers is extremely hard jobCan’t use deploy support tool

Create new Toolkit in Go Perfect matching with golang

Supporting multi platform is very tough

Hybrid (C++ for GUI, Go for logic)

Page 8: Go & multi platform GUI Trials and Errors

Solution 1Toolkit Wrapper

Page 9: Go & multi platform GUI Trials and Errors

Toolkit Wrapper

• I used wxPython, Qt for JavaScript etc…• The most intuitive solution

– You can use it in same methodology with original native language

Page 10: Go & multi platform GUI Trials and Errors

I tried go-qml

http://qiita.com/shibukawa/items/47cb0ea60bf4a7259b5e

Page 11: Go & multi platform GUI Trials and Errors

Toolkit Wrapper Cons (1)

• Qt provides deployment helper tool– macdeployqt, windeployqt etc– They collects required .dll/.framework and rewrite

dependency paths and create installer etc– This tool works only for Pre build Qt/C++ project

• Few documents/examples– We reinterpret original toolkit documents– C++ knowledge is required

• Debug is very hard– Where does this panic come from?

Page 12: Go & multi platform GUI Trials and Errors

Toolkit Wrapper Cons (2)

• Maintaining toolkit wrapper is hard work– GUI toolkit is contains many C++ classes

• Qt has more than 1200 classes– Many wrappers were discarded

• QtJS, QtJambi, ruby qtbindings, go-qml etc…– GUI toolkits update continuously

• Basically newer version is better• Almost all toolkit gave up updating• I have to update wrapper by myself to use newer future

– Languages update too

Page 13: Go & multi platform GUI Trials and Errors

Expect

Hours

Create My ApplicationMaintaining Toolkit

Page 14: Go & multi platform GUI Trials and Errors

Actually

Hours

Create My ApplicationMaintaining Toolkit

Page 15: Go & multi platform GUI Trials and Errors

Not all GUI toolkit wrappers are bad

PyQt Commercial (Qt 5.5 supports)

wxPython Nightly build still supports latest Python(2.7/3.5)

GTK# Xamarin supports?

pygobject or some libs gobject introspection is good for keeping fresh

Swing from Jython/JRuby Using toolkit directly

tkinter / Qt Quick Official wrapper provided by suppliers

• Some wrappers provide updates continuously• Maintaining is too heavy for individual developer• Community/Organizational support is required

Page 16: Go & multi platform GUI Trials and Errors

Solution 2Create New Toolkit in Go

Page 17: Go & multi platform GUI Trials and Errors

Implement GUI toolkit in Go• There are many small GUI toolkits in C/C++

– http://qiita.com/shimacpyon/items/e5d2a4e2019273345c37• Port one of them to Go!

– NanoGUI seems good to port

※ When start porting, I didn’t the existence of Shiny

Page 18: Go & multi platform GUI Trials and Errors

GUI toolkit on GL building blocks

Font Loader Font Renderer

Texture Management

Font TextureManagement

Event Handling

Vector GraphicsImage Loader

Widget Drawing

OpenGLOS Window System

Widget Management

Page 19: Go & multi platform GUI Trials and Errors

NanoGUI toolkit on GL building blocks

Font Loader Font Renderer

Texture Management

Font TextureManagement

Event Handling

Vector GraphicsImage Loader

Widget Drawing

OpenGLOS Window System

Widget Management

stb_truetype.h

stb_image.h

NanoVG

GLFW

NanoGUI

fontstash

Page 20: Go & multi platform GUI Trials and Errors

My toolkit on GL building blocks

Font Loader Font Renderer

Texture Management

Font TextureManagement

Event Handling

Vector GraphicsImage Loader

Widget Drawing

OpenGLOS Window System

Widget Management

go’s image

NanoVGo

goxjs/glfwgoxjs/gl

NanoGUI.go

Page 21: Go & multi platform GUI Trials and Errors

nanovgo

Page 22: Go & multi platform GUI Trials and Errors

nanogui-go

Page 23: Go & multi platform GUI Trials and Errors

Input Method Editor issue

• IME support is needed to input Asian chars• Now IME becomes more generic than before

– Voice / pen input uses same infrastructure– Modern OS treats IMEs as keyboards

• This is processed in GLFW (C++ layer)Japanese IME

English

Speech Input

Handwriting Input

OperatingSystem

Application

※On Windows, Handwriting features seem to be a part of MS IMEs

Page 24: Go & multi platform GUI Trials and Errors

IME support levels

• Level 1: Root window mode

• Level 2: Over the spot

Images from http://www.mozilla-japan.org/projects/intl/input-method-spec.htmlThis is not common classify. My original.※

Page 25: Go & multi platform GUI Trials and Errors

• Level 3: On the spot

• Level 4: Context aware conversion

Image from http://nyaruru.hatenablog.com/entry/20070309/p1

Page 26: Go & multi platform GUI Trials and Errors

IME tasks viewed from ApplicationLevel 1 Level 2 Level 3 Level 4

Receive multi byte input ✔ ✔ ✔ ✔

Send cursor position ✔ ✔ ✔

React to preedit text ✔ ✔

Send surrounding text around cursor

• To achieve higher level IME support, applications should communicate with IME more

• Each OS provides each API for IME. I should add new method to GLFW to absorb OS layers

Page 27: Go & multi platform GUI Trials and Errors

Achievement Unlocked!

Page 28: Go & multi platform GUI Trials and Errors

Current Status

• Windows– Level 3. Now I use IMM32(old), but Text Services

Framework (TSF) is preferred.– Old Chrome and Firefox use TSF (but MPL is not

good for GLFW)• MacOS X

– Level 3. Applications should implement NSTextInputClient protocol. I don’t implement Level 4 feature yet.

https://github.com/glfw/glfw/pull/658

Page 29: Go & multi platform GUI Trials and Errors

Current Status

• X11– Level 3. XIM protocol doesn’t support level 4.– On Linux/BSDs, there many input method frameworks

(UIM, SCIM, ibus, fcitx etc) and Qt/GTK communicate with them directly, but don’t use them (not to introduce new dependency to GLFW)

• Wayland– Not yet

• Mir– Not yet

https://github.com/glfw/glfw/pull/658

Page 30: Go & multi platform GUI Trials and Errors

Future Plan

• Improve GLFW more to support level 4 and Wayland, Mir

• Reconstruct NanoVGo– Cache geometry – Now it redraw from scratch on every frame

• Reconstruct NanoGUI.go– ListView support like QAbstractItemView /

QAbstractItemModel– Better image resource handling

Page 31: Go & multi platform GUI Trials and Errors

Solution 3Hybrid Style

Page 32: Go & multi platform GUI Trials and Errors

Hybrid Model

Thin GUI Logic

Page 33: Go & multi platform GUI Trials and Errors

C++ is not so fear than before

• Use Qt via C++ directly– No wrapper issues– Using C++ is not tough anymore– C++11 is much better than 20 years ago– Qt5 supports C++11 (C++11 is required from 5.7)

• But building code in Golang is still faster than C++ for me

Page 34: Go & multi platform GUI Trials and Errors

C++ support tool• Go is easy to build. C++ is boring• I made helper tool to make C++ easy

– https://github.com/qtpm/qtpm• Generate CMakeLists.txt. Support following IDEs:

– Qt Creator– CLion (thank you for Jetbrains OSS support program)

• It support the following commands like go:– qtpm get– qtpm build– qtpm vet– qtpm fmt

Page 35: Go & multi platform GUI Trials and Errors

How to communicate?

Thin GUI Logic

???

• I am trying several solutions for communication

Page 36: Go & multi platform GUI Trials and Errors

Qt’s thread model

• You should understand it if use Go with Qt• http://doc.qt.io/qt-5/threads-qobject.html• All Qt’s objects are belonging to thread

– Calling object’s method from non-owner thread causes panic

– To call other thread’s method, QMetaObject::invokeMethod is needed(signal/slot uses this behind it)

– Each thread has event loop– Of course, blocking main thread is not preferred

Page 37: Go & multi platform GUI Trials and Errors

3.1: QProcess

• Create standalone CUI program in Golang and call via QProcess class– If the job is simple, it is the best (it becomes

simple code)• Bi-directional communication is tough

– Receiving message in SLOT (callback)– No fluent code

Page 38: Go & multi platform GUI Trials and Errors

3.2: QLocalServer/QLocalSocket

• Qt provides special wrapper to provides high performance inter process communication– On Windows, it uses named pipe– On other environment, it uses UNIX domain socket

• Go implementation is ready– I tested on Windows and Mac

• How to manage process?– I need to implement some process supervisor

https://github.com/shibukawa/localsocket

Page 39: Go & multi platform GUI Trials and Errors

3.3: c-archive

• Creating c-archive and link with Qt application• Go 1.7 will support it on Windows too

Page 40: Go & multi platform GUI Trials and Errors

• If the API is simple, it is the best• All method is called synchronous by default• If callback from Golang to C++, it is little messy

– Polling is easy, but not beautiful– Pass function pointer from C++ and call it in Go

• https://gist.github.com/shibukawa/ab516964b0390cb234c7c42e5c9bfda1

• How to specify “-mmacosx-version-min=10.x”?

Page 41: Go & multi platform GUI Trials and Errors

3.4: dRuby style

• dRuby is a my favorite API in Ruby• dRuby hooks method call and transfers

method name and parameters via TCP/IP– Go: https://github.com/shibukawa/tobubus– C++: https://github.com/qtpm/QtTobubus

DRbObjectClient

hello(“world”)

method_missing()

DRbServerActualInstance

hello(“world”)

Page 42: Go & multi platform GUI Trials and Errors

tobubus

• Now it works on QLocalSocket/Server• It uses Qt

reflection(QMetaObject/QMetaMethod) and Go’s reflect

• Method call is serialized by CBOR– CBOR is almost RFC version of MessagePack

tobubusC++/Go Client

hello(“world”)

ProxyC++/Go ActualInstance

hello(“world”)

tobubus

CBOR on local socket

Page 43: Go & multi platform GUI Trials and Errors

3.5: gRPC

• I didn’t try it– I don’t want to create extra interface definitions

for communication• It is suitable for loose coupling of micro services

– It seems difficult to use with Qt threading model• I may need create wrappers for all interfaces in Qt

Page 44: Go & multi platform GUI Trials and Errors

Combinations of my solutionsInfrastructure

Connect Pros/Cons Simple Tested

QProcess stdin/stdout

bi-direction is difficultGo is always running in other process (should take care QThread)

S ✔

tobubus Not implemented yet A

QProcess + QLocalSocket/Server

tobubus It needs process supervisorC++ client code is not clean because of Qt thread modelGo is always running in other process (should take care QThread)

B ✔

c-archive linker Writing callback is little messyWindows doesn’t support until 1.7It works synchronously

S ✔

Page 45: Go & multi platform GUI Trials and Errors

Which is better?

• QProcess is the most simple now• tobubus + QLocalSocket/Server was originally

designed for generic plug-in system. I already implemented but I feel too heavy now.

• c-archive is a stable way. It is not bad if you can wait by 1.7 release.

Page 46: Go & multi platform GUI Trials and Errors

Conclusion

• This presentation doesn’t show the best solution for multi platform GUI

• I am exploring possibility about Go and GUI and creating experimental codes. – I will still improve GLFW IME support to realize pure

Go solution– If you can wait by Go 1.7, c-archive is better

• GUI programming is not easy• C++ is required

Page 47: Go & multi platform GUI Trials and Errors

Actually

Hours

Create My ApplicationMaintaining Toolkit

Page 48: Go & multi platform GUI Trials and Errors

In future I want to achieve

Hours

Create My ApplicationMaintaining Toolkit