Compare commits

...

17 Commits
0.3.3 ... main

Author SHA1 Message Date
Tom
f4e827253d
merge #51: expose windowing to other plugins
Expose Windowing APIs to Other Plugins
2025-03-15 22:06:39 +11:00
Blue Falcon
62fcf88fe6
feat: expose getCurrentWindow 2024-06-23 02:13:35 -07:00
Blue Falcon
09fe3c2d03
feat: expose windowing APIs to other plugins
exposes:
- getWindows()
- showWindows()
- hideWindows()
- toggleWindows()
2024-06-23 01:57:53 -07:00
cc0baa3226
fix: #29 #30 focus maximised windows on trigger after alt-tab 2023-09-04 21:26:01 +10:00
6a76f575ed
revert hotkey changes as per #19 2023-08-24 18:40:50 +10:00
b60e453b68
fix #28: restore dock on close, hide dock on focus if req 2023-08-24 18:39:19 +10:00
999213801f
set public manifest version to 0.3.3 2023-08-24 18:25:29 +10:00
77ce4bd54a
partial fix #28: will quit obsidian if single vault is closed
however, causes dock to vanish if multiple vaults are open?
2023-08-23 23:17:51 +10:00
096bb34d54
fix #19: use super for default hotkeys to avoid conflicts
e.g. CmdOrCtrl+Shift+Tab is bound by default to switch tabs in obsidian and chrome

note that this will not affect macos users, as super still binds to cmd
2023-08-23 22:37:51 +10:00
fb15c25171
fix #16: disable toggle on macos menubar icon click 2023-08-23 22:06:27 +10:00
b62d8862ab
theoretically fix #28: quit app on macos if no windows remain
fix: keep track of current window even after 'close' for toggling back to focus with hotkey if run in background enabled (bug introduced in 5cef2b0ed7)
2023-08-23 21:58:06 +10:00
4c0c3da898
theoretically fix #27: call app.dock.hide() on mac
+ restore hidden taskbar icons on plugin cleanup/disable
2023-08-23 21:38:04 +10:00
b8326526ef
fix #21: always create quick notes relative to root 2023-08-23 21:25:41 +10:00
5cef2b0ed7
fix #22: preserve maximised windows after minimisation 2023-08-23 20:40:47 +10:00
Tom
78e819cc45
merge pull request #24 from Zarpyk/main
semi-addresses #22: will re-maximise windows automatically when minimised

however, only works when run in background is enabled and causes weird window flickering on minimisation
2023-08-23 20:37:49 +10:00
Zarpyk
71ba850a15
Fix #22 2023-07-27 13:58:32 +02:00
Tom
a5abaf32e7
chore: remove "coming soon" from readme
plugin has been merged into official repo: https://github.com/obsidianmd/obsidian-releases/pull/1365
2023-04-21 15:30:17 +10:00
3 changed files with 99 additions and 34 deletions

View File

@ -33,7 +33,7 @@ Hotkeys can be assigned to the commands via Obsidian's built-in hotkey manager.
## Installation ## Installation
### Obsidian Marketplace (Coming Soon) ### Obsidian Marketplace
1. In Obsidian, navigate to **Settings****Community plugins**. 1. In Obsidian, navigate to **Settings****Community plugins**.
2. Press the **Browse** button beside the **Community plugins** option. 2. Press the **Browse** button beside the **Community plugins** option.

129
main.js
View File

