🐛 #77, 修复偏好设置不生效问题

This commit is contained in:
muwoo
2021-12-28 14:20:18 +08:00
parent f94c52f490
commit 56faae0e35
15 changed files with 687 additions and 363 deletions

View File

@@ -0,0 +1,23 @@
import commonConst from "@/common/utils/commonConst";
export default {
version: 2,
perf: {
shortCut: {
showAndHidden: "Option+R",
separate: "Ctrl+D",
quit: "Shift+Escape",
},
common: {
start: true,
space: true,
// 是否失焦隐藏。默认在dev环境不隐藏在打包后隐藏。
hideOnBlur: commonConst.production(),
autoPast: false,
},
local: {
search: true,
},
},
global: [],
}

View File

@@ -2,31 +2,10 @@ import path from "path";
import fs from "fs";
import getLocalDataFile from "./getLocalDataFile";
import commonConst from "./commonConst";
import defaultConfigForAnyPlatform from "../constans/defaultConfig";
const configPath = path.join(getLocalDataFile(), "./rubick-config.json");
const defaultConfigForAnyPlatform = {
version: 2,
perf: {
shortCut: {
showAndHidden: "Option+R",
separate: "Ctrl+D",
quit: "Shift+Escape",
},
common: {
start: true,
space: true,
// 是否失焦隐藏。默认在dev环境不隐藏在打包后隐藏。
hideOnBlur: commonConst.production(),
autoPast: false,
},
local: {
search: true,
},
},
global: [],
};
global.OP_CONFIG = {
config: null,
get() {
@@ -54,8 +33,11 @@ global.OP_CONFIG = {
return global.config;
}
},
set(key, value) {
global.config[key] = value;
set(value) {
global.config = {
...global.config,
...value,
};
fs.writeFileSync(configPath, JSON.stringify(global.config));
},
};

View File

@@ -39,14 +39,6 @@ export default () => {
const url = req.url.substr(8);
callback(decodeURI(url));
});
win.once("ready-to-show", () => {
win.show();
// 非隐藏式启动需要显示主窗口
if (!app.getLoginItemSettings().wasOpenedAsHidden) {
win.show();
}
});
win.on("closed", () => {
win = undefined;
});

View File

