226 points · by luu · 7y ago · 154 comments
At the end the result is good, but the code looks awful.
That being said it's been a while since I've used gtk-rs so it is entirely possible things have changed. I'd love to be corrected if that's the case :)
This is such a pain that even Gtk-rs samples have macros to work around cloning widget state into callbacks.
From my point of view language ergonomics still need to improve for Rust to actually be a candidate for GUI development, versus what the GC based languages (RC and tracing GC) offer out of the box with their UI kits.
Unless one envisions it just to take C++'s role of visualization layer and integration with graphics drivers.
Ownership defines who frees who. Parentage defines control nesting. References should to be handled via an indirection (e.g. names) and a protocol so that referees get notified when referents die. The indirection means that components get wired up in a late bound fashion; and the late binding means the gubbins for a protocol can be in place by then.
This stuff was figured out reasonably well with Delphi's VCL.
Even simple stuff like what Apple has demoed with SwiftUI designer can be a challenge, as you cannot just drag stuff around without changing the ownership semantics.
Naturally Swift has the advantage that Rc<RefCell<T>> is implicit, so it isn't as if the cost is magical gone.
However having to deal with it explicitly is a hard sell for anyone used to the comfort of GUI tooling.
So I imagine Rust is an easier sell for doing the low level stuff of a GUI engine, with what 90% of the devs using some managed language on top.
Basically what is happening to C++ on current desktop and mobile OSes, although for different reasons.
Or what is being done to Firefox actually.
Since then it got better libraries which reduce lots of the verbosity (e.g. gtk-rs), which is great. But the language itself probably won't change too much to better accommodate that use-case, since what things things painful here are the core features that prevent issues elsewhere.
I think we have to accept the fact that for different kinds of problems different programing languages are preferable instead of trying to find a single solution for all problems.
For UIs I think a naturally garbage-collected or refcounted language with reasonable support for dynamic dispatch is a lot easier to work with. In addition to that a restriction towards a single thread or even an inbuilt event loop can avoid bugs, improve interoperability between libraries, and reduce the amount of boilerplate and application-level design decisions even further.
If we take those properties we get the most common languages for GUI apps: Javascript/Dart which have single-threaded runtimes, and C#/Java/Kotlin/Swift as general-purpose languages which are more powerful but require an eventloop on application/library/framework level.
Those properties actually also expand to other very stateful and very concurrent applications. E.g. stateful network servers (something like Redis or a websocket server) have similar implementation challenges, and therefore are currently not very straightforward to write in Rust either (compared to e.g. in node.js).
This seems to work OK in SWT, so what's the problem in GTK/Rust? Are there some particular categories of objects that don't fit into that hierarchical model and need to be handled with a GC?
(Those objects would presumably be regular Java objects in an SWT app and handled by Java's GC.)
[1] https://www.eclipse.org/articles/swt-design-2/swt-design-2.h...
"Appendix: What the proposal won’t fix"
http://smallcultfollowing.com/babysteps/blog/2017/07/11/non-...
So one could say, it is a case of holding it wrong, and a Rust UI toolkit needs to be written from scratch. Which might be ok, but it does require some support for market adoption.
Having said this, Gtk-rs now has some help in the form of relm, exactly to overcome this issue.
I'm not really sure this can be reconciled with any arbitrary C library. One of the benefits I see from Rust is the ability to be unable to do unsafe things without explicitly stating you know what you're doing, thus preventing various classes of errors. But it feels like if the ownership model just doesn't fit with Rust's model that allows those guarantees to be made in the first place, it won't work. You can't write just anything in safe Rust without limiting the things you can do, but C libraries don't have such limits.
It might take creating new ways of representing GUI objects before the best model for that problem arises for Rust. I don't think libraries like GTK were designed with the lessons in safe memory management Rust learned from years after the fact in mind.
[0] http://way-cooler.org/blog/2019/04/29/rewriting-way-cooler-i...
It also might be that Rust is not the right language to write high level UI logic in at this time, and a GC'd language would be more appropriate. That's fine too. One of the strengths of Rust is its interoperability with other languages. C and C++ are certainly not the right languages to implement high level UI logic in, for example.
C++'s problem is that the path of righteousness is too narrow, but with heavy cultural idioms and norms, you can keep a lot of the people safely hemmed in, while retaining all the escape hatches.
After working with Vue/React, doing GUI apps "the old way" feels like writing quick-sort in assembly language.
You will notice that it is surprisingly simpler than React.
React got so popular because React implemented it very nicely in the web browsers, not because it brought new concepts.
When using C++, looks like you have to use things like QAbstractListModel https://qmlbook.github.io/ch16-qtcpp/qtcpp.html#a-simple-mod...
Even without C++, there are things like ListModel and ListElement https://qmlbook.github.io/ch07-modelview/modelview.html
This is a far cry from React where you just use language-native data structures directly (you only need to do mutations in specific ways).
As far as I can tell, you also can't just use native language constructs like functions/ifs/.map, etc. to compose the UI elements. Instead you have containers like Repeater.
It seems very different to me compared to React, like built around a significantly different philosophy.
Even defining what a "native" type is seems like it would be fraught. I guess the c++ way would be to accept begin and end iterators - but of what type?
WPF still has the same kind of approach and to be terribly honest, Silverlight, as hated as it was, did it before React.
It adds single 5mb DLL to the equation.
Yet it allows to build native applications where UI structure is defined by HTML, style by CSS and reactivity by script or by Rust (or by C++, Go, Python, etc.)
"Sciter makes GUI programming genuinely fun!" https://sciter.com/forums/topic/attach-detach-behavior/#post...
However it has one major issue : it's proprietary... And that's a no go for many project / companies.
I would not make my entire business depend on a blob which I have no control on it.
You can buy Sciter License.
This way you a) will solve the problem and b) will help further Sciter development.
Otherwise you don't really need Sciter sources.
As of HTML: most of HTML5 constructs (I participated in HTML5 spec development at W3C as Invited Expert)
As of CSS: CSS2.1 in full. Some practically useful modules of CSS3 - transitions, transforms, etc.
I haven't added FlexBox as I think it is an architectural disaster - will not survive on the long run. Instead Sciter offers "flow and flex units" module: https://sciter.com/docs/flex-flow/flex-layout.htm that covers as FlexBox as Grid in unified manner. Yet check this: https://terrainformatica.com/w3/flex-layout/flex-vs-flexbox....
Yet, I've added quite a lot of HTML/CSS features that are the must for specifically desktop UI:
- <menu class=popup> and <popup> elements and their CSS support - windowed DOM elements that are rendered outside of main window canvas.
- <frame type=pager> - print preview and print feature.
- view.dialog("some.html"), view.window("some.html"), view.msgbox("some.html") - HTML defined windows and dialogs.
- <htmlarea> - native WYSIWYG HTML editing widget.
More on this: https://sciter.com/developers/for-web-programmers/
McAfee - not.
But these AV and security providers - yes:
- ESET (NOD32 Antivirus)
- AVAST Antivirus
- AVG Antivirus
- Bitdefender Antivirus
- Comodo Antivirus
- Dr.WEB Antivirus
- WEBROOT Security Antivirus
- 360 ( 360.cn ) Antivirus
- Secui antivirus
So Sciter's code works on any PC that has any of these installed.
[0]https://github.com/revery-ui/revery [1]https://github.com/cljfx/cljfx
https://github.com/fn-fx/fn-fx
Fn-fx is a more complex beast and no one seems to really know how it all works internally except the creator who isn't maintaining it.. so unfortunately it's in a semi unmaintained state. But the interface was a bit cleaner when I tried it
CSS had to get WPF grid design in order to not be a poor table sibling in what concerns layouts.
While WPF is backed by DirectX, CSS requires playing with Z order, so that some browsers might eventually put the rendering into the GPU, but one needs to take care because space is limited.
While I can render anything down to pixel level control, if I wish to do so, I am still waiting for Houdini and worlets to actually become available.
And the whole template language with events and theming for low level customisation of control behaviours? Nowhere to be seen.
With the same source code, my applications are natively compiled to:
Linux, Windows, MacOS, Android, iOS, QNX, Browser (WebAssembly / WebGL Streaming)
And you are free to theme it as you like, one example:
I wrote my first GTK+ program in my high school years in 2014-2015, and I can't think of any native toolkit ever approached the ease of work of GTK.
Which other native UI toolkits do you have experience with to make that claim?
Here's very simple to implement change which saves dozens of MB of IO bandwidth every startup: https://github.com/Microsoft/vscode/issues/61343
"We will very likely not do this."
I remember someone telling me that it’s not as good a fit but I can’t remember why that is.
They provide some tutorial links in the README.
The usual workaround is to wrap the C++ API into a C API.
I wrote a .NET Core/QML interior layer.
No idea why anyone would want to do this, because it seems you’ll be hated by half the Linux users for using gtk, but seems cool for personal projects.
I did not see people asking for GTK apps to be ported to Qt (if it happens is much lower then the rewrite it in rust phenomenon) , there were a number of projects that switched from GTK2 to Qt instead of GTK3 because they re-evaluated the toolkits and decided Qt was better for those projects.
Actually this is a real benefit.
It means no one will show up and convince people on your project to waste time packaging your software for [pick a Linux Distribution].
Now instead of getting user reports from a hall of mirrors of various packages for various distros with various states of dependency disrepair, you've got one central source of truth.
Due to desktop Linux fragmentation you will never please everyone, but everyone will feel entitled that your care about their snowflake distribution.
It's kind of embarrassing (or a testament to hard of a problem that it is) the state we are in because of (IMO) Microsoft/Apple/Canonical.
If they had a really, really good GUI foundation, it'd be a quick job to do a minimal Slack or Things for macOS clone in C++/Rust.
Edit: Seems people get irritated about what the users want. But that's not really up to us to decide as coders...
WPF can also be styled extensively, at least in theory, but this is much harder to accomplish. MS has developed Blend to assist with this, but you still need a deep knowledge of the internal hierarchy if elements of a widget to get good results.
Given the choice I would prever native UI any day.
The smart phone design aesthetic has taken over and is now constantly being used in places where the whitespace is a waste and the giant buttons make things terrible. Canonical may have finally given up on "convergence" but few other companies have woken up.
> If they had a really, really good GUI foundation.
It is not true at all. They already have really nice GUI foundations. Brining the web tech to desktop apps gots so popular because the web is won so that devs are forced to make web apps, not because it is better than the "traditional" GUI development.
Do I want to code in 3 languages, let the app swallow copious amounts of RAM and break behaviours that users expect? NO. Will they do a hard pass on software if it's not pretty enough? YES.
If you want to chalk it up to not enough good themes in QT/JavaFX, that's fine two less languages and less packaging for them to worry about. But until those themes exist that makes the user turn its head...
But we need them because they're the only cross-platform and device-independent solution.
If you're going to allow a runtime as bloated and complicated as a web browser to count as "cross platform" then you'll have to admit that Java met those same criteria 2 decades ago.
Java doesn't even come pre-installed on a normal machine.
It's not about technical considerations, it's just that one cross-platform runtime has won.
Speed doesn't come into it that much. Current desktop systems are fast enough such that most GUI manipulation just isn't a speed bottleneck, except for things like realtime graphics or something.
Complex web UIs in general have perceivable latency and are perceivably sluggish for me. I noticed how bad the problem is when I noticed some latency in responding to mouse clicks in a React application I was developing. After unsuccessfully trying to bring it down, I compared it to a handful of other web apps I use routinely, and then a handful of native desktop apps. When I did that it became depressingly obvious how conditioned I am to the poor responsiveness of these UIs.
For the record, this is all on a 4-core i7 machine with an NVMe SSD and 16GB of RAM. It's fair to say that the average machine where these are used is far lower.
And obviously, most users don't seem to care that much. Otherwise you wouldn't have these web applications. Developers would spend the multiple amounts of work, gladly paid by their employers and investors and gladly reimbursed by the users through licensing fees, necessary to make hot-to-the touch user interfaces for everything we use computers nowadays.
Rust drives all of that down somewhat. Also webassembly will bring the ability to trust almost-native machine code.
Most of it is about trust, and you'll see certain web APIs built around asking for permission to access the camera, or image files or whatever.
Desktop apps often can't really trust each other. It's not a problem right now, but if you could XSS something into a game or social app which, with user-level permissions on your desktop could remote-control (for example click-simulating) your electron banking app, that would be really bad. Just a contrived example, but with great power comes great exploitability.
For the electron apps I use, they are generally available online as a website anyways, so I just use that, and get the whole common instance thing for free.
I think this isn’t really a problem of Electron, but more to Chromium. We bundle python inside the binary and it’s fine. I don’t think I’ll hate command line programs that embed nodejs inside the binary.
It’s the Chromium that’s problematic, and just by switching Chromium to the native WebView will make Electron apps much, much more lightweight.