Skip to content

Commit

Permalink
add: Switch To feature in run mode
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhas committed Jan 7, 2019
1 parent c11f71f commit 61e3b47
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/ts/config-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface FeatureOptions {
fileBrowser: boolean;
homeFolder: boolean;
programs: boolean;
runModeSwitchTo: boolean;
spotify: boolean;
systemSettings: boolean;
ueliCommands: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/ts/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const defaultConfig: ConfigOptions = {
fileBrowser: true,
homeFolder: true,
programs: true,
runModeSwitchTo: true,
spotify: true,
systemSettings: true,
ueliCommands: true,
Expand Down
4 changes: 3 additions & 1 deletion src/ts/global-ueli.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ConfigOptions } from "./config-options";
import { ExternalOnlinePlugin, ExternalRunPlugin } from "./external-plugin";
import { WebSocketSearchResult } from "./music-player/music-player-websocket";
import { WebSocketSearchResult } from "./music-player/websocket";
import { Taskbar } from "taskbar-node";

export interface GlobalUELI {
config: ConfigOptions;
runPluginCollection: ExternalRunPlugin[];
onlinePluginCollection: ExternalOnlinePlugin[];
taskbar: Taskbar;
webSocketCommandSender: (command: string) => void;
webSocketSearch: (input: string) => Promise<WebSocketSearchResult[]>;
}
67 changes: 49 additions & 18 deletions src/ts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Injector } from "./injector";
import { InputValidationService } from "./input-validation-service";
import { InputValidatorSearcherCombinationManager } from "./input-validator-searcher-combination-manager";
import { IpcChannels } from "./ipc-channels";
import { WebSocketSearchResult } from "./music-player/music-player-websocket";
import { WebSocketSearchResult } from "./music-player/websocket";
import { OnlineInputValidationService } from "./online-input-validation-service";
import { OnlineInputValidatorSearcherCombinationManager } from "./online-input-validator-searcher-combination-manager";
import { ProcessInputValidationService } from "./process-input-validation-service";
Expand Down Expand Up @@ -44,12 +44,11 @@ const nativeUtil = new NativeUtil();

let config = new ConfigFileRepository(defaultConfig, UeliHelpers.configFilePath).getConfig();

let taskbar: Taskbar | undefined;

const globalUELI: GlobalUELI = {
config,
onlinePluginCollection: [],
runPluginCollection: [],
taskbar: new Taskbar(),
webSocketCommandSender: (url: string) => {
mainWindow.webContents.send(IpcChannels.websocketPlayURL, url);
},
Expand Down Expand Up @@ -123,13 +122,19 @@ function loadSearcher() {

runIVS = new InputValidationService(
new InputValidatorSearcherCombinationManager(globalUELI).getCombinations());

onlineIVS = new OnlineInputValidationService(
new OnlineInputValidatorSearcherCombinationManager(globalUELI).getCombinations());

processIVS = new ProcessInputValidationService(config.useNativeApplicationIcon);
processIVS.taskbar = globalUELI.taskbar;

everythingIVS = new EverythingInputValidationService(nativeUtil, config.maxTotalSearchResult, config.everythingFilterFilePath);

executionService = new ExecutionService(
new ExecutionArgumentValidatorExecutorCombinationManager(globalUELI).getCombinations(),
new CountManager(new CountFileRepository(UeliHelpers.countFilePath)));

isReady = true;
}

Expand Down Expand Up @@ -282,9 +287,9 @@ function showMainWindow(): void {
nativeUtil.takeScreenshot(screenshotSize.width, screenshotSize.height, screenshotFile);
}

mainWindow.webContents.send(IpcChannels.mainShow);
mainWindow.restore();
mainWindow.show();
mainWindow.webContents.send(IpcChannels.mainShow);
}

function updateWindowSize(searchResultCount: number): void {
Expand All @@ -305,8 +310,6 @@ function hideMainWindow(): void {
mainWindow.hide();
}, 100);
}

destructTaskbar();
}