@@ -1,45 +1,62 @@
import { globalShortcut, BrowserWindow, screen } from "electron";
import { globalShortcut, BrowserWindow, screen, ipcMain, app} from "electron";
export default (mainWindow: BrowserWindow): void => {
const config = global.OP_CONFIG.get();
globalShortcut.unregisterAll();
// 注册偏好快捷键
globalShortcut.register(config.perf.shortCut.showAndHidden, () => {
const { x, y } = screen.getCursorScreenPoint();
const currentDisplay = screen.getDisplayNearestPoint({ x, y });
const wx = parseInt(
String(
currentDisplay.workArea.x + currentDisplay.workArea.width / 2 - 400
)
);
const wy = parseInt(
String(
currentDisplay.workArea.y + currentDisplay.workArea.height / 2 - 200
)
);
mainWindow.setAlwaysOnTop(true);
mainWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
mainWindow.focus();
mainWindow.setVisibleOnAllWorkspaces(false, { visibleOnFullScreen: true });
mainWindow.setPosition(wx, wy);
mainWindow.show();
});
globalShortcut.register(config.perf.shortCut.separate, () => {
// todo
});
globalShortcut.register(config.perf.shortCut.quit, () => {
// mainWindow.webContents.send('init-rubick');
// mainWindow.show();
});
// 注册自定义全局快捷键
config.global.forEach((sc) => {
if (!sc.key || !sc.value) return;
globalShortcut.register(sc.key, () => {
mainWindow.webContents.send("global-short-key", sc.value);
const registerHotKey = (mainWindow: BrowserWindow): void => {
// 设置开机启动
const setAutoLogin = () => {
const config = global.OP_CONFIG.get();
app.setLoginItemSettings({
openAtLogin: config.perf.common.start,
openAsHidden: true,
});
}
const init = () => {
setAutoLogin();
const config = global.OP_CONFIG.get();
globalShortcut.unregisterAll();
// 注册偏好快捷键
globalShortcut.register(config.perf.shortCut.showAndHidden, () => {
const { x, y } = screen.getCursorScreenPoint();
const currentDisplay = screen.getDisplayNearestPoint({ x, y });
const wx = parseInt(
String(
currentDisplay.workArea.x + currentDisplay.workArea.width / 2 - 400
)
);
const wy = parseInt(
String(
currentDisplay.workArea.y + currentDisplay.workArea.height / 2 - 200
)
);
mainWindow.setAlwaysOnTop(true);
mainWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
mainWindow.focus();
mainWindow.setVisibleOnAllWorkspaces(false, { visibleOnFullScreen: true });
mainWindow.setPosition(wx, wy);
mainWindow.show();
});
globalShortcut.register(config.perf.shortCut.separate, () => {
// todo
});
globalShortcut.register(config.perf.shortCut.quit, () => {
// mainWindow.webContents.send('init-rubick');
// mainWindow.show();
});
// 注册自定义全局快捷键
config.global.forEach((sc) => {
if (!sc.key || !sc.value) return;
globalShortcut.register(sc.key, () => {
mainWindow.webContents.send("global-short-key", sc.value);
});
});
}
init();
ipcMain.on("re-register", () => {
init();
});
};
export default registerHotKey;

View File

@@ -169,7 +169,7 @@ const showSeparate = () => {
const changeHideOnBlur = () => {
let cfg = { ...config.value };
cfg.perf.common.hideOnBlur = !cfg.perf.common.hideOnBlur;
opConfig.set("perf", cfg.perf);
opConfig.set(cfg.perf);
config.value = cfg;
};

View File

@@ -1,6 +1,6 @@
import { ref, watch } from "vue";
import throttle from "lodash.throttle";
import { remote } from "electron";
import { remote, ipcRenderer } from "electron";
import pluginClickEvent from "./pluginClickEvent";
import useFocus from "./clipboardWatch";
@@ -10,12 +10,12 @@ function formatReg(regStr) {
return new RegExp(pattern, flags);
}
function searchKeyValues(lists, value) {
function searchKeyValues(lists, value, strict = false) {
return lists.filter((item) => {
if (typeof item === "string") {
return item.toLowerCase().indexOf(value.toLowerCase()) >= 0;
}
if (item.type === "regex") {
if (item.type === "regex" && !strict) {
return formatReg(item.match).test(value);
}
return false;
@@ -30,15 +30,13 @@ const optionsManager = ({
}) => {
const optionsRef = ref([]);
watch(searchValue, () => search(searchValue.value));
// search Input operation
const search = throttle((value) => {
if (currentPlugin.value.name) return;
if (clipboardFile.value.length) return;
if (!value) {
optionsRef.value = [];
return;
}
// 全局快捷键
ipcRenderer.on("global-short-key", (e, msg) => {
const options = getOptionsFromSearchValue(msg, true);
options[0].click();
});
const getOptionsFromSearchValue = (value, strict = false) => {
const localPlugins = remote.getGlobal("LOCAL_PLUGINS").getLocalPlugins();
let options: any = [];
// todo 先搜索 plugin
@@ -47,7 +45,7 @@ const optionsManager = ({
// 系统插件无 features 的情况,不需要再搜索
if (!feature) return;
feature.forEach((fe) => {
const cmds = searchKeyValues(fe.cmds, value);
const cmds = searchKeyValues(fe.cmds, value, strict);
options = [
...options,
...cmds.map((cmd) => ({
@@ -63,10 +61,10 @@ const optionsManager = ({
cmd,
ext: cmd.type
? {
code: fe.code,
type: cmd.type || "text",
payload: searchValue.value,
}
code: fe.code,
type: cmd.type || "text",
payload: searchValue.value,
}
: null,
openPlugin,
});
@@ -109,7 +107,19 @@ const optionsManager = ({
return plugin;
}),
];
optionsRef.value = options;
return options;
}
watch(searchValue, () => search(searchValue.value));
// search Input operation
const search = throttle((value) => {
if (currentPlugin.value.name) return;
if (clipboardFile.value.length) return;
if (!value) {
optionsRef.value = [];
return;
}
optionsRef.value = getOptionsFromSearchValue(value);
}, 500);
const setOptionsRef = (options) => {