Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

multiple window feature in buffer explorer #775

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/background/completion/TabCompletionUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ export default class TabCompletionUseCase {
@inject("TabPresenter") private tabPresenter: TabPresenter
) {}

async queryTabs(query: string, excludePinned: boolean): Promise<TabItem[]> {
async queryTabs(query: string, excludePinned: boolean, onlyCurrentWin: boolean): Promise<TabItem[]> {
const multiIndex = (t: Tab) => t.index + 1 + parseFloat('0.' + t.windowId + '1')
const lastTabId = await this.tabPresenter.getLastSelectedId();
const allTabs = await this.tabRepository.getAllTabs(excludePinned);
const num = parseInt(query, 10);
const allTabs = await this.tabRepository.getAllTabs(excludePinned, onlyCurrentWin);
const num = parseFloat(query);
let tabs: Tab[] = [];
if (!isNaN(num)) {
const tab = allTabs.find((t) => t.index === num - 1);
if (tab) {
tabs = [tab];
let tab = allTabs.find((t) => t.index === num - 1);
if (!tab) {
tab = allTabs.find((t) => multiIndex(t) === num);
}
if(tab) {
tabs = [tab]
}

} else if (query == "%") {
const tab = allTabs.find((t) => t.active);
if (tab) {
Expand All @@ -32,8 +37,10 @@ export default class TabCompletionUseCase {
tabs = [tab];
}
} else {
tabs = await this.tabRepository.queryTabs(query, excludePinned);
tabs = await this.tabRepository.queryTabs(query, excludePinned, onlyCurrentWin);
}
const winSet = tabs.reduce( (wins, tab) => wins.add(tab.windowId), new Set<number>());
const multiWin = winSet.size > 1

return tabs.map((tab) => {
let flag = TabFlag.None;
Expand All @@ -43,7 +50,7 @@ export default class TabCompletionUseCase {
flag = TabFlag.LastTab;
}
return {
index: tab.index + 1,
index: tab.index + 1 + (multiWin ? parseFloat('0.' + tab.windowId + '1') : 0),
flag: flag,
title: tab.title,
url: tab.url,
Expand Down
5 changes: 3 additions & 2 deletions src/background/completion/TabRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ export type Tab = {
title: string;
url: string;
faviconUrl?: string;
windowId: number;
};

export default interface TabRepository {
queryTabs(query: string, excludePinned: boolean): Promise<Tab[]>;
queryTabs(query: string, excludePinned: boolean, onlyCurrentWin: boolean): Promise<Tab[]>;

getAllTabs(excludePinned: boolean): Promise<Tab[]>;
getAllTabs(excludePinned: boolean, onlyCurrentWin: boolean): Promise<Tab[]>;
}
20 changes: 15 additions & 5 deletions src/background/completion/impl/TabRepositoryImpl.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import TabRepository, { Tab } from "../TabRepository";

export default class TabRepositoryImpl implements TabRepository {
async queryTabs(query: string, excludePinned: boolean): Promise<Tab[]> {
const tabs = await browser.tabs.query({ currentWindow: true });
async queryTabs(query: string, excludePinned: boolean, onlyCurrentWin: boolean): Promise<Tab[]> {
const tabs = [
...await browser.tabs.query({ currentWindow: true }),
...(onlyCurrentWin ? [] : await browser.tabs.query({ currentWindow: false }))
]
return tabs
.filter((t) => {
return (
Expand All @@ -17,13 +20,19 @@ export default class TabRepositoryImpl implements TabRepository {
.map(TabRepositoryImpl.toEntity);
}

async getAllTabs(excludePinned: boolean): Promise<Tab[]> {
async getAllTabs(excludePinned: boolean, onlyCurrentWin: boolean): Promise<Tab[]> {
if (excludePinned) {
return (
await browser.tabs.query({ currentWindow: true, pinned: true })
[
...await browser.tabs.query({ currentWindow: true, pinned: true }),
...(onlyCurrentWin ? [] : await browser.tabs.query({ currentWindow: false, pinned: true }))
]
).map(TabRepositoryImpl.toEntity);
}
return (await browser.tabs.query({ currentWindow: true })).map(
return [
...await browser.tabs.query({ currentWindow: true }),
...(onlyCurrentWin ? [] : await browser.tabs.query({ currentWindow: false }))
].map(
TabRepositoryImpl.toEntity
);
}
Expand All @@ -36,6 +45,7 @@ export default class TabRepositoryImpl implements TabRepository {
title: tab.title!!,
faviconUrl: tab.favIconUrl,
index: tab.index,
windowId: tab.windowId,
};
}
}
5 changes: 3 additions & 2 deletions src/background/controllers/CompletionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ export default class CompletionController {

async queryTabs(
query: string,
excludePinned: boolean
excludePinned: boolean,
onlyCurrentWin: boolean
): Promise<ConsoleRequestTabsResponse> {
return this.tabCompletionUseCase.queryTabs(query, excludePinned);
return this.tabCompletionUseCase.queryTabs(query, excludePinned, onlyCurrentWin);
}

async getProperties(): Promise<ConsoleGetPropertiesResponse> {
Expand Down
3 changes: 2 additions & 1 deletion src/background/infrastructures/ContentMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export default class ContentMessageListener {
case messages.CONSOLE_REQUEST_TABS:
return this.completionController.queryTabs(
message.query,
message.excludePinned
message.excludePinned,
message.onlyCurrentWin
);
case messages.CONSOLE_GET_PROPERTIES:
return this.completionController.getProperties();
Expand Down
22 changes: 16 additions & 6 deletions src/background/presenters/TabPresenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export default interface TabPresenter {

getCurrent(): Promise<Tab>;

getAll(): Promise<Tab[]>;
getAll(onlyCurrentWin?: boolean): Promise<Tab[]>;

getLastSelectedId(): Promise<number | undefined>;

getByKeyword(keyword: string, excludePinned: boolean): Promise<Tab[]>;
getByKeyword(keyword: string, excludePinned: boolean, onlyCurrentWin?: boolean): Promise<Tab[]>;

select(tabId: number): Promise<void>;

Expand Down Expand Up @@ -56,8 +56,11 @@ export class TabPresenterImpl implements TabPresenter {
return tabs[0];
}

getAll(): Promise<Tab[]> {
return browser.tabs.query({ currentWindow: true });
getAll(onlyCurrentWin: boolean = true): Promise<Tab[]> {
return browser.tabs.query({ currentWindow: true })
.then( res =>
(onlyCurrentWin ? res : browser.tabs.query({ currentWindow: false })
.then( res2 => [...res, ...res2])))
}

async getLastSelectedId(): Promise<number | undefined> {
Expand All @@ -69,8 +72,11 @@ export class TabPresenterImpl implements TabPresenter {
return tabId;
}

async getByKeyword(keyword: string, excludePinned = false): Promise<Tab[]> {
const tabs = await browser.tabs.query({ currentWindow: true });
async getByKeyword(keyword: string, excludePinned = false, onlyCurrentWin: boolean = true): Promise<Tab[]> {
const tabs = [
...await browser.tabs.query({ currentWindow: true }),
...(onlyCurrentWin ? [] : await browser.tabs.query({ currentWindow: false }))
];
return tabs
.filter((t) => {
return (
Expand All @@ -85,6 +91,10 @@ export class TabPresenterImpl implements TabPresenter {

async select(tabId: number): Promise<void> {
await browser.tabs.update(tabId, { active: true });
const windowId = (await browser.tabs.get(tabId)).windowId;
if((await browser.windows.getCurrent()).id !== windowId) {
await browser.windows.update(windowId, {focused: true})
}
}

async remove(ids: number[]): Promise<void> {
Expand Down
4 changes: 4 additions & 0 deletions src/background/repositories/CachedSettingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export class CachedSettingRepositoryImpl implements CachedSettingRepository {
}
break;
}
case "searchOnlyCurrentWin":
current.properties.searchOnlyCurrentWin = newValue as boolean;
break;

}
await this.update(current);
}
Expand Down
6 changes: 4 additions & 2 deletions src/background/usecases/CommandUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ export default class CommandIndicator {
return;
}

const settings = await this.cachedSettingRepository.get();
const onlyCurrentWin = settings.properties.searchOnlyCurrentWin;
if (!isNaN(Number(keywords))) {
const tabs = await this.tabPresenter.getAll();
const tabs = await this.tabPresenter.getAll(onlyCurrentWin);
const index = parseInt(keywords, 10) - 1;
if (index < 0 || tabs.length <= index) {
throw new RangeError(`tab ${index + 1} does not exist`);
Expand All @@ -80,7 +82,7 @@ export default class CommandIndicator {
}

const current = await this.tabPresenter.getCurrent();
const tabs = await this.tabPresenter.getByKeyword(keywords, false);
const tabs = await this.tabPresenter.getByKeyword(keywords, false, onlyCurrentWin);
if (tabs.length === 0) {
throw new RangeError("No matching buffer for " + keywords);
}
Expand Down
6 changes: 4 additions & 2 deletions src/console/actions/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const propertyDocs: { [key: string]: string } = {
smoothscroll: "smooth scroll",
complete: "which are completed at the open page",
colorscheme: "color scheme of the console",
searchOnlyCurrentWin: "buffer switch and tab completion only in current browser window"
};

const hide = (): actions.ConsoleAction => {
Expand Down Expand Up @@ -194,9 +195,10 @@ const getTabCompletions = async (
original: string,
command: Command,
query: string,
excludePinned: boolean
excludePinned: boolean,
): Promise<actions.SetCompletionsAction> => {
const items = await completionClient.requestTabs(query, excludePinned);
const onlyCurrentWin = await settingClient.shouldSearchOnlyCurrentWin();
const items = await completionClient.requestTabs(query, excludePinned, onlyCurrentWin);
let completions: Completions = [];
if (items.length > 0) {
completions = [
Expand Down
3 changes: 2 additions & 1 deletion src/console/clients/CompletionClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ export default class CompletionClient {
return resp;
}

async requestTabs(query: string, excludePinned: boolean): Promise<TabItem[]> {
async requestTabs(query: string, excludePinned: boolean, onlyCurrentWin: boolean): Promise<TabItem[]> {
const resp = (await browser.runtime.sendMessage({
type: messages.CONSOLE_REQUEST_TABS,
query,
excludePinned,
onlyCurrentWin
})) as ConsoleRequestTabsResponse;
return resp;
}
Expand Down
14 changes: 12 additions & 2 deletions src/console/clients/SettingClient.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import Settings from "../../shared/settings/Settings";
import * as messages from "../../shared/messages";
import ColorScheme from "../../shared/ColorScheme";
import Properties from "../../shared/settings/Properties";

export default class SettingClient {
async getColorScheme(): Promise<ColorScheme> {

private async getProperties(): Promise<Properties> {
const json = await browser.runtime.sendMessage({
type: messages.SETTINGS_QUERY,
});
const settings = Settings.fromJSON(json);
return settings.properties.colorscheme;
return settings.properties;
}

async getColorScheme(): Promise<ColorScheme> {
return (await this.getProperties()).colorscheme;
}

async shouldSearchOnlyCurrentWin(): Promise<boolean> {
return (await this.getProperties()).searchOnlyCurrentWin;
}
}
1 change: 1 addition & 0 deletions src/shared/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export interface ConsoleRequestTabsMessage {
type: typeof CONSOLE_REQUEST_TABS;
query: string;
excludePinned: boolean;
onlyCurrentWin: boolean;
}

export interface ConsoleGetPropertiesMessage {
Expand Down
17 changes: 16 additions & 1 deletion src/shared/settings/Properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ export type PropertiesJSON = {
smoothscroll?: boolean;
complete?: string;
colorscheme?: ColorScheme;
searchOnlyCurrentWin?: boolean;
};

export type PropertyTypes = {
hintchars: string;
smoothscroll: string;
complete: string;
colorscheme: string;
searchOnlyCurrentWin: string;
};

type PropertyName = "hintchars" | "smoothscroll" | "complete" | "colorscheme";
type PropertyName = "hintchars" | "smoothscroll" | "complete" | "colorscheme" | "searchOnlyCurrentWin";

type PropertyDef = {
name: PropertyName;
Expand Down Expand Up @@ -43,13 +45,19 @@ const defs: PropertyDef[] = [
defaultValue: ColorScheme.System,
type: "string",
},
{
name: "searchOnlyCurrentWin",
defaultValue: true,
type: "boolean",
},
];

const defaultValues = {
hintchars: "abcdefghijklmnopqrstuvwxyz",
smoothscroll: false,
complete: "sbh",
colorscheme: ColorScheme.System,
searchOnlyCurrentWin: false,
};

export default class Properties {
Expand All @@ -61,21 +69,26 @@ export default class Properties {

public colorscheme: ColorScheme;

public searchOnlyCurrentWin: boolean;

constructor({
hintchars,
smoothscroll,
complete,
colorscheme,
searchOnlyCurrentWin,
}: {
hintchars?: string;
smoothscroll?: boolean;
complete?: string;
colorscheme?: ColorScheme;
searchOnlyCurrentWin?: boolean;
} = {}) {
this.hintchars = hintchars || defaultValues.hintchars;
this.smoothscroll = smoothscroll || defaultValues.smoothscroll;
this.complete = complete || defaultValues.complete;
this.colorscheme = colorscheme || defaultValues.colorscheme;
this.searchOnlyCurrentWin = searchOnlyCurrentWin || defaultValues.searchOnlyCurrentWin;
}

static fromJSON(json: PropertiesJSON): Properties {
Expand All @@ -88,6 +101,7 @@ export default class Properties {
smoothscroll: "boolean",
complete: "string",
colorscheme: "string",
searchOnlyCurrentWin: "boolean",
};
}

Expand All @@ -105,6 +119,7 @@ export default class Properties {
smoothscroll: this.smoothscroll,
complete: this.complete,
colorscheme: this.colorscheme,
searchOnlyCurrentWin: this.searchOnlyCurrentWin,
};
}
}
3 changes: 2 additions & 1 deletion src/shared/settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ export const DefaultSettingJSONText = `{
"hintchars": "abcdefghijklmnopqrstuvwxyz",
"smoothscroll": false,
"complete": "sbh",
"colorscheme": "system"
"colorscheme": "system",
"searchOnlyCurrentWin": false
},
"blacklist": [
]
Expand Down
3 changes: 3 additions & 0 deletions src/shared/settings/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
"colorscheme": {
"type": "string",
"enum": ["system", "light", "dark"]
},
"searchOnlyCurrentWin": {
"type": "boolean"
}
}
},
Expand Down
Loading