fix(web): keep the IME bar still and the pointer honest in portrait
Two separate faults showed up once the "I" shortcut was in use in portrait: the
phone input bar would not stay put, and the pointer stopped being where it
claimed to be.
#ime-bar was the last flex child of #screen-page, so it sat at the bottom of the
page, and the only thing keeping it above the keyboard was adjustImeBar()
writing padding-bottom on #screen-page -- the bar chased the keyboard height
frame by frame through visualViewport. The keyboard animation is asynchronous
and the events lag behind it, so while it rose the bar was still low with the
keyboard already covering everything from y=544. A later attempt clamped it
against keyboardTopY() and reserved the last measured keyboard height before the
keyboard arrived. That kept it visible, but the bar still moved whenever the
keyboard changed height -- switching input method, the QuickType row, the
AutoFill Contact chip appearing or going away -- and that is what "the input box
keeps getting pushed up" actually was.
The bar is now a fixed overlay at the bottom edge of the picture, placed by the
same formula the shortcut row already used: picture bottom + 4px. In portrait
adjustImeBar() has nothing left to chase -- it only clears the padding that
landscape may have left behind, and recomputes when the keyboard top changes.
That recompute is idempotent and the style is only written when the value
differs, so in the normal case it produces no movement at all. lastKbHeight,
KB_HEIGHT_GUESS, noteKeyboardHeight() and the old keyboard-top tracking are gone
with it.
One clamp survives, as a safety net: if the picture bottom is itself inside the
keyboard block -- a remote machine that is portrait, where the picture is taller
than the space above the keyboard -- the bar falls back to the keyboard top,
because there is nowhere else to put it. That case also has to follow the
keyboard, which is why the recompute above still exists. For a landscape/16:9
remote the clamp never fires and the position is a constant.
Getting that constant right needed a coordinate fix. #ime-bar hangs off
#screen-page while #input-shortcuts lives inside .canvas-container, so the two
have different containing blocks. Reusing the shortcut formula
(canvas.offsetTop + ...) put the bar a whole toolbar height too high -- 279
against a picture that ends at 319 -- which is why it always looked like it was
floating above the shortcut row. The bar's position now comes from the canvas
rect minus the #screen-page rect, so the containing block is whatever the bar
actually resolves against; that subtraction also cancels the translateY
compensation, since both rects are inside the translated element. It lands at
323..382, 4px under the picture.
Landscape keeps the flex behaviour via (orientation: landscape). That is not
just tidiness: an absolutely positioned child does not take padding into
account, because bottom: 0 resolves against the padding box, which includes the
padding. Left as an overlay, the bar would have stayed at the very bottom of the
screen and been covered by the keyboard instead of being pushed up by
padding-bottom.
Opening the keyboard must not move the picture either, and two things were
moving it:
- #screen-page had no overflow guard. #term-page already carries one for exactly
this reason ("stop iOS from quietly adding a scroll to body and lifting the
whole page"). Without it iOS hands the page a scroll and then scrolls it once
the keyboard comes up.
- Even with the page locked, iOS moves the *visual* viewport up when the keyboard
appears (visualViewport.offsetTop > 0). #screen-page is position: fixed and is
therefore pinned to the layout viewport, so the entire page -- picture
included -- was pushed off the top of the screen.
So #screen-page gets overflow: hidden + overscroll-behavior: contain, and
syncVisualOffset() translates it by exactly visualViewport.offsetTop to cancel
the shift. It is driven by the IME rAF loop and the viewport scroll event, and
clears the transform once the offset returns to 0. Nothing downstream has to
compensate for it: the picture, the pointer overlay and the bar are all
descendants of #screen-page, so they move with it.
The pointer overlay was the one thing that did not accept that. .cursor-overlay
was position: fixed, placed by taking the canvas's getBoundingClientRect() and
then subtracting whatever translateY the page currently carried -- two
independent sources of truth that have to agree exactly, the browser's idea of
where the canvas is and our own idea of how far we moved it. They do agree in
desktop Chrome, which is why the harness never saw the fault, but on the phone
they did not: once the IME was open the overlay was drawn off by the
compensation amount while taps still landed correctly, and closing the input box
-- offsetTop back to 0, transform gone -- put it back. The overlay is now
position: absolute inside .canvas-container, the same containing block the canvas
resolves against, and style.left/top hold canvas-local CSS pixels (bx/kx, by/ky).
No viewport rect, no compensation term: whatever an ancestor does to the picture,
it does to the pointer. pageShiftY() is gone with it.
The overlay also has to follow a re-fit, not just a move. resizeScreenCanvas()
keeps the last remote coordinate the overlay was placed at and replays it once
the bitmap has been resized, so the keyboard shrinking the layout viewport, a
rotation or a fullscreen toggle no longer leaves the pointer where the previous
geometry put it.
The touch mapping and the drawing path have to stay exact inverses --
positionCursorOverlay() turns remote coordinates into a position on the canvas,
screenToCanvas() turns a finger position back into remote coordinates -- otherwise
the cursor is drawn where nobody tapped. It was only after the keyboard had been
up for a while that this broke: switching the input method changes the keyboard
height, iOS pans the visual viewport to keep the focused textarea visible, and
from then on offsetTop is non-zero.
iOS also stacks a row of its own directly above the keyboard: the QuickType
predictions and, when Safari decides the field looks like it can take contact
info, an "AutoFill Contact" chip. The textarea gets the full set of suppression
attributes (autocomplete, autocorrect and autocapitalize off, spellcheck off,
inputmode text). That lowers the odds of the chip showing up at all, but iOS
does not promise to honour it -- the only guaranteed off switch is Settings >
Safari > Autofill > Use Contact Info, which is a per-device setting.
With the bar no longer able to hide under the keyboard, the shortcut row below
the picture is only in the way: that row exists to be tapped *before* the IME
opens, and while the IME is open the bar takes its place. Opening the IME now
hides it, so the bar sits 4px under the picture instead of queuing up below the
row. The class lives on #screen-page rather than being stripped off .visible,
because updateUIForOrientation() recomputes .visible from keyboardOpen and would
put the row straight back. Closing the IME -- the bar's own collapse button does
that -- or disconnecting restores it. The key buttons it holds stay reachable by
reopening the mobile keyboard, which is what showed the row in the first place.
The bar also has to exist only while the keyboard does. Tapping it is what wakes
the keyboard in the first place, and iOS then scrolls the page to keep the
focused field visible -- which is the "tap the input box and it shoves itself up"
report. updateImePresence() runs in the IME rAF loop and puts .ime-waiting on the
bar the moment the keyboard is not up -- opacity 0, so it is not there to touch
-- and closeIme() 450ms later once the keyboard has really gone. Two deliberate
choices in that CSS: it is opacity, not display or visibility, because the
textarea has to stay in the document and focusable or the keyboard never comes
up at all; and there is no pointer-events: none, because that would let touches
on that strip fall through to the canvas and become stray clicks on the remote
machine. The wait for the keyboard to appear is capped at 3000ms, after which the
IME is abandoned rather than left invisible forever. It is generous on purpose:
iOS sometimes does not update visualViewport until something scrolls, and a short
timeout would close a perfectly good IME on a slow device.
The RDP reset button redraws the whole frame, but it left the IME open. The bar
stayed on screen, so touching its textarea brought the keyboard back up -- and the
page got shoved again, with the pointer going with it. Reset now closes the IME
first. Since "close the IME" was being spelled out in three places (the I button,
the keyboard toggle, disconnect) and each copy had drifted a little, it is now one
closeIme() that toggleIme(), toggleKeyboard(), sendRdpReset() and disconnect()
all call. The cleanup half runs unconditionally -- imeMode and the DOM can fall out
of sync and a half-closed state is exactly what leaves a bar that pops the
keyboard -- while updateKeyboardButtons(false) is guarded by whether the IME was
actually open, so resetting with only the mobile keyboard up does not close it.
disconnect() additionally keeps its own updateKeyboardButtons(false), because
there the keyboard should close regardless, and it no longer needs to reset
imeMode by hand -- which also stops imeWatcher's rAF loop from spinning on after
a disconnect.
Deselecting the mouse (the control toggle) now drops the keyboard and the IME as
well, for the same reason: leaving the bar on screen means the next touch on its
textarea brings the keyboard back up and shoves the page. Closing only the IME
would not be enough here -- the mobile keyboard is a separate focus -- so
closeKeyboardAndIme() blurs both and is what the control toggle and disconnect()
now share.
Verified with visualViewport.height mocked so the keyboard arrives after the bar
has been placed, on 390x844 and 375x667, with a 16:9 and a portrait remote, at
keyboard heights 0/216/260/300/304/344. For the 16:9 remote the bar is 323..382
at every single step -- zero jump across the whole sweep, where the portrait
remote used to jump 300px. For a portrait remote it tracks the keyboard top
(485..544 at 300px), the only position available to it. No case leaves the bar
under the keyboard, and every measured position equals min(picture bottom + 4,
keyboard top - bar height) to 0.0px.
The bar is present exactly when the keyboard is, checked at every step: on tap
(before the keyboard rises) opacity 0; keyboard up, opacity 1; keyboard down 60ms,
opacity 0 again; 580ms, imeMode false and display none. Reopening with the keyboard
never arriving keeps it at opacity 0 and gives up after 3s. No page errors.
Picture placement across all five layouts is byte-identical to before the change.
Cursor accuracy is dx=dy=0 over portrait/landscape x scale 1/2/3 x offsetTop
0/150/200/300, and the round-trip test -- positionCursorOverlay() asked where to
draw, that screen position fed back through screenToCanvas() -- returns the
original remote point for all nine cases; before the inverse fix six of them were
off, by exactly offsetTop scaled into remote pixels (739 at offsetTop 150 for the
(1800,1000) point).
Tapping RDP reset with the IME open (keyboard mocked at 544, visualViewport
offsetTop at 200) leaves imeMode false, the bar display:none, the transform
cleared, visualOffsetY 0 and the watcher stopped, with the picture still at
y=100..319.4 instead of being shoved up; disconnect() from the same state clears
it identically. Deselecting the mouse from that same state (control on, mobile
keyboard focused, IME open, keyboard mocked at 544 and offsetTop at 180) leaves
controlEnabled false, imeMode false, neither element focused, the bar
display:none, the transform cleared and the watcher stopped, with the picture
still at y=100..319.4. Opening the IME moves the bar from the shortcut row's slot
to 323..382 and closing it puts the row back at 321..369, through both toggleIme()
and a direct class toggle. In landscape the flex path still works: with a 200px
padding the bar goes from 331..390 to 131..190, i.e. just above the keyboard.
The pointer was a second, unrelated bug, and a worse one. screenToCanvas() added
visualViewport.offsetTop on top of a clientX/clientY that is
already expressed in the same coordinate space as getBoundingClientRect(), i.e.
after the translateY compensation. Both quantities include the pan, so subtracting
them already cancels it; adding offsetTop counted it twice. That pushed every
computed remote y down by offsetTop scaled into remote pixels -- 492 remote pixels
at offsetTop 100 on a 16:9 remote -- so everything below the middle of the picture
overshot streamH and was clamped to streamH-1. The symptom is exactly what it
looks like: the pointer can only be moved in the upper part of the picture, a
fixed horizontal band cannot be crossed downwards, and clicking inside that band
lands on the remote taskbar. At offsetTop 300 the whole picture mapped past the
bottom, so every touch landed on the taskbar. The pinch origin had the same
mistake, and so did the cursor recomputation at the end of a pinch, which fed the
overlay's style.top -- a pre-compensation coordinate -- back into screenToCanvas.
Neither direction may add the compensation, and neither does: clientX/clientY and
getBoundingClientRect() are already in the same space, and the overlay no longer
works in screen coordinates at all. The round-trip test had been written against
the wrong premise -- it fed style.top straight into screenToCanvas() -- so it
kept passing while the absolute mapping was wrong. It now converts the overlay's
style values back through the canvas rect on the way in.
Verified against the picture's own rect rather than against the other function:
for points at 10/30/50/70/90/99% of the picture height, screenToCanvas() must
return the remote pixel that sits under that point. At offsetTop 0/100/300 all
eighteen cases are exact (error 0,0); before the fix, offsetTop 100 was off by
+492 on every point and the lower half clamped to 1079, and offsetTop 300 clamped
the entire picture to 1079. The overlay still renders where it should -- for a
pointer driven to the remote bottom it lands 4px above the picture's bottom edge,
the constant hotspot offset, at offsetTop 0 and 300 alike -- and the round-trip
test still returns the original remote point for all nine cases once the
transform is accounted for. With the overlay anchored to the canvas it holds that
alignment through the whole IME lifecycle: dx=dy=0 at offsetTop 0, 200 and 300, on
both a 16:9 remote in portrait and after rotating to landscape and back, and the
pointer lands back on the picture after the IME closes. The rotation case also
covers the re-fit path: the container changes size, the canvas is resized, and
the overlay is replayed at the new geometry instead of staying where the old one
put it.
Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
0a6cbbbf06
commit
ebc2d03290
1 file changed
+313
-47
+313
-47
@@ -889,6 +889,10 @@
|
||||
top: 0; left: 0; right: 0; bottom: 0;
|
||||
background: #000;
|
||||
padding: 0;
|
||||
/* 与 #term-page 同样的防护:内容一旦溢出,iOS 会偷偷给页面加 scroll,
|
||||
键盘弹出时再滚动页面,整个 fixed 页面就被顶上去。 */
|
||||
overflow: hidden;
|
||||
overscroll-behavior: contain;
|
||||
}
|
||||
#screen-page.active { display: flex !important; flex-direction: column; }
|
||||
.canvas-container { flex: 1; display: flex; align-items: center; justify-content: center; overflow: hidden; background: #111; position: relative; }
|
||||
@@ -1203,11 +1207,22 @@
|
||||
flex-direction: column;
|
||||
}
|
||||
}
|
||||
/* 位置参数写的是「画布内的 CSS 像素」,定位基准必须和画布一致:
|
||||
.canvas-container 是画布的包含块,画布 inset:0 铺满它的 padding box,
|
||||
所以画布左上角在容器里的偏移恒为 0,两者共用一套局部坐标。
|
||||
以前是 position:fixed + 用画布的视口矩形反推屏幕坐标,再手动减掉
|
||||
#screen-page 上那个 translateY 补偿 —— 等于把「浏览器怎么算矩形」和
|
||||
「我们自己怎么补偿」当成两个必须严丝合缝的东西。键盘一弹起就是
|
||||
translateY 非零的那一段,两边只要差一点,指针就和画面错开一截。
|
||||
改成 absolute 后覆盖层和画布同祖先、同包含块,祖上怎么平移两者一起动,
|
||||
没有第二个坐标系可以出错。 */
|
||||
/* Cursor overlay (touchpad mode). Per-variant classes are swapped via JS based on
|
||||
the remote IDC_* index. Each variant overrides --hx/--hy so the icon's tip
|
||||
(hotspot), not the div's top-left, lands on the reported cursor position. */
|
||||
.cursor-overlay {
|
||||
position: fixed;
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
pointer-events: none;
|
||||
@@ -1284,6 +1299,11 @@
|
||||
z-index: 101;
|
||||
}
|
||||
.input-shortcuts.visible { display: flex; }
|
||||
/* 手机输入法打开时收起快捷栏:输入条要占的就是这一条的位置。
|
||||
留着的话输入条只能排在快捷栏下面,离键盘更近,更容易被键盘上方那行
|
||||
AutoFill / 候选词压到。用 #screen-page 上的状态类而不是直接摘 .visible,
|
||||
否则 updateUIForOrientation() 一跑又会按 keyboardOpen 把它加回来。 */
|
||||
#screen-page.ime-open .input-shortcuts { display: none; }
|
||||
.input-shortcuts .shortcut-btn {
|
||||
min-width: 36px;
|
||||
height: 36px;
|
||||
@@ -1307,8 +1327,12 @@
|
||||
background: rgba(52,199,89,0.85);
|
||||
border-color: rgba(52,199,89,0.9);
|
||||
}
|
||||
/* "I" 手机输入法组合框(微信聊天风格):固定在屏幕底部,键盘弹出时随视觉视口上浮 */
|
||||
/* "I" 手机输入法组合框(微信聊天风格):竖屏贴在画面(快捷栏)下方,横屏退回 flex 子项 */
|
||||
.ime-bar {
|
||||
/* 竖屏浮层:位置由 updateInputShortcutsPosition 算,紧贴画面下方。
|
||||
横屏走 @media 里的 static,回到 flex 布局,靠 padding-bottom 让位给键盘。 */
|
||||
position: absolute;
|
||||
left: 0; right: 0; bottom: 0;
|
||||
display: none;
|
||||
align-items: flex-end;
|
||||
gap: 8px;
|
||||
@@ -1319,6 +1343,21 @@
|
||||
z-index: 200;
|
||||
}
|
||||
.ime-bar.visible { display: flex; }
|
||||
/* 键盘还没弹起来的那一小段(点 I 到键盘升上来,两三百毫秒)输入条必须"不存在":
|
||||
看不见,否则手指碰到它就会把键盘唤出来、iOS 再为露出这个聚焦的框把整页往上滚。
|
||||
用 opacity 而不是 display/visibility:它得留在文档里、能被 focus —— 不然键盘
|
||||
根本不会弹;而且不能加 pointer-events:none,那样这一条上的触摸会穿透到画布,
|
||||
变成一次发往远端的误点击。 */
|
||||
.ime-bar.ime-waiting {
|
||||
opacity: 0;
|
||||
}
|
||||
/* 横屏退回 flex 子项:画面几乎占满视口,只能靠 screen-page 的 padding-bottom 把
|
||||
内容区压矮来给键盘让位。注意 absolute 元素不吃这个 padding —— bottom:0 是贴
|
||||
padding box 的边(含 padding),所以竖屏那套浮层定位在横屏下会让输入条压在
|
||||
画面上、并被键盘盖住。 */
|
||||
@media (orientation: landscape) {
|
||||
.ime-bar { position: static; }
|
||||
}
|
||||
.ime-bar textarea {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
@@ -1825,7 +1864,13 @@
|
||||
<input type="text" id="mobile-keyboard" style="position:fixed;left:-9999px;opacity:0;" autocomplete="off" autocorrect="off" autocapitalize="off">
|
||||
<div class="ime-bar" id="ime-bar">
|
||||
<button type="button" class="ime-collapse" id="ime-collapse" title="Collapse" tabindex="-1" onmousedown="event.preventDefault()">▼</button>
|
||||
<textarea id="ime-input" rows="1" enterkeyhint="send" autocomplete="off" autocorrect="off" autocapitalize="off" placeholder="Type to send"></textarea>
|
||||
<!-- autocomplete/autocorrect/autocapitalize/spellcheck 全关:iOS 会按启发式
|
||||
判断这个框"能填联系人信息",在键盘上方挂一条 AutoFill Contact(半透明,
|
||||
会压住输入条)。这几个属性能把触发概率压到最低,但 iOS 并不保证一定听
|
||||
—— 彻底关掉要在 设置 → Safari 浏览器 → 自动填充 → 使用联系人信息。 -->
|
||||
<textarea id="ime-input" rows="1" enterkeyhint="send" inputmode="text"
|
||||
autocomplete="off" autocorrect="off" autocapitalize="off"
|
||||
spellcheck="false" placeholder="Type to send"></textarea>
|
||||
<button type="button" class="ime-send" id="ime-send" tabindex="-1" onmousedown="event.preventDefault()">Send</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -2075,6 +2120,11 @@
|
||||
// 画布盖住了整个 border box,所以画面基准还要再往下挪一个 padding-top。
|
||||
let viewAlignY = 'center';
|
||||
let viewPadTop = 0; // 容器 padding-top(CSS px)
|
||||
// 光标覆盖层上一次被摆放时对应的远端坐标。画布尺寸一变(键盘顶起布局视口、
|
||||
// 横竖屏切换、进出全屏),画面在画布里的 fit/off 就跟着变,覆盖层必须按新几何
|
||||
// 重算一次 —— 它存的是画布内 CSS 像素,不会自己跟着 fit 走。
|
||||
// 放在这里是因为 resizeScreenCanvas 在脚本中段就会被调用,那时 cursorState 还没声明。
|
||||
const overlayCursorPos = { x: 0, y: 0, valid: false };
|
||||
|
||||
function syncViewAlign() {
|
||||
const cont = document.querySelector('.canvas-container');
|
||||
@@ -2173,6 +2223,10 @@
|
||||
canvas.width = bw;
|
||||
canvas.height = bh;
|
||||
renderFrame();
|
||||
// 几何变了,覆盖层按新几何重新摆放(见 overlayCursorPos 的注释)。
|
||||
if (overlayCursorPos.valid) {
|
||||
positionCursorOverlay(overlayCursorPos.x, overlayCursorPos.y);
|
||||
}
|
||||
}
|
||||
|
||||
if (window.ResizeObserver) {
|
||||
@@ -5618,16 +5672,38 @@
|
||||
updateInputShortcutsPosition();
|
||||
}
|
||||
|
||||
// 软键盘(含键盘上方那行 AutoFill Contact / 候选词)在「屏幕坐标」里的上沿。
|
||||
// 屏幕坐标以视觉视口顶部为 0,键盘占掉的下半截不在其中,所以上沿就是
|
||||
// visualViewport.height。
|
||||
// 不要写成 offsetTop + height:那是布局坐标。输入条的 style.top 写的是屏幕坐标
|
||||
// ——#screen-page 上的 translateY 补偿正好等于 offsetTop,一加一减,落到 DOM 上
|
||||
// 的 rect 已经和屏幕对齐了。
|
||||
// 更不能用 window.innerHeight:那是布局视口高度,键盘弹起后不变。
|
||||
function keyboardTopY() {
|
||||
const vv = window.visualViewport;
|
||||
if (!vv) return window.innerHeight;
|
||||
return Math.min(window.innerHeight, vv.height);
|
||||
}
|
||||
|
||||
// #screen-page 上当前的 translateY 补偿量(只用于复位/调试)。
|
||||
// 注意:画面、光标覆盖层、输入条都是它的后代,补偿对它们一视同仁,
|
||||
// 所以没有任何一处做坐标换算时需要再减这个量。
|
||||
|
||||
// 上一次重排时看到的键盘上沿。只是用来避免每帧做无谓的重算 —— 重算的结果在常态下
|
||||
// 和上次完全一样(位置只由画面决定),所以这里的变化不会带来任何位移。
|
||||
let lastKbTopSeen = -1;
|
||||
|
||||
// Position input shortcuts bar below the canvas
|
||||
// 画面下边缘那两块浮层的位置:快捷栏(input-shortcuts)和输入条(ime-bar)。
|
||||
// 它们在同一坐标系里做同一件事 —— 贴着画面下边缘 —— 所以共用一个公式。
|
||||
function updateInputShortcutsPosition() {
|
||||
if (!isTouchDevice || !uiElements || !uiElements.inputShortcuts) return;
|
||||
if (isLandscape()) return; // Only for portrait mode
|
||||
if (!isTouchDevice) return;
|
||||
if (isLandscape()) return; // 横屏下 ime-bar 是 flex 子项,位置由 adjustImeBar 的 padding 决定
|
||||
|
||||
const canvas = document.getElementById('screen-canvas');
|
||||
const shortcuts = uiElements.inputShortcuts;
|
||||
if (!canvas || canvas.offsetHeight === 0) return;
|
||||
|
||||
// 画布已经铺满容器,画面本身按 fit 居中,所以快捷栏要贴在「画面」的下边缘,
|
||||
// 画布已经铺满容器,画面本身按 fit 居中,所以浮层要贴在「画面」的下边缘,
|
||||
// 而不是画布盒子的下边缘。
|
||||
// 还没收到首帧时 streamH 为 0,算不出画面高度,保持原位即可(放到画布正中反而更怪)。
|
||||
if (!streamH) return;
|
||||
@@ -5636,10 +5712,49 @@
|
||||
// 这里换算成 CSS px 直接用,别再假设居中。
|
||||
const imgTop = canvas.offsetTop + m.offY / m.d;
|
||||
const imgH = streamH * (m.fit / m.d);
|
||||
shortcuts.style.top = (imgTop + imgH + 2) + 'px';
|
||||
|
||||
// 输入条就是「画面下方的一块固定区域」:和快捷栏同一个公式、同一个坐标系。
|
||||
// 键盘高度怎么变(切输入法、AutoFill Contact / 候选词那一行出现消失、切表情
|
||||
// 键盘)它都纹丝不动 —— 以前它盯着键盘上沿走、一变就重排,才会看着像被输入法
|
||||
// 顶上去了。竖屏画面是宽度受限的(远端 16:9 时画面只占屏幕上半截),画面下方到
|
||||
// 键盘之间本来就有大片空白,根本不需要躲。
|
||||
const imeBar = document.getElementById('ime-bar');
|
||||
if (imeBar) {
|
||||
// 注意:输入条挂在 #screen-page 下,包含块是它;而 canvas.offsetTop 是相对
|
||||
// .canvas-container 的,两者差着顶部工具栏那一条(竖屏还差 canvas-container
|
||||
// 的 padding-top)。所以这里用 rect 相减,别照抄快捷栏那行 —— 否则输入条
|
||||
// 会整整高出一条工具栏,看着像飘在画面上。
|
||||
// translateY 补偿同时作用在 #screen-page 和它的后代上,相减自动抵消,
|
||||
// 不用再单独减 visualOffsetY。
|
||||
const pageEl = document.getElementById('screen-page');
|
||||
const pr = pageEl.getBoundingClientRect();
|
||||
const cr = canvas.getBoundingClientRect();
|
||||
const picTop = cr.top - pr.top + m.offY / m.d;
|
||||
const barTop = picTop + imgH + 4;
|
||||
const barH = imeBar.offsetHeight || 59;
|
||||
// 唯一的钳位:别让输入条掉到键盘底下看不见。只有当「画面下边缘已经伸进键盘
|
||||
// 覆盖的那一截」时这一钳才生效(远端是竖屏的时候会这样,画面比键盘上方的
|
||||
// 空间还高),那种情况下画面底本身就看不见,输入条只能退到键盘上沿 ——
|
||||
// 除此之外没有第二个位置可放。远端是横屏/16:9 时画面底远在键盘上方,
|
||||
// 这一钳永远不生效,输入条就是画面底 + 4px 的常量。
|
||||
const limit = Math.max(0, keyboardTopY() - barH);
|
||||
// 比较后再写:钳住的时候这个函数会被每帧调用,别让 style 每帧失效一次。
|
||||
const nextTop = Math.min(barTop, limit) + 'px';
|
||||
if (imeBar.style.top !== nextTop) imeBar.style.top = nextTop;
|
||||
if (imeBar.style.bottom !== 'auto') imeBar.style.bottom = 'auto';
|
||||
}
|
||||
|
||||
const shortcuts = uiElements && uiElements.inputShortcuts;
|
||||
if (shortcuts) {
|
||||
const nextTop = (imgTop + imgH + 2) + 'px';
|
||||
if (shortcuts.style.top !== nextTop) shortcuts.style.top = nextTop;
|
||||
}
|
||||
}
|
||||
|
||||
function sendRdpReset() {
|
||||
// 刷新画面会重绘整帧,输入法留在开着的状态没有意义:输入条还挂在画面上,
|
||||
// 手指一碰输入框键盘又弹起来,画面被顶上去,光标也跟着失准。先收干净再发。
|
||||
closeIme();
|
||||
if (ws && ws.readyState === WebSocket.OPEN && token) {
|
||||
ws.send(JSON.stringify({ cmd: 'rdp_reset', token, id: currentDeviceId() }));
|
||||
// Visual feedback - rotate icon
|
||||
@@ -5713,6 +5828,9 @@
|
||||
} else {
|
||||
// 非控制态:藏起本地指针,只留远程真实光标
|
||||
renderRemoteCursor();
|
||||
// 取消鼠标控制就收掉键盘和输入框。留着的话输入条还挂在画面上,
|
||||
// 手指一碰输入框输入法又弹起来,画面被顶、光标跟着失准。
|
||||
closeKeyboardAndIme();
|
||||
}
|
||||
// Sync the keyboard lock: in fullscreen + control mode, ESC and F11
|
||||
// must be passed through to the remote OS instead of triggering the
|
||||
@@ -5729,7 +5847,8 @@
|
||||
positionCursorOverlay(canvasX, canvasY);
|
||||
}
|
||||
|
||||
// 远端坐标 → 屏幕坐标。.cursor-overlay 是 position:fixed,所以给的是视口坐标。
|
||||
// 远端坐标 → 画布内 CSS 像素。覆盖层和画布同属 .canvas-container,
|
||||
// 给的直接就是画布局部坐标,不碰视口矩形,也不碰任何键盘补偿量。
|
||||
function positionCursorOverlay(canvasX, canvasY) {
|
||||
const cursorOverlay = document.getElementById('cursor-overlay');
|
||||
if (!cursorOverlay || !streamW || !streamH) return;
|
||||
@@ -5741,16 +5860,28 @@
|
||||
const ox = o.x, oy = o.y;
|
||||
const bx = ox + s * (m.fit * canvasX + m.offX + zoomState.translateX * m.d - ox);
|
||||
const by = oy + s * (m.fit * canvasY + m.offY + zoomState.translateY * m.d - oy);
|
||||
cursorOverlay.style.left = (r.rect.left + bx / r.kx) + 'px';
|
||||
cursorOverlay.style.top = (r.rect.top + by / r.ky) + 'px';
|
||||
cursorOverlay.style.left = (bx / r.kx) + 'px';
|
||||
// 这里**不要**再减 keyboard 补偿量:覆盖层是 #screen-page 的后代,
|
||||
// translateY 对它和画布一视同仁,两者不会因此分开。
|
||||
cursorOverlay.style.top = (by / r.ky) + 'px';
|
||||
overlayCursorPos.x = canvasX;
|
||||
overlayCursorPos.y = canvasY;
|
||||
overlayCursorPos.valid = true;
|
||||
}
|
||||
|
||||
// 屏幕坐标 → 远端坐标(positionCursorOverlay 的逆运算)
|
||||
// 屏幕坐标 → 远端坐标
|
||||
function screenToCanvas(screenX, screenY) {
|
||||
if (!streamW || !streamH) return { x: 0, y: 0 };
|
||||
const r = canvasPixelRatio();
|
||||
const m = getViewMetrics();
|
||||
const s = zoomState.scale;
|
||||
// 这里**不能**加任何键盘补偿量。
|
||||
// 手指/鼠标给的 clientX/clientY 和 getBoundingClientRect() 是同一个坐标系
|
||||
// ——两者相减,任何祖先上的 translateY 都自动抵消。以前这里加了一遍
|
||||
// offsetTop,等于把补偿算了两遍:键盘把视觉视口顶起 100px 时,整块画面算出
|
||||
// 来的远端 y 就偏低 492px,画面下半截全部越界、被钳到 streamH-1(远端最底部)
|
||||
// ——表现就是"指针只能在上半截动,下面有一条过不去的横线,点那儿反而点到远端
|
||||
// 任务栏"。
|
||||
const px = (screenX - r.rect.left) * r.kx;
|
||||
const py = (screenY - r.rect.top) * r.ky;
|
||||
const o = zoomOriginPx(m);
|
||||
@@ -6087,29 +6218,73 @@
|
||||
el.style.height = (el.scrollHeight + 2) + 'px';
|
||||
}
|
||||
|
||||
// 收起手机输入法。工具栏的 I 按钮、断开连接、RDP 重置都要用,别再各写一份 ——
|
||||
// 漏掉任何一处,输入法就留在"半开"状态:输入条还挂在画面上,手指一碰输入框
|
||||
// 键盘又弹起来,画面被顶、光标跟着失准。
|
||||
function closeIme() {
|
||||
const wasOpen = imeMode;
|
||||
imeMode = false;
|
||||
stopImeWatcher();
|
||||
// 清理部分无条件执行:imeMode 和 DOM 可能不同步(比如上一次只关了一半),
|
||||
// 只在"确实开着"时才做的动作放到下面单独判断。
|
||||
const btnIme = document.getElementById('btn-ime');
|
||||
if (btnIme) btnIme.classList.remove('active');
|
||||
const imeBar = document.getElementById('ime-bar');
|
||||
if (imeBar) imeBar.classList.remove('visible', 'ime-waiting');
|
||||
const imeInput = document.getElementById('ime-input');
|
||||
if (imeInput) imeInput.blur();
|
||||
const page = document.getElementById('screen-page');
|
||||
if (page) {
|
||||
page.style.paddingBottom = '';
|
||||
page.style.transform = '';
|
||||
page.classList.remove('ime-open'); // 快捷栏恢复
|
||||
}
|
||||
visualOffsetY = 0; // 与上面的 transform 一起复位,别留残值
|
||||
lastKbTopSeen = -1; // 下次打开按当时的键盘上沿重新判断,别沿用上一轮的
|
||||
imeKbSeen = false; // 键盘存在性跟踪一并复位,下次打开重新等键盘
|
||||
imeKbDownSince = 0;
|
||||
imeOpenedAt = 0;
|
||||
// 只在真的关掉输入法时才收手机键盘;本来就没开的话别顺手把人家的键盘也关了
|
||||
if (wasOpen) updateKeyboardButtons(false);
|
||||
updateInputShortcutsPosition();
|
||||
}
|
||||
|
||||
// 手机键盘 + "I" 输入框一起收掉。取消鼠标控制、断开连接都要用。
|
||||
// 只关其中一个是不够的:手机键盘还开着的话,输入条虽然收起了,一点输入框
|
||||
// 输入法又弹起来,画面照样被顶上去。
|
||||
function closeKeyboardAndIme() {
|
||||
closeIme();
|
||||
mobileKeyboard.blur();
|
||||
updateKeyboardButtons(false);
|
||||
}
|
||||
|
||||
function toggleIme() {
|
||||
imeMode = !imeMode;
|
||||
if (imeMode) { closeIme(); return; }
|
||||
imeMode = true;
|
||||
// 输入条先"隐身"等键盘:imeKbSeen=false 时 updateImePresence 会一直挂着
|
||||
// ime-waiting,直到键盘真的弹起来才让它出现。
|
||||
imeKbSeen = false;
|
||||
imeKbDownSince = 0;
|
||||
imeOpenedAt = performance.now();
|
||||
const imeBar = document.getElementById('ime-bar');
|
||||
const imeInput = document.getElementById('ime-input');
|
||||
const btnIme = document.getElementById('btn-ime');
|
||||
const page = document.getElementById('screen-page');
|
||||
if (imeMode) {
|
||||
if (btnIme) btnIme.classList.add('active');
|
||||
if (imeBar) { imeBar.classList.add('visible'); adjustImeBar(); }
|
||||
if (imeInput) {
|
||||
imeInput.focus();
|
||||
// 打开输入法时收起画面下方那排快捷键,把那条位置让给输入条(见 .ime-open 的 CSS)
|
||||
if (page) page.classList.add('ime-open');
|
||||
if (btnIme) btnIme.classList.add('active');
|
||||
if (imeBar) { imeBar.classList.add('visible', 'ime-waiting'); adjustImeBar(); }
|
||||
if (imeInput) {
|
||||
imeInput.focus();
|
||||
adjustImeBar();
|
||||
syncVisualOffset();
|
||||
startImeWatcher();
|
||||
// iOS 上软键盘弹出后 visualViewport 常要一次滚动才归位,强制滚回顶部触发重排
|
||||
setTimeout(function() {
|
||||
window.scrollTo(0, 0);
|
||||
adjustImeBar();
|
||||
startImeWatcher();
|
||||
// iOS 上软键盘弹出后 visualViewport 常要一次滚动才归位,强制滚回顶部触发重排
|
||||
setTimeout(function() { window.scrollTo(0, 0); adjustImeBar(); }, 60);
|
||||
}
|
||||
} else {
|
||||
stopImeWatcher();
|
||||
if (btnIme) btnIme.classList.remove('active');
|
||||
if (imeBar) imeBar.classList.remove('visible');
|
||||
if (imeInput) imeInput.blur();
|
||||
if (page) page.style.paddingBottom = '';
|
||||
updateKeyboardButtons(false); // 收起键盘,便于移动远程光标
|
||||
syncVisualOffset();
|
||||
}, 60);
|
||||
}
|
||||
updateInputShortcutsPosition();
|
||||
}
|
||||
@@ -6130,12 +6305,34 @@
|
||||
}
|
||||
}
|
||||
|
||||
// 键盘弹出时给 screen-page 加 padding-bottom = 键盘高度,把底部输入条顶到键盘上方。
|
||||
// 与 adjustTermViewport 同款做法:screen-page 是 flex 列,ime-bar 是最后一个 flex 子项。
|
||||
// 横屏专用:给 screen-page 加 padding-bottom = 键盘高度,把输入条顶到键盘上方。
|
||||
// 与 adjustTermViewport 同款做法:screen-page 是 flex 列,横屏下 ime-bar 是
|
||||
// 最后一个 flex 子项(见 @media (orientation: landscape) 里的 position: static)。
|
||||
//
|
||||
// 竖屏不走这里。竖屏的输入条位置只由 updateInputShortcutsPosition 算一次,
|
||||
// 公式和快捷栏一模一样,跟键盘彻底无关 —— 键盘高度怎么变(切输入法、AutoFill
|
||||
// Contact / 候选词那一行出现消失、切表情键盘)输入条都纹丝不动。以前这里盯着
|
||||
// 键盘上沿、一变就重排,才会看着像被输入法顶上去了。
|
||||
function adjustImeBar() {
|
||||
if (!imeMode) return;
|
||||
const page = document.getElementById('screen-page');
|
||||
if (!page) return;
|
||||
if (!isLandscape()) {
|
||||
// 竖屏只需要把横屏留下的 padding 清掉。输入条位置由画面决定,是常量;
|
||||
// 这里只在键盘上沿变了的时候重算一次(重算幂等,值没变就不写 style,
|
||||
// 所以常态下不会产生任何位移)。真正会动的情况只有一种:画面底伸进了
|
||||
// 键盘覆盖的那一截,输入条被钳在键盘上沿 —— 那时它只能跟着键盘走。
|
||||
if (page.style.paddingBottom) page.style.paddingBottom = '';
|
||||
const kt = keyboardTopY();
|
||||
if (kt !== lastKbTopSeen) {
|
||||
lastKbTopSeen = kt;
|
||||
updateInputShortcutsPosition();
|
||||
}
|
||||
return;
|
||||
}
|
||||
// 横屏:没有快捷栏,画面几乎占满,只能靠 padding 把输入条顶到键盘上方。
|
||||
const imeBar = document.getElementById('ime-bar');
|
||||
if (imeBar && imeBar.style.top) { imeBar.style.top = ''; imeBar.style.bottom = ''; }
|
||||
let inset = 0;
|
||||
if (window.visualViewport) {
|
||||
inset = Math.max(0, Math.round(
|
||||
@@ -6149,12 +6346,74 @@
|
||||
}
|
||||
}
|
||||
|
||||
// iOS 键盘弹出时会把视觉视口整体上移(visualViewport.offsetTop > 0),而
|
||||
// #screen-page 是 position:fixed、贴的是布局视口,于是整页在视觉上被推出屏幕上方
|
||||
// —— 看起来就是"画面被往上顶了一截"。这里反向平移同样的距离抵消掉。
|
||||
// offsetTop 归零时清掉 transform,避免常驻(有 transform 会让内部的 fixed 元素
|
||||
// 改成相对本元素定位,虽然此时两者坐标系一致,但没必要一直挂着)。
|
||||
let visualOffsetY = 0; // 当前补偿量(只做复位/对账,坐标换算一律不用它)
|
||||
|
||||
// 输入条只能活在键盘弹起的时候。下面几个变量就是在盯这件事。
|
||||
let imeKbSeen = false; // 这次打开是否已经确认过键盘弹起
|
||||
let imeKbDownSince = 0; // 键盘开始"不在"的时间戳,0 表示当前是弹起的
|
||||
let imeOpenedAt = 0; // 本次打开的时刻
|
||||
const IME_KB_CLOSE_MS = 450; // 超过这么久就认定键盘真的收起了,直接把输入法撤掉
|
||||
const IME_KB_WAIT_MS = 3000; // 打开后这么久还没等到键盘,才撤掉(聚焦失败 / 外接键盘)。
|
||||
// 留得宽是因为 iOS 的 visualViewport 有时要一次滚动才更新,
|
||||
// 太短会在慢设备上把刚打开的输入法误关掉。
|
||||
|
||||
// 键盘在不在。差值小于 40px 的一律当成"没弹"——地址栏、工具栏伸缩都会动这个数。
|
||||
function keyboardIsUp() {
|
||||
return keyboardTopY() < window.innerHeight - 40;
|
||||
}
|
||||
|
||||
// 输入条的存在必须和键盘严格绑定。键盘一收起就把它整个撤掉:留着的话
|
||||
// 手指一点它就会把键盘唤回来,iOS 再为了露出这个聚焦的框把整页往上滚,
|
||||
// 看到的就是"点一下输入框,它自己往上顶一截"。
|
||||
function updateImePresence() {
|
||||
const bar = document.getElementById('ime-bar');
|
||||
if (!bar) return;
|
||||
const now = performance.now();
|
||||
if (keyboardIsUp()) {
|
||||
imeKbSeen = true;
|
||||
imeKbDownSince = 0;
|
||||
bar.classList.remove('ime-waiting');
|
||||
return;
|
||||
}
|
||||
if (!imeKbDownSince) imeKbDownSince = now;
|
||||
const downMs = now - imeKbDownSince;
|
||||
|
||||
if (!imeKbSeen) {
|
||||
// 还在等键盘第一次上来:先隐身,但留着可聚焦
|
||||
bar.classList.add('ime-waiting');
|
||||
if (imeOpenedAt && now - imeOpenedAt > IME_KB_WAIT_MS) closeIme();
|
||||
return;
|
||||
}
|
||||
// 弹起过又收起了:**立刻**隐身,一帧都不留 —— 只要键盘不在,输入条就不该存在。
|
||||
// 确认收起(连续 IME_KB_CLOSE_MS 没有键盘)之后再把输入法整个撤掉。
|
||||
bar.classList.add('ime-waiting');
|
||||
if (downMs > IME_KB_CLOSE_MS) closeIme();
|
||||
}
|
||||
|
||||
function syncVisualOffset() {
|
||||
const page = document.getElementById('screen-page');
|
||||
if (!page) return;
|
||||
const vv = window.visualViewport;
|
||||
const top = vv ? Math.max(0, Math.round(vv.offsetTop || 0)) : 0;
|
||||
const next = top ? 'translateY(' + top + 'px)' : '';
|
||||
if (page.style.transform !== next) page.style.transform = next;
|
||||
visualOffsetY = top;
|
||||
}
|
||||
|
||||
// 键盘弹出是异步动画,iOS 上 visualViewport 甚至要一次滚动才更新,用 rAF 循环
|
||||
// 在 imeMode 期间持续修正,避免"必须手动滚动网页才归位"的问题。
|
||||
let imeRafId = null;
|
||||
function imeWatcher() {
|
||||
if (!imeMode) { imeRafId = null; return; }
|
||||
adjustImeBar();
|
||||
syncVisualOffset();
|
||||
updateImePresence(); // 键盘不在就把输入条撤掉(可能会调 closeIme)
|
||||
if (!imeMode) { imeRafId = null; return; } // closeIme 已经把它关了,别再排帧
|
||||
imeRafId = requestAnimationFrame(imeWatcher);
|
||||
}
|
||||
function startImeWatcher() {
|
||||
@@ -6196,6 +6455,8 @@
|
||||
// 画布铺满容器后画面只占其中一块(竖屏贴顶时尤其悬殊),照抄画布百分比会偏。
|
||||
const r = canvasPixelRatio();
|
||||
const m = getViewMetrics();
|
||||
// 和 screenToCanvas 一样:手指坐标与 rect 同坐标系,任何键盘补偿量
|
||||
// 都已互相抵消,不能再加(加了锚点会整体下移,捏合时画面往外甩)。
|
||||
const px = (center.x - r.rect.left) * r.kx;
|
||||
const py = (center.y - r.rect.top) * r.ky;
|
||||
const imgW = streamW * m.fit, imgH = streamH * m.fit;
|
||||
@@ -6455,8 +6716,11 @@
|
||||
// Cursor stayed fixed on screen, so reverse-calculate its new canvas coords
|
||||
if (cursorState.initialized && zoomState.hasZoomed) {
|
||||
const cursorOverlay = document.getElementById('cursor-overlay');
|
||||
const cursorScreenX = parseFloat(cursorOverlay.style.left) || 0;
|
||||
const cursorScreenY = parseFloat(cursorOverlay.style.top) || 0;
|
||||
// style.left/top 是画布内 CSS 像素,加上画布的视口矩形就回到
|
||||
// clientX/clientY 那套坐标 —— screenToCanvas 收的就是这个。
|
||||
const r = canvasPixelRatio();
|
||||
const cursorScreenX = r.rect.left + (parseFloat(cursorOverlay.style.left) || 0);
|
||||
const cursorScreenY = r.rect.top + (parseFloat(cursorOverlay.style.top) || 0);
|
||||
const newCoords = screenToCanvas(cursorScreenX, cursorScreenY);
|
||||
cursorState.x = newCoords.x;
|
||||
cursorState.y = newCoords.y;
|
||||
@@ -6565,7 +6829,7 @@
|
||||
wasKeyboardFocused = false; // Reset
|
||||
|
||||
if (isOpen) {
|
||||
if (imeMode) toggleIme(); // 关闭 "I" 输入框(内部会 updateKeyboardButtons(false))
|
||||
if (imeMode) closeIme(); // 关闭 "I" 输入框(内部会 updateKeyboardButtons(false))
|
||||
mobileKeyboard.blur();
|
||||
updateKeyboardButtons(false);
|
||||
} else {
|
||||
@@ -6748,17 +7012,10 @@
|
||||
frameCount = 0; lastFrameTime = performance.now(); fps = 0;
|
||||
// Reset control mode
|
||||
controlEnabled = false;
|
||||
// Reset keyboard state (blur event will update button state)
|
||||
mobileKeyboard.blur();
|
||||
// Reset "I" 手机输入法组合框
|
||||
imeMode = false;
|
||||
stopImeWatcher();
|
||||
const imeBar = document.getElementById('ime-bar');
|
||||
if (imeBar) imeBar.classList.remove('visible');
|
||||
const imePage = document.getElementById('screen-page');
|
||||
if (imePage) imePage.style.paddingBottom = '';
|
||||
const btnIme = document.getElementById('btn-ime');
|
||||
if (btnIme) btnIme.classList.remove('active');
|
||||
// 输入法状态整块交给 closeIme(),别在这儿再抄一份:抄漏一处就是"半开"状态,
|
||||
// 而且 imeMode 不复位的话 imeWatcher 的 rAF 循环会一直空转到下次连接。
|
||||
closeKeyboardAndIme();
|
||||
// Reset floating toolbar buttons
|
||||
const btnMouse = document.getElementById('btn-mouse');
|
||||
if (btnMouse) btnMouse.classList.remove('active');
|
||||
@@ -7051,10 +7308,19 @@
|
||||
}
|
||||
if (window.visualViewport) {
|
||||
window.visualViewport.addEventListener('resize', adjustImeBar);
|
||||
window.visualViewport.addEventListener('scroll', adjustImeBar);
|
||||
window.visualViewport.addEventListener('scroll', function() {
|
||||
adjustImeBar();
|
||||
syncVisualOffset();
|
||||
});
|
||||
}
|
||||
// iOS / 部分浏览器键盘弹出时触发的是 window resize,作为上浮兜底
|
||||
window.addEventListener('resize', adjustImeBar);
|
||||
// iOS / 部分浏览器键盘弹出时触发的是 window resize,作为上浮兜底。
|
||||
// 顺手重排浮层:地址栏收起、全屏切换这些会改 window.innerHeight 和画布高度,
|
||||
// 画面位置跟着变,输入条/快捷栏得跟上。注意键盘弹起不会改 innerHeight,
|
||||
// 所以这条路径不会因为键盘高度变化而挪动输入条 —— 这是有意为之。
|
||||
window.addEventListener('resize', function() {
|
||||
adjustImeBar();
|
||||
updateInputShortcutsPosition();
|
||||
});
|
||||
} else {
|
||||
// Desktop: hide keyboard buttons (physical keyboard available)
|
||||
const btnKeyboard = document.getElementById('btn-keyboard');
|
||||
|
||||
Reference in new issue
Block a user