A browser app joins the page's desktop: on a page with forge/web its main surface, overlays and subsurfaces are surfaces of that desktop, all painted through one WebGL context
The browser backend ran an app alone on a canvas over the page. An Eydos desktop in a browser needs crustace as its shell and other ltk apps as its windows on one page, managed by forge/web (forge's web/ directory), which publishes `globalThis.forgeWeb` and stacks, places and focuses the surfaces its clients ask for (forge/web/DESIGN.md). The desktop's client. On a page where `globalThis.forgeWeb` exists and `set_canvas` named no canvas, `platform/web/desktop.rs` connects as the app's `app_id` and asks for the main surface in the role the app's hooks give, as a Wayland compositor reads them: `shell_mode`, and for a layer `layer_anchor`, `layer_size`, `exclusive_zone` and `keyboard_exclusive`, for a window `app_id` and `window_size_hint`, and for `SessionLock` a full overlay that holds the keyboard. The app draws into the canvas forge/web gives and listens on the input element above it, takes its size and scale from forge/web's `configure` rather than from the window's `resize`, sends its input region in logical pixels when it changes, and leaves the keyboard focus to the desktop, which honours the surfaces that hold it exclusively. A `close` of the main surface reaches `App::on_close_requested`; on exit the surface is destroyed and the connection closed. The new `RunError::Desktop` says forge/web refused the main surface. Fullscreen inside a desktop does nothing yet. A page without a desktop, or one that named a canvas, runs the app as before. One WebGL context. A page holds few WebGL contexts, and the browser loses the oldest beyond them, while a shell has many surfaces. Every surface of an app now paints through one WebGL2 context on an `OffscreenCanvas`: the surface's FBO is blitted onto it, taken as an `ImageBitmap` and handed to the surface's own canvas, a `bitmaprenderer`, without a copy. Without WebGL2 each surface draws in software on a 2D context of its own. The context's loss is heard on the `OffscreenCanvas`, and now drops the GPU canvases of every surface, not only the main one's. Overlays. `platform/web/overlays.rs` keeps the app's overlays in step with `App::overlays()`, as the Wayland backend's `overlays_reconcile.rs` does with layer-shell: a new one becomes a layer surface or, for `Layer::Window`, a window, with its size and exclusive zone in logical pixels; a changed one gets an `update_role`; one that went is destroyed; forge/web's `close` of one delivers its `on_dismiss`. Popups anchored to a widget and the tooltip wait for a popup role forge/web does not have, and without a desktop an app still shows no overlays. What every backend does as overlays come and go, resolving an `OverlaySpec::size` and moving a drag and the focuses off an overlay that goes, moved out of the Wayland backend into `event_loop/overlays.rs` (`AppData::overlay_size`, `AppData::remove_overlay`); Wayland's behaviour is unchanged. Subsurfaces. `platform/web/subsurfaces.rs` makes each of `App::subsurfaces()` a surface of the desktop sized to its parent with an empty input region, painted on one shared GPU canvas only when it appears, its size changes or its `content_version` does, its frame then staying on its own canvas, and otherwise moved with an `update_role`, which the browser composes without painting. `App::on_subsurface_committed` follows each paint. A subsurface whose parent overlay was destroyed and made again is made again, since forge/web destroys a subsurface with its parent. `frame_presented` now knows whether a surface hosts subsurfaces, so while the app animates by subsurface motion only its frames keep coming without painting the parent, as Wayland keeps a parent's frame callbacks. Painting a view with no focus, hover or press on a canvas of its own, which both backends' subsurfaces do, moved out of the Wayland backend into `draw::paint_detached`. Input per surface. `input::listen` takes the surface's `SurfaceFocus`, which pointer, touch and keyboard focus events carry; a surface's blur no longer takes the keyboard from the surface focused after it. Listeners remove themselves when dropped, and a surface's live in its `WebSurfaceState`, so they go with it. The frames owed at the next animation frame are a list of surfaces instead of one, which with several surfaces left the main one waiting on a frame that never came. `examples/web_shell.rs` is a shell for the desktop: a background, a bar in the top layer with an exclusive zone, a panel that takes the keyboard, and a card on a subsurface that slides. `make web-example` builds it with web_hello (`WEB_EXAMPLES`); forge/web's `make hello` serves both at /shell.html. web-sys gains `OffscreenCanvas`, `ImageBitmap` and `ImageBitmapRenderingContext`. `CHANGELOG.md` describes the change. Not done. Popups and the tooltip in a desktop, overlays and subsurfaces without one, fullscreen and window moves through forge/web, and the cursor's shape over an overlay, which is still set on the main surface's element. The offscreen canvas is resized for each surface it paints, which reallocates its drawing buffer when the surfaces differ in size. There are no tests for the browser backend.
This commit is contained in:
+56
-24
@@ -1,56 +1,88 @@
|
||||
// SPDX-License-Identifier: LGPL-2.1-only
|
||||
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::cell::{ Cell, RefCell };
|
||||
use std::collections::HashMap;
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
|
||||
use web_sys::{ CanvasRenderingContext2d, Element, HtmlCanvasElement, WebGl2RenderingContext };
|
||||
use web_sys::{ Element, HtmlElement, OffscreenCanvas, WebGl2RenderingContext };
|
||||
|
||||
use crate::app::App;
|
||||
use crate::app::{ App, SubsurfaceId };
|
||||
use crate::event_loop::SurfaceFocus;
|
||||
use crate::render::Canvas;
|
||||
|
||||
use super::desktop::Connection;
|
||||
use super::run::Listener;
|
||||
use super::subsurfaces::WebSubsurface;
|
||||
|
||||
/// What the runtime's requests go through in a browser: the canvas, its context and what the loop must report back to the runtime.
|
||||
/// What the runtime's requests go through in a browser: the page's desktop when it has one, the one WebGL context every surface draws through, and what the loop must report back to the runtime.
|
||||
pub( crate ) struct WebPlatform<A: App>
|
||||
{
|
||||
pub canvas: HtmlCanvasElement,
|
||||
/// The canvas's WebGL2 context; `None` when the browser has none, and the canvas is drawn through `ctx2d`.
|
||||
pub webgl: Option<WebGl2RenderingContext>,
|
||||
/// forge/web, when the page has it; the app then has no canvas of its own over the page.
|
||||
pub desktop: Option<Connection>,
|
||||
/// Where every surface is painted on the GPU before its frame moves to the surface's own canvas, so the app holds one WebGL context however many surfaces it has. `None` without WebGL2: each surface is then drawn in software.
|
||||
pub offscreen: Option<OffscreenCanvas>,
|
||||
pub webgl: Option<WebGl2RenderingContext>,
|
||||
/// `webgl` as the GPU canvas draws through it, wrapped anew when the context is restored.
|
||||
pub gl: Option<Arc<glow::Context>>,
|
||||
pub ctx2d: Option<CanvasRenderingContext2d>,
|
||||
pub gl: Option<Arc<glow::Context>>,
|
||||
/// The browser took the WebGL context away; nothing is drawn until it gives it back.
|
||||
pub context_lost: bool,
|
||||
pub context_lost: bool,
|
||||
/// Where the cursor's shape is set: the element under the pointer that takes the main surface's input.
|
||||
pub cursor_target: HtmlElement,
|
||||
/// A hidden element padded by `env( safe-area-inset-* )`, which tells what a phone's notch and bars cover; only on ltk's own canvas over the page.
|
||||
pub inset_probe: Option<Element>,
|
||||
/// A frame was presented or asked for; the loop reports it at the next animation frame.
|
||||
pub frame_due: Cell<Option<SurfaceFocus>>,
|
||||
pub inset_probe: Option<Element>,
|
||||
/// The surfaces a frame was presented or asked for; the loop reports them at the next animation frame.
|
||||
pub frame_due: RefCell<Vec<SurfaceFocus>>,
|
||||
/// The id the next overlay's or subsurface's surface goes by in forge/web's protocol.
|
||||
pub next_surface: Cell<u32>,
|
||||
/// The subsurfaces of [`App::subsurfaces`], each a surface of the desktop.
|
||||
pub subsurfaces: HashMap<SubsurfaceId, WebSubsurface>,
|
||||
/// The GPU canvas every subsurface is painted on before its frame moves to its own canvas, which keeps it.
|
||||
pub subsurface_canvas: Option<Canvas>,
|
||||
/// Text input was asked for and not ended; ltk's own keyboard serves it.
|
||||
pub text_input: bool,
|
||||
/// The canvas's input and the window's `resize`, removed when the app exits.
|
||||
pub listeners: Vec<Listener>,
|
||||
_app: PhantomData<fn() -> A>,
|
||||
pub text_input: bool,
|
||||
/// The input of every surface, the window's `resize` and the context's loss, removed when the app exits.
|
||||
pub listeners: Vec<Listener>,
|
||||
_app: PhantomData<fn() -> A>,
|
||||
}
|
||||
|
||||
impl<A: App> WebPlatform<A>
|
||||
{
|
||||
pub( crate ) fn new( canvas: HtmlCanvasElement, webgl: Option<WebGl2RenderingContext>, ctx2d: Option<CanvasRenderingContext2d>, listeners: Vec<Listener>, inset_probe: Option<Element> ) -> Self
|
||||
pub( crate ) fn new( desktop: Option<Connection>, offscreen: Option<( OffscreenCanvas, WebGl2RenderingContext )>, cursor_target: HtmlElement, listeners: Vec<Listener>, inset_probe: Option<Element> ) -> Self
|
||||
{
|
||||
let ( offscreen, webgl ) = offscreen.unzip();
|
||||
let gl = webgl.clone().map( |context| Arc::new( glow::Context::from_webgl2_context( context ) ) );
|
||||
Self
|
||||
{
|
||||
canvas,
|
||||
desktop,
|
||||
offscreen,
|
||||
webgl,
|
||||
gl,
|
||||
ctx2d,
|
||||
context_lost: false,
|
||||
context_lost: false,
|
||||
cursor_target,
|
||||
inset_probe,
|
||||
frame_due: Cell::new( None ),
|
||||
text_input: false,
|
||||
frame_due: RefCell::new( Vec::new() ),
|
||||
next_surface: Cell::new( super::desktop::MAIN + 1 ),
|
||||
subsurfaces: HashMap::new(),
|
||||
subsurface_canvas: None,
|
||||
text_input: false,
|
||||
listeners,
|
||||
_app: PhantomData,
|
||||
_app: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Report a frame of `focus` at the next animation frame.
|
||||
pub( super ) fn owe_frame( &self, focus: SurfaceFocus )
|
||||
{
|
||||
let mut due = self.frame_due.borrow_mut();
|
||||
if !due.contains( &focus ) { due.push( focus ); }
|
||||
}
|
||||
|
||||
pub( super ) fn surface_id( &self ) -> u32
|
||||
{
|
||||
let id = self.next_surface.get();
|
||||
self.next_surface.set( id + 1 );
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user