dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / webview / resources.lucb
#==============================================================================================
#
# resources - the resource:// files the engine reads, written where it reads them
#
# DESCRIPTION:
# The loader reads 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), and the about:
# pages from resource://ladybird/about-pages (ResourceLoader::handle_about_load_request,
# which requires the directory). Of the about pages those without script are carried
# (about, newtab and their webui.css); version, bookmarks, settings and processes come
# with scripting, and until then load as Ladybird's unknown about pages do, as an empty
# document. 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 and about-pages/* (Ladybird's
# Base/res/ladybird, 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>
"""
## data/res/ladybird/about-pages/about.html.
let webview_about_page: str = """<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="color-scheme" content="light dark" />
<title>About URLs</title>
</head>
<body>
<h1>List of About URLs</h1>
<ul>
<li><a href="about:about">about:about</a></li>
<li><a href="about:bookmarks">about:bookmarks</a></li>
<li><a href="about:newtab">about:newtab</a></li>
<li><a href="about:processes">about:processes</a></li>
<li><a href="about:settings">about:settings</a></li>
<li><a href="about:version">about:version</a></li>
</ul>
</body>
</html>
"""
## data/res/ladybird/about-pages/newtab.html.
let webview_newtab_page: str = """<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>New Tab</title>
<style>
html {
color-scheme: light dark;
}
</style>
</head>
</html>
"""
## data/res/ladybird/about-pages/webui.css.
let webview_webui_css: str = """@media (prefers-color-scheme: light) {
:root {
--card-background-color: #f5f5f5;
--card-header-background-color: #f0f2f5;
}
}
@media (prefers-color-scheme: dark) {
:root {
--card-background-color: #242424;
--card-header-background-color: #252525;
}
}
* {
box-sizing: border-box;
}
body {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
header {
display: flex;
align-items: center;
margin-bottom: 30px;
}
header h1 {
font-size: 20px;
}
header img {
height: 48px;
margin-right: 10px;
float: left;
}
.card-title {
display: flex;
align-items: center;
justify-content: space-between;
font-size: 16px;
font-weight: 500;
margin: 0 0 8px 4px;
}
.card {
background-color: var(--card-background-color);
border-radius: 8px;
margin-bottom: 20px;
}
.card-body {
padding: 20px;
}
.card-body > hr {
margin: 0 8px 16px 8px;
}
.card-group {
margin-bottom: 20px;
}
.card-group:last-child {
margin-bottom: 0;
}
"""
## The resources, in the order they are written.
pub let webview_resources: WebViewResource[5] = [
WebViewResource(path = "ladybird/templates/error.html", text = webview_error_template),
WebViewResource(path = "ladybird/templates/directory.html", text = webview_directory_template),
WebViewResource(path = "ladybird/about-pages/about.html", text = webview_about_page),
WebViewResource(path = "ladybird/about-pages/newtab.html", text = webview_newtab_page),
WebViewResource(path = "ladybird/about-pages/webui.css", text = webview_webui_css),
]
## 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