Skip to content

Commit

Permalink
feat: basic
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertyyb committed Feb 7, 2021
1 parent df821f7 commit 576f386
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 34 deletions.
3 changes: 2 additions & 1 deletion app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function createWindow () {
webPreferences: {
nodeIntegration: true,
devTools: true,
contextIsolation: true
// contextIsolation: true
}
})
const htmlPath = path.join(__dirname, '../app/views/index.html')
Expand All @@ -34,6 +34,7 @@ app.whenReady().then(() => {
createWindow()
registerShortcut()
registerAllPlugin()
app.setAccessibilitySupportEnabled(true)
})

app.on('will-quit', () => {
Expand Down
31 changes: 31 additions & 0 deletions app/plugins/app-launcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fs = require('fs')

const macosAppPaths = [
'/System/Applications', // 系统应用
'/Applications', // 安装的应用
]

const getMacosApps = async (paths: String[]) => {
const dirApps = await Promise.all(paths.map(async (path) => {
const dir = await fs.promises.opendir(path);
return fs.readdirSync(path); // @todo 遍历目录
}));
return dirApps.flat()
};

class AppLauncher {
code = 'launcher'
data = {
list: ['']
}
async onCreate () {
this.data.list = await getMacosApps(macosAppPaths)
}
async onActive (setList: Function) {
setList(this.data.list)
}
async onSearch (keyword = '', setList: Function) {
setList(this.data.list.filter(name => name.includes(keyword)))
}
}
export default new AppLauncher()
33 changes: 1 addition & 32 deletions app/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,37 +94,6 @@
</div>
</li>
</ul>
<script>
const candidateList = [
{
code: 'lock',
title: '锁屏',
icon: '<span class="material-icons md-36 candidate-icon">lock</span>',
commad: 'lock'
},
{
code: 'wx',
title: '微信',
icon: '<span class="material-icons md-36 candidate-icon">message</span>',
command: 'lanch app: wechat'
}
]

const itemTemplate = (data) => `<li class="candidate-item">
${data.icon}
<div class="candidate-title">${data.title}</div>
<div class="candidate-subtitle">${data.subtitle || ''}</div>
</li>`

document.querySelector('.input').addEventListener('input', (e) => {
const val = e.target.value;
if (!val.length) {
document.querySelector('.candidate-list').style.display = 'none'
}
const html = candidateList.filter(item => item.code.includes(val)).map(itemTemplate).join('')
document.querySelector('.candidate-list').innerHTML = html
return document.querySelector('.candidate-list').style.display = 'block'
})
</script>
<script src="./index.js"></script>
</body>
</html>
54 changes: 54 additions & 0 deletions app/views/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const candidateList = [
{
code: 'lock',
title: '锁屏',
icon: '<span class="material-icons md-36 candidate-icon">lock</span>',
commad: 'lock',
action: () => {
const { exec } = require('child_process');

const lock = (cb, customCommands) => {
const lockCommands = customCommands || {
darwin: `open -a ScreenSaverEngine`,
win32: 'rundll32.exe user32.dll, LockWorkStation',
linux: '(hash gnome-screensaver-command 2>/dev/null && gnome-screensaver-command -l) || (hash dm-tool 2>/dev/null && dm-tool lock)'
};

if(Object.keys(lockCommands).indexOf(process.platform) === -1) {
throw new Error(`lockscreen doesn't support your platform (${process.platform})`);
} else {
exec(lockCommands[process.platform], (err, stdout) => {
console.log('callback', err, stdout)
});
}
}
lock()
}
},
{
code: 'wx',
title: '微信',
icon: '<span class="material-icons md-36 candidate-icon">message</span>',
command: 'lanch app: wechat'
}
]

const itemTemplate = (data) => `<li class="candidate-item">
${data.icon}
<div class="candidate-title">${data.title}</div>
<div class="candidate-subtitle">${data.subtitle || ''}</div>
</li>`

document.querySelector('.candidate-list').addEventListener('click', e => {
candidateList[0].action()
})

document.querySelector('.input').addEventListener('input', (e) => {
const val = e.target.value;
if (!val.length) {
document.querySelector('.candidate-list').style.display = 'none'
}
const html = candidateList.filter(item => item.code.includes(val)).map(itemTemplate).join('')
document.querySelector('.candidate-list').innerHTML = html
return document.querySelector('.candidate-list').style.display = 'block'
})
2 changes: 1 addition & 1 deletion dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function createWindow() {
webPreferences: {
nodeIntegration: true,
devTools: true,
contextIsolation: true
}
});
const htmlPath = path.join(__dirname, '../app/views/index.html');
Expand All @@ -54,6 +53,7 @@ electron_1.app.whenReady().then(() => {
createWindow();
registerShortcut();
plugin_1.default();
electron_1.app.setAccessibilitySupportEnabled(true);
});
electron_1.app.on('will-quit', () => {
// 注销所有快捷键
Expand Down
32 changes: 32 additions & 0 deletions dist/plugins/app-launcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require('fs');
const macosAppPaths = [
'/System/Applications',
'/Applications',
];
const getMacosApps = async (paths) => {
const dirApps = await Promise.all(paths.map(async (path) => {
const dir = await fs.promises.opendir(path);
return fs.readdirSync(path); // @todo 遍历目录
}));
return dirApps.flat();
};
class AppLauncher {
constructor() {
this.code = 'launcher';
this.data = {
list: ['']
};
}
async onCreate() {
this.data.list = await getMacosApps(macosAppPaths);
}
async onActive(setList) {
setList(this.data.list);
}
async onSearch(keyword = '', setList) {
setList(this.data.list.filter(name => name.includes(keyword)));
}
}
exports.default = new AppLauncher();

0 comments on commit 576f386

Please sign in to comment.