Skip to content

Commit

Permalink
Merge pull request #3 from AGIUI/dev
Browse files Browse the repository at this point in the history
5-31
  • Loading branch information
shadowcz007 committed May 31, 2023
2 parents 83f7146 + 0389f52 commit 4313a47
Show file tree
Hide file tree
Showing 22 changed files with 661 additions and 44 deletions.
Binary file added assets/TrayTemplate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/TrayTemplate@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/TrayTemplate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/TrayTemplate@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
}
},
"dependencies": {
"@ant-design/icons": "^5.1.4",
"@mozilla/readability": "^0.4.4",
"antd": "^5.3.2",
"axios": "^1.4.0",
Expand Down
4 changes: 2 additions & 2 deletions release/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 61 additions & 2 deletions src/main/libs/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ipcMain, app, BrowserWindow, shell, dialog } from 'electron';
import _ from 'lodash';
import axios from 'axios';
import fs from 'fs';

function delay(ms) {
return new Promise((resolve, reject) => {
setTimeout(() => {
Expand All @@ -12,8 +13,15 @@ function delay(ms) {

const processHandler = {
async dbt_close_app() {
app.quit();
process.exit(0);
let mainWin = BrowserWindow.fromId(global.mainWindowId);
if (!mainWin) {
return;
}

mainWin.hide();

// app.quit();
// process.exit(0);
},
bing_login() {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -104,6 +112,57 @@ const processHandler = {

evt.reply('importComboBack', { data: json });
},
dbt_pop_search(evt, data) {
let mainWin = BrowserWindow.fromId(global.mainWindowId);
if (!mainWin) {
return;
}

mainWin.show();
mainWin.focus();
mainWin.webContents.send('dbt_pop_search', data);
},
dbt_pop_typechange(evt, data) {
let mainWin = BrowserWindow.fromId(global.mainWindowId);
if (!mainWin) {
return;
}

// mainWin.show();
// mainWin.focus();
mainWin.webContents.send('dbt_pop_typechange', data);
},
dbt_main_typechange(evt, data) {
let popWin = BrowserWindow.fromId(global.popWindowId);
if (!popWin) {
return;
}

// mainWin.show();
// mainWin.focus();
popWin.webContents.send('dbt_main_typechange', data);
},
dbt_pop_close(evt, data) {
let popWin = BrowserWindow.fromId(global.popWindowId);
if (!popWin) {
return;
}

popWin.hide();
},
click_through(evt, data) {
let flag = data?.flag || false;
let popWin = BrowserWindow.fromId(global.popWindowId);
if (!popWin) {
return;
}

if (flag) {
popWin.setIgnoreMouseEvents(true, { forward: true });
} else {
popWin.setIgnoreMouseEvents(false);
}
},
};

export default function ipc() {
Expand Down
6 changes: 6 additions & 0 deletions src/main/libs/popWindow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import path from 'path';
import { app, BrowserWindow, shell, ipcMain } from 'electron';



export default createWindow;
153 changes: 137 additions & 16 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@
* `./src/main.js` using webpack. This gives us some performance wins.
*/
import path from 'path';
import { app, BrowserWindow, shell, ipcMain } from 'electron';
import {
app,
BrowserWindow,
shell,
ipcMain,
screen,
Tray,
Menu,
globalShortcut,
nativeImage,
} from 'electron';
import { autoUpdater } from 'electron-updater';
import { checkAccessibilityPermissions } from 'node-selection';
import watchSelection from './watchSelection';
import log from 'electron-log';
import MenuBuilder from './menu';
Expand All @@ -19,7 +30,7 @@ import ipc from './libs/ipc';

let mouseDownPos = { x: 0, y: 0 }; // 记录鼠标按下的坐标
let isMouseDown = false;
let tray = null;
let tray: any = null;

class AppUpdater {
constructor() {
Expand All @@ -30,6 +41,7 @@ class AppUpdater {
}

let mainWindow: BrowserWindow | null = null;
let popWindow: BrowserWindow | null = null;

ipcMain.on('ipc-example', async (event, arg) => {
const msgTemplate = (pingPong: string) => `IPC test: ${pingPong}`;
Expand All @@ -47,7 +59,7 @@ const isDebug =
process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true';

if (isDebug) {
require('electron-debug')();
// require('electron-debug')();
}

const installExtensions = async () => {
Expand All @@ -63,18 +75,67 @@ const installExtensions = async () => {
.catch(console.log);
};

const createWindow = async () => {
const RESOURCES_PATH = app.isPackaged
? path.join(process.cwd(), '/resources/assets') // ? path.join(process.resourcesPath, 'assets')
: path.join(process.cwd(), '/assets'); //path.join(__dirname, '../../assets');

const getAssetPath = (...paths: string[]): string => {
return path.join(RESOURCES_PATH, ...paths);
};

const createPopWindow = async () => {
if (!(await checkAccessibilityPermissions({ prompt: true }))) {
console.log('grant accessibility permissions and restart this program');
}

if (!popWindow) {
let xw = screen.getPrimaryDisplay().workAreaSize.width;

popWindow = new BrowserWindow({
width: xw * 0.35,
height: 200,
y: 5,
icon: getAssetPath('icon.png'),
show: false,
transparent: true,
alwaysOnTop: true,
skipTaskbar: true,
resizable: false,
frame: false,
movable: true,

webPreferences: {
preload: app.isPackaged
? path.join(__dirname, 'preload.js')
: path.join(__dirname, '../../.erb/dll/preload.js'),
},
});

global.popWindowId = popWindow.id;
popWindow.setPosition(xw * 0.325, 10);
}

if (isDebug) {
// await installExtensions();
// popWindow.webContents.openDevTools();
popWindow.loadURL(resolveHtmlPath('index.html', { hash: '/pop' }));
} else {
popWindow.loadFile(resolveHtmlPath('index.html', null), { hash: '/pop' });
}
popWindow.show();

const RESOURCES_PATH = app.isPackaged
? path.join(process.resourcesPath, 'assets')
: path.join(__dirname, '../../assets');
// popWindow.webContents.on('did-finish-load', () => {
// console.log('did-finish-load');
// });

const getAssetPath = (...paths: string[]): string => {
return path.join(RESOURCES_PATH, ...paths);
};
// popWindow.on('closed', () => {
// popWindow = null;
// });
};

const createWindow = async () => {
if (isDebug) {
// await installExtensions();
}

mainWindow = new BrowserWindow({
show: false,
Expand All @@ -89,7 +150,54 @@ const createWindow = async () => {
},
});

mainWindow.loadURL(resolveHtmlPath('index.html'));
global.mainWindowId = mainWindow.id;

if (isDebug) {
mainWindow.webContents.openDevTools();
mainWindow.loadURL(resolveHtmlPath('index.html', null));
} else {
mainWindow.loadFile(resolveHtmlPath('index.html', null));
}

if (!tray) {
let icon = nativeImage.createFromDataURL(
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAB9NJREFUWEfNl2tsFOcVhp9vZtbGNr5grw0Gcwm2gykGEkyhsR0DEYiU0hAJNVILldL2RwuhCKLQ9k+aikqNEhCphFCrVEpzqShN1CihFLVEEC6OuTgkMXaIuRQDtoEa39f4sjuX6nzfrDFVf6Kko5V2ZnbnnPec855z3lF8xYf6iv1zL4CKisiXAujs2UTSjwEwa1YVvrUe/Gn4/wXqfiOyCMC6hlJ7uXi+VlFathb4rVJWUUZ2JpHUcaAUllIopfT56BHIWSAW9JH8lnMLhR/4+pkgCPS3PGmu5UR/GB4cZKC3jyDw21BqswD4TClr/qTiGUwvexDLiYQALCKOg689BRpQ0qkfBMYJysBRCtuyCHxxDCOui2NZ2rl8a4BK4fk+iZERrjdfoPXiZQHRoCgpc9OzMu15NVU4aeN0WJY8DNi2ox0lr8WAH4BjWyQ8TxsWxybaJMAA13X1M+Z3ZWwqyZDJzODAAJ8dPc5gf8wTAEFOQT7za6qwHCdMrjGnIxX0YiR0oF0p8AQJYWbCkkkJ5BmBL8DlSJbBllKENhPxOA3Ha+m73cldAEursR1HRyZO5IHR+o2pt6WERQG+pBsB4qNsazRCKYMANiUSboRZknt+gOt5OkNNtXX0dtw2ALIL8plXUyk5D+tlDEoQmoghcolHaipOXXGsiXU39eI0GbkunX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0qkT+VblfDIzx7PnWBNv1V+i+cxZ+m8nAeTlUlZdyZTsDLYtK6eyeJIeJpI6sdzeHeOVDxupbe0lEnHo7+qhp+0GpQvm4wY+Pbc68GMxpsg2xWPDomJWziokNcUBy9ZUisdd9jde49WTFxlMeJyvO01PchRLBuYuqdZDJjNisXTmRBZPyycjNcKl2318cKGd5q47WHpUKwb6+2lvaKKoZCbWuFRutbQSnVRAdEYRw3GXNEfxSFEu8wpzyE5LpWdwhE/bOqlv7cTH0l0jg6g3uYyy86MagB2J6JQmXJeIUoxLjTDkmhWc7HqTGJ/YvzvovdGhyZoVzSVaNBlfLyHDXbMVAxyltD6Q63G2rQeWG4/T+NGpe7dhuaxj2f/S2aEVK0yfkC0c8ZqYYkz2hvzPdIEMBDOa5VI0gjyjVcWY/aDFSRAg67ipbgwA2YZzqh/BdmQBmX2vDcsCUiocu8nRZ7afGBMg2plnNqPeB6H60eTUAsa0rNh1ZLSHAD4fCyCnIEr5o0aQyFYT4BKBFh2W0ptP2i45e8XYWImmlY/ck0Wlx7IoHy1LtG7wwkxJOfQ6TiRoqj05WgIvPSvTemhpNU5aulkwWr0ERmzokWuMS1YEU1Lr6f0vTkKpJREKALmXFGm6fMnSaG2gGBoYoOFYLcOxAV9ROrtZhPnk4geYNnsWkdTU0TVrFE9oLlQ0yhJRYuvUx0NyGRVsBJj8W36zQ6GS3Jpa4BAwMjRE2xcXuHnlqjxwQVH6tR9AsEMplZeWOZ5UEaahztFDNklrk71RhZQUKKMkDGW3pmQoyY2qMm1htmegZflQbEBudaOCZ7X4pXT24xA8TcCU0T4K5el9/9I6zrpB4L/B5eaDd9865LWsry/tvjv8Xwazs4cIX8/+z15Ov5Tw73XyH6014Lgc0Pu+AAAAAElFTkSuQmCC'
);
tray = new Tray(icon);
const contextMenu = Menu.buildFromTemplate([
{
label: '打开搜索栏',
type: 'normal',
click: () => {
let popWin = BrowserWindow.fromId(global.popWindowId);
if (popWin) {
popWin.show();
}
},
},
{
label: '退出',
type: 'normal',
click: () => {
if (app) {
app.quit();
process.exit(0);
}
},
},
]);

tray.setContextMenu(contextMenu);

tray.setToolTip('Earth');

tray.on('click', () => {
let xWindow = BrowserWindow.fromId(global.mainWindowId);
if (xWindow) {
xWindow.isVisible() ? xWindow.hide() : xWindow.show();
}
});
}

mainWindow.on('ready-to-show', () => {
if (!mainWindow) {
Expand All @@ -100,12 +208,13 @@ const createWindow = async () => {
} else {
mainWindow.show();
}

createPopWindow();
});

mainWindow.webContents.on('did-finish-load', () => {
console.log('did-finish-load');
watchSelection(mainWindow)

watchSelection(mainWindow);
});

mainWindow.on('closed', () => {
Expand All @@ -130,8 +239,7 @@ const createWindow = async () => {
* Add event listeners...
*/

app.on('before-quit', () => {
});
app.on('before-quit', () => {});

app.on('window-all-closed', () => {
// Respect the OSX convention of having the application in memory even
Expand All @@ -144,6 +252,19 @@ app.on('window-all-closed', () => {
app
.whenReady()
.then(() => {
globalShortcut.register('Ctrl+Shift+M', () => {
let xWindow = BrowserWindow.fromId(global.mainWindowId);
if (xWindow) {
xWindow.isVisible() ? xWindow.hide() : xWindow.show();
}
});

globalShortcut.register('Ctrl+Shift+F', () => {
let popWin = BrowserWindow.fromId(global.popWindowId);
if (popWin) {
popWin.isVisible() ? popWin.hide() : popWin.show();
}
});
createWindow();
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
Expand Down
7 changes: 5 additions & 2 deletions src/main/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
import { URL } from 'url';
import path from 'path';

export function resolveHtmlPath(htmlFileName: string) {
export function resolveHtmlPath(htmlFileName: string, opts: any) {
if (process.env.NODE_ENV === 'development') {
const port = process.env.PORT || 1212;
const url = new URL(`http://localhost:${port}`);
url.pathname = htmlFileName;
if (opts?.hash) {
url.hash = opts.hash;
}
return url.href;
}
return `file://${path.resolve(__dirname, '../renderer/', htmlFileName)}`;
return path.resolve(__dirname, '../renderer/', htmlFileName);
}
Loading

0 comments on commit 4313a47

Please sign in to comment.