mirror of
https://github.com/dragonwocky/obsidian-tray.git
synced 2025-04-04 12:09:03 +00:00
Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
f4e827253d | |||
|
62fcf88fe6 | ||
|
09fe3c2d03 | ||
cc0baa3226 | |||
6a76f575ed | |||
b60e453b68 | |||
999213801f | |||
77ce4bd54a | |||
096bb34d54 | |||
fb15c25171 | |||
b62d8862ab | |||
4c0c3da898 | |||
b8326526ef | |||
5cef2b0ed7 | |||
78e819cc45 | |||
|
71ba850a15 | ||
a5abaf32e7 | |||
d49c49fe83 | |||
6f81acb0ad | |||
aac711518b | |||
aa4e824723 | |||
d37152dd4e | |||
fcce4935a3 | |||
b8834b1286 | |||
16d577d67e | |||
|
f0c6a5d49f | ||
4e097cca14 |
41
README.md
41
README.md
@ -1,38 +1,39 @@
|
||||
<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 that
|
||||
toggle app window visibility and can 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 to
|
||||
toggle window visibility and 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 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._ |  |
|
||||
| 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 |
|
||||
| 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 |  |
|
||||
| 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> |
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
### 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) | CmdOrCtrl+Shift+Q |
|
||||
| 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> |
|
||||
|
||||
## Installation
|
||||
|
||||
### Obsidian Marketplace (Coming Soon)
|
||||
### Obsidian Marketplace
|
||||
|
||||
1. In Obsidian, navigate to **Settings** → **Community plugins**.
|
||||
2. Press the **Browse** button beside the **Community plugins** option.
|
||||
|
195
main.js
195
main.js
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* obsidian-tray v0.3.0
|
||||
* obsidian-tray v0.3.5
|
||||
* (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 = "Add Quick Note",
|
||||
ACTION_OPEN = "Open Obsidian",
|
||||
ACTION_HIDE = "Hide Obsidian",
|
||||
ACTION_QUICK_NOTE = "Quick Note",
|
||||
ACTION_SHOW = "Show Vault",
|
||||
ACTION_HIDE = "Hide Vault",
|
||||
ACTION_RELAUNCH = "Relaunch Obsidian",
|
||||
ACTION_QUIT = "Quit Obsidian",
|
||||
ACTION_CLOSE = "Close Vault",
|
||||
DEFAULT_DATE_FORMAT = "YYYY-MM-DD",
|
||||
ACCELERATOR_FORMAT = `
|
||||
This hotkey is registered globally and will be detected even if Obsidian does
|
||||
@ -38,31 +38,59 @@ const LOG_PREFIX = "obsidian-tray",
|
||||
|
||||
let tray, plugin;
|
||||
const obsidian = require("obsidian"),
|
||||
{ app, Tray, Menu, nativeImage } = require("electron").remote,
|
||||
{ app, Tray, Menu } = require("electron").remote,
|
||||
{ nativeImage, BrowserWindow } = require("electron").remote,
|
||||
{ getCurrentWindow, globalShortcut } = require("electron").remote;
|
||||
|
||||
const childWindows = new Set(),
|
||||
observeChildWindows = () => {
|
||||
getCurrentWindow().webContents.on("did-create-window", (win) => {
|
||||
childWindows.add(win);
|
||||
win.on("close", () => childWindows.delete(win));
|
||||
const vaultWindows = new Set(),
|
||||
maximizedWindows = new Set(),
|
||||
getWindows = () => [...vaultWindows],
|
||||
observeWindows = () => {
|
||||
const onWindowCreation = (win) => {
|
||||
vaultWindows.add(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);
|
||||
getAllWindows().forEach((win) => win.show());
|
||||
getWindows().forEach((win) => {
|
||||
if (maximizedWindows.has(win)) {
|
||||
win.maximize();
|
||||
win.focus();
|
||||
} else win.show();
|
||||
});
|
||||
},
|
||||
hideWindows = () => {
|
||||
log(LOG_HIDING_WINDOWS);
|
||||
getAllWindows().forEach((win) => [
|
||||
getWindows().forEach((win) => [
|
||||
win.isFocused() && win.blur(),
|
||||
plugin.settings.runInBackground ? win.hide() : win.minimize(),
|
||||
]);
|
||||
},
|
||||
toggleWindows = (checkForFocus = true) => {
|
||||
const openWindows = getAllWindows().some((win) => {
|
||||
const openWindows = getWindows().some((win) => {
|
||||
return (!checkForFocus || win.isFocused()) && win.isVisible();
|
||||
});
|
||||
if (openWindows) hideWindows();
|
||||
@ -91,10 +119,13 @@ const onWindowClose = (event) => event.preventDefault(),
|
||||
window.removeEventListener("beforeunload", onWindowUnload, true);
|
||||
};
|
||||
|
||||
const setHideTaskbarIcon = () => {
|
||||
getAllWindows().forEach((win) => {
|
||||
win.setSkipTaskbar(plugin.settings.hideTaskbarIcon);
|
||||
});
|
||||
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();
|
||||
},
|
||||
setLaunchOnStartup = () => {
|
||||
const { launchOnStartup, runInBackground, hideOnLaunch } = plugin.settings;
|
||||
@ -102,17 +133,29 @@ const setHideTaskbarIcon = () => {
|
||||
openAtLogin: launchOnStartup,
|
||||
openAsHidden: runInBackground && hideOnLaunch,
|
||||
});
|
||||
};
|
||||
|
||||
const cleanup = () => {
|
||||
log(LOG_CLEANUP);
|
||||
unregisterHotkeys();
|
||||
showTaskbarIcons();
|
||||
allowWindowClose();
|
||||
destroyTray();
|
||||
},
|
||||
relaunchObsidian = () => {
|
||||
relaunchApp = () => {
|
||||
app.relaunch();
|
||||
app.exit(0);
|
||||
},
|
||||
quitObsidian = () => {
|
||||
closeVault = () => {
|
||||
log(LOG_CLEANUP);
|
||||
unregisterHotkeys();
|
||||
allowWindowClose();
|
||||
destroyTray();
|
||||
getAllWindows().forEach((win) => win.destroy());
|
||||
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());
|
||||
};
|
||||
|
||||
const addQuickNote = () => {
|
||||
@ -121,10 +164,23 @@ const addQuickNote = () => {
|
||||
date = obsidian.moment().format(pattern),
|
||||
name = obsidian
|
||||
.normalizePath(`${quickNoteLocation ?? ""}/${date}`)
|
||||
.replace(/\*|"|\\|<|>|:|\||\?/g, "-");
|
||||
plugin.app.fileManager.createAndOpenMarkdownFile(name);
|
||||
.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));
|
||||
showWindows();
|
||||
},
|
||||
replaceVaultName = (str) => {
|
||||
return str.replace(/{{vault}}/g, plugin.app.vault.getName());
|
||||
},
|
||||
destroyTray = () => {
|
||||
tray?.destroy();
|
||||
tray = undefined;
|
||||
@ -145,7 +201,7 @@ const addQuickNote = () => {
|
||||
},
|
||||
{
|
||||
type: "normal",
|
||||
label: ACTION_OPEN,
|
||||
label: ACTION_SHOW,
|
||||
accelerator: plugin.settings.toggleWindowFocusHotkey,
|
||||
click: showWindows,
|
||||
},
|
||||
@ -156,13 +212,19 @@ const addQuickNote = () => {
|
||||
click: hideWindows,
|
||||
},
|
||||
{ type: "separator" },
|
||||
{ label: ACTION_RELAUNCH, click: relaunchObsidian },
|
||||
{ label: ACTION_QUIT, click: quitObsidian },
|
||||
{ label: ACTION_RELAUNCH, click: relaunchApp },
|
||||
{ label: ACTION_CLOSE, click: closeVault },
|
||||
]);
|
||||
tray = new Tray(obsidianIcon);
|
||||
tray.setContextMenu(contextMenu);
|
||||
tray.setToolTip("Obsidian");
|
||||
tray.on("click", () => toggleWindows(false));
|
||||
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);
|
||||
});
|
||||
};
|
||||
|
||||
const registerHotkeys = () => {
|
||||
@ -214,19 +276,22 @@ const OPTIONS = [
|
||||
default: false,
|
||||
onChange() {
|
||||
setLaunchOnStartup();
|
||||
const runInBackground = plugin.settings.runInBackground;
|
||||
if (!runInBackground) showWindows();
|
||||
if (plugin.settings.runInBackground) interceptWindowClose();
|
||||
else [allowWindowClose(), 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 all Linux-based OSes.
|
||||
is recommended if using this option. This may not work on Linux-based OSes.
|
||||
`,
|
||||
type: "toggle",
|
||||
default: false,
|
||||
onChange: setHideTaskbarIcon,
|
||||
onChange() {
|
||||
if (plugin.settings.hideTaskbarIcon) hideTaskbarIcons();
|
||||
else showTaskbarIcons();
|
||||
},
|
||||
},
|
||||
{
|
||||
key: "createTrayIcon",
|
||||
@ -249,6 +314,18 @@ 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,
|
||||
@ -314,18 +391,14 @@ 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(plugin.settings[opt.key] ?? opt.default)
|
||||
.onChange(onChange);
|
||||
toggle.setValue(value).onChange(onChange);
|
||||
});
|
||||
} else if (opt.type === "image") {
|
||||
const previewImg = setting.descEl.querySelector("img[data-preview");
|
||||
if (previewImg) {
|
||||
const src = plugin.settings[opt.key] ?? opt.default;
|
||||
previewImg.src = src;
|
||||
}
|
||||
if (previewImg) previewImg.src = value;
|
||||
const fileUpload = setting.descEl.createEl("input");
|
||||
fileUpload.style.visibility = "hidden";
|
||||
fileUpload.type = "file";
|
||||
@ -348,15 +421,21 @@ class SettingsTab extends obsidian.PluginSettingTab {
|
||||
moment
|
||||
.setPlaceholder(opt.placeholder)
|
||||
.setDefaultFormat(opt.default ?? "")
|
||||
.setValue(plugin.settings[opt.key] ?? opt.default ?? "")
|
||||
.setValue(value)
|
||||
.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(plugin.settings[opt.key] ?? opt.default ?? "")
|
||||
.onChange(onChange);
|
||||
.setValue(value)
|
||||
.onChange((value) => [onChange(value), updatePreview(value)]);
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -374,10 +453,10 @@ class TrayPlugin extends obsidian.Plugin {
|
||||
plugin = this;
|
||||
createTrayIcon();
|
||||
registerHotkeys();
|
||||
setHideTaskbarIcon();
|
||||
setLaunchOnStartup();
|
||||
observeChildWindows();
|
||||
observeWindows();
|
||||
if (settings.runInBackground) interceptWindowClose();
|
||||
if (settings.hideTaskbarIcon) hideTaskbarIcons();
|
||||
if (settings.hideOnLaunch) {
|
||||
this.registerEvent(this.app.workspace.onLayoutReady(hideWindows));
|
||||
}
|
||||
@ -387,16 +466,24 @@ class TrayPlugin extends obsidian.Plugin {
|
||||
this.addCommand({
|
||||
id: "relaunch-app",
|
||||
name: ACTION_RELAUNCH,
|
||||
callback: relaunchObsidian,
|
||||
callback: relaunchApp,
|
||||
});
|
||||
this.addCommand({
|
||||
id: "close-vault",
|
||||
name: ACTION_CLOSE,
|
||||
callback: closeVault,
|
||||
});
|
||||
}
|
||||
onunload() {
|
||||
log(LOG_CLEANUP);
|
||||
unregisterHotkeys();
|
||||
allowWindowClose();
|
||||
destroyTray();
|
||||
cleanup();
|
||||
}
|
||||
|
||||
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());
|
||||
|
@ -1,10 +1,10 @@
|
||||
{
|
||||
"id": "obsidian-tray",
|
||||
"id": "tray",
|
||||
"name": "Tray",
|
||||
"author": "dragonwocky",
|
||||
"authorUrl": "https://dragonwocky.me/",
|
||||
"description": "Launch Obsidian on startup and run it in the background from the system tray.",
|
||||
"version": "0.3.0",
|
||||
"description": "Run Obsidian from the system tray for customisable window management & global quick notes",
|
||||
"version": "0.3.5",
|
||||
"isDesktopOnly": true,
|
||||
"minAppVersion": "1.0.0"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user