Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / webview / resources.lucb

#==============================================================================================
#
#   resources - the resource:// files the engine reads, written where it reads them
#
#   DESCRIPTION:
#       The loader reads two of Ladybird's resources through Core::Resource
#       (resource://ladybird/templates/error.html for a failed load's page and directory.html
#       for a file: directory's listing, Loader/GeneratedPagesLoader.cpp). Ladybird installs
#       them with the application under Base/res; the engine carries them in data/res. An
#       application built on this module carries them here, as text, and writes them into a
#       resource directory of its choosing the first time it starts (or when they changed), then
#       installs Core::ResourceImplementationFile over that directory as WebContent's main does.
#
#       The text of data/res/ladybird/templates/*.html (Ladybird's Base/res/ladybird/templates,
#       BSD-2-Clause, data/res/LICENSE); tests_webview_platform checks the copies agree.
#
#       Copyright (c) 2018-2026, the Ladybird developers.
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## A resource the module writes: its path under the resource directory and its text.
pub struct WebViewResource:
    pub let path: str
    pub let text: str

## data/res/ladybird/templates/error.html.
let webview_error_template: str = """<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Error!</title>
        <style>
            :root {
                color-scheme: light dark;
                font-family: system-ui, sans-serif;
            }
            body {
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
                min-height: 100vh;
                box-sizing: border-box;
                margin: 0;
                padding: 1rem;
                text-align: center;
            }
            header {
                display: flex;
                flex-direction: column;
                align-items: center;
                gap: 2rem;
                margin-bottom: 1rem;
            }
            svg {
                height: 64px;
                width: auto;
                stroke: currentColor;
                fill: none;
                stroke-width: 1.5;
                stroke-linecap: round;
                stroke-linejoin: round;
            }
            h1 {
                margin: 0;
                font-size: 1.5rem;
            }
            p {
                font-size: 1rem;
                color: #555;
            }
        </style>
    </head>
    <body>
        <header>
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 17.5 21.5">
                <path d="M11.75.75h-9c-1.1 0-2 .9-2 2v16c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2v-13l-5-5z" />
                <path d="M10.75.75v4c0 1.1.9 2 2 2h4M5.75 9.75v2M11.75 9.75v2M5.75 16.75c1-2.67 5-2.67 6 0" />
            </svg>
            <h1>Failed to load %failed_url%</h1>
        </header>
        <p>%error_message%</p>
    </body>
</html>
"""

## data/res/ladybird/templates/directory.html.
let webview_directory_template: str = """<!doctype html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>Index of %path%</title>
        <style>
            html {
                color-scheme: light dark;
            }

            header {
                margin-bottom: 10px;
            }

            h1 {
                display: inline;
            }

            a {
                text-decoration: none;
            }

            a:focus,
            a:hover {
                text-decoration: underline;
            }

            table {
                font-family: monospace;
            }

            .folder,
            .file,
            .open-parent {
                display: inline-block;
                width: 16px;
                height: 16px;
                margin-right: 5px;
                background-size: contain;
            }

            .folder {
                background-image: url("resource://icons/32x32/filetype-folder.png");
            }

            .file {
                background-image: url("resource://icons/32x32/filetype-unknown.png");
            }

            .open-parent {
                background-image: url("resource://icons/16x16/open-parent-directory.png");
            }
        </style>
    </head>
    <body>
        <header>
            <span class="folder" style="width: 24px; height: 24px"></span>
            <h1>Index of %path%</h1>
        </header>
        <p>
            <a href="%parent_url%"><span class="open-parent"></span>Open Parent Directory</a>
        </p>
        <hr />
        %contents%
        <hr />
    </body>
</html>
"""

## The resources, in the order they are written.
pub let webview_resources: WebViewResource[2] = [
    WebViewResource(path = "ladybird/templates/error.html", text = webview_error_template),
    WebViewResource(path = "ladybird/templates/directory.html", text = webview_directory_template),
]

## Writes each resource under `directory` whose file is missing or differs, making the
## directories on the way. Fails when a file cannot be written.
pub func webview_write_resources(directory: str) -> !:
    for resource in webview_resources:
        let path = strings.join([directory, "/", resource.path], "") catch failure: trap("out of memory")
        defer strings.release(path)
        if webview_file_has_text(path, resource.text):
            continue
        try files.ensure_directory(paths.directory(path))
        try files.write_text(path, resource.text)

## Whether the file at `path` holds exactly `text`.
func webview_file_has_text(path: str, text: str) -> bool:
    let found = files.read_text(path) catch failure: return false
    defer found.release()
    return found.value == text