日本語

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.

Cargo.toml
[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:

examples/counter/src/main.rs
#[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>.

A 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.

SystemWeb viewWhat the system needs
macOSWKWebViewNothing more; WKWebView is part of macOS
WindowsWebView2The WebView2 Runtime, which Windows 11 includes
LinuxWebKitGTKGTK 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:

FeatureAddsPage
testingtesting::FakeOs, an Os whose dialogs, clipboard, and windows a test answers and readsTesting without a window
updaterApp::updates and Updater, which find, download, verify, and install a new versionUpdates
smokeSmoke runs, started by the HYPERSIG_DESKTOP_SMOKE and HYPERSIG_DESKTOP_SMOKE_SCRIPT environment variables; a build without it ignores bothSmoke 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:

Cargo.toml
[dependencies]
hypersig_desktop = "0.18"

[dev-dependencies]
hypersig_desktop = { version = "0.18", features = ["testing"] }

The pages of this section

PageCovers
Why hypersig_desktopThe applications it suits, what it gives them, and its start-up and size beside Tauri and Electron
The applicationApp::new, the window's size, App::data_dir, App::run, and its errors
Opening windowsWindows::open, Window, and windows a page opens
File dialogsOs::dialog for opening and saving files
The clipboardOs::clipboard and the clipboard's text
Links to other sitesLinks that leave the application, and Os::open_external
Testing without a windowtesting::FakeOs with Page::simulate
Smoke runs in CIThe smoke feature and its environment variables
Packagingcargo packager and the packages for each system
UpdatesThe updater feature, App::updates, and Updater
Who the server answersWhat the server refuses, and cookies inside the window
Signals kept between runsThe port the server listens on, and Page::persist
LimitsWhat 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.