Skip to content

Commit

Permalink
+ Shortcut #91
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyChen777 committed Jun 26, 2022
1 parent f6cf8ca commit ee8ea5f
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 40 deletions.
27 changes: 16 additions & 11 deletions packages/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {

Store.initRenderer();

const preference = new Store({});

// Disable GPU Acceleration for Windows 7
if (release().startsWith("6.1")) app.disableHardwareAcceleration();

Expand Down Expand Up @@ -72,7 +74,7 @@ async function createWindow() {
return { action: "deny" };
});

setMainMenu(win);
setMainMenu(win, preference);

win.on("blur", () => {
win?.webContents.send("window-lost-focus");
Expand Down Expand Up @@ -142,18 +144,21 @@ async function createWindow() {
winPlugin?.setSize(600, 48);
});

const ret = globalShortcut.register("CommandOrControl+Shift+I", () => {
win?.blur();
const ret = globalShortcut.register(
(preference.get("shortcutPlugin") as string) || "CommandOrControl+Shift+I",
() => {
win?.blur();

const { x, y } = screen.getCursorScreenPoint();
const currentDisplay = screen.getDisplayNearestPoint({ x, y });
const bounds = currentDisplay.bounds;
const centerx = bounds.x + (bounds.width - 600) / 2;
const centery = bounds.y + (bounds.height - 48) / 2;
winPlugin?.setPosition(centerx, centery);
const { x, y } = screen.getCursorScreenPoint();
const currentDisplay = screen.getDisplayNearestPoint({ x, y });
const bounds = currentDisplay.bounds;
const centerx = bounds.x + (bounds.width - 600) / 2;
const centery = bounds.y + (bounds.height - 48) / 2;
winPlugin?.setPosition(centerx, centery);

winPlugin?.show();
});
winPlugin?.show();
}
);

if (!ret) {
console.log("registration failed");
Expand Down
18 changes: 10 additions & 8 deletions packages/main/menu.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { app, BrowserWindow, Menu } from "electron";
import { app, BrowserWindow, ipcMain, Menu } from "electron";
import { autoUpdater } from "electron-updater";
import Store from "electron-store";

export function setMainMenu(mainWindow: BrowserWindow) {
export function setMainMenu(mainWindow: BrowserWindow, preference: Store) {
const isMac = process.platform === "darwin";

const template = [
Expand Down Expand Up @@ -55,15 +56,16 @@ export function setMainMenu(mainWindow: BrowserWindow) {
submenu: [
{
label: "Open",
accelerator: "Enter",
accelerator: preference.get("shortcutOpen") || "Enter",
click: () => {
mainWindow.webContents.send("shortcut-Enter");
},
},
{ type: "separator" },
{
label: "Copy Bibtex",
accelerator: isMac ? "cmd+shift+c" : "ctrl+shift+c",
accelerator:
preference.get("shortcutCopy") || "CommandOrControl+Shift+C",
click: () => {
mainWindow.webContents.send("shortcut-cmd-shift-c");
},
Expand All @@ -77,21 +79,21 @@ export function setMainMenu(mainWindow: BrowserWindow) {
submenu: [
{
label: "Scrape",
accelerator: isMac ? "cmd+r" : "ctrl+r",
accelerator: preference.get("shortcutScrape") || "CommandOrControl+R",
click: () => {
mainWindow.webContents.send("shortcut-cmd-r");
},
},
{
label: "Edit Metadata",
accelerator: isMac ? "cmd+e" : "ctrl+e",
accelerator: preference.get("shortcutEdit") || "CommandOrControl+E",
click: () => {
mainWindow.webContents.send("shortcut-cmd-e");
},
},
{
label: "Flag",
accelerator: isMac ? "cmd+f" : "ctrl+f",
accelerator: preference.get("shortcutFlag") || "CommandOrControl+F",
click: () => {
mainWindow.webContents.send("shortcut-cmd-f");
},
Expand Down Expand Up @@ -123,7 +125,7 @@ export function setMainMenu(mainWindow: BrowserWindow) {
submenu: [
{
label: "Preview",
accelerator: "Space",
accelerator: preference.get("shortcutPreview") || "Space",
click: () => {
mainWindow.webContents.send("shortcut-Space");
},
Expand Down
16 changes: 16 additions & 0 deletions packages/preload/utils/preference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ export interface PreferenceStore {

lastVersion: string;

shortcutPlugin: string;
shortcutPreview: string;
shortcutOpen: string;
shortcutCopy: string;
shortcutScrape: string;
shortcutEdit: string;
shortcutFlag: string;

[Key: string]: unknown;
}

Expand Down Expand Up @@ -92,6 +100,14 @@ const defaultPreferences: PreferenceStore = {
httpsproxy: "",

lastVersion: "",

shortcutPlugin: "CommandOrControl+Shift+I",
shortcutPreview: "Space",
shortcutOpen: "Enter",
shortcutCopy: "CommandOrControl+Shift+C",
shortcutScrape: "CommandOrControl+R",
shortcutEdit: "CommandOrControl+E",
shortcutFlag: "CommandOrControl+F",
};

export class Preference {
Expand Down
29 changes: 8 additions & 21 deletions packages/renderer/src/ui/main-view/main-view.vue
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,17 @@ function preventSpaceArrowScrollEvent(event: KeyboardEvent) {
event.code === "ArrowDown" ||
event.code === "ArrowUp"
) {
if (event.target instanceof HTMLInputElement) {
return true;
}
if (
!window.appInteractor.getState("viewState.isEditViewShown") &&
!window.appInteractor.getState("viewState.isPreferenceViewShown")
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLTextAreaElement
) {
return true;
}
if (event.target == document.body) {
event.preventDefault();
}
if (
event.code === "ArrowDown" &&
!window.appInteractor.getState("viewState.isEditViewShown") &&
!window.appInteractor.getState("viewState.isPreferenceViewShown")
) {
if (event.code === "ArrowDown") {
const currentIndex = selectedIndex.value[0] || 0;
const newIndex =
currentIndex + 1 >
Expand All @@ -260,11 +256,7 @@ function preventSpaceArrowScrollEvent(event: KeyboardEvent) {
);
}
if (
event.code === "ArrowUp" &&
!window.appInteractor.getState("viewState.isEditViewShown") &&
!window.appInteractor.getState("viewState.isPreferenceViewShown")
) {
if (event.code === "ArrowUp") {
const currentIndex = selectedIndex.value[0] || 0;
const newIndex = currentIndex - 1 < 0 ? 0 : currentIndex - 1;
window.appInteractor.setState(
Expand All @@ -273,12 +265,7 @@ function preventSpaceArrowScrollEvent(event: KeyboardEvent) {
);
}
if (
event.code === "Space" &&
selectedEntities.value.length >= 1 &&
!window.appInteractor.getState("viewState.isEditViewShown") &&
!window.appInteractor.getState("viewState.isPreferenceViewShown")
) {
if (event.code === "Space" && selectedEntities.value.length >= 1) {
previewSelectedEntities();
}
}
Expand Down
147 changes: 147 additions & 0 deletions packages/renderer/src/ui/preference-view/components/hotkey-options.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<script setup lang="ts">
import { ref } from "vue";
const props = defineProps({
title: {
type: String,
required: true,
},
choosedKey: {
type: String,
required: true,
},
});
// From a to z and 0 to 9
const baseKeys = ref({
none: "none",
A: "A",
B: "B",
C: "C",
D: "D",
E: "E",
F: "F",
G: "G",
H: "H",
I: "I",
J: "J",
K: "K",
L: "L",
M: "M",
N: "N",
O: "O",
P: "P",
Q: "Q",
R: "R",
S: "S",
T: "T",
U: "U",
V: "V",
W: "W",
X: "X",
Y: "Y",
Z: "Z",
0: "0",
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
Enter: "Enter",
});
const keyPart = props.choosedKey.split("+");
const key = ref(keyPart.pop() || "none");
let modifier1 = ref("none");
let modifier2 = ref("none");
if (keyPart.length > 1) {
modifier2.value = keyPart.pop() || "none";
}
if (keyPart.length > 0) {
modifier1.value = keyPart.pop() || "none";
}
const emit = defineEmits(["update"]);
</script>

<template>
<div class="flex justify-between">
<div class="flex flex-col my-auto">
<div class="text-xs font-semibold">{{ title }}</div>
</div>
<div class="flex space-x-2">
<div
class="flex bg-neutral-200 dark:bg-neutral-700 rounded-md w-28 h-6"
:class="modifier1 === 'none' ? 'opacity-50' : ''"
>
<select
id="countries"
class="bg-gray-50 border text-xxs border-gray-300 text-gray-900 text-sm rounded-md focus:ring-blue-500 focus:border-blue-500 block w-full dark:bg-neutral-700 dark:border-neutral-600 dark:placeholder-neutral-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
@change="
(e) => {
// @ts-ignore
modifier1 = e.target.value;
emit('update', `${modifier1}+${modifier2}+${key}`);
}
"
>
<option :selected="modifier1 === 'none'" value="none">none</option>
<option
:selected="modifier1 === 'CommandOrControl'"
value="CommandOrControl"
>
CMD / CTRL
</option>
</select>
</div>
<div
class="flex bg-neutral-200 dark:bg-neutral-700 rounded-md w-28 h-6"
:class="modifier2 === 'none' ? 'opacity-50' : ''"
>
<select
id="countries"
class="bg-gray-50 border text-xxs border-gray-300 text-gray-900 text-sm rounded-md focus:ring-blue-500 focus:border-blue-500 block w-full dark:bg-neutral-700 dark:border-neutral-600 dark:placeholder-neutral-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
@change="
(e) => {
// @ts-ignore
modifier2 = e.target.value;
emit('update', `${modifier1}+${modifier2}+${key}`);
}
"
>
<option :selected="modifier2 === 'none'" value="none">none</option>
<option :selected="modifier2 === 'Shift'" value="Shift">Shift</option>
</select>
</div>
<div
class="flex bg-neutral-200 dark:bg-neutral-700 rounded-md w-28 h-6"
:class="key === 'none' ? 'opacity-50' : ''"
>
<select
id="countries"
class="bg-gray-50 border text-xxs border-gray-300 text-gray-900 text-sm rounded-md focus:ring-blue-500 focus:border-blue-500 block w-full dark:bg-neutral-700 dark:border-neutral-600 dark:placeholder-neutral-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
@change="
(e) => {
// @ts-ignore
key = e.target.value;
emit('update', `${modifier1}+${modifier2}+${key}`);
}
"
>
<option
:value="v"
:selected="key === k"
v-for="[k, v] of Object.entries(baseKeys)"
>
{{ k }}
</option>
</select>
</div>
</div>
</div>
</template>
Loading

0 comments on commit ee8ea5f

Please sign in to comment.