Skip to content

Commit

Permalink
feat: 自动更新软件
Browse files Browse the repository at this point in the history
  • Loading branch information
027xiguapi committed Dec 6, 2023
1 parent afcce35 commit f153fe2
Show file tree
Hide file tree
Showing 14 changed files with 223 additions and 198 deletions.
4 changes: 4 additions & 0 deletions packages/desktop/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @pear-rec/desktop

## 1.3.2

feat: 自动更新软件

## 1.3.1

feat: 增加录全屏
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/electron-builder.json5
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @see https://www.electron.build/configuration/configuration
*/
{
appId: 'com.electron.pear',
appId: 'com.electron.pear-rec',
asar: false,
productName: 'pear-rec',
copyright: 'Copyright © 2023 ${author}',
Expand Down
5 changes: 4 additions & 1 deletion packages/desktop/electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { release } from 'node:os';
import * as mainWin from '../win/mainWin';
import { initTray } from './tray';
import './ipcMain';
import { update } from './update';
import { registerFileProtocol } from './protocol';
import { registerGlobalShortcut, unregisterAllGlobalShortcut } from './globalShortcut';
import { initConfig } from './config';
Expand Down Expand Up @@ -39,14 +40,16 @@ if (!app.requestSingleInstanceLock()) {
// process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'

async function createWindow() {
mainWin.createMainWin();
mainWin.openMainWin();
}

app.whenReady().then(() => {
registerFileProtocol();
createWindow();
initTray(config.language);
registerGlobalShortcut();
// Apply electron-updater
update();
});

app.on('will-quit', () => {
Expand Down
7 changes: 0 additions & 7 deletions packages/desktop/electron/main/ipcMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ function initIpcMain() {
clipScreenWin.setIgnoreMouseEventsClipScreenWin(event, false);
clipScreenWin.setIsPlayClipScreenWin(false);
});
ipcMain.handle('rs:close-record', () => {
return recorderScreenWin.getBoundsRecorderScreenWin();
});
ipcMain.handle('rs:get-cursor-screen-point', () => {
return recorderScreenWin.getCursorScreenPointRecorderScreenWin();
});
Expand Down Expand Up @@ -279,17 +276,13 @@ function initIpcMain() {
const audios = await viewAudioWin.getAudios(audioUrl);
return audios;
});
ipcMain.handle('va:set-historyAudio', async (e, audioUrl) => {
// store.setHistoryAudio(audioUrl);
});
// 设置
ipcMain.on('se:open-win', () => {
settingWin.closeSettingWin();
settingWin.openSettingWin();
});
ipcMain.on('se:close-win', () => {
settingWin.closeSettingWin();
mainWin.showMainWin();
});
ipcMain.handle('se:set-filePath', async (e, filePath) => {
let res = await dialog.showOpenDialog({
Expand Down
6 changes: 3 additions & 3 deletions packages/desktop/electron/main/tray.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Menu, Tray, app, shell } from 'electron';
import { ICON } from './contract';
import { showMainWin } from '../win/mainWin';
import { openMainWin } from '../win/mainWin';
import { openShotScreenWin } from '../win/shotScreenWin';
import { openClipScreenWin } from '../win/clipScreenWin';
import { openRecorderAudioWin } from '../win/recorderAudioWin';
Expand Down Expand Up @@ -85,7 +85,7 @@ export function initTray(lng: string) {
{
label: t.home,
click: () => {
showMainWin();
openMainWin();
},
},
{
Expand Down Expand Up @@ -120,7 +120,7 @@ export function initTray(lng: string) {
appIcon.setToolTip('梨子REC');
appIcon.setContextMenu(contextMenu);
appIcon.addListener('click', function () {
showMainWin();
openMainWin();
});
return appIcon;
}
42 changes: 23 additions & 19 deletions packages/desktop/electron/main/update.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import { app, ipcMain } from 'electron';
import { type ProgressInfo, type UpdateDownloadedEvent, autoUpdater } from 'electron-updater';
import * as mainWin from '../win/mainWin';

export function update(win: Electron.BrowserWindow) {
export function update() {
// When set to false, the update download will be triggered through the API
autoUpdater.autoDownload = false;
autoUpdater.disableWebInstaller = false;
autoUpdater.allowDowngrade = false;

// start check
autoUpdater.on('checking-for-update', function () {});
autoUpdater.on('checking-for-update', function () {
console.log('Checking for update.');
});
// update available
autoUpdater.on('update-available', (arg) => {
win.webContents.send('update-can-available', {
update: true,
version: app.getVersion(),
newVersion: arg?.version,
});
mainWin.sendEuUpdateCanAvailable(arg, true);
// win.webContents.send('eu:update-can-available', {
// update: true,
// version: app.getVersion(),
// newVersion: arg?.version,
// });
});
// update not available
autoUpdater.on('update-not-available', (arg) => {
win.webContents.send('update-can-available', {
update: false,
version: app.getVersion(),
newVersion: arg?.version,
});
mainWin.sendEuUpdateCanAvailable(arg, false);
// win.webContents.send('eu:update-can-available', {
// update: false,
// version: app.getVersion(),
// newVersion: arg?.version,
// });
});

// Checking for updates
ipcMain.handle('check-update', async () => {
ipcMain.handle('eu:check-update', async () => {
if (!app.isPackaged) {
const error = new Error('The update feature is only available after the package.');
return { message: error.message, error };
Expand All @@ -41,26 +45,26 @@ export function update(win: Electron.BrowserWindow) {
});

// Start downloading and feedback on progress
ipcMain.handle('start-download', (event) => {
ipcMain.handle('eu:start-download', (event) => {
startDownload(
(error, progressInfo) => {
if (error) {
// feedback download error message
event.sender.send('update-error', { message: error.message, error });
event.sender.send('eu:update-error', { message: error.message, error });
} else {
// feedback update progress message
event.sender.send('download-progress', progressInfo);
event.sender.send('eu:download-progress', progressInfo);
}
},
() => {
// feedback update downloaded message
event.sender.send('update-downloaded');
event.sender.send('eu:update-downloaded');
},
);
});

// Install now
ipcMain.handle('quit-and-install', () => {
ipcMain.handle('eu:quit-and-install', () => {
autoUpdater.quitAndInstall(false, true);
});
}
Expand Down
23 changes: 18 additions & 5 deletions packages/desktop/electron/preload/electronAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
//vaWin
sendVaOpenWin: (search?: any) => ipcRenderer.send('va:open-win', search),
invokeVaGetAudios: (audioUrl: any) => ipcRenderer.invoke('va:get-audios', audioUrl),
sendVaSetHistoryAudio: (audioUrl: any) => ipcRenderer.send('va:set-historyAudio', audioUrl),
//seWin
//seWin 设置
sendSeOpenWin: () => ipcRenderer.send('se:open-win'),
invokeSeGetUser: () => ipcRenderer.invoke('se:get-user'),
invokeSeSetFilePath: (filePath: string) => ipcRenderer.invoke('se:set-filePath', filePath),
Expand All @@ -94,16 +93,30 @@ contextBridge.exposeInMainWorld('electronAPI', {
sendSeSetLanguage: (lng: string) => ipcRenderer.send('se:set-language', lng),
invokeSeGetOpenAtLogin: () => ipcRenderer.invoke('se:get-openAtLogin'),

//re
//re 记录
sendReOpenWin: () => ipcRenderer.send('re:open-win'),

//pi
//pi 钉图
sendPiOpenWin: (search?: any) => ipcRenderer.send('pi:open-win', search),
sendPiCloseWin: () => ipcRenderer.send('pi:close-win'),
sendPiMinimizeWin: () => ipcRenderer.send('pi:minimize-win'),
sendPiMaximizeWin: () => ipcRenderer.send('pi:maximize-win'),

// rfs
// rfs 全屏录屏
sendRfsOpenWin: () => ipcRenderer.send('rfs:open-win'),
sendRfsCloseWin: () => ipcRenderer.send('rfs:close-win'),

// Eu 自动更新
handleEuUpdateCanAvailable: (callback: any) =>
ipcRenderer.on('eu:update-can-available', callback),
handleEuUpdateeError: (callback: any) => ipcRenderer.on('eu:update-error', callback),
handleEuDownloadProgress: (callback: any) => ipcRenderer.on('eu:download-progress', callback),
handleEuUpdateDownloaded: (callback: any) => ipcRenderer.on('eu:update-downloaded', callback),
invokeEuQuitAndInstall: () => ipcRenderer.invoke('eu:quit-and-install'),
invokeEuStartDownload: () => ipcRenderer.invoke('eu:start-download'),
invokeEuCheckUpdate: () => ipcRenderer.invoke('eu:check-update'),
offEuUpdateCanAvailable: (callback: any) => ipcRenderer.on('eu:update-can-available', callback),
offEuUpdateeError: (callback: any) => ipcRenderer.on('eu:update-error', callback),
offEuDownloadProgress: (callback: any) => ipcRenderer.on('eu:download-progress', callback),
offEuUpdateDownloaded: (callback: any) => ipcRenderer.on('eu:update-downloaded', callback),
});
40 changes: 17 additions & 23 deletions packages/desktop/electron/win/mainWin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { app, screen, BrowserWindow, shell, ipcMain } from 'electron';
import { join } from 'node:path';
// import { update } from '../main/update';
import { ICON, preload, url, WEB_URL } from '../main/contract';

const indexHtml = join(process.env.DIST, 'index.html');
Expand Down Expand Up @@ -53,9 +52,6 @@ const createMainWin = (): BrowserWindow => {
return { action: 'deny' };
});

// Apply electron-updater
// update(mainWin);

// mainWin.onbeforeunload = (e) => {
// console.log('I do not want to be closed');

Expand All @@ -73,22 +69,17 @@ const createMainWin = (): BrowserWindow => {
};

function closeMainWin() {
if (!mainWin?.isDestroyed()) {
mainWin!.close();
if (mainWin && !mainWin?.isDestroyed()) {
mainWin?.close();
}
mainWin = null;
}

function openMainWin() {
mainWin = createMainWin();
mainWin!.show();
}

function showMainWin() {
if (!mainWin || mainWin?.isDestroyed()) {
mainWin = createMainWin();
}
mainWin!.show();
mainWin?.show();
}

function hideMainWin() {
Expand All @@ -99,30 +90,33 @@ function minimizeMainWin() {
mainWin!.minimize();
}

function maximizeMainWin() {
mainWin!.maximize();
}

function unmaximizeMainWin() {
mainWin!.unmaximize();
}

function focusMainWin() {
if (mainWin) {
if (!mainWin || mainWin?.isDestroyed()) {
mainWin = createMainWin();
} else {
// Focus on the main window if the user tried to open another
if (mainWin.isMinimized()) mainWin.restore();
if (!mainWin.isVisible()) mainWin.show();
mainWin.focus();
}
}

function sendEuUpdateCanAvailable(arg, update) {
if (mainWin && !mainWin?.isDestroyed()) {
mainWin.webContents.send('eu:update-can-available', {
update: update,
version: app.getVersion(),
newVersion: arg?.version,
});
}
}

export {
mainWin,
createMainWin,
closeMainWin,
openMainWin,
hideMainWin,
showMainWin,
focusMainWin,
minimizeMainWin,
sendEuUpdateCanAvailable,
};
2 changes: 1 addition & 1 deletion packages/desktop/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pear-rec/desktop",
"version": "1.3.0",
"version": "1.3.2",
"main": "dist-electron/main/index.js",
"description": "pear-rec",
"author": {
Expand Down
4 changes: 4 additions & 0 deletions packages/web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @pear-rec/web

## 1.3.2

feat: 自动更新软件

## 1.3.1

feat: 增加录全屏
Expand Down
2 changes: 1 addition & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@pear-rec/web",
"private": true,
"version": "1.3.0",
"version": "1.3.2",
"scripts": {
"dev": "vite",
"build": "rimraf dist && tsc && vite build",
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/components/home/HomeFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { SettingOutlined, HistoryOutlined } from '@ant-design/icons';
import { Button } from 'antd';

const logo = './imgs/icons/png/512x512.png';
import UpdateElectron from '../update';

const HomeFooter = () => {
const { t } = useTranslation();
Expand Down Expand Up @@ -33,6 +32,7 @@ const HomeFooter = () => {
title={t('nav.record')}
onClick={handleOpenRecordWin}
/>
<UpdateElectron />
</div>
);
};
Expand Down
Loading

0 comments on commit f153fe2

Please sign in to comment.