Skip to main content

Crate jflib

Crate jflib 

Source
Expand description

§jflib

JFLib is a GUI library and everything it stands on, without the standard library: windows and controls, text, images in many formats, the GPU, files, the network, documents, and a runtime of its own. This one dependency brings all of it; a program links only what it uses. The same app runs on Windows, Linux, macOS, iOS, Android and in the browser.

docs/GUIDE.md in this package explains how an app is written, docs/API.md lists every public item. Both lie below ~/.cargo/registry/src/ once Cargo has downloaded the crate; the guide is also on docs.rs as jflib::guide, the API reference on GitHub.

§Quick start

cargo install cargo-jflib
cargo jflib new hallo         # or new --std hallo: an app with std (below)
cd hallo
cargo run
cargo jflib test              # runs tests/*.uis
cargo jflib watch             # hot reload while you edit
cargo jflib web --serve       # the app in the browser
cargo jflib android --install # the app on a phone (Android SDK and NDK)
cargo jflib mac --run         # on a Mac: the app as NAME.app
cargo jflib package           # a setup for Windows and a .deb, every style inside
                              #   (--formats all: also MSIX, AppImage, RPM, Flatpak;
                              #   on a Mac --formats dmg)

cargo jflib new writes the template that lies in this package as template/. Without the tool, copy it (cp -r ~/.cargo/registry/src/*/jflib-0.2.*/template hallo, then rename Cargo.toml.in to Cargo.toml).

FileWhat it is for
Cargo.tomljflib, and jflib-build for the build script; the profiles below
build.rsjflib_build::app(), or jflib_build::console() for a command-line program
.cargo/config.tomlthe linker (rust-lld, it comes with rustup) and what Linux needs (with std only +crt-static for Windows)
src/lib.rsthe app: a ui::App and ui::main!
src/main.rsthe program, it only links the app, so the same code builds for the browser
PERMISSIONSwhat the app may do; without the file everything
PACKAGEits name, id and file types for --register
assets/ (optional)files the app reads as app://NAME
res/app.ico (optional)its icon on Windows
tests/smoke.uisa test script, run with --script

Names come from the crate in code: use jflib::{style, ui};, then ui::Ui, plat::read_file.

§Apps with std

JFLib is no_std and brings everything an app needs from one hand. Write apps no_std as well where you can, and look in docs/API.md before adding a crate. An app that needs a crate with std turns on the feature std (cargo jflib new --std writes such an app):

jflib = { features = ["std"], version = "0.2" }

Then src/lib.rs has no #![no_std], src/main.rs is extern crate hallo; fn main() { jflib::plat::std_start() }, .cargo/config.toml only links the C runtime in on Windows (+crt-static) and PERMISSIONS covers JFLib’s own calls only. Linux programs with std are built on Linux (cc, glibc), macOS and iOS ones on a Mac; in the browser std has no clock, files or threads and there is no threads mode; on Android std::fs needs the app’s real folder (docs/GUIDE.md, “An app with std”).

Don’t add a second window loop or UI crate (winit, eframe, egui, SDL): ui::launch is the whole stack, not one crate of several as with egui and eframe. Don’t run an async runtime (tokio) on the UI thread: JFLib runs async blocks itself (ui.spawn); a crate that needs tokio gets it on a thread of its own, with results back through plat::wake().

§The profiles

Cargo takes no profile from a dependency, so every project needs these in its Cargo.toml. Without panic = "abort" the build stops at ui::main! with a message that says so. An app with std that must survive panics of its own code turns on the feature unwind instead of std and writes panic = "unwind" in both profiles: a panic it catches with std::panic::catch_unwind leaves it running, one nobody catches ends it with JFLib’s crash report (docs/GUIDE.md, “An app with std”).

[profile.dev]
panic = "abort"
opt-level = 1
debug = 0
debug-assertions = false
overflow-checks = false

[profile.release]
panic = "abort"
opt-level = 2
codegen-units = 1
lto = "fat"
debug = 0
strip = "symbols"

§Targets

TargetBuildNotes
Windowscargo build; ARM64: --target aarch64-pc-windows-msvcrust-lld; the program links without import libraries, the build scripts need what Rust’s msvc toolchain always needs (the Visual Studio Build Tools)
Linuxcargo build --target x86_64-unknown-linux-gnu; ARM64: --target aarch64-unknown-linux-gnua window links against glibc (the GPU); a command-line program can be one static file (.cargo/config.toml)
macOScargo build on a Mac; cargo jflib mac makes NAME.appApple silicon; Apple’s linker needs nothing in .cargo/config.toml
iOScargo build --target aarch64-apple-ios (or aarch64-apple-ios-sim) on a Macdevices and the simulator; cargo jflib makes no iOS app yet
Browsercargo jflib webscalar, SIMD and with threads, the page around them and the licences in target/jflib-web/NAME/; threads need rustup component add rust-src
Androidcargo jflib androidan APK for arm64-v8a (or --abi x86_64 for the emulator) in target/jflib-android/; needs the Android SDK, the NDK, a JDK and rustup target add aarch64-linux-android; the app’s code in src/lib.rs as the template has it

