fluid.desktop from @fluid-app/sites:
The app checks and limits everything your Site sends. Everything here is optional: the same Site runs in every browser, where
fluid.desktop does nothing.
Before you start
- The SDK release. Menus and window options need
@fluid-app/sites0.1.178 or later, andwindow.openand permissions need 0.1.179. Notifications, global shortcuts and the menu-bar item need 0.1.181, andwindow.showandtray.onRemovedneed a release after 0.1.181. In an older SDK, the missing part isundefined, so calling it, such asfluid.desktop.tray.set(…), throws in every browser, and before 0.1.178 so does readingfluid.desktop.available: update the package first. - The Mist release. The app gets menus, window options and
window.openfrom Mist 1.38.0. Permissions, the microphone, notifications and global shortcuts need Mist 1.40.0 or later, and the menu-bar item,window.showandtray.onRemoveda Mist release after 1.40.0. In an app built by an older Mist, a permission it doesn’t have isunavailable. After an update, Mist rebuilds installed apps when it next starts; for apps that are open, see Update a site app. - Publish first. The app opens your published Site.
fluid sites dev, Mist’s preview and every browser show your Site withoutfluid.desktop, so check these features in the installed app.
How the app runs your Site
The app opens your Site’s published address in its own window, with the storefront around your Site as on the web. Its toolbar has Back, Forward, Site home and Reload. Each app has its own sign-in, so the person signs in to the storefront once inside the app. Only your Site’s own frame getsfluid.desktop. The storefront page around it, embeds, and frames inside your Site don’t.
Detect the app and fall back
fluid.desktop.available is true only in the app. Everywhere else, every call does nothing and resolves at once:
An app from an older Mist may not have a call yet.
permissions, notifications, shortcuts, tray and window.show then answer as in a browser, and any other call it lacks rejects with ENOSYS.
- Keep everything a visitor needs in the page. The app adds to your Site; nothing depends on it.
- Branch on what a call returns (a state,
nullorfalse), never on the user agent. - Set your menu and window up on every page load, in
src/main.tsx. The app forgets the menu when your Site’s page loads again. A page change through your router keeps it. - A refused call rejects with an
Errorwhose message starts with a code, such asEINVAL: "CmdOrCtrl+R" is reserved…. See Errors.
Set the menu bar
fluid.desktop.menu.setApplicationMenu(template) sets the menu bar for your Site’s window, as an Electron menu template. Each call replaces the last menu. null puts the app’s own menu back. The menu bar shows the menu of the window the person is in.
Any other field is dropped.
- Limits: at most 500 items in the whole template, and 4 levels deep.
- Roles:
undo,redo,cut,copy,paste,pasteAndMatchStyle,delete,selectAll,startSpeaking,stopSpeaking,appMenu,fileMenu,editMenu,windowMenu,about,hide,hideOthers,unhide,quit,minimize,zoom,closeandfront. A role item gets the system’s label, shortcut and contents, and keeps only itsid,type,enabled,visibleandchecked. - Shortcuts: an Electron accelerator, at most 64 characters, with Command or Control in it, such as
CmdOrCtrl+Shift+N. The app keeps these for itself: any Command+Q, Option+Command+Escape, and its own Command+R (Reload), Command+[ (Back), Command+] (Forward), Shift+Command+H (Site Home) and Shift+Command+T (Show Toolbar). - Clicks:
fluid.desktop.onMenuClick(({ menu, id, checked }) => …)hears picks of items with anidor aclick.menuis"application","context"or"tray"(the menu-bar item’s menu),idis your id (or one the SDK made, for an item with only aclick), andcheckedis a checkbox’s or radio’s state after the click. Roles, separators and submenu headings never report.
What the app adds
The app keeps a way back to navigation, whatever your menu says:- The first menu is always the app’s: About, Permissions, Hide, Hide Others, Show All and Quit. An
appMenurole you put first is dropped. - The app adds its View items to your top-level menu labelled View, or adds a View menu before the Window menu: New Window, New Tab, Show Tab Bar, Show All Tabs, Show Toolbar, Reload, Back, Forward and Site Home.
- The app adds an Edit menu after File when you have none, so Copy, Paste and Undo keep their shortcuts.
- The app adds no Close Window item. Put
{ role: "close" }in your File menu so Command+W closes the window. - The app always has its own Window menu: Minimize, Zoom, Bring All to Front, Merge All Windows and Move Tab to New Window. It goes where you put
{ role: "windowMenu" }, or last. Don’t label a menu of your own Window, or the bar shows two. - New Window and New Tab take Command+N and Command+T unless your menu uses those shortcuts. Then yours win. A menu shortcut runs before your page sees the key, so put Command+N in your menu, not in a
keydownhandler.
Open a context menu
fluid.desktop.menu.popup(template) opens a menu under the pointer and resolves when it closes. It takes the same fields as the menu bar, but its only roles are the Edit roles that don’t paste: undo, redo, cut, copy, delete, selectAll, startSpeaking and stopSpeaking.
Style the window and title bar
fluid.desktop.window.setOptions(options) styles the window. Each call replaces your last options: send every option you want each time. It resolves with { applied, options }, where options is what the app took after limiting it, and applied is "next_open" when a new title bar style waits for the next window, else "now".
Values outside a range are moved into it. An option the app doesn’t know, a value of the wrong type, or
trafficLightPosition or dragHeight with "default" rejects with EINVAL.
The app keeps your options, apart from toolbar, for every window and the next launch, until you send others. setOptions({}) clears them. The app forgets toolbar when the window loads a new page or relaunches, so send it on every load.
fluid.desktop.window.getOptions() resolves with { options, titleBarStyle, toolbarVisible }: the options you last set, the title bar the window has now, and whether the toolbar shows.
Draw your own title bar
- Mark your header
app-region: drag, and its buttons, links and inputsno-drag. The app readsapp-regionfrom your stylesheets and inline styles, up to 64 regions. - Add a class such as
in-appto<html>whenfluid.desktop.availableistrue, so the browser keeps your usual header. - The person can always bring the toolbar back from View › Show Toolbar, or Shift+Command+T. Their choice wins for that window. The toolbar always shows on a page that isn’t your Site’s, such as sign-in.
Open new windows and tabs
fluid.desktop.window.open(path, { tab }) opens a page of your Site in a new window, or with { tab: true } in a new tab of this window. It resolves true once the app opened it. In a browser it resolves false, so open the page yourself.
- The path is a path on your Site’s own address, such as
/archiveor/orders?status=open, at most 2,048 characters. Path segments may use letters, digits and. _ ~ % -(percent-encode anything else), and the first can’t start with_. A hash route (/#/orders/42) or an element id (/faq#returns) is kept. - Call it from the person’s input: within 2 seconds of a click or a key press in your page, or a pick of one of your menu items. One input opens one window.
- At most 20 windows and tabs are open at once.
- Tabs use macOS’s tab bar, which needs the system’s title bar. While a window is a tab, it has the system’s title bar, and it gets your style back once it’s alone. So a header that draws its own title bar should also look right under the system’s. From a minimized or full-screen window, a tab opens as a window.
- A link with
target="_blank", or the page’s ownwindow.open, opens in the person’s browser.mailto:andtel:links do nothing in the app.
Bring the window to the front
fluid.desktop.window.show() brings your page’s window to the front, and shows it again when the app runs in the menu bar alone. Like window.open, it works only within 2 seconds of the person’s input, a pick in the menu-bar item’s menu included, or it rejects with EBUSY. One input brings the window forward once, and the same input still works for window.open or menu.popup in that handler. It resolves true once the window is in front, and false in a browser or an older app. Call it in a menu-bar item’s click before opening a dialog.
Ask for permissions
Your Site can ask the person for four permissions:microphone, notifications, shortcuts (global shortcuts) and menuBar (an item in the macOS menu bar).
Declare them
Declare what your Site may ask for inindex.html’s <head>. Add the tag there, not from a script: the app reads it once, when the page has loaded.
index.html
unavailable, and one it names still needs the person’s yes. Its content is JSON of at most 16,000 characters. Only the first 32 names count, and names the app doesn’t know are ignored, so you can name a newer permission safely.
Read and ask
Read permissions throughfluid.desktop.permissions, not navigator.permissions, which can’t show prompt in the app.
query()resolves with every permission’s state:{ microphone, notifications, shortcuts, menuBar }. Read it on every page load.request(name)asks the person when the state isprompt, and resolves with the state after they answer. In any other state it answers at once. Call it from a click on the control that needs it, never on load.onChange(({ permission, state }) => …)hears the person answer, or change an answer from the app menu, while your page is open. It returns a function that stops listening.
- The question is a sheet on your Site’s window, in the app’s own name, such as “Allow Field Notes to use your microphone?”. Don’t Allow is the default, and Escape counts as Don’t Allow.
- When it asks:
requestrejects withENOTSUPwhile the window is hidden or minimized. While the window is behind other windows, it waits until the window comes to the front. - How long an answer lasts: a no lasts until the person changes it. A yes to the microphone lasts for the published version of your Site it was given on, so the app asks again after the next publish. A yes to notifications, global shortcuts or the menu-bar item lasts until the person takes it back.
- The app menu’s Permissions lists Microphone, Notifications, Global Shortcuts and Menu Bar Item. The person turns each on or off there. Taking a yes to the microphone back reloads your page, which ends a recording. Taking one to global shortcuts back releases every shortcut, and taking one to the menu-bar item back removes the item.
Use the microphone
The app gives your Site audio from the microphone, never the camera.getUserMedia({ audio: true }) without request asks the same question. If the person refused the app the microphone in System Settings › Privacy & Security › Microphone, the state is denied and the Permissions item is off: tell them to allow it there. In a browser, the storefront lets your Site ask, and the browser shows its own prompt.
Show notifications
Withnotifications granted, your Site shows notifications under the app’s name and icon.
Notifications need the SDK and Mist releases in Before you start.
show(text, { silent })resolves with the notification’s id. The text is plain text: control and invisible characters become spaces, the ends are trimmed, and what’s left must be 1 to 300 characters.silent: trueshows it without a sound. It resolvesnullin a browser or an older app, and rejects withEPERMwithout the permission.close(id)takes one off the screen and out of Notification Center.onClickhears a click. The app brings the window that showed it to the front, or the window used last when that one has closed, and tells your Site in it, whatever page it’s on. Ignore ids you don’t know. With no window open, the app opens one and doesn’t report the click.- Ids are the app’s, new each launch. The app keeps the last 50 and closes older ones. Taking the permission back closes them all.
- macOS asks the person once more, on the first notification. If they refuse there, or turn the app’s notifications off in System Settings later, notifications don’t appear and the state stays
granted. Show anything important in your page too. - The page’s own
Notificationstays denied in the app. Usefluid.desktop.notifications, and in a browser show the message in your page.
Register global shortcuts
Withshortcuts granted, your Site registers shortcuts that work while another app is in front. They need the SDK and Mist releases in Before you start.
- The chord is Command or Control, at least one more modifier, and one key, such as
Control+Shift+G. While the app runs, it takes that chord from every other app, so pick one the person doesn’t rely on elsewhere. These reject withEINVAL: one modifier, Option and Shift alone, Escape, VoiceOver’s chords (Control+Option, Command+F5), any Command+Q, the app’s own menu shortcuts, and the system’s screenshot, emoji, Dock, Zoom and Help shortcuts. register(accelerator)resolvestruewhen the app holds the chord, andfalsewhen another app or the system holds it, or in a browser. Withoutshortcutsgranted, it rejects withEPERM. The app holds at most 5; a 6th rejects withEBUSY. Chords compare by key, not spelling, so registering one your Site already holds resolvestrue.unregister(accelerator)releases one, in any spelling, andunregisterAll()releases them all. The app runs yourregisterandunregistercalls in the order you make them.- A press brings the window the person used last, of those whose page registered the chord, to the front, and
onPresshears it with the chord as your page wrote it. Nothing is reported while the screen is locked. - How long: until the page goes (a new page or a reload), the window closes, or the person takes the permission back. Register again on every page load.
Add a menu-bar item
WithmenuBar granted, your Site puts one item in the macOS menu bar, beside the clock. It needs the SDK and Mist releases in Before you start.
fluid.desktop.tray.set(item) shows the item, or replaces it: one per app. It resolves true once the item shows, and false in a browser or an older app. Without menuBar granted, it rejects with EPERM. The item needs an icon or a title, or both:
- Clicks: with a
menu, a click on the item opens it. Without one, a click is a press: the app brings the window whose page set the item to the front, andfluid.desktop.tray.onPress(() => …)hears it, and a right-click opens the app’s Open and Quit. Choose a menu or a press, not both. A pick in the menu doesn’t bring a window forward, so an action can run in the background. To show a dialog from one, callfluid.desktop.window.show()in itsclickfirst, or the dialog opens out of sight. remove()takes the item away.- Running in the menu bar: while the item shows, closing the app’s last window hides it and keeps the app running, with no Dock icon until a window opens. Your page keeps running, so its menu and presses still reach it. A press, the item’s Open, or opening the app again from Finder or Spotlight shows the window. Quit (in the item’s menu or the app menu) or Command+Q quits. When Mist updates, an app running in the menu bar alone quits and opens there again on the new version, with its window hidden until your page sets its item, or for at most 15 seconds.
- How long: until the page goes (a new page or a reload), its window closes for good, or the person takes the permission back. Set it again on every page load. If the item goes while its window is hidden, the window comes back, unless the person took the permission back: then an app running in the menu bar alone quits.
- When its window closes: if the item’s window closes, or its page leaves your Site (another address, a crash or a failed load), while another window is open, the item goes. Your pages in the other windows hear
fluid.desktop.tray.onRemoved(() => …), so one can set it again. It isn’t sent on a reload that stays on your Site, when your Site callsremove(), or when the person takes the permission back, and an older app never sends it.
Errors
A refused call rejects with anError whose message is the code, a colon and the reason.
What a Mac app can’t do
- Read or write the Mac’s files, beyond what the person picks in a file input
- Run processes or commands
- More than one menu-bar item, a Dock menu or a badge
- The camera, location, or USB, HID or serial devices. Copy works; reading the clipboard doesn’t
- Run on Windows or Linux
Example
A notes Site with its own menus and title bar, and a voice memo that uses the microphone in the app and the browser’s prompt elsewhere.src/desktop.ts
setUpDesktop from src/main.tsx, add the <meta name="fluid-desktop-manifest"> tag for the microphone to index.html, and style .in-app .title-bar as in Draw your own title bar. The Record button calls microphone() from Use the microphone. When it returns null in the app, read query(): on denied, say where to turn it on, and on unavailable, hide the button.
Related pages
- Install a Site as a Mac app: installing, opening and removing the app in Mist
- The Sites SDK: the rest of
@fluid-app/sites