Skip to content

Commit

Permalink
feat: add separate setting for overriding standard mode rendering #176
Browse files Browse the repository at this point in the history
Change overriding standard mode behavior setting into two separate settings. Introduce a new setting to control the rendering behavior, and repurpose the old, existing setting to control only the open (tab navigation) behavior.
  • Loading branch information
darlal committed Jan 3, 2025
1 parent 2a60873 commit b122011
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 18 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ In the demo below, `Quick Switcher++: Open Symbols for the active editor` global
| Hide path for root items | **Enabled**: path information will be hidden for items at the root of the vault. | enabled |
| Mode trigger escape character | A character that can be used as a prefix to force a mode trigger character to be treated just as normal text. | `!` |
| Default to open in new pane | **Enabled**: navigating to un-opened files will open a new editor pane whenever possible (as if cmd/ctrl were held). When the file is already open, the existing pane will be activated. This overrides all other pane settings. | enabled |
| Override Standard mode behavior | **Enabled**: Switcher++ will change the default Obsidian builtin Switcher functionality (Standard mode) to inject custom behavior. Custom behavior includes features like enhance rendering of suggestion items to display additional information, special path display handling, and tab navigation features.<br />**Disabled**: No changes are made to the default Obsidian builtin functionality (Standard Mode). | enabled |
| Override Standard mode file open behavior | **Enabled**: Switcher++ will change the default Obsidian builtin Switcher functionality (Standard mode) to inject custom file open (tab navigation) behavior.<br />**Disabled**: No changes are made to the default Obsidian builtin functionality (Standard Mode). | enabled |
| Override Standard mode rendering | **Enabled**: Switcher++ will change the default Obsidian builtin Switcher functionality (Standard mode) to render suggestions as multi-line. Including display of additional indicator information, and special path display handling.<br />**Disabled**: No changes are made to the default Obsidian builtin functionality (Standard Mode). | enabled |
| Show indicator icons | **Enabled**: Display icons to indicate that an item is recent, bookmarks, etc... | enabled |
| Result priority adjustments | **Enabled**: Artificially increase the match score of the specified item types by a fixed percentage so they appear higher in the results list (does not apply to Standard Mode). See the default list of [priority types](src/settings/switcherPlusSettings.ts#72) that can be prioritized. Note that the adjustments are independent of each other, e.g. if adjustments are specified for both `Bookmarked items` and `Open items` a result for a file that is open and bookmarked will receive both adjustments.<br />**Disabled** result match scores are not adjusted. | disabled |
| Use filename as alias | **Enabled**: The file basename will be set as the link alias.<br />**Disabled**: An alias will not be set | enabled |
Expand Down
14 changes: 13 additions & 1 deletion src/settings/__tests__/generalSettingsTabSection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,25 @@ describe('generalSettingsTabSection', () => {

expect(addToggleSettingSpy).toHaveBeenCalledWith(
mockContainerEl,
'Override Standard mode behavior',
'Override Standard mode file open behavior',
expect.any(String),
config.overrideStandardModeBehaviors,
'overrideStandardModeBehaviors',
);
});

it('should show the overrideStandardModeRendering setting', () => {
sut.display(mockContainerEl);

expect(addToggleSettingSpy).toHaveBeenCalledWith(
mockContainerEl,
'Override Standard mode rendering',
expect.any(String),
config.overrideStandardModeRendering,
'overrideStandardModeRendering',
);
});

it('should show the hidePathIfRoot setting', () => {
sut.display(mockContainerEl);

Expand Down
2 changes: 2 additions & 0 deletions src/settings/__tests__/switcherPlusSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function getDefaultSettingsData(): SettingsData {
enabledRelatedItems: Object.values(RelationType),
showOptionalIndicatorIcons: true,
overrideStandardModeBehaviors: true,
overrideStandardModeRendering: true,
enabledRibbonCommands: [
Mode[Mode.HeadingsList] as keyof typeof Mode,
Mode[Mode.SymbolList] as keyof typeof Mode,
Expand Down Expand Up @@ -219,6 +220,7 @@ function getTransientSettingsData(): SettingsData {
enabledRelatedItems: chance.pickset(Object.values(RelationType), 2),
showOptionalIndicatorIcons: chance.bool(),
overrideStandardModeBehaviors: chance.bool(),
overrideStandardModeRendering: chance.bool(),
enabledRibbonCommands,
fileExtAllowList: [],
matchPriorityAdjustments: {
Expand Down
12 changes: 10 additions & 2 deletions src/settings/generalSettingsTabSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ export class GeneralSettingsTabSection extends SettingsTabSection {

this.addToggleSetting(
containerEl,
'Override Standard mode behavior',
'When enabled, Switcher++ will change the default Obsidian builtin Switcher functionality (Standard mode) to inject custom behavior.',
'Override Standard mode file open behavior',
'When enabled, Switcher++ will change the default Obsidian builtin Switcher functionality (Standard mode) to inject custom file open behavior.',
config.overrideStandardModeBehaviors,
'overrideStandardModeBehaviors',
);

this.addToggleSetting(
containerEl,
'Override Standard mode rendering',
'When enabled, Switcher++ will change the default Obsidian builtin Switcher functionality (Standard mode) to render suggestions as multi-line.',
config.overrideStandardModeRendering,
'overrideStandardModeRendering',
);

this.addToggleSetting(
containerEl,
'Show indicator icons',
Expand Down
9 changes: 9 additions & 0 deletions src/settings/switcherPlusSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class SwitcherPlusSettings {
enabledRelatedItems: Object.values(RelationType),
showOptionalIndicatorIcons: true,
overrideStandardModeBehaviors: true,
overrideStandardModeRendering: true,
enabledRibbonCommands: [
Mode[Mode.HeadingsList] as keyof typeof Mode,
Mode[Mode.SymbolList] as keyof typeof Mode,
Expand Down Expand Up @@ -520,6 +521,14 @@ export class SwitcherPlusSettings {
this.data.overrideStandardModeBehaviors = value;
}

get overrideStandardModeRendering(): boolean {
return this.data.overrideStandardModeRendering;
}

set overrideStandardModeRendering(value: boolean) {
this.data.overrideStandardModeRendering = value;
}

get enabledRibbonCommands(): Array<keyof typeof Mode> {
return this.data.enabledRibbonCommands;
}
Expand Down
42 changes: 31 additions & 11 deletions src/switcherPlus/__tests__/modeHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,23 @@ describe('modeHandler', () => {
},
);

test('with overrideStandardModeRendering enabled, renderSuggestion() should use the StandardExHandler', () => {
mockSettings.overrideStandardModeRendering = true;
const sugg = makeFileSuggestion();

const renderSuggestionSpy = jest
.spyOn(StandardExHandler.prototype, 'renderSuggestion')
.mockImplementation();

sut.renderSuggestion(sugg, null);

expect(renderSuggestionSpy).toHaveBeenCalledWith(sugg, null);

renderSuggestionSpy.mockRestore();

mockSettings.overrideStandardModeRendering = false;
});

test('with overrideStandardModeBehaviors enabled, onChooseSuggestion should use the StandardExHandler', () => {
mockSettings.overrideStandardModeBehaviors = true;
const sugg = makeFileSuggestion();
Expand Down Expand Up @@ -1328,20 +1345,23 @@ describe('modeHandler', () => {
sut = new ModeHandler(mockApp, mockSettings, mock<SwitcherPlusKeymap>());
});

test('with overrideStandardModeBehaviors enabled, it should add properties to a Standard mode file suggestion', () => {
mockSettings.overrideStandardModeBehaviors = true;
const sugg = makeFileSuggestion();
const addPropsSpy = jest
.spyOn(StandardExHandler.prototype, 'addPropertiesToStandardSuggestions')
.mockImplementation();
test.each([makeFileSuggestion(), makeAliasSuggestion()])(
'with overrideStandardModeRendering enabled, it should add properties to a supported Standard mode suggestion',
(sugg) => {
mockSettings.overrideStandardModeBehaviors = false;
mockSettings.overrideStandardModeRendering = true;
const addPropsSpy = jest
.spyOn(StandardExHandler.prototype, 'addPropertiesToStandardSuggestions')
.mockImplementation();

sut.addPropertiesToStandardSuggestions([sugg], mockSettings);
sut.addPropertiesToStandardSuggestions([sugg], mockSettings);

expect(addPropsSpy).toHaveBeenCalled();
expect(addPropsSpy).toHaveBeenCalled();

addPropsSpy.mockRestore();
mockSettings.overrideStandardModeBehaviors = false;
});
addPropsSpy.mockRestore();
mockSettings.overrideStandardModeRendering = false;
},
);

test('with overrideStandardModeBehaviors enabled, it should add properties to a Standard mode bookmark suggestion', () => {
mockSettings.overrideStandardModeBehaviors = true;
Expand Down
9 changes: 6 additions & 3 deletions src/switcherPlus/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export class ModeHandler {
renderSuggestion(sugg: AnySuggestion, parentEl: HTMLElement): boolean {
const {
inputInfo,
settings: { overrideStandardModeBehaviors },
settings: { overrideStandardModeRendering },
} = this;
const { mode } = inputInfo;
const isHeadingMode = mode === Mode.HeadingsList;
Expand All @@ -315,7 +315,7 @@ export class ModeHandler {
handled = true;
}
} else if (!systemBehaviorPreferred.has(sugg.type)) {
if (overrideStandardModeBehaviors || isHeadingMode || isExSuggestion(sugg)) {
if (overrideStandardModeRendering || isHeadingMode || isExSuggestion(sugg)) {
// when overriding standard mode, or, in Headings mode, StandardExHandler should
// handle rendering for FileSuggestion and Alias suggestion
const handler = this.getHandler(sugg);
Expand Down Expand Up @@ -708,7 +708,10 @@ export class ModeHandler {
suggestions: AnySuggestion[],
config: SwitcherPlusSettings,
): void {
if (!suggestions || !config.overrideStandardModeBehaviors) {
if (
!suggestions ||
!(config.overrideStandardModeBehaviors || config.overrideStandardModeRendering)
) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/types/sharedTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ export interface SettingsData {
enabledRelatedItems: RelationType[];
showOptionalIndicatorIcons: boolean;
overrideStandardModeBehaviors: boolean;
overrideStandardModeRendering: boolean;
enabledRibbonCommands: Array<keyof typeof Mode>;
fileExtAllowList: Array<string>;
matchPriorityAdjustments: {
Expand Down

0 comments on commit b122011

Please sign in to comment.