The style: jflib::style is WinUI unless the environment variable JFLIB_STYLE names another (classic, kde, macos, netlab). All five are always there under their own names for the switch at run time (jflib::style_kde::STYLE).

§Good to know

  • A file added later: after adding PERMISSIONS, PACKAGE, assets/ or res/app.ico to a project that had none, run cargo clean -p NAME once. The build script watches only what exists; watching a missing file would compile the program again on every build.
  • Always --target for Linux, on Linux too: without it Cargo hands the target’s flags to the build scripts and proc macros as well, and the static relocation model breaks them. With it they are linked by cc as usual and only the program by rust-lld. Without the two linker flags cc links the program too.
  • Paths in a release program: panic messages name the source files with the build machine’s paths. RUSTFLAGS="--remap-path-prefix=$HOME=~" removes them.
  • Versions: the crates below jflib (jflib-ui, jflib-plat, …) always come in the same version and change together. Only the way through jflib is promised, together with jflib-build and cargo jflib (docs/VERSIONING.md, “The crates on crates.io”).
  • Your own system calls: JFLib checks a program’s file, network and process access against its PERMISSIONS in its platform layer. A crate that calls the system itself goes around it; only the sandbox line confines it, as far as the operating system enforces the manifest (docs/PERMISSIONS.md, “OS sandbox”).

§What’s new in 0.2.0

JFLib runs natively on macOS and iOS now, with windows through AppKit and UIKit, VoiceOver, the app’s menus in the Mac’s menu bar and a Metal backend for the GPU, and cargo jflib makes a Mac app and its disk image. Windows and Linux run on ARM64 as well. Three crates join the facade: jflib::render3d, a 3D engine on the GPU layer, jflib::gltf, which imports glTF 2.0 models, and jflib::render2d, a 2D renderer for CAD drawings. NetLab is an official style (jflib::style_netlab, JFLIB_STYLE=netlab), and the feature unwind lets an app with std survive the panics it catches. The UI learned what phones and tablets and apps that host web content need, and shows GPU content beside its widgets. Programs link again with Rust 1.99, which failed on Windows with 0.1.0, and a round of fixes closes holes against hostile documents, images and packets and removes deadlocks and races. plat::dl_sym and plat::dl_close are unsafe fn now. Every crate has a README and a changelog of its own; the library’s changelog sums them up and links to each.

§License

MIT or Apache-2.0, at your choice. Parts carry third-party licenses in addition, named in each crate’s manifest and its licenses/ folder.

Re-exports§

pub use self::style_winui as style;
pub use rt;
pub use agent_core;
pub use plat;
pub use base;
pub use agent_io;
pub use gfx;
pub use img_core;
pub use archive;
pub use doc_core;
pub use doc_layout;
pub use doc_io;
pub use img_png;
pub use text;
pub use img_jpeg;
pub use img_bmp;
pub use img_gif;
pub use img_tga;
pub use img_tiff;
pub use img_webp;
pub use img_hevc;
pub use img_av1;
pub use img_heif;
pub use img_avif;
pub use img_jxl;
pub use img_jxr;
pub use img_dds;
pub use img_pdn;
pub use fx;
pub use img_svg;
pub use img_all;
pub use spell;
pub use ui;
pub use doc_view;
pub use datetime;
pub use crypto;
pub use compress;
pub use shader;
pub use wgsl;
pub use netpkt;
pub use gpu;
pub use eapm;
pub use render3d;
pub use render2d;
pub use gltf;
pub use cas;
pub use datastore;
pub use netio;
pub use update;
pub use img_jxl_enc;

Modules§

guide
How an app is written with JFLib: the guide of the repository (docs/GUIDE.md), which also lies in this crate’s package as docs/GUIDE.md. Only documentation, no code: it exists when rustdoc builds the docs (docs.rs), its links lead to the repository on GitHub (build.rs).
style_classic
the classic look: grey 3D edges, dark blue title bars
style_kde
KDE Plasma (Breeze)
style_macos
macOS: white buttons, blue selection, the traffic lights on the left
style_netlab
NetLab: sober grey surfaces, a clear frame on every control, compact
style_winui
WinUI 3 (Fluent): STYLE for ui::launch and the style switch at run time