"""Offline Playwright Chromium lab for jQuery EasyUI Ch 47: Custom Window Toolbar."""
import json
from playwright.sync_api import sync_playwright
HTML_DOC = r"""<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>EasyUI Custom Window Tools Offline Lab</title>
<style>
body { font-family: sans-serif; margin: 16px; font-size: 13px; }
.win-wrap { border: 1px solid #95b8e7; background: #e0ecff; padding: 4px; width: 220px; box-sizing: border-box; }
.win-wrap.closed { display: none; }
.win-wrap.maximized { width: 640px; }
.win-header { display: flex; justify-content: space-between; align-items: center; padding: 2px 4px; font-weight: bold; }
.win-tools { display: flex; gap: 4px; }
.win-tools button { padding: 0 5px; font-size: 11px; cursor: pointer; }
.win-body { background: #fff; border: 1px solid #95b8e7; padding: 10px; height: 100px; box-sizing: border-box; }
.win-body.collapsed { display: none; }
</style>
</head>
<body>
<div id="mount"></div>
<script>
const actionLog = [];
function createWindow(opts = {}) {
const cfg = {
title: "My Window",
collapsible: true,
minimizable: true,
maximizable: true,
closable: true,
tools: [],
...opts
};
const mount = document.getElementById("mount");
mount.innerHTML = "";
const wrap = document.createElement("div");
wrap.className = "win-wrap";
const header = document.createElement("div");
header.className = "win-header";
header.innerHTML = `<span class="panel-title">${cfg.title}</span><div class="win-tools"></div>`;
const toolsBar = header.querySelector(".win-tools");
const body = document.createElement("div");
body.className = "win-body";
body.textContent = "window content";
// Custom tools render before built-in panel tools, matching EasyUI layout
(cfg.tools || []).forEach(t => {
const btn = document.createElement("button");
btn.type = "button";
btn.className = "custom-tool " + t.iconCls;
btn.dataset.tool = t.iconCls;
btn.textContent = t.iconCls;
btn.addEventListener("click", () => t.handler());
toolsBar.appendChild(btn);
});
if (cfg.collapsible) {
const b = document.createElement("button");
b.type = "button";
b.dataset.tool = "panel-tool-collapse";
b.textContent = "^";
b.addEventListener("click", () => body.classList.toggle("collapsed"));
toolsBar.appendChild(b);
}
if (cfg.minimizable) {
const b = document.createElement("button");
b.type = "button";
b.dataset.tool = "panel-tool-min";
b.textContent = "_";
b.addEventListener("click", () => wrap.classList.add("closed"));
toolsBar.appendChild(b);
}
if (cfg.maximizable) {
const b = document.createElement("button");
b.type = "button";
b.dataset.tool = "panel-tool-max";
b.textContent = "[]";
b.addEventListener("click", () => wrap.classList.toggle("maximized"));
toolsBar.appendChild(b);
}
if (cfg.closable) {
const b = document.createElement("button");
b.type = "button";
b.dataset.tool = "panel-tool-close";
b.textContent = "x";
b.addEventListener("click", () => wrap.classList.add("closed"));
toolsBar.appendChild(b);
}
wrap.append(header, body);
mount.appendChild(wrap);
return {
getToolNames: () => Array.from(toolsBar.querySelectorAll("button")).map(b => b.dataset.tool),
isBodyVisible: () => getComputedStyle(body).display !== "none",
isWinVisible: () => getComputedStyle(wrap).display !== "none",
getWidth: () => Math.round(wrap.getBoundingClientRect().width)
};
}
window.lab = { createWindow, actionLog };
</script>
</body>
</html>
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page()
page.set_content(HTML_DOC)
# Case 1: Default window exposes all 4 built-in tools
page.evaluate("() => { window.w1 = window.lab.createWindow(); }")
tools_default = page.evaluate("() => window.w1.getToolNames()")
page.click('button[data-tool="panel-tool-collapse"]')
collapsed_vis = page.evaluate("() => window.w1.isBodyVisible()")
page.click('button[data-tool="panel-tool-collapse"]')
restored_vis = page.evaluate("() => window.w1.isBodyVisible()")
page.click('button[data-tool="panel-tool-max"]')
max_w = page.evaluate("() => window.w1.getWidth()")
res1 = {
"tools": tools_default,
"bodyVisibleWhenCollapsed": collapsed_vis,
"bodyVisibleWhenRestored": restored_vis,
"maximizedWidthPx": max_w,
}
exp1 = {
"tools": ["panel-tool-collapse", "panel-tool-min", "panel-tool-max", "panel-tool-close"],
"bodyVisibleWhenCollapsed": False,
"bodyVisibleWhenRestored": True,
"maximizedWidthPx": 640,
}
cases.append(dict(name="default_four_window_tools", expected=exp1, actual=res1, passed=(res1 == exp1)))
# Case 2: Disabling collapsible/minimizable/maximizable leaves only closable
page.evaluate("""() => {
window.w2 = window.lab.createWindow({ collapsible: false, minimizable: false, maximizable: false });
}""")
tools_only_close = page.evaluate("() => window.w2.getToolNames()")
page.click('button[data-tool="panel-tool-close"]')
visible_after_close = page.evaluate("() => window.w2.isWinVisible()")
res2 = {"tools": tools_only_close, "visibleAfterClose": visible_after_close}
exp2 = {"tools": ["panel-tool-close"], "visibleAfterClose": False}
cases.append(dict(name="closable_only_window_configuration", expected=exp2, actual=res2, passed=(res2 == exp2)))
# Case 3: Custom tools array with icon-add & icon-remove handlers
page.evaluate("""() => {
window.lab.actionLog.length = 0;
window.w3 = window.lab.createWindow({
collapsible: false,
minimizable: false,
maximizable: false,
tools: [
{ iconCls: "icon-add", handler: () => window.lab.actionLog.push("add") },
{ iconCls: "icon-remove", handler: () => window.lab.actionLog.push("remove") }
]
});
}""")
tools_custom = page.evaluate("() => window.w3.getToolNames()")
page.click('button[data-tool="icon-add"]')
page.click('button[data-tool="icon-remove"]')
actions = page.evaluate("() => window.lab.actionLog")
res3 = {"tools": tools_custom, "firedHandlers": actions}
exp3 = {
"tools": ["icon-add", "icon-remove", "panel-tool-close"],
"firedHandlers": ["add", "remove"],
}
cases.append(dict(name="custom_tools_array_handlers", expected=exp3, actual=res3, passed=(res3 == exp3)))
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/jeasyui-win-win2-lab/explore-win-win2.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-win-win2.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联68章) · 含当前章节或实例 · mbskycom202612a964.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1027.zip