Expand description
Runs a hypersig application in a desktop window.
The application’s Router is served in the same process,
on 127.0.0.1, and the window shows it in the operating system’s web
view: WKWebView on macOS, WebView2 on Windows, and WebKitGTK on Linux.
The pages, actions, and patches are the ones the application serves on the
web, so only main changes:
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()
})
}Every window shows a page of the same server, so two windows that open a
stream such as a StreamAction fed by one
broadcast channel show the same state, as two browser tabs do. A handler
opens another window with Windows::open, through the Os that
App::run gives the setup, and a page’s window.open, or a link with
target="_blank", opens one too. Closing the last window closes the
application, on macOS as well, unless Os::tray shows a tray icon.
The windows reach the server over HTTP/1.1, and a web view keeps at most
six connections to one host for all its windows together. A stream holds
its connection open, so with six windows that each hold a stream, a seventh
request waits until one closes; in WKWebView, the todo example’s sixth
window left the first window’s actions unanswered. Until the windows reach
the server without HTTP, an application keeps at most five streams open
across all its windows.
A handler reaches the system through the same Os: Os::dialog
shows a file dialog, Os::clipboard reads and writes the clipboard’s
text, and Os::open_external opens a URL in the person’s browser, as a
window does with a link to another site. With the testing feature,
testing::FakeOs stands in for the system, so a test calls the handlers
with dialogs it answers and reads back what they did, and clicks through
the pages with Page::simulate, without a
window. examples/notes is written and tested this way.
A page uses the microphone or the camera through getUserMedia only
when App::allow names that Device, and the web view refuses the
others without asking the person.
Os::shortcut registers a Shortcut the application hears while
another application has focus, as push-to-talk needs.
With the updater feature, App::updates names where new versions are
announced and the key that signs them, and the Updater that App::run
then hands the setup finds a newer version, downloads it, checks its signature, and installs it, in the
format cargo-packager builds and signs packages in.
The server answers only the application’s own windows. It listens on
127.0.0.1 alone, refuses a request whose Host is not that address, and
refuses one without the cookie the first window receives when it opens,
which the others share. No window leaves the application’s origin.
The server listens on the port it listened on the previous time the
application ran, so that the signals a page keeps in the browser with
Page::persist are found again. When another
program holds that port, the server takes another one, and those signals
start over.
A second copy of the application started while the first runs stops with
ErrorKind::AlreadyRunning.
WKWebView, which shows the window on macOS, does not send a Secure
cookie to http://127.0.0.1, and neither does Playwright’s build of
WebKit, the engine of WebKitGTK on Linux. A session layer that marks
its cookie Secure, as tower-sessions does by default, signs no one in
inside the window, so the desktop build of such an application sets its
cookie without Secure. The checks above keep other programs off the
server.
On macOS the menu bar holds the application, Edit, and Window menus, because macOS sends the shortcuts for copying, pasting, closing, and quitting to the menu rather than to the web view.
§Smoke runs
In a build with the smoke feature, for CI and benchmarks, and with
HYPERSIG_DESKTOP_SMOKE set, the application checks that its first
page is one hypersig rendered, writes to standard output how many
milliseconds after App::run the page was shown, and closes. It ends
with an error when the page is not hypersig’s, as when the server refused
the window, or does not load within 10 seconds.
With HYPERSIG_DESKTOP_SMOKE_SCRIPT set to the path of a JavaScript file,
the run then evaluates that file in the first page and in the page of
every other window, and closes once the first page sets
window.hypersigSmoke to "pass". Anything else it sets, or nothing
within 20 seconds, ends the run with an error. An empty
HYPERSIG_DESKTOP_SMOKE_SCRIPT counts as unset.
Modules§
Structs§
- App
- A desktop application, named by a reverse domain name.
- Autostart
- Whether the application starts at login, from
Os::autostart. - Clicks
- The clicks on a
MenuItem, fromMenuItem::clicks. - Clipboard
- The system clipboard, from
Os::clipboard. - Dialog
- A file dialog, from
Os::dialog, shown by one of its async methods. - Downloaded
- A package
Update::downloaddownloaded and verified. - Error
- Why an operation of this crate failed.
- Launch
- Another start of the application, which handed its command line to this
copy instead of running, from
Launches::next. - Launches
- The launches handed to this copy after
Os::launcheswas called. - Menu
- A menu of the menu bar, for
Os::menus. - Menu
Item - An item of a menu, whose
clicksthe application waits on. - Message
- A message box’s title and text, for
Os::telland the others. - Modifiers
- The modifier keys of a
Shortcut, joined withand. - Notification
- A notification for
Os::notify: a title and an optional body. - Notified
- A notification the system showed, from
Os::notify. - Os
- The operating system’s windows and services, for the application’s handlers.
- Presses
- The presses and releases of a
Shortcut, fromOs::shortcut. - Shortcut
- A key and its modifiers, for
Os::shortcut. - Tray
- An icon in the system tray with a menu, for
Os::tray. - Update
- A newer version, from
Updater::check. - Updater
- What finds the application’s new versions, from the setup of
App::run. - Updates
- Where an application finds its new versions, and the key that signs them.
- Window
- A window
Windows::openopened. - Windows
- The application’s windows, from
Os::windows.
Enums§
- Answer
- The button the person chose in
Os::ask’s message box. - Device
- A capture device the pages may use, for
App::allow. - Error
Kind - The kind of an
Error. - Frame
- What the system draws around a window, for
App::frame. - Key
- The key of a
Shortcut, named by where it is on a US keyboard. - Layer
- Whether a window stays above the others, for
Window::layer. - Level
- How serious a
Messageis, which picks the icon the system shows. - Placement
- How a window fills the screen, for
Window::place. - Press
- Whether a
Shortcutwent down or came up.
Traits§
- Into
Router - What the future given to
App::runreturns: a router, or why there is none. - Moves
Window - A part of a page that moves the window when the person drags it.