function reloadApp(): void {
Expand All @@ -317,8 +320,6 @@ function reloadApp(): void {
runIVS.destruct();
onlineIVS.destruct();

destructTaskbar();

(async () => loadSearcher())();

mainWindow.reload();
Expand All @@ -335,7 +336,6 @@ function quitApp(): void {
executionService.destruct();
runIVS.destruct();
onlineIVS.destruct();
destructTaskbar();

mainWindow.webContents.session.clearCache(() => {/**/});
mainWindow.webContents.session.clearStorageData();
Expand All @@ -359,7 +359,13 @@ function getSearch(userInput: string): void {
);

runIVS.getSearchResult(userInput, currentWorkingDirectory)
.then(sendResult);
.then((results) => {
if (config.features.runModeSwitchTo) {
prependSwitchProcesses(results);
}

sendResult(results);
});

break;
}
Expand Down Expand Up @@ -392,10 +398,6 @@ function getSearch(userInput: string): void {
break;
}
case InputModes.WINDOWS: {
if (taskbar === undefined) {
taskbar = new Taskbar();
}
processIVS.taskbar = taskbar;
processIVS.getSearchResult(userInput)
.then(sendResult);
break;
Expand All @@ -416,10 +418,39 @@ function getSearch(userInput: string): void {
}
}

function destructTaskbar() {
if (taskbar !== undefined) {
taskbar.destruct();
taskbar = undefined;
function prependSwitchProcesses(results: SearchResultItem[]): void {
let index = 0;
const procs = globalUELI.taskbar.getAllApps();
const matchedProc = [] as Array<{ index: number; item: SearchResultItem }>;
for (const result of results) {
if (index > 4) {
break;
}

if (!result.target) {
index++;
continue;
}

for (const proc of procs) {
if (result.target !== proc.getProgramPath()) {
continue;
}

const item = {
breadCrumb: [proc.getWindowTitle() || proc.getProcessName()],
executionArgument: `HWND:${proc.getHWND()}`,
icon: result.icon,
name: `Switch to ${result.name}`,
};

matchedProc.unshift({ index, item });
}
index++;
}

for (const matched of matchedProc) {
results.splice(matched.index, 0, matched.item);
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/ts/process-input-validation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Taskbar } from "taskbar-node";
import { app, NativeImage } from "electron";

export class ProcessInputValidationService {
public taskbar: Taskbar | undefined;
public taskbar: Taskbar;
private searchEngine: SearchEngine;
private shouldFetchIcon: boolean;

Expand All @@ -19,10 +19,6 @@ export class ProcessInputValidationService {
const result = [] as SearchResultItem[];
userInput = StringHelpers.trimAndReplaceMultipleWhiteSpacesWithOne(userInput);

if (!this.taskbar) {
return result;
}

const allProcesses = this.taskbar.getAllApps();
for (const process of allProcesses) {
let icon: string | undefined;
Expand Down
32 changes: 18 additions & 14 deletions src/ts/search-plugins/programs-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,33 @@ export class ProgramsPlugin implements SearchPlugin {

let tags: string[] | undefined;
let icon: string | undefined;
let target: string | undefined;

if (detail.isLnk) {
const info = this.getShortcutInfo(file.fullPath);

if (info) {
tags = this.pathToTags(info.target);
target = info.target.replace(
/%([^%]+)%/g,
(original: string, varName: string): string => {
const varValue = process.env[varName];

if (varValue) {
return varValue;
}

return original;
},
);

tags = this.pathToTags(target);

if (this.shouldFetchIcon) {
if (info.icon && !info.icon.endsWith(".dll")) {
icon = await this.pathToIcon(info.icon);
}

if (!icon && info.target) {
icon = await this.pathToIcon(info.target);
if (!icon && target) {
icon = await this.pathToIcon(target);
}
}
}
Expand All @@ -68,6 +81,7 @@ export class ProgramsPlugin implements SearchPlugin {
icon: icon || Icons.PROGRAM,
name: detail.name,
tags,
target,
} as SearchResultItem);
}
}
Expand Down Expand Up @@ -134,16 +148,6 @@ export class ProgramsPlugin implements SearchPlugin {
return;
}

iconTarget = iconTarget.replace(/%([^%]+)%/g, (original: string, varName: string): string => {
const varValue = process.env[varName];

if (varValue) {
return varValue;
}

return original;
});

app.getFileIcon(iconTarget, (error, image: NativeImage) => {
if (error) {
resolve();
Expand Down
1 change: 1 addition & 0 deletions src/ts/search-result-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface SearchResultItem extends BareSearchResultItem {
executionArgument: string;
hideDescription?: boolean;
icon: string;
target?: string;
}

export interface SearchResultItemViewModel extends SearchResultItem {
Expand Down

0 comments on commit 61e3b47

Please sign in to comment.