Go & multi platform GUI Trials and Errors Go Conference 2016 sprint @ 4/23/2016 @shibukawa (Twitter) github.com/shibukawa Yoshiki Shibukawa
Before talking my presentation… • Machine readable docs for web service API
Before talking my presentation… • Machine readable docs for web service API YES!
Conclusion • This presentation doesn’t show the best soluti on for multi platform GUI • I am exploring possibility about Go and GUI an d creating experimental codes. • GUI programming is not easy • C++ is required
Solutions I tried Pros Cons Toolkit Wrapper The most intuitive solution Maintaining wrappers is extremely hard job Can’t use deploy support tool Create new Toolkit in Go Perfect matching with Supporting multi platform golang is very tough Hybrid (C++ for GUI, Go for logic)
Solution 1 Toolkit Wrapper
I tried go-qml http://qi ta.com/shibukawa/items/47cb0ea60bf4a7259b5e
Toolkit Wrapper Cons (1) • Qt provides deployment helper tool – macdeployqt, windeployqt etc – They collects required .dll/.framework and rewrite depend ency 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?
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
Expect Hours Create My Application Maintaining Toolkit
Actually Hours Create My Application Maintaining Toolkit
Not all GUI toolkit wrappers are bad • Some wrappers provide updates continuously • Maintaining is too heavy for individual developer • Community/Organizational support is required PyQt Commercial (Qt 5.5 supports) wxPython Nightly build stil 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
GUI toolkit on GL building blocks Font Texture Fon F t on tLoa L d oa er Fon F t on tRe R ndere r r Font Textur Man a age g ment n Te T xt e u xt re r Imag a e g Loa L der e Ma M na n ge g ment n Ve V ct c or t or Gra r p a hics Wi W dge g t e t Wid Wi ge g t e tDra r w a i w ng Ma M nag a e g ment n Ev E e v nt n tHa H n a dling OS O Wi W ndow o Sy S s y t s e t m Op O enGL
NanoGUI toolkit on GL building blocks Na N noV o G V st s b t _ b tr t u r et e yp y e.h fon f t on s t t s a t s a h Font Texture Fon F t on tLoa L d oa er Fon F t on tRe R ndere r r Font Textur Man a age g ment n st s b t _imag a e g .h . Te T xt e u xt re r Imag a e g Loa L der e Ma M na n ge g ment n Ve V ct c or t or Gra r p a hics NanoGUI Wi W dge g t e t NanoGUI Wid Wi ge g t e tDra r w a i w ng Ma M nag a e g ment n Ev E e v nt n tHa H n a dling OS O Wi W ndow o Sy S s y t s e t m Op O enGL GLF L W
My toolkit on GL building blocks Na N n a oVG oV o Font Texture Fon F t on tLoa L d oa er Fon F t on tRe R ndere r r Font Textur Man a age g ment n go’ g s o’ image g Te T xt e u xt re r Imag a e g Loa L der e Ma M na n ge g ment n Ve V ct c or t or Gra r p a hics Nan a oGUI U .go g Wi W dge g t e t Wid Wi ge g t e tDra r w a i w ng Ma M nag a e g ment n Ev E e v nt n tHa H n a dling OS O Wi W ndow o Sy S s y t s e t m Op O enGL go g xjs o /g / lfw go g xjs o /g / l
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) Jap a an a ese IM I E Eng En lish Op O era r ti a n ti g Application Speech c In I put Sy S s y t s e t m Applicati Ha H n a dw d rirting In I put ※On Windows, Handwriting features seem to be a part of MS IMEs
IME support levels • Level 1: Root window mode • Level 2: Over the spot Images from http://www.mozil a-japan.org/projects/intl/input-method-spec.html T ※ his is not common classify. My original.
IME tasks viewed from Application Level 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
Current Status • Windows – Level 3. Now I use IMM32(old), but Text Services F ramework (TSF) is preferred. – Old Chrome and Firefox use TSF (but MPL is not go od for GLFW) • MacOS X – Level 3. Applications should implement NSTextInp utClient protocol. I don’t implement Level 4 featur e yet. https://github.com/glfw/glfw/pul /658
Current Status • X11 – Level 3. XIM protocol doesn’t support level 4. – On Linux/BSDs, there many input method frameworks (U IM, 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/pul /658
Future Plan • Improve GLFW more to support level 4 and Way land, Mir • Reconstruct NanoVGo – Cache geometry – Now it redraw from scratch on every frame • Reconstruct NanoGUI.go – ListView support like QAbstractItemView / QAbstrac tItemModel – Better image resource handling
Solution 3 Hybrid Style
Hybrid Model Thin GUI Logic
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 stil faster than C++ for me
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 fol owing IDEs: – Qt Creator – CLion (thank you for Jetbrains OSS support program) • It support the fol owing commands like go: – qtpm get – qtpm build – qtpm vet – qtpm fmt
How to communicate? ??? Thin GUI Logic • I am trying several solutions for communication
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 p anic – To call other thread’s method, QMetaObject::invokeMet hod is needed (signal/slot uses this behind it) – Each thread has event loop – Of course, blocking main thread is not preferred
3.1: QProcess • Create standalone CUI program in Golang and cal via QProcess class – If the job is simple, it is the best (it becomes simpl e code) • Bi-directional communication is tough – Receiving message in SLOT (callback) – No fluent code
3.2: QLocalServer/QLocalSocket • Qt provides special wrapper to provides high per formance 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 https://github.com/shibukawa/localsocket • How to manage process? – I need to implement some process supervisor
3.3: c-archive • Creating c-archive and link with Qt application • Go 1.7 wil support it on Windows too
• If the API is simple, it is the best • All method is cal ed synchronous by default • If cal back 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/ab516964b0390cb2 34c7c42e5c9bfda1 • How to specify “-mmacosx-version-min=10.x”?
tobubus • Now it works on QLocalSocket/Server • It uses Qt reflection(QMetaObject/QMetaMet hod) and Go’s reflect • Method cal is serialized by CBOR – CBOR is almost RFC version of MessagePack C++/Go Actual tobubus C++/Go Client tobubus Proxy Instance hel o(“world”) CBOR on local socket hel o(“world”)
3.5: gRPC • I didn’t try it – I don’t want to create extra interface definitions fo r 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
Combinations of my solutions Infrastructur Connect Pros/Cons Simple Teste e d QProcess stdin/std bi-direction is difficult S ✔ out Go is always running in other process (should take care QThread) tobubus Not implemented yet A QProcess + tobubus It needs process supervisor B ✔ QLocalSocket C++ client code is not clean because of /Server Qt thread model Go is always running in other process (should take care QThread) c-archive linker Writing cal back is little messy S ✔ Windows doesn’t support until 1.7 It works synchronously
Which is better? • QProcess is the most simple now • tobubus + QLocalSocket/Server was original y designed for generic plug-in system. I already i mplemented but I feel too heavy now. • c-archive is a stable way. It is not bad if you ca n wait by 1.7 release.
Conclusion • This presentation doesn’t show the best solutio n 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
Actually Hours Create My Application Maintaining Toolkit
In future I want to achieve Hours Create My Application Maintaining Toolkit