bt_app 完整示例
bt_app 完整示例
纯本地 HTML 桌面应用
文件结构:
html-demo/ index.html
index.html:
<!doctype html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> </head> <body> <h1>HTML Demo</h1> </body> </html>
运行:
./bt_app.exe run
首次运行会生成默认 app.json。如果项目有 style.css、main.js 或图片等资源,并且需要打包分发,请创建自己的 app.json,把这些文件写入 resources。
本地 HTML 加 BT 函数
文件结构:
static-demo/ app.json index.html main.bt
app.json:
{ "app": { "name": "StaticDemo", "title": "Static Demo", "mode": "static", "entry": "index.html", "main": "main.bt" }, "window": { "width": 900, "height": 700, "resizable": true }, "dev": { "watch": true, "delay": 500, "devtools": true, "console": true }, "resources": [ "index.html", "main.bt" ], "exclude": [] }
main.bt:
fn add(args) { return { error:false, message:'ok', data:args.a + args.b } }
index.html:
<!doctype html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Static Demo</title> </head> <body> <button id="btn">计算</button> <pre id="out"></pre> <script> document.getElementById('btn').onclick = async () => { const result = await window.bt.call('add', { a: 20, b: 22 }); document.getElementById('out').textContent = JSON.stringify(result, null, 2); }; </script> </body> </html>
本地 BT 服务加 Web 页面
文件结构:
server-demo/ app.json server.bt main.bt www/ main.bt index.bt
app.json:
{ "app": { "name": "ServerDemo", "title": "BT Server Demo", "mode": "server", "entry": "http://127.0.0.1:18280", "main": "main.bt" }, "window": { "width": 900, "height": 700 }, "dev": { "watch": true, "delay": 500, "devtools": false, "console": true }, "resources": [ "server.bt", "main.bt", "www/**" ], "exclude": [] }
server.bt:
net.listen({ type:'web' bind:'127.0.0.1:18280' sites:[ { domains:['127.0.0.1'] root:'www/' entry:'main.bt' } ] })
www/main.bt:
include('www/index.bt')
www/static/index.bt:
# TPL 首页 <!doctype html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Server Demo</title> </head> <body> <h1>Server Demo</h1> </body> </html>
远程 Web 应用包装
app.json:
{ "app": { "name": "RemoteDemo", "title": "BT Remote Demo", "mode": "remote", "entry": "https://example.com", "main": false }, "window": { "width": 1100, "height": 760, "resizable": true }, "dev": { "watch": true, "delay": 500, "devtools": false, "console": false }, "resources": [], "exclude": [] }
带图标和窗口配置
app.json:
{ "app": { "name": "IconDemo", "title": "带图标的应用", "version": "1.0.0", "description": "BT 桌面图标示例", "copyright": "Copyright 2026 BT", "mode": "static", "entry": "index.html", "icon": "logo.ico", "main": false }, "window": { "width": 960, "height": 640, "resizable": true, "fullscreen": false, "hide_titlebar": false, "always_on_top": false }, "dev": { "watch": true, "delay": 500, "devtools": false, "console": false }, "resources": [ "index.html", "logo.ico", "assets/**" ], "exclude": [] }
当前 logo.ico 必须是项目内 .ico 文件。