Skip to content

Commit

Permalink
Merge pull request #146 from caorushizi/fix/addForm
Browse files Browse the repository at this point in the history
fix: 🐛  fix browser window event
  • Loading branch information
caorushizi authored May 21, 2024
2 parents 4379a6a + 3969d97 commit cb3277c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
10 changes: 7 additions & 3 deletions packages/main/src/controller/DownloadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import MainWindow from "../windows/MainWindow";
import ElectronStore from "../vendor/ElectronStore";
import DownloadService from "../services/DownloadService";
import VideoRepository from "../repository/VideoRepository";
import BrowserWindow from "../windows/BrowserWindow";

@injectable()
export default class DownloadController implements Controller {
Expand All @@ -25,26 +26,29 @@ export default class DownloadController implements Controller {
private readonly downloadService: DownloadService,
@inject(TYPES.MainWindow)
private readonly mainWindow: MainWindow,
@inject(TYPES.BrowserWindow)
private readonly browserWindow: BrowserWindow,
) {}

@handle("show-download-dialog")
async showDownloadDialog(e: IpcMainEvent, data: DownloadItem) {
this.mainWindow.window?.webContents.send("show-download-dialog", data);
this.browserWindow.send("show-download-dialog", data);
this.mainWindow.send("show-download-dialog", data);
}

@handle("add-download-item")
async addDownloadItem(e: IpcMainEvent, video: DownloadItem) {
const item = await this.videoRepository.addVideo(video);
// 这里向页面发送消息,通知页面更新
this.mainWindow.window?.webContents.send("download-item-notifier", item);
this.mainWindow.send("download-item-notifier", item);
return item;
}

@handle("add-download-items")
async addDownloadItems(e: IpcMainEvent, videos: DownloadItem[]) {
const items = await this.videoRepository.addVideos(videos);
// 这里向页面发送消息,通知页面更新
this.mainWindow.window?.webContents.send("download-item-notifier", items);
this.mainWindow.send("download-item-notifier", items);
return items;
}

Expand Down
9 changes: 2 additions & 7 deletions packages/main/src/controller/HomeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ export default class HomeController implements Controller {
@handle("on-favorite-item-context-menu")
async onFavoriteItemContextMenu(e: IpcMainEvent, id: number) {
const send = (action: string) => {
const window = this.mainWindow.window;
if (!window) return;
window.webContents.send("favorite-item-event", {
this.mainWindow.send("favorite-item-event", {
action,
payload: id,
});
Expand Down Expand Up @@ -162,10 +160,7 @@ export default class HomeController implements Controller {
@handle("on-download-list-context-menu")
async downloadListContextMenu(e: IpcMainEvent, id: number) {
const send = (action: string) => {
const window = this.mainWindow.window;
if (!window) return;

window.webContents.send("download-item-event", {
this.mainWindow.send("download-item-event", {
action,
payload: id,
});
Expand Down
10 changes: 7 additions & 3 deletions packages/main/src/windows/BrowserWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ export default class BrowserWindow extends Window {
}

storeChange = (store: any) => {
if (!this.window) return;
// 向所有窗口发送通知
this.window.webContents.send("store-change", store);
this.send("store-change", store);
};

handleNewWindowsVal = (newValue: any) => {
Expand Down Expand Up @@ -76,4 +74,10 @@ export default class BrowserWindow extends Window {

this.window.close();
};

send(channel: string, ...args: any[]) {
if (!this.window) return;

this.window.webContents.send(channel, ...args);
}
}
1 change: 1 addition & 0 deletions packages/renderer/src/nodes/SourceExtract/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ const SourceExtract: React.FC<SourceExtractProps> = ({ page = false }) => {
};

const onShowDownloadDialog = async (e: any, data: DownloadForm[]) => {
console.log(data);
setDownloadItems(data);
setCurrentDownloadForm(data[0]);
};
Expand Down

0 comments on commit cb3277c

Please sign in to comment.