-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (96 loc) · 2.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const fs = require('fs')
const path = require('path')
const gui = require('gui')
const menu = gui.MenuBar.create([
{
label: '文件',
submenu: [
{
label: '退出',
accelerator: 'CmdOrCtrl+Q',
onClick: () => gui.MessageLoop.quit()
},
],
},
{
label: '编辑',
submenu: [
{ role: 'copy', label: "复制" },
{ role: 'cut', label: "剪切" },
{ role: 'paste', label: "粘贴" },
{ role: 'select-all', label: "选择全部" },
{ type: 'separator' },
{ role: 'undo', label: "撤销" },
{ role: 'redo', label: "恢复" },
],
},
])
const win = gui.Window.create({})
win.setContentSize({width: 400, height: 400})
win.onClose = () => gui.MessageLoop.quit()
const contentView = gui.Container.create()
contentView.setStyle({flexDirection: 'row'})
win.setContentView(contentView)
let sidebar
if (process.platform == 'darwin') {
sidebar = gui.Vibrant.create()
sidebar.setBlendingMode('behind-window')
sidebar.setMaterial('dark')
} else {
sidebar = gui.Container.create()
}
sidebar.setStyle({
padding: 5,
width: 100,
height: 60
})
contentView.addChildView(sidebar)
const edit = gui.TextEdit.create()
edit.setStyle({flex: 1})
contentView.addChildView(edit)
let filename
let folder
const open = gui.Button.create('')
open.setImage(gui.Image.createFromPath(__dirname + '/[email protected]'))
open.setStyle({marginBottom: 5})
open.onClick = () => {
const dialog = gui.FileOpenDialog.create()
dialog.setOptions(gui.FileDialog.optionShowHidden)
dialog.setFilters([
{ description: 'All Files', extensions: ['*'] },
{ description: 'JavaScript Files', extensions: ['js'] },
])
if (dialog.runForWindow(win)) {
const p = dialog.getResult()
folder = path.dirname(p)
filename = path.basename(p)
edit.setText(String(fs.readFileSync(p)))
edit.focus()
win.setTitle(filename)
}
}
sidebar.addChildView(open)
const save = gui.Button.create('')
save.setImage(gui.Image.createFromPath(__dirname + '/[email protected]'))
save.onClick = () => {
if (!folder)
return
const dialog = gui.FileSaveDialog.create()
dialog.setFolder(folder)
dialog.setFilename(filename)
if (dialog.runForWindow(win)) {
fs.writeFileSync(String(dialog.getResult()), edit.getText())
}
}
sidebar.addChildView(save)
sidebar.setStyle({width: sidebar.getPreferredSize().width})
if (process.platform == 'darwin')
gui.app.setApplicationMenu(menu)
else
win.setMenuBar(menu)
win.center()
win.activate()
if (!process.versions.yode) {
gui.MessageLoop.run()
process.exit(0)
}