feat: #2 customisable tray icon image

This commit is contained in:
dragonwocky 2023-04-10 21:24:13 +10:00
parent 84a02f157a
commit db671961c5
Signed by: dragonwocky
GPG Key ID: 7998D08F7D7BD7A8

35
main.js
View File

@ -122,7 +122,9 @@ const addQuickNote = (plugin) => {
},
createTrayIcon = (plugin) => {
log(LOG_TRAY_ICON);
const obsidianIcon = nativeImage.createFromDataURL(OBSIDIAN_BASE64_ICON),
const obsidianIcon = nativeImage.createFromDataURL(
plugin.settings.trayIconImage ?? OBSIDIAN_BASE64_ICON
),
contextMenu = Menu.buildFromTemplate([
{
type: "normal",
@ -236,6 +238,16 @@ const OPTIONS = [
type: "toggle",
default: true,
},
{
key: "trayIconImage",
desc: `
Set the image used by the tray/menubar icon. Recommended size: 16x16
<br>Preview: <img data-preview style="height: 16px; vertical-align: bottom;">
<br>${REQUIRES_RESTART}
`,
type: "image",
default: OBSIDIAN_BASE64_ICON,
},
{
key: "toggleWindowFocusHotkey",
desc: ACCELERATOR_FORMAT,
@ -311,6 +323,27 @@ class SettingsTab extends obsidian.PluginSettingTab {
.setValue(this.plugin.settings[opt.key] ?? opt.default)
.onChange(onChange);
});
} else if (opt.type === "image") {
const previewImg = setting.descEl.querySelector("img[data-preview");
if (previewImg) {
const src = this.plugin.settings[opt.key] ?? opt.default;
previewImg.src = src;
}
const fileUpload = setting.descEl.createEl("input");
fileUpload.style.visibility = "hidden";
fileUpload.type = "file";
fileUpload.onchange = (event) => {
const file = event.target.files[0],
reader = new FileReader();
reader.onloadend = () => {
onChange(reader.result);
if (previewImg) previewImg.src = reader.result;
};
reader.readAsDataURL(file);
};
setting.addButton((button) => {
button.setIcon("image").onClick(() => fileUpload.click());
});
} else if (opt.type === "moment") {
setting.addMomentFormat((moment) => {
const previewEl = setting.descEl.querySelector("[data-preview]");