From c8efb5ff3f5ee9c7a502bf1a4012faadb0ea0014 Mon Sep 17 00:00:00 2001 From: rordaz Date: Thu, 11 Jun 2026 13:11:25 -0500 Subject: [PATCH] feat: enhance color tab functionality with colorable leaf checks and auto pinning logic --- main.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/main.ts b/main.ts index 860e50a..316df87 100644 --- a/main.ts +++ b/main.ts @@ -34,7 +34,7 @@ const DEFAULT_SETTINGS: ColorTabSettings = { }; export default class ColorTabPlugin extends Plugin { - settings: ColorTabSettings; + settings!: ColorTabSettings; async onload() { await this.loadSettings(); @@ -71,6 +71,8 @@ export default class ColorTabPlugin extends Plugin { // ── Context menu ────────────────────────────────────────────────────────── private addColorMenuItems(menu: Menu, leaf: WorkspaceLeaf) { + if (!this.isColorableLeaf(leaf)) return; + menu.addSeparator(); this.settings.colors.forEach(({ name, color }) => { @@ -102,6 +104,8 @@ export default class ColorTabPlugin extends Plugin { // ── Color application ───────────────────────────────────────────────────── setTabColor(leaf: WorkspaceLeaf, color: string) { + if (!this.isColorableLeaf(leaf)) return; + const path = this.getFilePath(leaf); if (path) { this.settings.fileColors[path] = color; @@ -114,6 +118,8 @@ export default class ColorTabPlugin extends Plugin { } removeTabColor(leaf: WorkspaceLeaf) { + if (!this.isColorableLeaf(leaf)) return; + const path = this.getFilePath(leaf); if (path) { delete this.settings.fileColors[path]; @@ -160,11 +166,22 @@ export default class ColorTabPlugin extends Plugin { applyAllColors() { this.app.workspace.iterateAllLeaves((leaf) => { const path = this.getFilePath(leaf); + const tabHeader = ( + leaf as unknown as { tabHeaderEl?: HTMLElement } + ).tabHeaderEl; + const wasAccidentallyColoredNonFileLeaf = + !path && !!tabHeader?.classList.contains("color-tab-colored"); const color = path ? (this.settings.fileColors[path] ?? null) : null; this.applyColorToLeaf(leaf, color); if (this.settings.autoPinColoredTabs && color) { leaf.setPinned(true); } + if ( + this.settings.autoPinColoredTabs && + wasAccidentallyColoredNonFileLeaf + ) { + leaf.setPinned(false); + } }); } @@ -176,6 +193,10 @@ export default class ColorTabPlugin extends Plugin { return file?.path ?? null; } + private isColorableLeaf(leaf: WorkspaceLeaf): boolean { + return this.getFilePath(leaf) !== null; + } + // ── Settings persistence ────────────────────────────────────────────────── async loadSettings() {