Skip to content

Commit

Permalink
fix: issue with setting icon in tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWoelki committed Oct 18, 2024
1 parent 71bfed4 commit 60db32c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 35 deletions.
12 changes: 6 additions & 6 deletions src/lib/custom-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ const doesMatchFileType = (
* Determines whether a given file or folder matches a specified custom rule.
* @param plugin Plugin instance.
* @param rule CustomRule to check against the file or folder.
* @param file TAbstractFile to check against the custom rule.
* @param filePath String to check against the custom rule.
* @returns Promise that resolves to `true` if the file matches the rule, `false` otherwise.
*/
const isApplicable = async (
plugin: Plugin,
rule: CustomRule,
file: TAbstractFile,
filePath: string,
): Promise<boolean> => {
const metadata = await plugin.app.vault.adapter.stat(file.path);
const metadata = await plugin.app.vault.adapter.stat(filePath);
if (!metadata) {
return false;
}
Expand All @@ -51,7 +51,7 @@ const isApplicable = async (
return false;
}

return doesMatchPath(rule, file.path);
return doesMatchPath(rule, filePath);
};

/**
Expand Down Expand Up @@ -140,7 +140,7 @@ const add = async (
return false;
}

const doesMatch = await isApplicable(plugin, rule, file);
const doesMatch = await isApplicable(plugin, rule, file.path);
if (doesMatch) {
IconCache.getInstance().set(file.path, {
iconNameWithPrefix: rule.icon,
Expand Down Expand Up @@ -192,7 +192,7 @@ const getFileItems = async (
for (const fileExplorer of plugin.getRegisteredFileExplorers()) {
const files = Object.values(fileExplorer.fileItems);
for (const fileItem of files) {
if (await isApplicable(plugin, rule, fileItem.file)) {
if (await isApplicable(plugin, rule, fileItem.file.path)) {
result.push(fileItem);
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/lib/icon-tabs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TFile } from 'obsidian';
import IconizePlugin, { FolderIconObject } from '@app/main';
import { DEFAULT_FILE_ICON, getAllOpenedFiles } from '@app/util';
import { TabHeaderLeaf } from '@app/@types/obsidian';
Expand Down Expand Up @@ -41,13 +40,13 @@ interface AddOptions {
* Adds an icon to the tab and its container. This function respects the
* custom rules and individually icon set.
* @param plugin IconizePlugin instance.
* @param file TFile instance of the file to add the icon to.
* @param filePath String file path to add the icon to.
* @param iconContainer HTMLElement where the icon will be added to.
* @param options AddOptions for the add function which can optionally be used.
*/
const add = async (
plugin: IconizePlugin,
file: TFile,
filePath: string,
iconContainer: HTMLElement,
options?: AddOptions,
): Promise<void> => {
Expand All @@ -69,7 +68,7 @@ const add = async (

// Add icons to tabs if a custom rule is applicable.
for (const rule of customRule.getSortedRules(plugin)) {
const isApplicable = await customRule.isApplicable(plugin, rule, file);
const isApplicable = await customRule.isApplicable(plugin, rule, filePath);
if (isApplicable) {
dom.setIconForNode(plugin, rule.icon, iconContainer, {
color: rule.color,
Expand All @@ -81,7 +80,7 @@ const add = async (
}

// Add icons to tabs if there is an icon set.
const iconData = data.find(([dataPath]) => dataPath === file.path);
const iconData = data.find(([dataPath]) => dataPath === filePath);
if (!iconData) {
return;
}
Expand Down
14 changes: 5 additions & 9 deletions src/lib/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
nextIdentifier,
} from '@app/icon-pack-manager';
import config from '@app/config';
import { MarkdownView, Notice, requireApiVersion } from 'obsidian';
import { Notice, requireApiVersion } from 'obsidian';
import { IconCache } from './icon-cache';
import { logger } from './logger';

Expand Down Expand Up @@ -193,15 +193,11 @@ const addAll = (
// Adds icons to already open file tabs.
if (plugin.getSettings().iconInTabsEnabled) {
for (const leaf of plugin.app.workspace.getLeavesOfType('markdown')) {
if (!(leaf.view instanceof MarkdownView)) {
continue;
}

const file = leaf.view.file;
if (file) {
const filePath = leaf.view.file?.path ?? leaf.view.getState().file;
if (typeof filePath === 'string') {
const tabHeaderLeaf = leaf as TabHeaderLeaf;
const iconColor = plugin.getIconColor(file.path);
iconTabs.add(plugin, file, tabHeaderLeaf.tabHeaderInnerIconEl, {
const iconColor = plugin.getIconColor(filePath);
iconTabs.add(plugin, filePath, tabHeaderLeaf.tabHeaderInnerIconEl, {
iconColor,
});
}
Expand Down
14 changes: 5 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,7 @@ export default class IconizePlugin extends Plugin {
for (const tabLeaf of tabLeaves) {
// Add timeout to ensure that the default icon is already set.
setTimeout(() => {
iconTabs.add(
this,
file as TFile,
tabLeaf.tabHeaderInnerIconEl,
);
iconTabs.add(this, file.path, tabLeaf.tabHeaderInnerIconEl);
}, 5);
}
}
Expand Down Expand Up @@ -343,7 +339,7 @@ export default class IconizePlugin extends Plugin {
this.addIconInTitle(rule.icon);
const tabLeaves = iconTabs.getTabLeavesOfFilePath(this, file.path);
for (const tabLeaf of tabLeaves) {
iconTabs.add(this, file as TFile, tabLeaf.tabHeaderInnerIconEl, {
iconTabs.add(this, file.path, tabLeaf.tabHeaderInnerIconEl, {
iconName: rule.icon,
});
}
Expand Down Expand Up @@ -521,7 +517,7 @@ export default class IconizePlugin extends Plugin {
for (const openedFile of getAllOpenedFiles(this)) {
const leaf = openedFile.leaf as TabHeaderLeaf;
const iconColor = this.getIconColor(leaf.view.file.path);
iconTabs.add(this, openedFile, leaf.tabHeaderInnerIconEl, {
iconTabs.add(this, openedFile.path, leaf.tabHeaderInnerIconEl, {
iconColor,
});
}
Expand Down Expand Up @@ -683,7 +679,7 @@ export default class IconizePlugin extends Plugin {
for (const openedFile of getAllOpenedFiles(this)) {
const leaf = openedFile.leaf as TabHeaderLeaf;
const iconColor = this.getIconColor(leaf.view.file.path);
iconTabs.add(this, openedFile, leaf.tabHeaderInnerIconEl, {
iconTabs.add(this, openedFile.path, leaf.tabHeaderInnerIconEl, {
iconColor,
});
}
Expand All @@ -699,7 +695,7 @@ export default class IconizePlugin extends Plugin {
const iconColor = this.getIconColor(tabHeaderLeaf.view.file.path);
iconTabs.add(
this,
tabHeaderLeaf.view.file,
tabHeaderLeaf.view.file.path,
tabHeaderLeaf.tabHeaderInnerIconEl,
{
iconColor,
Expand Down
15 changes: 10 additions & 5 deletions src/settings/ui/customIconRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class CustomIconRuleSetting extends IconFolderSetting {
const applicable = await customRule.isApplicable(
this.plugin,
rule,
openedFile,
openedFile.path,
);
if (!applicable) {
continue;
Expand All @@ -74,10 +74,15 @@ export default class CustomIconRuleSetting extends IconFolderSetting {
replaceWithDefaultIcon: true,
});
} else {
iconTabs.add(this.plugin, openedFile, leaf.tabHeaderInnerIconEl, {
iconName: rule.icon,
iconColor: rule.color,
});
iconTabs.add(
this.plugin,
openedFile.path,
leaf.tabHeaderInnerIconEl,
{
iconName: rule.icon,
iconColor: rule.color,
},
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/settings/ui/toggleIconInTabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class ToggleIconInTabs extends IconFolderSetting {
// Adds the icons to already opened files.
iconTabs.add(
this.plugin,
file,
file.path,
tabHeaderLeaf.tabHeaderInnerIconEl,
);
} else {
Expand Down

0 comments on commit 60db32c

Please sign in to comment.