@tishlang/tish-macos
v3.1.0
Published
AppKit native UI host for Tish JSX
Downloads
125
Readme
tish-macos
AppKit host for Tish JSX. Published to npm as @tishlang/tish-macos — apps npm install @tishlang/tish-macos and import { macos } from "@tishlang/tish-macos". (Inside the tish monorepo only, import … from "tish:macos" resolves this crate directly — local debug.) See crate docs in src/lib.rs and the examples/ tree.
Editor: go to definition and hover
Native window.* helpers are implemented with Rust closures, not pub fn exports, so tish-lsp resolves them via lsp-pragmas.d.tish at this repo root (same // @tish-source convention as tish/stdlib/builtins.d.tish, with an optional | hover text suffix).
- Go to definition on e.g.
window.innerHeightjumps tosrc/appkit/window_api.rs. - Hover shows the optional doc line and a clickable
file://link to that Rust location (no compiler checkout required for macos-only sources).
To also jump built-in JS globals (console, …) from the main compiler tree, set tish.tishlangSourceRoot to your tish repository root (directory that contains crates/).
Quick test layout
- Open
tish-macos-dev.code-workspace(adds the siblingtishfolder and setstish.tishlangSourceRoot), or openexamples/kitchen-sink-macosalone (folder settings point at../../../tishfor the compiler repo). - Build the language server once:
cargo build -p tishlang_lspfrom thetishrepo (debug binary:target/debug/tish-lsp). This repo’s.vscode/settings.json,tish-macos-dev.code-workspace, andexamples/kitchen-sink-macos/.vscode/settings.jsonalready settish.languageServerPathto that binary via${workspaceFolder}/${workspaceFolder:tish-compiler}(expanded by the Tish extension). - In
examples/kitchen-sink-macos/src/main.tish, hoverinnerHeighton thewindow.innerHeight()line — you should see the pragma doc and an “Open Rust implementation” link.
Async on the main thread
On the native backend await blocks the thread it runs on, and Tish code must stay on the thread
that started it. In an AppKit app that thread is the main one, so awaiting a fetch there freezes
the UI. Two calls let the app keep going and be called back on the main thread:
macos.whenSettled(promise, cb)waits for any Tish promise (fetch(url),res.text(),reader.read(), …) on a background thread, then callscb(value, error)on the main thread (erroris null when it fulfilled). A non-promise value is passed tocbon the next turn.macos.startTimers()drives the globalsetTimeout/setIntervalfrom the main run loop (every 32 ms).macos.runalready does this; call it when your app sets up AppKit itself.
Streaming a response, chunk by chunk as the server sends it:
fn pump(reader, onChunk, done) {
macos.whenSettled(reader.read(), (r, err) => {
if (err !== null || r.done) { done(err); return }
onChunk(r.value)
pump(reader, onChunk, done)
})
}
macos.whenSettled(fetch(url), (res, err) => pump(res.body.getReader(), show, finish))A failed request settles with an error response ({ ok: false, error }), like await fetch.
See examples/async-macos.
System services
Namespaces on macos for macOS services that aren't UI. Callbacks run on the main thread.
| API | What |
|---|---|
| macos.timeZones.names() / .local() / .at(id, unix) / .byAbbreviation(abbr) | macOS's time-zone database: ids, the local zone, { offset, abbreviation } at a moment (daylight saving included), a zone for "CET" |
| macos.dictionary.lookup(word) | the plain text of each homograph in the system dictionary, first one first |
| macos.pasteboard.readText() / .writeText(text) / .watch(cb) | the general pasteboard's text; watch calls cb({ text, app, appPath }) for each copy, skipping password-manager items, with app "" for this app's own writes |
| macos.workspace.open(target) / .reveal(path) / .trash(path) / .appsFor(path) / .openWith(path, app) | open a URL or path, select a file in Finder, move to the Trash ({ ok, path, error }), the apps that can open a file (default first), open with one |
| macos.watchFolders(paths, latency, cb) | cb() after changes under paths, coalesced over latency seconds (FSEvents) |
| macos.onOpenUrl(cb) | cb(url) for each URL macOS opens with the app (a scheme in its Info.plist); call before the run loop starts |
| macos.system.lockScreen() / .sleep() / .sleepDisplays() / .screenSaver() / .restart() / .shutDown() / .logOut() / .emptyTrash(cb) / .darkMode() / .setDarkMode(on) / .volume() / .setVolume(n) / .setMuted(on) / .ejectAll(cb) | system actions; each returns null or why it failed (emptyTrash and ejectAll answer cb from a background thread's work) |
| macos.apps.running() / .act(pid, action) / .quitAll() / .hideAll() | apps in the Dock with their memory footprint; switch, hide, unhide, quit or force-quit one |
| macos.shell.run(command, { cwd, args, timeoutMs }, cb) | /bin/sh -c command in the background -> id; cb({ id, code, stdout, stderr, ms, timedOut }). args are $1, $2, …; output capped at 1 MB; killed with its children after timeoutMs (60 s) |
| macos.systemInfo() | { os, model, chip, cores, memory, uptime, diskTotal, diskFree, battery } |
| macos.contacts.status() / .request(cb) / .query(text, limit, cb) | the address book; needs NSContactsUsageDescription in the app's Info.plist |
| macos.accessibility.trusted(prompt) / .selectedText() / .replaceBeforeCursor(typed, text) / .focusedWindow() / .setFocusedWindowFrame(x, y, w, h) / .windowAction(pid, action) | other apps through Accessibility; windowAction minimizes, unminimizes (all), toggles full screen, closes or raises an app's window -> { count } or { error } (needs the app in System Settings › Privacy & Security › Accessibility) |
| macos.screens() | [{ visible, frame }] for every display, in Accessibility's top-left coordinates |
| macos.icons.file(path) / .symbol(name) / .image(path, template, reload) / .onLoaded(cb) | named images for <image src>: Finder icons (loaded in the background; onLoaded when done), SF Symbols, image files |
| macos.statusItem({ image, title, tooltip, menu, onClick, onMenu }) | a menu bar icon with a menu ([{ title, id, key, enabled } \| { separator: true }]); calling again updates it |
| macos.spotlight.query(query, { scope, max }, cb?) | files from Spotlight's index for an MDQuery string: [{ path, contentType, size, created, modified, lastUsed }]; with cb it runs on a background thread |
The native backend lowers some method names to the string/array built-ins whatever the receiver
(at, search, match, replace, push, slice, …), so these APIs avoid them, except
macos.timeZones.at: bind it to a name before calling it (let zoneAt = macos.timeZones.at).
