fix(android): keep scaled layouts within the screen

This commit is contained in:
Yuri Karamian committed 2026-11-10 11:09:01 +04:00
1 parent 338db1e7b0
commit fbabcf5099
3 files changed
+107 -6

No files matched your search

+8
View File
@@ -3881,6 +3881,8 @@
}
.settings-panel.mobile .font-size-options {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(85px, 1fr));
gap: 8px;
}
@@ -3888,6 +3890,12 @@
padding: 12px 6px;
}
@media (max-width: 480px) {
.settings-panel.mobile .two-col-sections {
grid-template-columns: minmax(0, 1fr);
}
}
/* ── Custom Themes ── */
.section-header-row {
display: flex;
+21 -6
View File
@@ -34,14 +34,29 @@ export const uiScalePresets: { label: string; value: number }[] = isAndroid
// positioned from clientX/getBoundingClientRect. Pinning the viewport scale instead shrinks the
// layout viewport, like the system "Display size" setting, so all coordinates stay in CSS px.
// Needs useWideViewPort, which MainActivity enables.
function applyAndroidViewportScale(scale: number) {
let androidUiScale = defaultUiScale;
let androidResizeListenerInstalled = false;
function updateAndroidViewportScale() {
const meta = document.querySelector('meta[name="viewport"]');
if (!meta) return;
scale = Math.min(scale, maxAndroidUiScale);
meta.setAttribute(
'content',
`initial-scale=${scale}, minimum-scale=${scale}, maximum-scale=${scale}, user-scalable=no, viewport-fit=cover`
);
const viewport = window.visualViewport;
// WebView can keep its 980px fallback layout if only a scale is specified. Give it the
// actual visible width in CSS px, including in landscape and split-screen windows.
const availableWidth = Math.round(viewport ? viewport.width * viewport.scale : screen.width);
const scale = Math.min(androidUiScale, maxAndroidUiScale, Math.max(1, availableWidth / minAndroidLayoutWidth));
const width = Math.floor(availableWidth / scale);
const content = `width=${width}, initial-scale=${scale}, minimum-scale=${scale}, maximum-scale=${scale}, user-scalable=no, viewport-fit=cover`;
if (meta.getAttribute('content') !== content) meta.setAttribute('content', content);
}
function applyAndroidViewportScale(scale: number) {
androidUiScale = scale;
updateAndroidViewportScale();
if (!androidResizeListenerInstalled) {
window.addEventListener('resize', updateAndroidViewportScale);
androidResizeListenerInstalled = true;
}
}
export async function applyUiScale(scale: number) {
+78
View File
@@ -0,0 +1,78 @@
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import test from 'node:test';
import { createContext, Script } from 'node:vm';
import ts from 'typescript';
const source = await readFile(new URL('../src/lib/ui-scale.ts', import.meta.url), 'utf8');
const script = new Script(ts.transpileModule(source.replace(/^import .*;\n/gm, ''), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 }
}).outputText);
function setup(android = true) {
let content = 'width=device-width, initial-scale=1';
let writes = 0;
const listeners = [];
const zooms = [];
const viewport = { width: 411, scale: 1 };
const context = createContext({
exports: {}, isAndroid: android, console,
screen: { width: 411, height: 850 },
window: { visualViewport: viewport, addEventListener: (event, handler) => listeners.push({ event, handler }) },
document: { querySelector: () => ({
getAttribute: () => content,
setAttribute: (_name, value) => { content = value; writes++; }
}) },
getCurrentWebview: () => ({ setZoom: async (scale) => zooms.push(scale) })
});
script.runInContext(context);
return {
apply: context.exports.applyUiScale, viewport, listeners, zooms,
get writes() { return writes; },
get values() { return Object.fromEntries(content.split(',').map((part) => part.trim().split('='))); }
};
}
test('Android scales the layout to the visible phone width, keeping tablet layout out', async () => {
const app = setup();
for (const scale of [1, 1.15, 1.25, 1.4]) {
await app.apply(scale);
const width = Number(app.values.width);
assert.ok(width <= 411 / scale, 'the layout must fit inside the scaled visible area');
assert.ok(width >= 290, 'mobile menus must still fit');
assert.ok(width < 900, 'a phone must not acquire the tablet breakpoint');
assert.equal(Number(app.values['initial-scale']), scale);
}
});
test('Android adapts to rotation and a narrower app window without multiplying the saved scale', async () => {
const app = setup();
await app.apply(1.25);
app.viewport.width = 850 / 1.25;
app.viewport.scale = 1.25;
app.listeners[0].handler();
assert.equal(Number(app.values.width), 680);
app.viewport.width = 360 / 1.25;
app.listeners[0].handler();
assert.equal(Number(app.values.width), 290);
assert.ok(Number(app.values.width) * Number(app.values['initial-scale']) <= 360);
});
test('reapplying a scale does not duplicate resize listeners or rewrite the same viewport', async () => {
const app = setup();
await app.apply(1.15);
await app.apply(1.15);
assert.equal(app.listeners.length, 1);
assert.equal(app.listeners[0].event, 'resize');
assert.equal(app.writes, 1);
app.listeners[0].handler();
assert.equal(app.writes, 1);
});
test('desktop keeps using native WebView zoom without changing the viewport', async () => {
const app = setup(false);
await app.apply(1.5);
assert.deepEqual(app.zooms, [1.5]);
assert.equal(app.writes, 0);
assert.equal(app.listeners.length, 0);
});