From 69a284cc163efae0a2563ac18a0f087e63e56c11 Mon Sep 17 00:00:00 2001 From: zhandouxiaojiji Date: Wed, 14 Oct 2020 11:26:43 +0800 Subject: [PATCH] test event --- main.tsx | 7 +++---- src/AppMenu.ts | 28 +++++++++++++++++++--------- src/Editor/index.tsx | 16 ++++++++++++++++ src/EventType.ts | 3 +++ src/index.tsx | 2 -- 5 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 src/EventType.ts diff --git a/main.tsx b/main.tsx index bd4bb70..6a3bcb4 100644 --- a/main.tsx +++ b/main.tsx @@ -8,7 +8,7 @@ let createWindow = function () { width: 1280, height: 800, webPreferences: { - // nodeIntegration: true + nodeIntegration: true } // fullscreenable:false, // maximizable:false @@ -18,6 +18,7 @@ let createWindow = function () { mainWindow.on('closed', function () { mainWindow = null }); + initMenu(mainWindow); } app.on('ready', createWindow) app.on('window-all-closed', () => { @@ -30,6 +31,4 @@ app.on('activate', () => { if (mainWindow === null) { createWindow() } -}); - -initMenu(); \ No newline at end of file +}); \ No newline at end of file diff --git a/src/AppMenu.ts b/src/AppMenu.ts index b502325..aef4456 100644 --- a/src/AppMenu.ts +++ b/src/AppMenu.ts @@ -1,20 +1,30 @@ import { BrowserView, Menu, app, shell, dialog, BrowserWindow, MenuItem } from 'electron'; +import EventType from './EventType'; -export function initMenu() { +export function initMenu(mainWindow: BrowserWindow) { const menu: Menu = new Menu(); const fileMenu: MenuItem = new MenuItem({ label: "行为树", submenu: [ - { + { label: "打开文件", - accelerator: "Ctrl+O" + accelerator: "Ctrl+O", + click: () => { + (async () => { + const res = await dialog.showOpenDialog({ + properties: ['openFile', 'openDirectory'] + }); + console.log("open res", res); + mainWindow.webContents.send(EventType.MAIN_OPEN_FILE, res); + })(); + } }, - { + { label: "打开目录", accelerator: "Ctrl+Shift+O" - + }, - { type: 'separator'}, + { type: 'separator' }, { label: "最近打开", submenu: [] @@ -22,11 +32,11 @@ export function initMenu() { { label: "最近目录", submenu: [ - {label: "master"}, - {label: "分支0908"}, + { label: "master" }, + { label: "分支0908" }, ] }, - { type: 'separator'}, + { type: 'separator' }, { label: "关闭", click: () => { diff --git a/src/Editor/index.tsx b/src/Editor/index.tsx index 7eb293c..33b09aa 100644 --- a/src/Editor/index.tsx +++ b/src/Editor/index.tsx @@ -1,7 +1,23 @@ import React, { Component } from "react"; import { DatePicker } from 'antd'; +import { ipcRenderer } from 'electron'; +import EventType from "../EventType"; + +interface EditorState { + workspace: string; +} export default class Editor extends Component { + state: EditorState = { + workspace: '' + } + + componentDidMount() { + ipcRenderer.on(EventType.MAIN_OPEN_FILE, (event: any, path: any) => { + console.log("on", event, path); + }) + } + render() { return ( diff --git a/src/EventType.ts b/src/EventType.ts new file mode 100644 index 0000000..c59a7dd --- /dev/null +++ b/src/EventType.ts @@ -0,0 +1,3 @@ +export default { + MAIN_OPEN_FILE: "MAIN_OPEN_FILE", +} \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index ac34240..fc1dd13 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,12 +1,10 @@ import * as React from "react"; import * as ReactDOM from "react-dom"; -import Hello from "./components/Hello"; import Editor from "./Editor"; import 'antd/dist/antd.dark.css'; import './index.css'; ReactDOM.render( , - //
hello!!!!!
, document.getElementById('root') as HTMLElement ); \ No newline at end of file