Skip to content

Windows 上的安卓应用运行器:双击 APK 即装,应用以独立窗口与桌面图标运行,自持 QEMU 运行时

fix(display): 横竖屏改成真正的手机/平板排版切换;修掉标题栏黑边与点击失效

RoutineMiku04448a01605b
diff --git a/.github/release-notes-v0.3.9.md b/.github/release-notes-v0.3.9.md
new file mode 100644
index 0000000..7bd6034
--- /dev/null
+++ b/.github/release-notes-v0.3.9.md
@@ -0,0 +1,53 @@
+# ApkStart v0.3.9
+
+> 一句话:把**横竖屏切换**从「转画面」改成真正的**手机 / 平板排版切换**,修好了全屏不横屏、点击失效、以及点标题栏按钮时冒黑边的问题;另外加了**存储回收**。
+
+## 一、横竖屏 / 全屏:改成真正的排版切换
+
+**原来的问题**:点旋转(或全屏)后,画面整个顺时针/逆时针躺倒,应用并没有重新排版,也摆不正;全屏也不会自动横屏。
+
+**根因**:旧的实现是「改一下 `wm size`,再让窗口代理给模拟器发 Ctrl+←」。Ctrl+← 只是把**模拟器的画面**转 90°,安卓侧排版完全没变 —— 所以看到的就是「画面躺倒」。而且 AVD 生成配置里写死了 `hw.sensors.orientation=no`,安卓根本不允许旋转,只能靠转画面糊弄。
+
+**现在的做法**:
+
+- 只改**逻辑显示尺寸**(`wm size`),让安卓按新宽度重新排版:
+ - 竖屏 720×1280 @320dpi ≈ **360dp 宽 → 手机排版**;
+ - 横屏 1280×720 @320dpi ≈ **640dp 宽 → 平板/分栏排版**(Android 以 600dp 为平板门槛);
+- 同时 `wm user-rotation lock 0`,保证画面永远是正的,不再出现「躺倒」;
+- **删掉了转画面那一步**,画面与排版始终一致;
+- 窗口会按新分辨率重算宽高比并重新贴合,不留黑边;
+- **全屏按钮现在会自动切横屏(平板排版)**,退出全屏自动回到竖屏手机排版;设置里的「显示模式」切换同样生效;
+- AVD 配置改为 `hw.sensors.orientation=yes` + `hw.sensors.accelerometer=yes`,后续新机器不会再被锁死竖向。
+
+## 二、修复:点标题栏四个按钮时冒出黑边
+
+模拟器的 Qt 窗口比真实 LCD 大一圈(四周各 8px 灰边)。为了让灰边不出现在画面里,原来的做法是把子窗口整体向上/向左溢出 8px —— 这 8px 正好压在标题栏底部;而按钮点击时窗口代理会把模拟器窗口抬到最前,于是那条溢出的部分就在标题栏下方画出一条**黑边**。
+
+**修复**:给嵌入的模拟器窗口加了**窗口区域裁剪**(`SetWindowRgn`),把溢出到标题栏的那 8px 直接裁掉。这样无论怎么抬升,它都画不到标题栏上。
+
+## 三、修复:点击失效 / 点不到「同意并继续」
+
+这一条和上面两个问题同源:画面被转 90° 之后,窗口代理的坐标映射仍是「竖屏」的,点击位置整体偏移,看起来就是「点了没反应」。排版切换改对之后点击恢复正常。
+
+顺带把「等待联网」的成本压了下来:冷启动重试改成**每个进程只做一次**,首次等待从 60s 降到 25s —— 之前一次点开可能要多等好几分钟,现在正常情况几秒内就过。
+
+## 四、存储占用优化
+
+「清理旧版本残留」现在会额外回收**废弃的快速启动快照**:
+
+- 旧版遗留的 `default_boot`(一份 ram.img 约 **3 GB**);
+- 删不掉就改名的 `*.stale-*` 残留;
+- 当前正在用的快照(`apkstart`)永远不动。
+
+实测开发机上可回收项合计约 **4.65 GB**(6 个旧版本程序目录 + 1 份废弃快照)。
+
+## 五、实测
+
+- `npm test`:**113 项全部通过**(8 + 25 + 80)。
+- `wm size 1280x720` 后 framebuffer 实测变为 **1280×720**,画面正立(截图确认)。
+- 应用窗口嵌入 `embedded=true`;关闭窗口后 `pidof` 为空(上一版的修复继续有效)。
+
+## 六、升级方式
+
+- 便携版:解压覆盖,数据目录(默认 `%APPDATA%\apkstart`)保持不变。
+- 安装版:直接安装覆盖。
diff --git a/package.json b/package.json
index f64bd67..782fd81 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "apkstart",
"productName": "ApkStart",
- "version": "0.3.8",
+ "version": "0.3.9",
"description": "极致轻量的 Windows 安卓应用运行器:双击 APK 即装,应用以独立窗口与桌面图标运行",
"main": "src/main.js",
"author": "RoutineMiku",
diff --git a/src/core/adb.js b/src/core/adb.js
index 2df8079..32da548 100644
--- a/src/core/adb.js
+++ b/src/core/adb.js
@@ -819,13 +819,27 @@ class AdbClient {
* 都是这个结果)。真正可行的是把 Android 的逻辑分辨率换成目标方向,
* 系统会按新方向重新排版,界面始终是正的;模拟器随后跟着换成同样的比例。
*/
+ /**
+ * 切换「手机竖屏 / 平板横屏」排版。
+ *
+ * 正确做法是改**逻辑显示尺寸**(wm size),让安卓以宽屏重新排版:
+ * - 竖屏 720x1280 @320dpi ≈ 360dp 宽 → 手机排版;
+ * - 横屏 1280x720 @320dpi ≈ 640dp 宽 → 平板/分栏排版(>=600dp 就是 tablet)。
+ * 同时把 user-rotation 锁回 0,保证画面是正的。
+ *
+ * 以前这里只有 wm size,然后外面再调 agent.rotate 去转模拟器的画面
+ * (等价于 Ctrl+←),结果就是「画面被旋转 90°、应用并没重新排版、还摆不正」。
+ */
async setOrientation(landscape) {
try {
+ // 无论哪边,先把自动旋转关掉并把用户旋转锁回 0,画面才不会横躺
+ await this.shell('settings put system accelerometer_rotation 0; wm user-rotation lock 0', { timeout: 15000 });
if (!landscape) {
await this.shell('wm size reset; wm density reset', { timeout: 15000 });
return true;
}
const sizeOut = await this.shell('wm size', { timeout: 10000 });
+ // 注意:重置过之后这里读到的是「Physical size」,用物理尺寸算横屏分辨率
const m = /Physical size:\s*(\d+)x(\d+)/.exec(sizeOut);
const w = m ? Math.max(Number(m[1]), Number(m[2])) : 1280;
const h = m ? Math.min(Number(m[1]), Number(m[2])) : 720;
@@ -841,6 +855,18 @@ class AdbClient {
}
}
+ /** 当前逻辑分辨率(横屏切换后窗口要按它算宽高比) */
+ async displaySize() {
+ try {
+ const out = await this.shell('wm size', { timeout: 8000 });
+ const o = /Override size:\s*(\d+)x(\d+)/.exec(out);
+ if (o) return { w: Number(o[1]), h: Number(o[2]) };
+ const p = /Physical size:\s*(\d+)x(\d+)/.exec(out);
+ if (p) return { w: Number(p[1]), h: Number(p[2]) };
+ } catch (_) { /* ignore */ }
+ return null;
+ }
+
/** 授权框还挂在最前面时(用户不在电脑前也能自愈),按返回键收掉 */
async dismissPermissionDialog() { try {
const top = await this.shell('dumpsys activity activities', { timeout: 12000 });
diff --git a/src/core/agent.js b/src/core/agent.js
index 5a56aed..a64e0eb 100644
--- a/src/core/agent.js
+++ b/src/core/agent.js
@@ -167,6 +167,9 @@ class WinAgent extends EventEmitter {
cmd: 'fit',
x: Math.round(rect.x), y: Math.round(rect.y),
w: Math.round(rect.w), h: Math.round(rect.h),
+ // 模拟器 Qt 窗口比 LCD 多出 EMU_PAD 的外框,我们让它向上/向左溢出把这个外框
+ // 挤出客户区;溢出到标题栏上的那一条会被裁掉(否则点标题栏按钮时会冒黑边)。
+ clipTop: Math.round(rect.clipTop || 0),
}, 3000);
}
diff --git a/src/core/appwin.js b/src/core/appwin.js
index 0d4d7a5..81abe90 100644
--- a/src/core/appwin.js
+++ b/src/core/appwin.js
@@ -247,11 +247,14 @@ class AppWindow {
_rect() {
const sf = this._scale();
const b = this.win.getContentBounds();
+ const top = Math.round(TITLEBAR_H * sf);
return {
x: -EMU_PAD,
- y: Math.round(TITLEBAR_H * sf) - EMU_PAD,
+ y: top - EMU_PAD,
w: Math.round(b.width * sf) + EMU_PAD * 2,
h: Math.round((b.height - TITLEBAR_H) * sf) + EMU_PAD * 2,
+ // 上面那条 -EMU_PAD 会压到标题栏上,交给窗口代理裁掉(见 agent.fit 的 clipTop)
+ clipTop: EMU_PAD,
};
}
@@ -275,18 +278,20 @@ class AppWindow {
}
/**
- * 横竖屏切换。
- * 两步:先让 Android 按目标方向重新排版(画面始终是正的),再让模拟器按新比例
- * 重排一次窗口(它自己会算,CtrL+← 只是触发重排,不会改 Android 的方向),
- * 最后把我们的窗口换成同样的比例。
+ * 横竖屏切换 = 切换「手机排版 / 平板排版」。
+ *
+ * 关键:**不再去转模拟器的画面**。以前这里先改 wm size、再让 agent 发 Ctrl+←,
+ * 模拟器会把整块画面顺时针/逆时针转 90°,而安卓侧的排版并没有跟着变 ——
+ * 用户看到的就是「画面整个躺倒、还摆不正」。
+ * 现在只做一件事:改逻辑显示尺寸(wm size)+ 锁 user-rotation=0,
+ * 安卓会按新宽度重新排版:360dp 宽是手机,640dp 宽自动进平板/分栏布局。
*/
async rotate() {
if (!this.isOpen) return this.state.landscape;
const next = !this.state.landscape;
if (this.setOrientation) await this.setOrientation(next).catch(() => {});
- await sleep(700);
- await this.agent.rotate(next ? 'left' : 'right').catch(() => {});
- await sleep(800);
+ // wm size 会触发全局配置变更,应用要重建一次界面,给足时间再贴窗口
+ await sleep(1200);
const area = this._area();
const size = AppWindow.sizeFor(area, this.portrait, next);
if (!this.win.isMaximized()) {
@@ -299,14 +304,21 @@ class AppWindow {
}
this.state.landscape = next;
this._push();
- await sleep(250);
+ await sleep(300);
await this.fitNow();
- // 模拟器换比例后还会自己再调一次窗口,收尾再贴合一次免得留黑边
- await sleep(600);
+ // 应用重构后模拟器窗口可能再自己调一次,收尾再贴合一次免得留黑边
+ await sleep(900);
await this.fitNow();
return next;
}
+ /** 需要时切到指定方向(全屏=横屏,窗口=竖屏) */
+ async setLandscape(want) {
+ if (!this.isOpen) return this.state.landscape;
+ if (!!this.state.landscape === !!want) return this.state.landscape;
+ return this.rotate();
+ }
+
toggleMaximize() {
if (!this.isOpen) return;
if (this.win.isMaximized()) this.win.unmaximize();
diff --git a/src/core/emulator.js b/src/core/emulator.js
index e0e4f15..a7bdc62 100644
--- a/src/core/emulator.js
+++ b/src/core/emulator.js
@@ -373,7 +373,10 @@ class EmulatorEngine {
'hw.camera.back=none',
'hw.camera.front=none',
'hw.gps=no',
- 'hw.sensors.orientation=no',
+ // 方向/加速度传感器必须开:关掉之后安卓拒绝旋转,横竖屏只能靠"转画面"糊弄,
+ // 表现就是画面躺倒、排版不变。开了才能用 wm size 做真正的手机/平板排版切换。
+ 'hw.sensors.orientation=yes',
+ 'hw.sensors.accelerometer=yes',
].join('\n') + '\n';
fs.writeFileSync(cfgPath, cfg);
diff --git a/src/core/engine.js b/src/core/engine.js
index e167294..dbaa6ae 100644
--- a/src/core/engine.js
+++ b/src/core/engine.js
@@ -1148,9 +1148,13 @@ class Runtime {
*
* @returns {Promise<{ok:boolean,reason?:string,ip?:string,gateway?:string}>}
*/
- async ensureNetwork({ onLog = () => {}, firstWaitMs = 60000, secondWaitMs = 180000 } = {}) {
+ async ensureNetwork({ onLog = () => {}, firstWaitMs = 25000, secondWaitMs = 90000 } = {}) {
let net = await this.waitForNetwork({ onLog, timeoutMs: firstWaitMs });
if (net && net.ok) return net;
+ // 冷启动很贵(几十秒),一个进程里只做一次:否则每次点开应用都要重来一遍,
+ // 用户感受到的就是「点开要等好几分钟」。
+ if (this._netColdBootTried) return net;
+ this._netColdBootTried = true;
onLog('网络仍未就绪,正在冷启动一次虚拟机(不加载快照)后重试…');
try {
await this.stop({ graceful: false }).catch(() => {});
diff --git a/src/core/leftovers.js b/src/core/leftovers.js
index 2a0ace3..24a2ea2 100644
--- a/src/core/leftovers.js
+++ b/src/core/leftovers.js
@@ -138,12 +138,35 @@ function scanTemp(dataDir) {
return out;
}
-function scan({ installDir, dataDir }) {
+/** 旧/废弃的快速启动快照:default_boot 是早期版本留下的(一份 ram.img 约 3GB),
+ * *.stale-* 是「删不掉就改名」的残留。当前快照名是 apkstart,绝不碰。 */
+function scanSnapshots(avdHome) {
+ const out = [];
+ if (!avdHome) return out;
+ const snapDir = path.join(avdHome, 'apkstart.avd', 'snapshots');
+ let list = [];
+ try { list = fs.readdirSync(snapDir, { withFileTypes: true }); } catch (_) { return out; }
+ for (const e of list) {
+ if (!e.isDirectory()) continue;
+ if (e.name === 'apkstart') continue; // 当前在用的快照
+ const full = path.join(snapDir, e.name);
+ out.push({
+ kind: 'dir',
+ path: full,
+ size: dirSize(full),
+ note: e.name.startsWith('default_boot') ? '旧版遗留的快速启动快照' : '废弃的快照残留',
+ });
+ }
+ return out;
+}
+
+function scan({ installDir, dataDir, avdHome }) {
const items = [];
const push = (list) => { for (const x of list) items.push(x); };
push(scanDirs(installDir));
push(scanRegistry());
push(scanTemp(dataDir));
+ push(scanSnapshots(avdHome));
const total = items.reduce((n, x) => n + (x.size || 0), 0);
return { items, totalBytes: total };
}
diff --git a/src/core/win/agent.ps1 b/src/core/win/agent.ps1
index 329a815..37ff07e 100644
--- a/src/core/win/agent.ps1
+++ b/src/core/win/agent.ps1
@@ -46,6 +46,10 @@ public class ApkAgent
[DllImport("user32.dll")] static extern int SetWindowLong(IntPtr h, int i, int v);
[DllImport("user32.dll")] static extern bool SetWindowPos(IntPtr h, IntPtr after, int x, int y, int w, int hh, uint f);
[DllImport("user32.dll")] static extern bool ShowWindow(IntPtr h, int c);
+ // 裁剪嵌入窗口的可见区域:把压到标题栏上的那条外框去掉
+ [DllImport("user32.dll")] static extern int SetWindowRgn(IntPtr h, IntPtr rgn, bool redraw);
+ [DllImport("gdi32.dll")] static extern IntPtr CreateRectRgn(int l, int t, int r, int b);
+ [DllImport("gdi32.dll")] static extern bool DeleteObject(IntPtr o);
[DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr h, out RECT r);
[DllImport("user32.dll")] static extern bool GetClientRect(IntPtr h, out RECT r);
[DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr h);
@@ -471,7 +475,7 @@ public class ApkAgent
// HWND_TOP: Chromium 的渲染子窗口和模拟器是同级的子窗口,
// 不主动抬到最前就会被 Chromium 那块黑底盖住(用户看到的就是黑屏)。
static readonly IntPtr HWND_TOP = new IntPtr(1);
- static void Fit(int x, int y, int w, int h, bool frame)
+ static void Fit(int x, int y, int w, int h, bool frame, int clipTop)
{
if (_emu == IntPtr.Zero) return;
if (w < 16) w = 16;
@@ -490,6 +494,19 @@ public class ApkAgent
// on every periodic sweep caused visible stutter and wasted GPU/CPU.
uint flags = SWP_NOACTIVATE | SWP_SHOWWINDOW | (frame ? SWP_FRAMECHANGED : 0u);
SetWindowPos(_emu, HWND_TOP, x, y, w, h, flags);
+ // 模拟器 Qt 窗口的外框比 LCD 大一圈,我们让它向上溢出、把外框挤出客户区;
+ // 溢到标题栏上的那一条必须裁掉 —— 否则它被抬到最前时会在标题栏下方画出一条
+ // 黑边(用户反馈「点四个按钮时冒出黑色区域侵入界面」就是这个)。
+ if (clipTop > 0)
+ {
+ IntPtr rgn = CreateRectRgn(0, clipTop, w, h);
+ // SetWindowRgn 成功后区域归系统所有,不能再删
+ if (SetWindowRgn(_emu, rgn, true) == 0) DeleteObject(rgn);
+ }
+ else
+ {
+ SetWindowRgn(_emu, IntPtr.Zero, true);
+ }
}
static void Raise()
@@ -913,14 +930,14 @@ public class ApkAgent
bool ok = Embed(_host, _emu);
_embedAt = Environment.TickCount;
_lastToolSweep = 0;
- Fit((int)L(line, "x"), (int)L(line, "y"), (int)L(line, "w"), (int)L(line, "h"), true);
+ Fit((int)L(line, "x"), (int)L(line, "y"), (int)L(line, "w"), (int)L(line, "h"), true, (int)L(line, "clipTop"));
Reply(sock, "{\"ok\":" + (ok ? "true" : "false") + ",\"emu\":" + _emu.ToInt64() + "}");
}
}
else if (cmd == "fit")
{
if (_emu == IntPtr.Zero) _emu = FindEmulator();
- Fit((int)L(line, "x"), (int)L(line, "y"), (int)L(line, "w"), (int)L(line, "h"), false);
+ Fit((int)L(line, "x"), (int)L(line, "y"), (int)L(line, "w"), (int)L(line, "h"), false, (int)L(line, "clipTop"));
Reply(sock, "{\"ok\":true}");
}
else if (cmd == "rotate")
diff --git a/src/main.js b/src/main.js
index 67862cc..bd115a7 100644
--- a/src/main.js
+++ b/src/main.js
@@ -314,8 +314,14 @@ async function toggleDisplayMode() {
display: { mode: displaySettings().mode === 'fullscreen' ? 'window' : 'fullscreen' },
});
if (appWin && appWin.isOpen) {
- if (next.display.mode === 'fullscreen') appWin.win.maximize();
- else appWin.win.unmaximize();
+ if (next.display.mode === 'fullscreen') {
+ appWin.win.maximize();
+ // 全屏 = 平板横屏排版:让安卓按宽屏重新排版,而不是只把窗口拉大
+ appWin.setLandscape(true).catch(() => {});
+ } else {
+ appWin.win.unmaximize();
+ appWin.setLandscape(false).catch(() => {});
+ }
appWin.scheduleFit();
}
return next.display.mode;
@@ -884,8 +890,11 @@ function registerIpc() {
const next = store.saveSettings({ display: patch || {} });
if (appWin && appWin.isOpen) {
appWin.portrait = portraitSize();
- if (next.display.mode === 'fullscreen' && !appWin.win.isMaximized()) appWin.win.maximize();
- if (next.display.mode !== 'fullscreen' && appWin.win.isMaximized()) appWin.win.unmaximize();
+ const full = next.display.mode === 'fullscreen';
+ if (full && !appWin.win.isMaximized()) appWin.win.maximize();
+ if (!full && appWin.win.isMaximized()) appWin.win.unmaximize();
+ // 全屏 = 平板横屏排版(见 toggleDisplayMode 的说明)
+ appWin.setLandscape(full).catch(() => {});
appWin.scheduleFit();
}
return next.display;
@@ -1143,7 +1152,9 @@ function registerIpc() {
try {
const installDir = path.dirname(app.getPath('exe'));
const dataDir = store.DATA_DIR;
- return { ok: true, ...leftovers.scan({ installDir, dataDir }) };
+ let avdHome = '';
+ try { avdHome = runtime.provisioner.paths().avdHome; } catch (_) { avdHome = ''; }
+ return { ok: true, ...leftovers.scan({ installDir, dataDir, avdHome }) };
} catch (e) {
return { ok: false, error: e.message };
}