mirror of
https://github.com/rubickCenter/rubick
synced 2025-10-26 22:51:25 +08:00
Compare commits
9 Commits
v0.0.3-bet
...
v0.0.3-bet
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
614d5ae369 | ||
|
|
f287d10ca9 | ||
|
|
a04efd0d4f | ||
|
|
153d9dc5d7 | ||
|
|
de138955b5 | ||
|
|
8d340cb76c | ||
|
|
579b283a55 | ||
|
|
3858f35559 | ||
|
|
57504ab1eb |
@@ -36,13 +36,8 @@ module.exports = {
|
||||
path: '/blogs/plugin/',
|
||||
},
|
||||
{
|
||||
title: '右击增强实现原理'
|
||||
},
|
||||
{
|
||||
title: '系统插件实现原理'
|
||||
},
|
||||
{
|
||||
title: '文件检索实现原理'
|
||||
title: '超级面板实现原理',
|
||||
path: '/blogs/superPannel/',
|
||||
},
|
||||
]
|
||||
},
|
||||
|
||||
175
docs/docs/blogs/superPannel/README.md
Normal file
175
docs/docs/blogs/superPannel/README.md
Normal file
@@ -0,0 +1,175 @@
|
||||
## 前言
|
||||
超级面板用于增强用户右击能力,实现快速呼起插件的能力,本次实现方式是通过 `robotjs` 以及 `iohook` 一起来完成
|
||||
|
||||
### 功能截图:
|
||||
|
||||
#### 文件夹下长按右建
|
||||
<img src=https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/9b3d6ea7a25b4b908acc471471628979~tplv-k3u1fbpfcp-watermark.image width=300 />
|
||||
|
||||
#### 选择文件后长按右键
|
||||
<img src=https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/8e220166cb464328b432f42d14ba9ab2~tplv-k3u1fbpfcp-watermark.image width=300 />
|
||||
|
||||
#### 选择文字后长按右键
|
||||
<img src=https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f9f909d08a8441e8a506831796908ef9~tplv-k3u1fbpfcp-watermark.image width=300 />
|
||||
|
||||
## 实现原理
|
||||
|
||||
### 获取选中文案
|
||||
要实现改功能核心是要读取当前用户选中的文案或者文件,根据当前选择内容进行不同功能展示。但是核心有一个问题是如何来实现获取当前选中的内容。这个问题思考了很久很久,要想获取选中的文案,感觉唯一的办法是使用 `ctrl + c` 或者 `command + c` 来先复制到剪切板,再通过 `electron clipboard` 来获取当前剪切板内容。但是 `utools` 可不是通过先复制再长按这样的操作来实现的,而是直接选中文本或者文件长按后呼起超级面板。**所以一定要在右击长按前获取到当前选中的内容。**
|
||||
|
||||
如果要这么干,可能真的无解了,之前就因为这么想,才被无解了。正确的思路应该是先长按再获取选中的内容。别看只是掉了个个,但实现确实天壤之别:
|
||||
|
||||
1. 先获取选中内容:这就要求我们必须监听原生系统选中事件,但是 `electron` 并没有提供能力,我们也无法监听系统选择事件。
|
||||
2. 先右击,后获取内容,这样的好处在于先右击可以通过监听鼠标右击事件,相比选择事件更加容易。
|
||||
|
||||
所以思路就有了,先监听长按右击事件:
|
||||
|
||||
```js
|
||||
// macos
|
||||
const mouseEvents = require("osx-mouse");
|
||||
|
||||
const mouseTrack = mouseEvents();
|
||||
// 按下去的 time
|
||||
let down_time = 0;
|
||||
|
||||
// 是否弹起
|
||||
let isPress = false;
|
||||
|
||||
// 监听右击
|
||||
mouseTrack.on('right-down', () => {
|
||||
isPress = true;
|
||||
down_time = Date.now();
|
||||
// 长按 500ms 后触发
|
||||
setTimeout(async () => {
|
||||
if (isPress) {
|
||||
// 获取选中内容
|
||||
const copyResult = await getSelectedText();
|
||||
}, 500);
|
||||
})
|
||||
mouseTrack.on('right-up', () => {
|
||||
isPress = false;
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
接下来一步就是要去实现获取选中内容,要获取选中内容有个比较骚的操作,就是:
|
||||
|
||||
1. 通过 `clipboard` 先获取当前剪切板内容,并存下 A
|
||||
2. 通过 `robot.js` 来调用系统 `command + c` 或者 `ctrl + c`
|
||||
3. 再通过 `clipboard` 先获取当前剪切板内容,并存下 B
|
||||
4. 再将 A 写到剪切板中,返回 B
|
||||
|
||||
先存剪切板内容的目的在于我们是偷偷帮用户执行了复制动作,当读取完用户选择内容后,需要回复用户之前的剪切板内容。接下来看一下简单的实现:
|
||||
|
||||
```js
|
||||
const getSelected = () => {
|
||||
return new Promise((resolve) => {
|
||||
// 缓存之前的文案
|
||||
const lastText = clipboard.readText('clipboard');
|
||||
|
||||
const platform = process.platform;
|
||||
|
||||
// 执行复制动作
|
||||
if (platform === 'darwin') {
|
||||
robot.keyTap('c', 'command');
|
||||
} else {
|
||||
robot.keyTap('c', 'control');
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
// 读取剪切板内容
|
||||
const text = clipboard.readText('clipboard') || ''
|
||||
const fileUrl = clipboard.read('public.file-url');
|
||||
|
||||
// 恢复剪切板内容
|
||||
clipboard.writeText(lastText);
|
||||
|
||||
resolve({
|
||||
text,
|
||||
fileUrl
|
||||
})
|
||||
}, 300);
|
||||
})
|
||||
}
|
||||
```
|
||||
### 通知超级面板窗口当前选中内容
|
||||
当获取到了选中内容后,接下来就是需要创建超级面板的 `BrowserWindow`:
|
||||
```js
|
||||
const { BrowserWindow, ipcMain, app } = require("electron");
|
||||
|
||||
module.exports = () => {
|
||||
let win;
|
||||
|
||||
let init = (mainWindow) => {
|
||||
if (win === null || win === undefined) {
|
||||
createWindow();
|
||||
}
|
||||
};
|
||||
|
||||
let createWindow = () => {
|
||||
win = new BrowserWindow({
|
||||
frame: false,
|
||||
autoHideMenuBar: true,
|
||||
width: 250,
|
||||
height: 50,
|
||||
show: false,
|
||||
alwaysOnTop: true,
|
||||
webPreferences: {
|
||||
webSecurity: false,
|
||||
enableRemoteModule: true,
|
||||
backgroundThrottling: false,
|
||||
nodeIntegration: true,
|
||||
devTools: false,
|
||||
},
|
||||
});
|
||||
win.loadURL(`file://${__static}/plugins/superPanel/index.html`);
|
||||
win.once('ready-to-show', () => win.show());
|
||||
win.on("closed", () => {
|
||||
win = undefined;
|
||||
});
|
||||
};
|
||||
|
||||
let getWindow = () => win;
|
||||
|
||||
return {
|
||||
init: init,
|
||||
getWindow: getWindow,
|
||||
};
|
||||
};
|
||||
```
|
||||
然后再通知 `superPanel` 进行内容展示:
|
||||
```js
|
||||
win.webContents.send('trigger-super-panel', {
|
||||
...copyResult,
|
||||
optionPlugin: optionPlugin.plugins,
|
||||
});
|
||||
```
|
||||
|
||||
### 超级面板点击操作
|
||||
接下来要实现超级面板点击操作,这块也是比较简单的了,直接上代码好了:
|
||||
#### 1. 打开 Terminal
|
||||
|
||||
```js
|
||||
const { spawn } = require ('child_process');
|
||||
|
||||
spawn('open', [ '-a', 'Terminal', fileUrl ]);
|
||||
```
|
||||
|
||||
#### 2. 新建文件
|
||||
|
||||
```js
|
||||
remote.dialog.showSaveDialog({
|
||||
title: "请选择要保存的文件名",
|
||||
buttonLabel: "保存",
|
||||
defaultPath: fileUrl.replace('file://', ''),
|
||||
showsTagField: false,
|
||||
nameFieldLabel: '',
|
||||
}).then(result => {
|
||||
fs.writeFileSync(result.filePath, '');
|
||||
});
|
||||
```
|
||||
|
||||
#### 3. 复制路径
|
||||
```js
|
||||
clipboard.writeText(fileUrl.replace('file://', ''))
|
||||
```
|
||||
13
package.json
13
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "rubick2",
|
||||
"version": "0.0.3-beta.6",
|
||||
"version": "0.0.3-beta.12",
|
||||
"author": "muwoo <2424880409@qq.com>",
|
||||
"description": "An electron-vue project",
|
||||
"license": null,
|
||||
@@ -11,7 +11,7 @@
|
||||
"dev": "node .electron-vue/dev-runner.js",
|
||||
"rebuild": " ./node_modules/.bin/electron-rebuild",
|
||||
"rebuild_win": "npm rebuild --runtime=electron --target=11.4.10 --disturl=https://atom.io/download/atom-shell --abi=85",
|
||||
"rebuild_linux": "npm rebuild --runtime=electron --target=12.0.15 --disturl=https://atom.io/download/atom-shell --abi=87"
|
||||
"rebuild_linux": "npm rebuild --runtime=electron --target=11.4.10 --disturl=https://atom.io/download/atom-shell --abi=85"
|
||||
},
|
||||
"build": {
|
||||
"asar": true,
|
||||
@@ -56,7 +56,9 @@
|
||||
},
|
||||
"linux": {
|
||||
"icon": "build/icons/",
|
||||
"publish": ["github"]
|
||||
"publish": [
|
||||
"github"
|
||||
]
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
@@ -67,6 +69,7 @@
|
||||
"download-git-repo": "^3.0.2",
|
||||
"electron-is-dev": "^2.0.0",
|
||||
"electron-store": "^8.0.0",
|
||||
"icon-extractor": "^1.0.3",
|
||||
"iohook": "^0.9.3",
|
||||
"is-chinese": "^1.4.2",
|
||||
"jian-pinyin": "^0.2.3",
|
||||
@@ -109,7 +112,7 @@
|
||||
"css-loader": "^0.28.11",
|
||||
"del": "^3.0.0",
|
||||
"devtron": "^1.4.0",
|
||||
"electron": "^12.0.15",
|
||||
"electron": "^11.4.10",
|
||||
"electron-builder": "22.10.5",
|
||||
"electron-debug": "^1.5.0",
|
||||
"electron-devtools-installer": "^2.2.4",
|
||||
@@ -140,7 +143,7 @@
|
||||
"iohook": {
|
||||
"targets": [
|
||||
"node-83",
|
||||
"electron-87"
|
||||
"electron-85"
|
||||
],
|
||||
"platforms": [
|
||||
"darwin",
|
||||
|
||||
@@ -14,6 +14,7 @@ module.exports = () => {
|
||||
win = new BrowserWindow({
|
||||
height: 60,
|
||||
useContentSize: true,
|
||||
resizable: false,
|
||||
width: 800,
|
||||
frame: false,
|
||||
title: '拉比克',
|
||||
|
||||
@@ -7,7 +7,7 @@ const configPath = path.join(getlocalDataFile(), './rubick-config.json')
|
||||
|
||||
let defaultConfig = {
|
||||
Darwin: {
|
||||
version: 2,
|
||||
version: 3,
|
||||
perf: {
|
||||
shortCut: {
|
||||
showAndHidden: 'Option+R',
|
||||
@@ -32,7 +32,7 @@ let defaultConfig = {
|
||||
global: []
|
||||
},
|
||||
Windows_NT: {
|
||||
version: 2,
|
||||
version: 3,
|
||||
perf: {
|
||||
shortCut: {
|
||||
showAndHidden: 'Option+R',
|
||||
@@ -57,7 +57,7 @@ let defaultConfig = {
|
||||
global: []
|
||||
},
|
||||
Linux: {
|
||||
version: 1,
|
||||
version: 3,
|
||||
perf: {
|
||||
shortCut: {
|
||||
showAndHidden: 'Option+R',
|
||||
@@ -85,13 +85,13 @@ let defaultConfig = {
|
||||
global.opConfig = {
|
||||
config: null,
|
||||
get() {
|
||||
const platform = os.type()
|
||||
const platform = os.type();
|
||||
try {
|
||||
if (!opConfig.config) {
|
||||
opConfig.config = JSON.parse(fs.readFileSync(configPath) || JSON.stringify(defaultConfig[platform]))
|
||||
}
|
||||
// 重置
|
||||
if (!opConfig.version || opConfig.version < defaultConfig[platform].version) {
|
||||
if (!opConfig.config.version || opConfig.config.version < defaultConfig[platform].version) {
|
||||
opConfig.config = defaultConfig[platform]
|
||||
fs.writeFileSync(configPath, JSON.stringify(opConfig.config))
|
||||
}
|
||||
@@ -102,7 +102,7 @@ global.opConfig = {
|
||||
}
|
||||
},
|
||||
set(key, value) {
|
||||
opConfig.config[key] = value
|
||||
opConfig.config[key] = value;
|
||||
fs.writeFileSync(configPath, JSON.stringify(opConfig.config))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,6 +194,7 @@ class Listener {
|
||||
}
|
||||
|
||||
initTouchBar(mainWindow) {
|
||||
if (!commonConst.macOS()) return;
|
||||
const { TouchBarButton, TouchBarGroup, TouchBarPopover } = TouchBar;
|
||||
let items = [];
|
||||
let system = [];
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
"
|
||||
class="main-input"
|
||||
@change="(e) => search({ value: e.target.value })"
|
||||
@keydown.ctrl.86="shouldPaste"
|
||||
:value="searchValue"
|
||||
:maxLength="selected && selected.key !== 'plugin-container' ? 0 : 1000"
|
||||
@keydown.down="(e) => changeCurrent(1)"
|
||||
@@ -113,7 +114,7 @@
|
||||
</template>
|
||||
<script>
|
||||
import { mapActions, mapMutations, mapState } from "vuex";
|
||||
import { ipcRenderer, remote } from "electron";
|
||||
import { ipcRenderer, remote, clipboard } from "electron";
|
||||
import {
|
||||
getWindowHeight,
|
||||
debounce,
|
||||
@@ -208,6 +209,18 @@ export default {
|
||||
methods: {
|
||||
...mapActions("main", ["onSearch", "showMainUI", "openPlugin"]),
|
||||
...mapMutations("main", ["commonUpdate"]),
|
||||
shouldPaste(e) {
|
||||
let filePath = '';
|
||||
if (process.platform === 'win32') {
|
||||
const rawFilePath = clipboard.read('FileNameW');
|
||||
filePath = rawFilePath.replace(new RegExp(String.fromCharCode(0), 'g'), '');
|
||||
if (filePath.indexOf('plugin.json') >= 0) {
|
||||
this.search({
|
||||
filePath,
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
search(v) {
|
||||
if (!this.searchFn) {
|
||||
this.searchFn = debounce(this.onSearch, 200);
|
||||
|
||||
120
src/renderer/assets/common/darwin-app.js
Normal file
120
src/renderer/assets/common/darwin-app.js
Normal file
@@ -0,0 +1,120 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import {nativeImage} from "electron";
|
||||
import translate from "./translate";
|
||||
import {APP_FINDER_PATH} from "./constans";
|
||||
import iconvLite from "iconv-lite";
|
||||
import bpList from "bplist-parser";
|
||||
|
||||
const fileLists = [];
|
||||
|
||||
const isZhRegex = /[\u4e00-\u9fa5]/;
|
||||
const getDisplayNameRegex = /\"(?:CFBundleDisplayName)\"\s\=\s\"(.*)\"/;
|
||||
|
||||
async function getAppZhName(rootPath, appName) {
|
||||
try {
|
||||
const ERROR_RESULT = '';
|
||||
const systemPath = path.join(rootPath, `${appName}/Contents/Resources/zh_CN.lproj/InfoPlist.strings`);
|
||||
const customizePath = path.join(rootPath, `${appName}/Contents/Resources/zh-Hans.lproj/InfoPlist.strings`);
|
||||
let appInfoPath = '';
|
||||
|
||||
if (fs.existsSync(systemPath)) {
|
||||
appInfoPath = systemPath;
|
||||
} else if (fs.existsSync(customizePath)) {
|
||||
appInfoPath = customizePath;
|
||||
} else {
|
||||
return ERROR_RESULT;
|
||||
}
|
||||
let appZhName = '';
|
||||
if (rootPath == '/Applications') {
|
||||
const container = iconvLite.decode(fs.readFileSync(appInfoPath), 'utf-16');
|
||||
if (container) {
|
||||
const res = container.match(getDisplayNameRegex);
|
||||
appZhName = res && res[1];
|
||||
} else {
|
||||
return ERROR_RESULT;
|
||||
}
|
||||
} else {
|
||||
const [{ CFBundleDisplayName = '', CFBundleName = '' }] = await bpList.parseFile(appInfoPath);
|
||||
appZhName = CFBundleDisplayName || CFBundleName;
|
||||
}
|
||||
|
||||
return appZhName;
|
||||
} catch (error) {
|
||||
return ERROR_RESULT;
|
||||
}
|
||||
}
|
||||
|
||||
function getDarwinAppList () {
|
||||
APP_FINDER_PATH.forEach((searchPath, index) => {
|
||||
fs.readdir(searchPath, async (err, files) => {
|
||||
try {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const appName = files[i];
|
||||
const extname = path.extname(appName);
|
||||
const appSubStr = appName.split(extname)[0];
|
||||
if ((extname === '.app' || extname === '.prefPane') >= 0) {
|
||||
try {
|
||||
const path1 = path.join(searchPath, `${appName}/Contents/Resources/App.icns`);
|
||||
const path2 = path.join(searchPath, `${appName}/Contents/Resources/AppIcon.icns`);
|
||||
const path3 = path.join(searchPath, `${appName}/Contents/Resources/${appSubStr}.icns`);
|
||||
const path4 = path.join(searchPath, `${appName}/Contents/Resources/${appSubStr.replace(' ', '')}.icns`);
|
||||
let iconPath = path1;
|
||||
if (fs.existsSync(path1)) {
|
||||
iconPath = path1;
|
||||
} else if (fs.existsSync(path2)) {
|
||||
iconPath = path2;
|
||||
} else if (fs.existsSync(path3)) {
|
||||
iconPath = path3;
|
||||
} else if (fs.existsSync(path4)) {
|
||||
iconPath = path4;
|
||||
} else {
|
||||
// 性能最低的方式
|
||||
const resourceList = fs.readdirSync(path.join(searchPath, `${appName}/Contents/Resources`));
|
||||
const iconName = resourceList.filter((file) => path.extname(file) === '.icns')[0];
|
||||
iconPath = path.join(searchPath, `${appName}/Contents/Resources/${iconName}`);
|
||||
}
|
||||
const img = await nativeImage.createThumbnailFromPath(iconPath, { width: 64, height: 64 });
|
||||
|
||||
const appZhName = await getAppZhName(searchPath, appName);
|
||||
|
||||
const fileOptions = {
|
||||
value: 'plugin',
|
||||
icon: img.toDataURL(),
|
||||
desc: path.join(searchPath, appName),
|
||||
type: 'app',
|
||||
action: `open ${path.join(searchPath, appName).replace(' ', '\\ ')}`,
|
||||
keyWords: [appSubStr]
|
||||
};
|
||||
|
||||
if (appZhName && isZhRegex.test(appZhName)) {
|
||||
const py = translate(appZhName);
|
||||
const pinyinArr = py.split(',');
|
||||
const firstLatter = pinyinArr.map(py => py[0]);
|
||||
// 拼音
|
||||
fileOptions.keyWords.push(pinyinArr.join(''));
|
||||
// 缩写
|
||||
fileOptions.keyWords.push(firstLatter.join(''));
|
||||
// 中文
|
||||
fileOptions.keyWords.push(appZhName);
|
||||
}
|
||||
|
||||
fileLists.push({
|
||||
...fileOptions,
|
||||
name: appSubStr,
|
||||
names: JSON.parse(JSON.stringify(fileOptions.keyWords)),
|
||||
});
|
||||
} catch (e) {}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export const getApp = {
|
||||
init: getDarwinAppList,
|
||||
fileLists,
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
import { WINDOW_MAX_HEIGHT, WINDOW_MIN_HEIGHT, PRE_ITEM_HEIGHT, SYSTEM_PLUGINS } from './constans';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import process from 'child_process';
|
||||
import child_process from 'child_process';
|
||||
import Store from 'electron-store';
|
||||
import downloadFile from 'download';
|
||||
import { nativeImage, ipcRenderer } from 'electron';
|
||||
import { APP_FINDER_PATH } from './constans';
|
||||
import { ipcRenderer } from 'electron';
|
||||
import { getlocalDataFile } from '../../../main/common/utils';
|
||||
import iconvLite from 'iconv-lite';
|
||||
import bpList from 'bplist-parser';
|
||||
|
||||
import translate from './translate'
|
||||
const getApp = process.platform === 'win32' ? require('./win-app').getApp : require('./darwin-app').getApp;
|
||||
|
||||
const store = new Store();
|
||||
|
||||
getApp.init();
|
||||
const fileLists = getApp.fileLists;
|
||||
|
||||
function getWindowHeight(searchList) {
|
||||
if (!searchList) return WINDOW_MAX_HEIGHT;
|
||||
if (!searchList.length) return WINDOW_MIN_HEIGHT;
|
||||
@@ -50,7 +50,7 @@ async function downloadZip(downloadRepoUrl, name) {
|
||||
const temp_dest = `${plugin_path}/${name}`;
|
||||
// 下载模板
|
||||
if (await existOrNot(temp_dest)) {
|
||||
await process.execSync(`rm -rf ${temp_dest}`);
|
||||
await child_process.execSync(`rm -rf ${temp_dest}`);
|
||||
}
|
||||
|
||||
await downloadFile(downloadRepoUrl, plugin_path, { extract: true });
|
||||
@@ -158,110 +158,6 @@ function find(p, target = 'plugin.json') {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
const fileLists = [];
|
||||
// 默认搜索目录
|
||||
const isZhRegex = /[\u4e00-\u9fa5]/;
|
||||
const getDisplayNameRegex = /\"(?:CFBundleDisplayName)\"\s\=\s\"(.*)\"/;
|
||||
|
||||
async function getAppZhName(rootPath, appName) {
|
||||
try {
|
||||
const ERROR_RESULT = '';
|
||||
const systemPath = path.join(rootPath, `${appName}/Contents/Resources/zh_CN.lproj/InfoPlist.strings`);
|
||||
const customizePath = path.join(rootPath, `${appName}/Contents/Resources/zh-Hans.lproj/InfoPlist.strings`);
|
||||
let appInfoPath = '';
|
||||
|
||||
if (fs.existsSync(systemPath)) {
|
||||
appInfoPath = systemPath;
|
||||
} else if (fs.existsSync(customizePath)) {
|
||||
appInfoPath = customizePath;
|
||||
} else {
|
||||
return ERROR_RESULT;
|
||||
}
|
||||
let appZhName = '';
|
||||
if (rootPath == '/Applications') {
|
||||
const container = iconvLite.decode(fs.readFileSync(appInfoPath), 'utf-16');
|
||||
if (container) {
|
||||
const res = container.match(getDisplayNameRegex);
|
||||
appZhName = res && res[1];
|
||||
} else {
|
||||
return ERROR_RESULT;
|
||||
}
|
||||
} else {
|
||||
const [{ CFBundleDisplayName = '', CFBundleName = '' }] = await bpList.parseFile(appInfoPath);
|
||||
appZhName = CFBundleDisplayName || CFBundleName;
|
||||
}
|
||||
|
||||
return appZhName;
|
||||
} catch (error) {
|
||||
return ERROR_RESULT;
|
||||
}
|
||||
}
|
||||
APP_FINDER_PATH.forEach((searchPath, index) => {
|
||||
fs.readdir(searchPath, async (err, files) => {
|
||||
try {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const appName = files[i];
|
||||
const extname = path.extname(appName);
|
||||
const appSubStr = appName.split(extname)[0];
|
||||
if ((extname === '.app' || extname === '.prefPane') >= 0) {
|
||||
try {
|
||||
const path1 = path.join(searchPath, `${appName}/Contents/Resources/App.icns`);
|
||||
const path2 = path.join(searchPath, `${appName}/Contents/Resources/AppIcon.icns`);
|
||||
const path3 = path.join(searchPath, `${appName}/Contents/Resources/${appSubStr}.icns`);
|
||||
const path4 = path.join(searchPath, `${appName}/Contents/Resources/${appSubStr.replace(' ', '')}.icns`);
|
||||
let iconPath = path1;
|
||||
if (fs.existsSync(path1)) {
|
||||
iconPath = path1;
|
||||
} else if (fs.existsSync(path2)) {
|
||||
iconPath = path2;
|
||||
} else if (fs.existsSync(path3)) {
|
||||
iconPath = path3;
|
||||
} else if (fs.existsSync(path4)) {
|
||||
iconPath = path4;
|
||||
} else {
|
||||
// 性能最低的方式
|
||||
const resourceList = fs.readdirSync(path.join(searchPath, `${appName}/Contents/Resources`));
|
||||
const iconName = resourceList.filter((file) => path.extname(file) === '.icns')[0];
|
||||
iconPath = path.join(searchPath, `${appName}/Contents/Resources/${iconName}`);
|
||||
}
|
||||
const img = await nativeImage.createThumbnailFromPath(iconPath, { width: 64, height: 64 });
|
||||
|
||||
const appZhName = await getAppZhName(searchPath, appName);
|
||||
|
||||
const fileOptions = {
|
||||
value: 'plugin',
|
||||
icon: img.toDataURL(),
|
||||
desc: path.join(searchPath, appName),
|
||||
type: 'app',
|
||||
action: `open ${path.join(searchPath, appName).replace(' ', '\\ ')}`,
|
||||
keyWords: [appSubStr]
|
||||
};
|
||||
|
||||
if (appZhName && isZhRegex.test(appZhName)) {
|
||||
const py = translate(appZhName);
|
||||
const pinyinArr = py.split(',');
|
||||
const firstLatter = pinyinArr.map(py => py[0]);
|
||||
// 拼音
|
||||
fileOptions.keyWords.push(pinyinArr.join(''));
|
||||
// 缩写
|
||||
fileOptions.keyWords.push(firstLatter.join(''));
|
||||
// 中文
|
||||
fileOptions.keyWords.push(appZhName);
|
||||
}
|
||||
|
||||
fileLists.push({
|
||||
...fileOptions,
|
||||
name: appSubStr,
|
||||
names: JSON.parse(JSON.stringify(fileOptions.keyWords)),
|
||||
});
|
||||
} catch (e) {}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function debounce(fn, delay) {
|
||||
let timer;
|
||||
|
||||
93
src/renderer/assets/common/win-app.js
Normal file
93
src/renderer/assets/common/win-app.js
Normal file
@@ -0,0 +1,93 @@
|
||||
import path from "path";
|
||||
import os from 'os';
|
||||
import child from 'child_process';
|
||||
import iconv from 'iconv-lite';
|
||||
import translate from "./translate";
|
||||
|
||||
const fileLists = [];
|
||||
const isZhRegex = /[\u4e00-\u9fa5]/;
|
||||
|
||||
const getico = apps =>{
|
||||
const iconExtractor = require('icon-extractor');
|
||||
|
||||
iconExtractor.emitter.on('icon', function (data) {
|
||||
apps[data.Context].icon = 'data:image/png;base64,' + data.Base64ImageData;
|
||||
});
|
||||
|
||||
apps.forEach((app, i) => {
|
||||
iconExtractor.getIcon(i, app.desc);
|
||||
});
|
||||
}
|
||||
|
||||
const powershell = (cmd, callback) => {
|
||||
const ps = child.spawn('powershell', ['-NoProfile', '-Command', cmd], { encoding: 'buffer' })
|
||||
let chunks = [];
|
||||
let err_chunks = [];
|
||||
ps.stdout.on('data', chunk => {
|
||||
chunks.push(iconv.decode(chunk, 'cp936'))
|
||||
})
|
||||
ps.stderr.on('data', err_chunk => {
|
||||
err_chunks.push(iconv.decode(err_chunk, 'cp936'))
|
||||
})
|
||||
ps.on('close', code => {
|
||||
let stdout = chunks.join("");
|
||||
let stderr = err_chunks.join("");
|
||||
callback(stdout, stderr)
|
||||
})
|
||||
}
|
||||
|
||||
const getWinAppList = () => {
|
||||
let filterValues = "Select-Object DisplayName,DisplayIcon,UninstallString,DisplayVersion,InstallDate,Publisher,InstallLocation"
|
||||
let localMatcine = `Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | ${filterValues}`;
|
||||
let currentUser = `Get-ItemProperty HKCU:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | ${filterValues}`;
|
||||
let Wow6432Node = `Get-ItemProperty HKLM:\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | ${filterValues}`;
|
||||
let x64 = process.arch === 'x64' ? `;${Wow6432Node}` : '';
|
||||
powershell(`${localMatcine};${currentUser}${x64}`, (stdout, stderr) => {
|
||||
let apps = stdout.trim().replace(/\r\n[ ]{10,}/g,"").split('\r\n\r\n');
|
||||
for (const app of apps) {
|
||||
const dict = {}
|
||||
let lines = app.split('\r\n')
|
||||
for (var line of lines) {
|
||||
if (line) {
|
||||
const key = line.split(/\s+:\s*/)[0];
|
||||
const value = line.split(/\s+:\s*/)[1];
|
||||
dict[key] = value;
|
||||
}
|
||||
}
|
||||
if (dict.DisplayName && dict.DisplayIcon && dict.DisplayIcon.indexOf('.exe') >= 0) {
|
||||
dict.LegalName = dict.DisplayName.replace(/[\\\/\:\*\?\"\<\>\|]/g, "");
|
||||
dict.Icon = path.join(os.tmpdir(), 'ProcessIcon', `${encodeURIComponent(dict.LegalName)}.png`);
|
||||
const firstLatter = dict.DisplayName.split(' ').map(name => name[0]).join('');
|
||||
const appPath = dict.DisplayIcon.split(',')[0].replace(/"/g, '');
|
||||
const keyWords = [dict.DisplayName, firstLatter];
|
||||
if (isZhRegex.test(dict.DisplayName)) {
|
||||
const py = translate(dict.DisplayName);
|
||||
const pinyinArr = py.split(',');
|
||||
const zh_firstLatter = pinyinArr.map(py => py[0]);
|
||||
// 拼音
|
||||
keyWords.push(pinyinArr.join(''));
|
||||
// 缩写
|
||||
keyWords.push(zh_firstLatter.join(''));
|
||||
}
|
||||
|
||||
fileLists.push({
|
||||
...dict,
|
||||
value: 'plugin',
|
||||
icon: dict.Icon,
|
||||
desc: appPath,
|
||||
type: 'app',
|
||||
action: `start "dummyclient" "${appPath}"`,
|
||||
keyWords: keyWords,
|
||||
name: dict.DisplayName,
|
||||
names: JSON.parse(JSON.stringify(keyWords)),
|
||||
});
|
||||
}
|
||||
getico(fileLists);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export const getApp = {
|
||||
init: getWinAppList,
|
||||
fileLists,
|
||||
};
|
||||
@@ -107,10 +107,10 @@ const actions = {
|
||||
commit('commonUpdate', { searchValue: value });
|
||||
return;
|
||||
}
|
||||
const fileUrl = clipboard.read('public.file-url').replace('file://', '');
|
||||
const fileUrl = paylpad.filePath || clipboard.read('public.file-url').replace('file://', '');
|
||||
commit('commonUpdate', { searchValue: value });
|
||||
// 复制文件
|
||||
if (fileUrl && value === 'plugin.json') {
|
||||
if (paylpad.filePath || (fileUrl && value === 'plugin.json')) {
|
||||
const config = JSON.parse(fs.readFileSync(fileUrl, 'utf-8'));
|
||||
|
||||
const pluginConfig = {
|
||||
@@ -132,7 +132,6 @@ const actions = {
|
||||
name: 'plugin.json'
|
||||
},
|
||||
searchValue: '',
|
||||
devPlugins: [pluginConfig, ...state.devPlugins],
|
||||
options: [
|
||||
{
|
||||
name: '新建rubick开发插件',
|
||||
@@ -142,6 +141,7 @@ const actions = {
|
||||
click: (router) => {
|
||||
commit('commonUpdate', {
|
||||
showMain: true,
|
||||
devPlugins: [pluginConfig, ...state.devPlugins],
|
||||
selected: {
|
||||
key: 'plugin',
|
||||
name: '新建rubick开发插件'
|
||||
|
||||
Reference in New Issue
Block a user