Compare commits

..

No commits in common. "main" and "0.3.1" have entirely different histories.
main ... 0.3.1

4 changed files with 76 additions and 164 deletions

View File

@ -1,39 +1,38 @@
<img alt="" src="tray.png" align="right" height="128px">
**Tray** is an [Obsidian](https://obsidian.md/) plugin that can be used to launch the app
on system startup and run it in the background, adding global hotkeys and a tray menu to
toggle window visibility and create quick notes from anywhere in your operating system.
on system startup and run it in the background, adding global hotkeys and a tray menu that
toggle app window visibility and can create quick notes from anywhere in your operating system.
## Configuration
### Window management
| Option | Description | Default |
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------ |
| -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- |
| Launch on startup | Open Obsidian automatically whenever you log into your computer. | Disabled |
| Hide on launch | Minimises Obsidian automatically whenever the app is launched. If the "Run in background" option is enabled, windows will be hidden to the system tray/menubar instead of minimised to the taskbar/dock. | Disabled |
| Run in background | Hide the app and continue to run it in the background instead of quitting it when pressing the window close button or toggle focus hotkey. | Disabled |
| Hide taskbar icon | Hides the window's icon from from the dock/taskbar. This may not work on Linux-based OSes. | Disabled |
| Create tray icon | Add an icon to your system tray/menubar to bring hidden Obsidian windows back into focus on click or force a full quit/relaunch of the app through the right-click menu. | Enabled |
| Tray icon image | Set the image used by the tray/menubar icon. Recommended size: 16x16 | ![](obsidian.png) |
| Tray icon tooltip | Set a title to identify the tray/menubar icon by. The `{{vault}}` placeholder will be replaced by the vault name. | `{{vault}} \| Obsidian` |
| Toggle window focus hotkey | This hotkey is registered globally and will be detected even if Obsidian does not have keyboard focus. Format: [Electron accelerator](https://www.electronjs.org/docs/latest/api/accelerator) | <kbd>CmdOrCtrl+Shift+Tab</kbd> |
| Hide taskbar icon | Hides the window's icon from from the dock/taskbar. This may not work on all Linux-based OSes. | Disabled |
| Create tray icon | Add an icon to your system tray/menubar to bring hidden Obsidian windows back into focus on click or force a full quit/relaunch of the app through the right-click menu. _Changing this option requires a restart to take effect._ | Enabled |
| Tray icon image | Set the image used by the tray/menubar icon. Recommended size: 16x16 _Changing this option requires a restart to take effect._ | ![](obsidian.png) |
| Toggle window focus hotkey | This hotkey is registered globally and will be detected even if Obsidian does not have keyboard focus. Format: [Electron accelerator](https://www.electronjs.org/docs/latest/api/accelerator) | CmdOrCtrl+Shift+Tab |
The `Relaunch Obsidian` and `Close Vault` actions can be triggered from the tray/menubar context menu,
or with the in-app command palette (search for "Tray: Relaunch Obsidian" or "Tray: Close Vault").
Hotkeys can be assigned to the commands via Obsidian's built-in hotkey manager.
The `Relaunch Obsidian` action can be triggered from the tray/menubar context menu, or with the in-app
command palette (search for "Tray: Relaunch Obsidian"). Hotkeys can be assigned to the command via
Obsidian's built-in hotkey manager.
### Quick notes
| Option | Description | Default |
| ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- |
| ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------- |
| Quick note location | New quick notes will be placed in this folder. | |
| Quick note date format | New quick notes will use a filename of this pattern. Format: [Moment.js format string](https://momentjs.com/docs/#/displaying/format/) | `YYYY-MM-DD` |
| Quick note hotkey | This hotkey is registered globally and will be detected even if Obsidian does not have keyboard focus. Format: [Electron accelerator](https://www.electronjs.org/docs/latest/api/accelerator) | <kbd>CmdOrCtrl+Shift+Q</kbd> |
| Quick note date format | New quick notes will use a filename of this pattern. Format: [Moment.js format string](https://momentjs.com/docs/#/displaying/format/) | YYYY-MM-DD |
| Quick note hotkey | This hotkey is registered globally and will be detected even if Obsidian does not have keyboard focus. Format: [Electron accelerator](https://www.electronjs.org/docs/latest/api/accelerator) | CmdOrCtrl+Shift+Q |
## Installation
### Obsidian Marketplace
### Obsidian Marketplace (Coming Soon)
1. In Obsidian, navigate to **Settings****Community plugins**.
2. Press the **Browse** button beside the **Community plugins** option.

193
main.js
View File

@ -1,5 +1,5 @@
/**
* obsidian-tray v0.3.5
* obsidian-tray v0.3.1
* (c) 2023 dragonwocky <thedragonring.bod@gmail.com> (https://dragonwocky.me/)
* (https://github.com/dragonwocky/obsidian-tray/) under the MIT license
*/
@ -15,11 +15,11 @@ const LOG_PREFIX = "obsidian-tray",
LOG_TRAY_ICON = "creating tray icon",
LOG_REGISTER_HOTKEY = "registering hotkey",
LOG_UNREGISTER_HOTKEY = "unregistering hotkey",
ACTION_QUICK_NOTE = "Quick Note",
ACTION_SHOW = "Show Vault",
ACTION_HIDE = "Hide Vault",
ACTION_QUICK_NOTE = "Add Quick Note",
ACTION_OPEN = "Open Obsidian",
ACTION_HIDE = "Hide Obsidian",
ACTION_RELAUNCH = "Relaunch Obsidian",
ACTION_CLOSE = "Close Vault",
ACTION_QUIT = "Quit Obsidian",
DEFAULT_DATE_FORMAT = "YYYY-MM-DD",
ACCELERATOR_FORMAT = `
This hotkey is registered globally and will be detected even if Obsidian does
@ -38,59 +38,31 @@ const LOG_PREFIX = "obsidian-tray",
let tray, plugin;
const obsidian = require("obsidian"),
{ app, Tray, Menu } = require("electron").remote,
{ nativeImage, BrowserWindow } = require("electron").remote,
{ app, Tray, Menu, nativeImage } = require("electron").remote,
{ getCurrentWindow, globalShortcut } = require("electron").remote;
const vaultWindows = new Set(),
maximizedWindows = new Set(),
getWindows = () => [...vaultWindows],
observeWindows = () => {
const onWindowCreation = (win) => {
vaultWindows.add(win);
const childWindows = new Set(),
observeChildWindows = () => {
getCurrentWindow().webContents.on("did-create-window", (win) => {
childWindows.add(win);
win.on("close", () => childWindows.delete(win));
win.setSkipTaskbar(plugin.settings.hideTaskbarIcon);
win.on("close", () => {
if (win !== getCurrentWindow()) vaultWindows.delete(win);
});
// preserve maximised windows after minimisation
if (win.isMaximized()) maximizedWindows.add(win);
win.on("maximize", () => maximizedWindows.add(win));
win.on("unmaximize", () => maximizedWindows.delete(win));
};
onWindowCreation(getCurrentWindow());
getCurrentWindow().webContents.on("did-create-window", onWindowCreation);
if (process.platform === "darwin") {
// on macos, the "hide taskbar icon" option is implemented
// via app.dock.hide(): thus, the app as a whole will be
// hidden from the dock, including windows from other vaults.
// when a vault is closed via the "close vault" button,
// the cleanup process will call app.dock.show() to restore
// access to any other open vaults w/out the tray enabled
// => thus, this listener is required to re-hide the dock
// if switching to another vault with the option enabled
getCurrentWindow().on("focus", () => {
if (plugin.settings.hideTaskbarIcon) hideTaskbarIcons();
});
}
},
getAllWindows = () => [...childWindows, getCurrentWindow()],
showWindows = () => {
log(LOG_SHOWING_WINDOWS);
getWindows().forEach((win) => {
if (maximizedWindows.has(win)) {
win.maximize();
win.focus();
} else win.show();
});
getAllWindows().forEach((win) => win.show());
},
hideWindows = () => {
log(LOG_HIDING_WINDOWS);
getWindows().forEach((win) => [
getAllWindows().forEach((win) => [
win.isFocused() && win.blur(),
plugin.settings.runInBackground ? win.hide() : win.minimize(),
]);
},
toggleWindows = (checkForFocus = true) => {
const openWindows = getWindows().some((win) => {
const openWindows = getAllWindows().some((win) => {
return (!checkForFocus || win.isFocused()) && win.isVisible();
});
if (openWindows) hideWindows();
@ -119,13 +91,10 @@ const onWindowClose = (event) => event.preventDefault(),
window.removeEventListener("beforeunload", onWindowUnload, true);
};
const hideTaskbarIcons = () => {
getWindows().forEach((win) => win.setSkipTaskbar(true));
if (process.platform === "darwin") app.dock.hide();
},
showTaskbarIcons = () => {
getWindows().forEach((win) => win.setSkipTaskbar(false));
if (process.platform === "darwin") app.dock.show();
const setHideTaskbarIcon = () => {
getAllWindows().forEach((win) => {
win.setSkipTaskbar(plugin.settings.hideTaskbarIcon);
});
},
setLaunchOnStartup = () => {
const { launchOnStartup, runInBackground, hideOnLaunch } = plugin.settings;
@ -133,29 +102,17 @@ const hideTaskbarIcons = () => {
openAtLogin: launchOnStartup,
openAsHidden: runInBackground && hideOnLaunch,
});
};
const cleanup = () => {
log(LOG_CLEANUP);
unregisterHotkeys();
showTaskbarIcons();
allowWindowClose();
destroyTray();
},
relaunchApp = () => {
relaunchObsidian = () => {
app.relaunch();
app.exit(0);
},
closeVault = () => {
quitObsidian = () => {
log(LOG_CLEANUP);
cleanup();
const vaultWindows = getWindows(),
obsidianWindows = BrowserWindow.getAllWindows();
if (obsidianWindows.length === vaultWindows.length) {
// quit app directly if only remaining windows are in the
// current vault - necessary for successful quit on macos
app.quit();
} else vaultWindows.forEach((win) => win.destroy());
unregisterHotkeys();
allowWindowClose();
destroyTray();
getAllWindows().forEach((win) => win.destroy());
};
const addQuickNote = () => {
@ -164,23 +121,10 @@ const addQuickNote = () => {
date = obsidian.moment().format(pattern),
name = obsidian
.normalizePath(`${quickNoteLocation ?? ""}/${date}`)
.replace(/\*|"|\\|<|>|:|\||\?/g, "-"),
// manually create and open file instead of depending
// on createAndOpenMarkdownFile to force file creation
// relative to the root instead of the active file
// (in case user has default location for new notes
// set to "same folder as current file")
leaf = plugin.app.workspace.getLeaf(),
root = plugin.app.fileManager.getNewFileParent(""),
openMode = { active: true, state: { mode: "source" } };
plugin.app.fileManager
.createNewMarkdownFile(root, name)
.then((file) => leaf.openFile(file, openMode));
.replace(/\*|"|\\|<|>|:|\||\?/g, "-");
plugin.app.fileManager.createAndOpenMarkdownFile(name);
showWindows();
},
replaceVaultName = (str) => {
return str.replace(/{{vault}}/g, plugin.app.vault.getName());
},
destroyTray = () => {
tray?.destroy();
tray = undefined;
@ -201,7 +145,7 @@ const addQuickNote = () => {
},
{
type: "normal",
label: ACTION_SHOW,
label: ACTION_OPEN,
accelerator: plugin.settings.toggleWindowFocusHotkey,
click: showWindows,
},
@ -212,19 +156,13 @@ const addQuickNote = () => {
click: hideWindows,
},
{ type: "separator" },
{ label: ACTION_RELAUNCH, click: relaunchApp },
{ label: ACTION_CLOSE, click: closeVault },
{ label: ACTION_RELAUNCH, click: relaunchObsidian },
{ label: ACTION_QUIT, click: quitObsidian },
]);
tray = new Tray(obsidianIcon);
tray.setContextMenu(contextMenu);
tray.setToolTip(replaceVaultName(plugin.settings.trayIconTooltip));
tray.on("click", () => {
if (process.platform === "darwin") {
// macos does not register separate left/right click actions
// for menu items, icon should open menu w/out causing toggle
tray.popUpContextMenu();
} else toggleWindows(false);
});
tray.setToolTip("Obsidian");
tray.on("click", () => toggleWindows(false));
};
const registerHotkeys = () => {
@ -276,22 +214,19 @@ const OPTIONS = [
default: false,
onChange() {
setLaunchOnStartup();
if (plugin.settings.runInBackground) interceptWindowClose();
else [allowWindowClose(), showWindows()];
const runInBackground = plugin.settings.runInBackground;
if (!runInBackground) showWindows();
},
},
{
key: "hideTaskbarIcon",
desc: `
Hides the window's icon from from the dock/taskbar. Enabling the tray icon first
is recommended if using this option. This may not work on Linux-based OSes.
is recommended if using this option. This may not work on all Linux-based OSes.
`,
type: "toggle",
default: false,
onChange() {
if (plugin.settings.hideTaskbarIcon) hideTaskbarIcons();
else showTaskbarIcons();
},
onChange: setHideTaskbarIcon,
},
{
key: "createTrayIcon",
@ -314,18 +249,6 @@ const OPTIONS = [
default: OBSIDIAN_BASE64_ICON,
onChange: createTrayIcon,
},
{
key: "trayIconTooltip",
desc: `
Set a title to identify the tray/menubar icon by. The
<code>{{vault}}</code> placeholder will be replaced by the vault name.
<br>Preview: <b class="u-pop" data-preview></b>
`,
type: "text",
default: "{{vault}} | Obsidian",
postprocessor: replaceVaultName,
onChange: createTrayIcon,
},
{
key: "toggleWindowFocusHotkey",
desc: ACCELERATOR_FORMAT,
@ -391,14 +314,18 @@ class SettingsTab extends obsidian.PluginSettingTab {
await opt.onChange?.();
};
const value = plugin.settings[opt.key] ?? opt.default ?? "";
if (opt.type === "toggle") {
setting.addToggle((toggle) => {
toggle.setValue(value).onChange(onChange);
toggle
.setValue(plugin.settings[opt.key] ?? opt.default)
.onChange(onChange);
});
} else if (opt.type === "image") {
const previewImg = setting.descEl.querySelector("img[data-preview");
if (previewImg) previewImg.src = value;
if (previewImg) {
const src = plugin.settings[opt.key] ?? opt.default;
previewImg.src = src;
}
const fileUpload = setting.descEl.createEl("input");
fileUpload.style.visibility = "hidden";
fileUpload.type = "file";
@ -421,21 +348,15 @@ class SettingsTab extends obsidian.PluginSettingTab {
moment
.setPlaceholder(opt.placeholder)
.setDefaultFormat(opt.default ?? "")
.setValue(value)
.setValue(plugin.settings[opt.key] ?? opt.default ?? "")
.onChange(onChange);
});
} else {
const previewEl = setting.descEl.querySelector("[data-preview]"),
updatePreview = (value) => {
if (!previewEl) return;
previewEl.innerText = opt?.postprocessor(value) ?? value;
};
updatePreview(value);
setting.addText((text) => {
text
.setPlaceholder(opt.placeholder)
.setValue(value)
.onChange((value) => [onChange(value), updatePreview(value)]);
.setValue(plugin.settings[opt.key] ?? opt.default ?? "")
.onChange(onChange);
});
}
}
@ -453,10 +374,10 @@ class TrayPlugin extends obsidian.Plugin {
plugin = this;
createTrayIcon();
registerHotkeys();
setHideTaskbarIcon();
setLaunchOnStartup();
observeWindows();
observeChildWindows();
if (settings.runInBackground) interceptWindowClose();
if (settings.hideTaskbarIcon) hideTaskbarIcons();
if (settings.hideOnLaunch) {
this.registerEvent(this.app.workspace.onLayoutReady(hideWindows));
}
@ -466,24 +387,16 @@ class TrayPlugin extends obsidian.Plugin {
this.addCommand({
id: "relaunch-app",
name: ACTION_RELAUNCH,
callback: relaunchApp,
});
this.addCommand({
id: "close-vault",
name: ACTION_CLOSE,
callback: closeVault,
callback: relaunchObsidian,
});
}
onunload() {
cleanup();
log(LOG_CLEANUP);
unregisterHotkeys();
allowWindowClose();
destroyTray();
}
getCurrentWindow = getCurrentWindow
getWindows = getWindows;
showWindows = showWindows;
hideWindows = hideWindows;
toggleWindows = toggleWindows;
async loadSettings() {
const DEFAULT_SETTINGS = OPTIONS.map((opt) => ({ [opt.key]: opt.default }));
this.settings = Object.assign(...DEFAULT_SETTINGS, await this.loadData());

View File

@ -3,8 +3,8 @@
"name": "Tray",
"author": "dragonwocky",
"authorUrl": "https://dragonwocky.me/",
"description": "Run Obsidian from the system tray for customisable window management & global quick notes",
"version": "0.3.5",
"description": "Launch Obsidian on startup and run it in the background from the system tray.",
"version": "0.3.1",
"isDesktopOnly": true,
"minAppVersion": "1.0.0"
}

BIN
tray.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 8.4 KiB