@vincentbanana/banana-gui 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/desktop/main.cjs +55 -5
- package/desktop/preload.cjs +20 -0
- package/package.json +2 -2
- package/ui/app.js +1251 -242
- package/ui/index.html +81 -30
- package/ui/style.css +906 -1
package/desktop/main.cjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// Banana 桌面版主进程:拉起本地 GUI 服务,再用窗口加载它。
|
|
2
2
|
// 界面资源仍来自本包 ui/,agent 内核来自 @vincentbanana/bananacli。
|
|
3
|
-
const { app, BrowserWindow, Menu, shell, dialog } = require('electron');
|
|
3
|
+
const { app, BrowserWindow, Menu, shell, dialog, ipcMain } = require('electron');
|
|
4
4
|
const { spawn, spawnSync } = require('node:child_process');
|
|
5
5
|
const { createRequire } = require('node:module');
|
|
6
6
|
const { join } = require('node:path');
|
|
7
|
-
const { mkdirSync } = require('node:fs');
|
|
7
|
+
const { mkdirSync, writeFileSync } = require('node:fs');
|
|
8
8
|
|
|
9
9
|
// 用户数据、Electron 缓存统一放 D 盘,不写系统盘
|
|
10
10
|
try {
|
|
@@ -97,6 +97,7 @@ function startServer() {
|
|
|
97
97
|
|
|
98
98
|
async function createWindow() {
|
|
99
99
|
const url = await startServer();
|
|
100
|
+
const frameless = process.platform !== 'darwin';
|
|
100
101
|
win = new BrowserWindow({
|
|
101
102
|
width: 1180,
|
|
102
103
|
height: 820,
|
|
@@ -105,10 +106,20 @@ async function createWindow() {
|
|
|
105
106
|
backgroundColor: '#ffffff',
|
|
106
107
|
title: 'Banana',
|
|
107
108
|
autoHideMenuBar: true,
|
|
109
|
+
// Windows / Linux 用自绘标题栏;macOS 保留原生红绿灯更符合习惯
|
|
110
|
+
frame: !frameless,
|
|
111
|
+
titleBarStyle: frameless ? 'default' : 'hiddenInset',
|
|
112
|
+
trafficLightPosition: frameless ? undefined : { x: 14, y: 16 },
|
|
113
|
+
hasShadow: true,
|
|
108
114
|
// 先不显示,等页面就绪再 show():启动器用了 windowsHide,
|
|
109
115
|
// 那种情况下 Electron 会把首个窗口以隐藏状态创建,不显式 show 就看不到
|
|
110
116
|
show: false,
|
|
111
|
-
webPreferences: {
|
|
117
|
+
webPreferences: {
|
|
118
|
+
contextIsolation: true,
|
|
119
|
+
nodeIntegration: false,
|
|
120
|
+
sandbox: true,
|
|
121
|
+
preload: join(__dirname, 'preload.cjs'),
|
|
122
|
+
},
|
|
112
123
|
});
|
|
113
124
|
await win.loadURL(url);
|
|
114
125
|
win.show();
|
|
@@ -116,6 +127,14 @@ async function createWindow() {
|
|
|
116
127
|
win.on('closed', () => {
|
|
117
128
|
win = null;
|
|
118
129
|
});
|
|
130
|
+
// 自绘标题栏需要知道最大化状态来切换图标
|
|
131
|
+
const reportMaximized = () => {
|
|
132
|
+
if (!win) return;
|
|
133
|
+
win.webContents.send('banana:maximized', win.isMaximized());
|
|
134
|
+
};
|
|
135
|
+
win.on('maximize', reportMaximized);
|
|
136
|
+
win.on('unmaximize', reportMaximized);
|
|
137
|
+
win.webContents.on('did-finish-load', reportMaximized);
|
|
119
138
|
// 外部链接交给系统浏览器,窗口只负责本地界面
|
|
120
139
|
win.webContents.setWindowOpenHandler(({ url: target }) => {
|
|
121
140
|
shell.openExternal(target);
|
|
@@ -124,8 +143,36 @@ async function createWindow() {
|
|
|
124
143
|
if (process.argv.includes('--selftest')) await selfTest(url);
|
|
125
144
|
}
|
|
126
145
|
|
|
146
|
+
// 页面只能通过 preload 发三个固定动作;发送方必须就是主窗口。
|
|
147
|
+
ipcMain.on('banana:window', (event, action) => {
|
|
148
|
+
const target = BrowserWindow.fromWebContents(event.sender);
|
|
149
|
+
if (!target || target !== win) return;
|
|
150
|
+
if (action === 'minimize') target.minimize();
|
|
151
|
+
else if (action === 'toggle-maximize') {
|
|
152
|
+
if (target.isMaximized()) target.unmaximize();
|
|
153
|
+
else target.maximize();
|
|
154
|
+
} else if (action === 'close') target.close();
|
|
155
|
+
});
|
|
156
|
+
|
|
127
157
|
/** --selftest:不靠人眼看,直接在页面里断言界面渲染成功,然后退出。 */
|
|
128
158
|
async function selfTest(url) {
|
|
159
|
+
const report = (payload) => {
|
|
160
|
+
const line = 'SELFTEST ' + JSON.stringify(payload);
|
|
161
|
+
console.log(line);
|
|
162
|
+
// Electron 在 Windows 上是 GUI 子系统,stdout 常被丢掉;同时落一份文件方便自动化读取
|
|
163
|
+
for (const target of [
|
|
164
|
+
join(app.getPath('temp'), 'banana-desktop-selftest.json'),
|
|
165
|
+
join(__dirname, 'selftest.json'),
|
|
166
|
+
join(process.env.BANANA_HOME || __dirname, 'banana-desktop-selftest.json'),
|
|
167
|
+
]) {
|
|
168
|
+
try {
|
|
169
|
+
writeFileSync(target, line + '\n', 'utf8');
|
|
170
|
+
break;
|
|
171
|
+
} catch {
|
|
172
|
+
/* 换下一个位置 */
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
};
|
|
129
176
|
try {
|
|
130
177
|
const probe = await win.webContents.executeJavaScript(`(() => ({
|
|
131
178
|
title: document.title,
|
|
@@ -133,13 +180,16 @@ async function selfTest(url) {
|
|
|
133
180
|
sidebar: Boolean(document.querySelector('#sidebar')),
|
|
134
181
|
scriptLoaded: typeof window.fetch === 'function' && Boolean(document.querySelector('#input')),
|
|
135
182
|
theme: getComputedStyle(document.documentElement).getPropertyValue('--bg').trim(),
|
|
183
|
+
frameless: Boolean(window.bananaDesktop?.frameless),
|
|
184
|
+
winControls: !document.querySelector('#winControls')?.hidden,
|
|
185
|
+
customTitlebar: getComputedStyle(document.querySelector('.topbar')).webkitAppRegion,
|
|
136
186
|
}))()`);
|
|
137
187
|
const ok = probe.title === 'Banana' && probe.composed && probe.sidebar && probe.scriptLoaded;
|
|
138
|
-
|
|
188
|
+
report({ ok, url: url.replace(/\/t\/[a-f0-9]+\//, '/t/<token>/'), ...probe });
|
|
139
189
|
child?.kill();
|
|
140
190
|
app.exit(ok ? 0 : 1);
|
|
141
191
|
} catch (error) {
|
|
142
|
-
|
|
192
|
+
report({ ok: false, error: String(error?.message ?? error) });
|
|
143
193
|
child?.kill();
|
|
144
194
|
app.exit(1);
|
|
145
195
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// 桌面版预加载:只暴露窗口控制所需的最小接口。
|
|
2
|
+
// 页面拿不到 Node,也拿不到任意 ipcRenderer,只能发三个固定动作。
|
|
3
|
+
const { contextBridge, ipcRenderer } = require('electron');
|
|
4
|
+
|
|
5
|
+
const ACTIONS = new Set(['minimize', 'toggle-maximize', 'close']);
|
|
6
|
+
|
|
7
|
+
contextBridge.exposeInMainWorld('bananaDesktop', {
|
|
8
|
+
platform: process.platform,
|
|
9
|
+
frameless: process.platform !== 'darwin',
|
|
10
|
+
minimize: () => ipcRenderer.send('banana:window', 'minimize'),
|
|
11
|
+
toggleMaximize: () => ipcRenderer.send('banana:window', 'toggle-maximize'),
|
|
12
|
+
close: () => ipcRenderer.send('banana:window', 'close'),
|
|
13
|
+
onMaximizeChange: (callback) => {
|
|
14
|
+
if (typeof callback !== 'function') return () => {};
|
|
15
|
+
const handler = (_event, maximized) => callback(Boolean(maximized));
|
|
16
|
+
ipcRenderer.on('banana:maximized', handler);
|
|
17
|
+
return () => ipcRenderer.removeListener('banana:maximized', handler);
|
|
18
|
+
},
|
|
19
|
+
isAction: (action) => ACTIONS.has(action),
|
|
20
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vincentbanana/banana-gui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Banana 的图形界面:本地服务 + 浏览器。黑白主题,自绘图标,无第三方 UI 依赖",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"electron-builder": "^25.0.0"
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
|
-
"@vincentbanana/bananacli": "^0.4.
|
|
74
|
+
"@vincentbanana/bananacli": "^0.4.9"
|
|
75
75
|
},
|
|
76
76
|
"license": "MIT",
|
|
77
77
|
"publishConfig": {
|