Reference a PNG or JPEG file for an image property.
Absolute path of the image file, inside the package directory or the data directory.
Optionaloptions: ImageOptions
maxSide: decode the image scaled down, aspect kept, so
that neither side is over it.
A new ImageRef, owned by this scope.
Returns at once without touching the file. The player checks the file each
time it receives the reference (a property write, a window or settings page
opening, a new instance of a panel): it must be inside this plugin's
package directory or Plugin.dataDirectory after following links, at
most 16 MB and 4096×4096 pixels, and PNG or JPEG by content (the extension
does not matter). A failed check of a whole-value image is a failed
property write: missing for a file that does not exist, invalid
otherwise. It goes to Scope.onError and the property keeps its
previous image; in the initial properties of a tool window it makes
Window.show reject. A nested image (a field of a row) that fails
its check is drawn empty instead, and the rest of the value is shown, as a
Markdown image shows its alt text.
The player caches decoded images per plugin (up to 64 MB, least recently
used first) by path, size and modification time, and does not watch
files. After rewriting a file under the same path, call image() again
and write the new reference: the SDK does not send a value that did not
change, so the player would not look at the file again.
A window shows a PNG the plugin wrote into its data directory. The file
must be inside the package or data directory; image returns at once.
The player decodes the file when it receives the value and notices a
rewritten file by its size and modification time; the plugin still
calls image() again after rewriting it, because the SDK does not send
a value that did not change.
const thumbnailWindow = plugin.windows.create("ui/thumbnail.slint");
const file = `${plugin.dataDirectory}/thumbnail.png`;
// … write the PNG to `file` (for example with plugin.tools.run("ffmpeg", …)) …
thumbnailWindow.set({ thumbnail: plugin.ui.image(file) });
await thumbnailWindow.show();
Download an https: PNG or JPEG image (a thumbnail, a repository's
plugin icon) and reference the local copy, as Ui.image does.
The image's https: address.
Optionaloptions: ImageOptions
As for Ui.image.
The reference, owned by this scope, once the file is in the cache.
Downloads like Ui.markdown's remote images: Node's fetch in
this plugin's process, at most 4 MB and 10 s per image, 4 downloads at a
time per plugin, into <dataDirectory>/.vestra-cache/images/; the cache
keeps 32 MB and fetches an address again after 24 hours, so a list that
shows the same thumbnails again reads them from disk.
unavailable (rejects) the download failed, timed out, was too
large, or is not a PNG or JPEG image; or the plugin has no data
directory.
A results window lists each hit with its thumbnail, or a placeholder
when a thumbnail cannot be fetched. remoteImage downloads the image
in the plugin's process into the data directory's cache (at most 4 at a
time) and resolves with a local reference; a failed download rejects
with unavailable, which the catch turns into the placeholder.
const results = [{ title: "A", thumbnailUrl: "https://example.com/a.jpg" }];
const placeholder = plugin.ui.image(`${plugin.dataDirectory}/blank.png`);
const rows = await Promise.all(
results.map(async (r) => ({
title: r.title,
thumb: await plugin.ui
.remoteImage(r.thumbnailUrl, { maxSide: 320 })
.catch(() => placeholder), // `unavailable`: download failed
})),
);
plugin.windows.create("ui/results.slint").set({ rows });
Prepare a Markdown document for MarkdownView (@vestra/markdown.slint).
The Markdown source (CommonMark with GitHub tables, task lists and strikethrough), at most 256 KB as UTF-8.
Optionaloptions: MarkdownOptions
Where relative addresses point and whether to download remote images.
The document, owned by this scope, once its remote images are downloaded (or have failed).
The player renders headings, paragraphs (bold, italic, strikethrough,
inline code, links), lists and task lists, code blocks, block quotes,
images, tables and rules; embedded HTML is not shown. A click on a link
calls the view's link-clicked(url) callback with the address resolved
against base; opening it is up to the plugin (for example with
System.openUrl).
Images: PNG or JPEG. Local ones follow Ui.image's rules (this
plugin's package or data directory). https: images are downloaded by
this call into <dataDirectory>/.vestra-cache/markdown/ (each at most
4 MB and 10 s, at most 64 per document; the cache keeps 32 MB and fetches
an address again after 24 hours). A failed or unsupported image (SVG,
GIF, http:…) shows its alt text; the call itself does not fail for it.
The text is at most 256 KB; very long documents are cut off after 4000 blocks.
MarkdownView scrolls one document. MarkdownBlocks (players after 0.4.0)
draws one without scrolling, as tall as its content, for a layout of your
own or a ListView row. For a list of documents, write the whole list with
one document per row: the player parses only the documents that changed and
updates the list in place (rows added or removed at the end, a growing
document's changed blocks), so re-sending the list on every streamed chunk
keeps the scroll position and costs little.
Both views take selectable: true (default false): the user can select
and copy the text, one block at a time; blocks with links keep their
clickable links and copy as text (url). Code blocks are always
selectable. MarkdownBlock.plain (the block as plain text) and
MarkdownBlock.links (its link addresses) are readable in Slint. See
Reference: theme › Markdown.
invalid (rejects) text is not a string or is over 256 KB, or
base is neither an http(s): address nor an absolute path.
A chat window lists messages as formatted text, with a Copy button on
each code block that puts the code on the clipboard. Each row's body
is a document from ui.markdown; the whole list is written again on
each change, and the player parses only the documents that changed.
MarkdownBlocks with code-copy calls copy-code, which the plugin
handles with on("copyCode").
ui/chat.slint:
import { ListView } from "std-widgets.slint";
import { MarkdownBlocks, MarkdownBlock } from "@vestra/markdown.slint";
export struct Message { id: string, role: string, body: [MarkdownBlock] }
export component Chat inherits Window {
in property <[Message]> messages;
callback copy-code(string);
ListView {
for message in root.messages: VerticalLayout {
padding: 8px;
MarkdownBlocks {
blocks: message.body;
code-copy: true;
copy-code(code) => { root.copy-code(code); }
}
}
}
}
const chat = plugin.windows.create("ui/chat.slint");
const history: { id: string; role: string; text: string }[] = [];
chat.on("copyCode", (code: string) => plugin.clipboard.writeText(code));
// Re-send the whole list: unchanged documents cost the player nothing.
async function render() {
const messages = [];
for (const { id, role, text } of history)
messages.push({ id, role, body: await plugin.ui.markdown(text) });
chat.set({ messages });
}
history.push({ id: "1", role: "user", text: "Show me `ls`." });
await render();
await chat.show();
A window shows a README downloaded from the web, its images included,
and clicked links open in the browser. base makes relative links and
images resolve against the README's address; markdown resolves after
its https: images are downloaded. A click on a link makes
MarkdownView call link-clicked, which ui/readme.slint forwards as
open-link; the plugin opens the address with openUrl.
const readmeUrl = "https://example.com/plugin/README.md";
const response = await fetch(readmeUrl);
if (!response.ok) throw new Error(`README: HTTP ${response.status}`);
const text = await response.text();
const readmeWindow = plugin.windows.create("ui/readme.slint");
// Bound before the first `show`, as a tool window requires.
readmeWindow.on("openLink", (url: string) => plugin.system.openUrl(url));
readmeWindow.set({ doc: await plugin.ui.markdown(text, { base: readmeUrl }) });
await readmeWindow.show();
Values for Slint properties that are not plain JSON: images from files and Markdown documents.
plugin.ui.Remarks
A Slint file can only name images compiled into it (
@image-url). To show an image the plugin picks at run time (cover art, a thumbnail it rendered, a downloaded icon), make an ImageRef with Ui.image and write it to animageproperty. To show formatted text, make a MarkdownRef with Ui.markdown and give it toMarkdownView. Both work in tool windows, panels (the backdrop and the floating sidebar included) and settings pages.Example: Show a Markdown file in a window (examples/markdown)
A menu item opens a window with a formatted Markdown file that ships with the plugin. The plugin reads the file, turns it into a document with
ui.markdown(relative images resolve against the plugin's directory throughbase) and writes it to the window'sdocproperty, which the Slint file hands toMarkdownView.ui/document.slint:See
ImageRef, MarkdownRef, Example: Markdown, Theme
Since
1.0.0