hypersig_desktop/lib.rs
1//! Runs a hypersig application in a desktop window.
2//!
3//! The application's [`Router`](axum::Router) is served in the same process,
4//! on `127.0.0.1`, and the window shows it in the operating system's web
5//! view: `WKWebView` on macOS, `WebView2` on Windows, and `WebKitGTK` on Linux.
6//! The pages, actions, and patches are the ones the application serves on the
7//! web, so only `main` changes:
8//!
9//! ```no_run
10//! use axum::Router;
11//! use hypersig::{Page, Route, RouterExt, Signals};
12//! use hypersig_desktop::App;
13//! use serde::{Deserialize, Serialize};
14//!
15//! #[derive(Signals, Serialize, Deserialize)]
16//! struct Home {}
17//!
18//! hypersig::routes! {
19//! fn pages(Router<()>);
20//! const INDEX: Route = "/" => index;
21//! }
22//!
23//! async fn index((): (), (): ()) -> Page<Home> {
24//! Page::new("Home", Home {})
25//! }
26//!
27//! const APP: App = App::new("dev.example.home");
28//!
29//! fn main() -> Result<(), hypersig_desktop::Error> {
30//! APP.size(800, 600).run(&INDEX.url(()), |_os| async {
31//! pages(Router::new()).hypersig_runtime()
32//! })
33//! }
34//! ```
35//!
36//! Every window shows a page of the same server, so two windows that open a
37//! stream such as a [`StreamAction`](hypersig::StreamAction) fed by one
38//! broadcast channel show the same state, as two browser tabs do. A handler
39//! opens another window with [`Windows::open`], through the [`Os`] that
40//! [`App::run`] gives the setup, and a page's `window.open`, or a link with
41//! `target="_blank"`, opens one too. Closing the last window closes the
42//! application, on macOS as well, unless `Os::tray` shows a tray icon.
43//!
44//! The windows reach the server over HTTP/1.1, and a web view keeps at most
45//! six connections to one host for all its windows together. A stream holds
46//! its connection open, so with six windows that each hold a stream, a seventh
47//! request waits until one closes; in `WKWebView`, the todo example's sixth
48//! window left the first window's actions unanswered. Until the windows reach
49//! the server without HTTP, an application keeps at most five streams open
50//! across all its windows.
51//!
52//! A handler reaches the system through the same [`Os`]: [`Os::dialog`]
53//! shows a file dialog, [`Os::clipboard`] reads and writes the clipboard's
54//! text, and [`Os::open_external`] opens a URL in the person's browser, as a
55//! window does with a link to another site. With the `testing` feature,
56//! `testing::FakeOs` stands in for the system, so a test calls the handlers
57//! with dialogs it answers and reads back what they did, and clicks through
58//! the pages with [`Page::simulate`](hypersig::Page::simulate), without a
59//! window. `examples/notes` is written and tested this way.
60//!
61//! A page uses the microphone or the camera through `getUserMedia` only
62//! when [`App::allow`] names that [`Device`], and the web view refuses the
63//! others without asking the person.
64//!
65//! [`Os::shortcut`] registers a [`Shortcut`] the application hears while
66//! another application has focus, as push-to-talk needs.
67//!
68//! With the `updater` feature, `App::updates` names where new versions are
69//! announced and the key that signs them, and the `Updater` that `App::run`
70//! then hands the setup finds a newer version, downloads it, checks its signature, and installs it, in the
71//! format `cargo-packager` builds and signs packages in.
72//!
73//! The server answers only the application's own windows. It listens on
74//! `127.0.0.1` alone, refuses a request whose `Host` is not that address, and
75//! refuses one without the cookie the first window receives when it opens,
76//! which the others share. No window leaves the application's origin.
77//!
78//! The server listens on the port it listened on the previous time the
79//! application ran, so that the signals a page keeps in the browser with
80//! [`Page::persist`](hypersig::Page::persist) are found again. When another
81//! program holds that port, the server takes another one, and those signals
82//! start over.
83//!
84//! A second copy of the application started while the first runs stops with
85//! [`ErrorKind::AlreadyRunning`].
86//!
87//! `WKWebView`, which shows the window on macOS, does not send a `Secure`
88//! cookie to `http://127.0.0.1`, and neither does Playwright's build of
89//! `WebKit`, the engine of `WebKitGTK` on Linux. A session layer that marks
90//! its cookie `Secure`, as `tower-sessions` does by default, signs no one in
91//! inside the window, so the desktop build of such an application sets its
92//! cookie without `Secure`. The checks above keep other programs off the
93//! server.
94//!
95//! On macOS the menu bar holds the application, Edit, and Window menus,
96//! because macOS sends the shortcuts for copying, pasting, closing, and
97//! quitting to the menu rather than to the web view.
98//!
99//! # Smoke runs
100//!
101//! In a build with the `smoke` feature, for CI and benchmarks, and with
102//! `HYPERSIG_DESKTOP_SMOKE` set, the application checks that its first
103//! page is one hypersig rendered, writes to standard output how many
104//! milliseconds after [`App::run`] the page was shown, and closes. It ends
105//! with an error when the page is not hypersig's, as when the server refused
106//! the window, or does not load within 10 seconds.
107//!
108//! With `HYPERSIG_DESKTOP_SMOKE_SCRIPT` set to the path of a JavaScript file,
109//! the run then evaluates that file in the first page and in the page of
110//! every other window, and closes once the first page sets
111//! `window.hypersigSmoke` to `"pass"`. Anything else it sets, or nothing
112//! within 20 seconds, ends the run with an error. An empty
113//! `HYPERSIG_DESKTOP_SMOKE_SCRIPT` counts as unset.
114
115mod app;
116mod arrange;
117mod autostart;
118mod badge;
119mod clipboard;
120mod device;
121mod error;
122mod frame;
123#[cfg(target_os = "linux")]
124mod framing;
125mod guard;
126mod handoff;
127mod instance;
128mod item;
129mod menu;
130mod message;
131mod notification;
132mod os;
133mod port;
134mod run;
135mod shortcut;
136mod smoke;
137#[cfg(feature = "testing")]
138pub mod testing;
139mod tray;
140#[cfg(feature = "updater")]
141mod updates;
142mod window;
143
144pub use app::{App, IntoRouter};
145pub use arrange::{Layer, Placement};
146pub use device::Device;
147pub use error::{Error, ErrorKind};
148pub use frame::{Frame, MovesWindow};
149pub use handoff::{Launch, Launches};
150pub use item::{Clicks, MenuItem};
151pub use menu::Menu;
152pub use message::{Answer, Level, Message};
153pub use notification::{Notification, Notified};
154pub use os::{Autostart, Clipboard, Dialog, Os, Window, Windows};
155pub use shortcut::{Key, Modifiers, Press, Presses, Shortcut};
156pub use tray::{Tray, TrayItem};
157#[cfg(feature = "updater")]
158pub use updates::{Downloaded, Update, Updater, Updates};