@ -1,5 +1,5 @@
/** /**
* obsidian-tray v0.3.3 * obsidian-tray v0.3.5
* (c) 2023 dragonwocky <thedragonring.bod@gmail.com> (https://dragonwocky.me/) * (c) 2023 dragonwocky <thedragonring.bod@gmail.com> (https://dragonwocky.me/)
* (https://github.com/dragonwocky/obsidian-tray/) under the MIT license * (https://github.com/dragonwocky/obsidian-tray/) under the MIT license
*/ */
@ -38,31 +38,59 @@ const LOG_PREFIX = "obsidian-tray",
let tray, plugin; let tray, plugin;
const obsidian = require("obsidian"), 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; { getCurrentWindow, globalShortcut } = require("electron").remote;
const childWindows = new Set(), const vaultWindows = new Set(),
observeChildWindows = () => { maximizedWindows = new Set(),
getCurrentWindow().webContents.on("did-create-window", (win) => { getWindows = () => [...vaultWindows],
childWindows.add(win); observeWindows = () => {
win.on("close", () => childWindows.delete(win)); const onWindowCreation = (win) => {
vaultWindows.add(win);
win.setSkipTaskbar(plugin.settings.hideTaskbarIcon); 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 = () => { showWindows = () => {
log(LOG_SHOWING_WINDOWS); log(LOG_SHOWING_WINDOWS);
getAllWindows().forEach((win) => win.show()); getWindows().forEach((win) => {
if (maximizedWindows.has(win)) {
win.maximize();
win.focus();
} else win.show();
});
}, },
hideWindows = () => { hideWindows = () => {
log(LOG_HIDING_WINDOWS); log(LOG_HIDING_WINDOWS);
getAllWindows().forEach((win) => [ getWindows().forEach((win) => [
win.isFocused() && win.blur(), win.isFocused() && win.blur(),
plugin.settings.runInBackground ? win.hide() : win.minimize(), plugin.settings.runInBackground ? win.hide() : win.minimize(),
]); ]);
}, },
toggleWindows = (checkForFocus = true) => { toggleWindows = (checkForFocus = true) => {
const openWindows = getAllWindows().some((win) => { const openWindows = getWindows().some((win) => {
return (!checkForFocus || win.isFocused()) && win.isVisible(); return (!checkForFocus || win.isFocused()) && win.isVisible();
}); });
if (openWindows) hideWindows(); if (openWindows) hideWindows();
@ -91,10 +119,13 @@ const onWindowClose = (event) => event.preventDefault(),
window.removeEventListener("beforeunload", onWindowUnload, true); window.removeEventListener("beforeunload", onWindowUnload, true);
}; };
const setHideTaskbarIcon = () => { const hideTaskbarIcons = () => {
getAllWindows().forEach((win) => { getWindows().forEach((win) => win.setSkipTaskbar(true));
win.setSkipTaskbar(plugin.settings.hideTaskbarIcon); if (process.platform === "darwin") app.dock.hide();
}); },
showTaskbarIcons = () => {
getWindows().forEach((win) => win.setSkipTaskbar(false));
if (process.platform === "darwin") app.dock.show();
}, },
setLaunchOnStartup = () => { setLaunchOnStartup = () => {
const { launchOnStartup, runInBackground, hideOnLaunch } = plugin.settings; const { launchOnStartup, runInBackground, hideOnLaunch } = plugin.settings;
@ -102,6 +133,14 @@ const setHideTaskbarIcon = () => {
openAtLogin: launchOnStartup, openAtLogin: launchOnStartup,
openAsHidden: runInBackground && hideOnLaunch, openAsHidden: runInBackground && hideOnLaunch,
}); });
};
const cleanup = () => {
log(LOG_CLEANUP);
unregisterHotkeys();
showTaskbarIcons();
allowWindowClose();
destroyTray();
}, },
relaunchApp = () => { relaunchApp = () => {
app.relaunch(); app.relaunch();
@ -109,10 +148,14 @@ const setHideTaskbarIcon = () => {
}, },
closeVault = () => { closeVault = () => {
log(LOG_CLEANUP); log(LOG_CLEANUP);
unregisterHotkeys(); cleanup();
allowWindowClose(); const vaultWindows = getWindows(),
destroyTray(); obsidianWindows = BrowserWindow.getAllWindows();
getAllWindows().forEach((win) => win.destroy()); 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 = () => { const addQuickNote = () => {
@ -121,8 +164,18 @@ const addQuickNote = () => {
date = obsidian.moment().format(pattern), date = obsidian.moment().format(pattern),
name = obsidian name = obsidian
.normalizePath(`${quickNoteLocation ?? ""}/${date}`) .normalizePath(`${quickNoteLocation ?? ""}/${date}`)
.replace(/\*|"|\\|<|>|:|\||\?/g, "-"); .replace(/\*|"|\\|<|>|:|\||\?/g, "-"),
plugin.app.fileManager.createAndOpenMarkdownFile(name); // 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(); showWindows();
}, },
replaceVaultName = (str) => { replaceVaultName = (str) => {
@ -165,7 +218,13 @@ const addQuickNote = () => {
tray = new Tray(obsidianIcon); tray = new Tray(obsidianIcon);
tray.setContextMenu(contextMenu); tray.setContextMenu(contextMenu);
tray.setToolTip(replaceVaultName(plugin.settings.trayIconTooltip)); tray.setToolTip(replaceVaultName(plugin.settings.trayIconTooltip));
tray.on("click", () => toggleWindows(false)); 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 = () => { const registerHotkeys = () => {
@ -217,8 +276,8 @@ const OPTIONS = [
default: false, default: false,
onChange() { onChange() {
setLaunchOnStartup(); setLaunchOnStartup();
const runInBackground = plugin.settings.runInBackground; if (plugin.settings.runInBackground) interceptWindowClose();
if (!runInBackground) showWindows(); else [allowWindowClose(), showWindows()];
}, },
}, },
{ {
@ -229,7 +288,10 @@ const OPTIONS = [
`, `,
type: "toggle", type: "toggle",
default: false, default: false,
onChange: setHideTaskbarIcon, onChange() {
if (plugin.settings.hideTaskbarIcon) hideTaskbarIcons();
else showTaskbarIcons();
},
}, },
{ {
key: "createTrayIcon", key: "createTrayIcon",
@ -391,10 +453,10 @@ class TrayPlugin extends obsidian.Plugin {
plugin = this; plugin = this;
createTrayIcon(); createTrayIcon();
registerHotkeys(); registerHotkeys();
setHideTaskbarIcon();
setLaunchOnStartup(); setLaunchOnStartup();
observeChildWindows(); observeWindows();
if (settings.runInBackground) interceptWindowClose(); if (settings.runInBackground) interceptWindowClose();
if (settings.hideTaskbarIcon) hideTaskbarIcons();
if (settings.hideOnLaunch) { if (settings.hideOnLaunch) {
this.registerEvent(this.app.workspace.onLayoutReady(hideWindows)); this.registerEvent(this.app.workspace.onLayoutReady(hideWindows));
} }
@ -413,12 +475,15 @@ class TrayPlugin extends obsidian.Plugin {
}); });
} }
onunload() { onunload() {
log(LOG_CLEANUP); cleanup();
unregisterHotkeys();
allowWindowClose();
destroyTray();
} }
getCurrentWindow = getCurrentWindow
getWindows = getWindows;
showWindows = showWindows;
hideWindows = hideWindows;
toggleWindows = toggleWindows;
async loadSettings() { async loadSettings() {
const DEFAULT_SETTINGS = OPTIONS.map((opt) => ({ [opt.key]: opt.default })); const DEFAULT_SETTINGS = OPTIONS.map((opt) => ({ [opt.key]: opt.default }));
this.settings = Object.assign(...DEFAULT_SETTINGS, await this.loadData()); this.settings = Object.assign(...DEFAULT_SETTINGS, await this.loadData());

View File

@ -4,7 +4,7 @@
"author": "dragonwocky", "author": "dragonwocky",
"authorUrl": "https://dragonwocky.me/", "authorUrl": "https://dragonwocky.me/",
"description": "Run Obsidian from the system tray for customisable window management & global quick notes", "description": "Run Obsidian from the system tray for customisable window management & global quick notes",
"version": "0.3.3", "version": "0.3.5",
"isDesktopOnly": true, "isDesktopOnly": true,
"minAppVersion": "1.0.0" "minAppVersion": "1.0.0"
} }