Overview
hypersig_desktop serves an application's Router in the same process and shows its pages in the system's web view. Only main changes.
Add the crate
Add hypersig_desktop next to the crates a hypersig application already uses: hypersig and axum for the router, and serde for the signal structs.
[dependencies]
axum = "0.8"
hypersig = "0.18"
hypersig_desktop = "0.18"
serde = { version = "1", features = ["derive"] }
# Only for code that calls Tokio itself, such as `tokio::spawn`.
tokio = { version = "1", features = ["rt"] }App::run starts the Tokio runtime itself, so main needs no tokio dependency. Add tokio when the application's own code calls it, as tokio::spawn does, which needs the rt feature.
On Linux, the crate links GTK and WebKitGTK at build time, and reads the desktop's color scheme over D-Bus. On Debian and Ubuntu, the repository's CI installs libwebkit2gtk-4.1-dev and libdbus-1-dev for these.
Open the first window
The routes, handlers, and pages are the ones the application serves on the web. Only main changes: it names the application with App::new, and calls App::run with the first page and a closure that builds the router.
use axum::Router;
use hypersig::{Page, Route, RouterExt, Signals};
use hypersig_desktop::App;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Home {}
hypersig::routes! {
fn pages(Router<()>);
const INDEX: Route = "/" => index;
}
async fn index((): (), (): ()) -> Page<Home> {
Page::new("Home", Home {})
}
const APP: App = App::new("dev.example.home");
fn main() -> Result<(), hypersig_desktop::Error> {
APP.size(800, 600).run(&INDEX.url(()), |_os| async {
pages(Router::new()).hypersig_runtime()
})
}cargo run opens an 800 by 600 window that shows GET /, titled Home after the page's <title>. The first page is a Url, which only Route::url produces, so run cannot be given a path that no route declares. The closure receives an Os, through which the handlers open windows and reach the system, and returns a future that produces the Router. The application covers each argument and what run returns.
One package can build both: examples/counter serves the page on port 3000 with cargo run -p example-counter, and opens it in a window with --features desktop, which makes hypersig_desktop an optional dependency and picks one of two main functions:
#[cfg(not(feature = "desktop"))]
#[tokio::main]
async fn main() -> std::io::Result<()> {
hypersig::serve(app(), 3000).await
}
/// With `--features desktop`, the counter opens in a window of its own.
#[cfg(feature = "desktop")]
fn main() -> Result<(), hypersig_desktop::Error> {
const APP: hypersig_desktop::App = hypersig_desktop::App::new("dev.hypersig.example-counter");
APP.size(560, 480)
.run(&INDEX.url(()), |_os| async { app() })
}Why `main` is not `#[tokio::main]`
App::run keeps the thread that calls it for the windows and their event loop, and on macOS that thread has to be the main thread; run panics on any other. It starts a multi-threaded Tokio runtime of its own and runs the server and the closure's future there. So main is a plain fn that returns Result<(), hypersig_desktop::Error>.
main with #[tokio::main] that calls App::run compiles, and run returns an error of the kind ErrorKind::InRuntime as it starts, without opening a window.What runs where
The server and the windows are one process. The Router the closure returns is served over HTTP/1.1 on 127.0.0.1, and each window is the system's web view, which requests the pages and sends the actions and streams to that server, as a browser tab does. The server answers only the application's own windows; Who the server answers covers how.
| System | Web view | What the system needs |
|---|---|---|
| macOS | WKWebView | Nothing more; WKWebView is part of macOS |
| Windows | WebView2 | The WebView2 Runtime, which Windows 11 includes |
| Linux | WebKitGTK | GTK 3 and WebKitGTK 4.1 |
Limits covers what the windows share, such as the connections to the server.
Features
No feature is on by default. Each one adds items or behavior for one purpose:
| Feature | Adds | Page |
|---|---|---|
testing | testing::FakeOs, an Os whose dialogs, clipboard, and windows a test answers and reads | Testing without a window |
updater | App::updates and Updater, which find, download, verify, and install a new version | Updates |
smoke | Smoke runs, started by the HYPERSIG_DESKTOP_SMOKE and HYPERSIG_DESKTOP_SMOKE_SCRIPT environment variables; a build without it ignores both | Smoke runs in CI |
testing is for tests only, so examples/notes turns it on in [dev-dependencies], and the binary it ships is built without it:
[dependencies]
hypersig_desktop = "0.18"
[dev-dependencies]
hypersig_desktop = { version = "0.18", features = ["testing"] }The pages of this section
| Page | Covers |
|---|---|
| Why hypersig_desktop | The applications it suits, what it gives them, and its start-up and size beside Tauri and Electron |
| The application | App::new, the window's size, App::data_dir, App::run, and its errors |
| Opening windows | Windows::open, Window, and windows a page opens |
| File dialogs | Os::dialog for opening and saving files |
| The clipboard | Os::clipboard and the clipboard's text |
| Links to other sites | Links that leave the application, and Os::open_external |
| Testing without a window | testing::FakeOs with Page::simulate |
| Smoke runs in CI | The smoke feature and its environment variables |
| Packaging | cargo packager and the packages for each system |
| Updates | The updater feature, App::updates, and Updater |
| Who the server answers | What the server refuses, and cookies inside the window |
| Signals kept between runs | The port the server listens on, and Page::persist |
| Limits | What the windows share, and what each system needs |
Next, The application covers what App holds and what App::run does from the call to its return.