Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,16 @@ export async function mount(container: HTMLElement, api: PluginAPI): Promise<voi
}
}

// Focusing the terminal is what raises a soft keyboard, so the bar only
// restores focus the tap found there; with the keyboard down, keys go to the
// shell and the keyboard stays down. Recorded on pointerdown, before
// anything the tap does can move focus.
let keyboardWasUp = false;
keybar.addEventListener('pointerdown', () => { keyboardWasUp = !!activeSession()?.hasFocus(); }, true);
const keepKeyboard = (session: TerminalSession | undefined): void => {
if (keyboardWasUp) session?.focus();
};

for (const key of MOBILE_KEYS) {
const button = el('button', 'wt-key');
button.type = 'button';
Expand All @@ -656,16 +666,23 @@ export async function mount(container: HTMLElement, api: PluginAPI): Promise<voi
if (key.action === 'ctrl') session.pendingCtrl = !session.pendingCtrl;
else session.pendingAlt = !session.pendingAlt;
syncModifierButtons();
session.focus();
keepKeyboard(session);
});
} else if (key.action === 'paste') {
button.addEventListener('click', () => {
activeSession()?.paste().catch((err: Error) => showToast(err.message));
const session = activeSession();
session?.paste()
.then(() => keepKeyboard(session))
.catch((err: Error) => showToast(err.message));
});
} else if (key.action === 'keyboard') {
button.addEventListener('click', () => activeSession()?.focus());
} else {
button.addEventListener('click', () => activeSession()?.sendKey(key.seq!));
button.addEventListener('click', () => {
const session = activeSession();
session?.sendKey(key.seq!);
keepKeyboard(session);
});
}
keybar.appendChild(button);
}
Expand Down
9 changes: 6 additions & 3 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,12 @@ export class TerminalSession {
this.sendBytes(encoder.encode(data));
}

/** Types a literal sequence, as the mobile key bar and paste button do. */
/**
* Types a literal sequence, as the mobile key bar does. Focus is left to the
* caller: on a phone, focusing the terminal raises the soft keyboard.
*/
sendKey(sequence: string): void {
this.sendBytes(encoder.encode(sequence));
this.focus();
}

async paste(): Promise<void> {
Expand All @@ -410,7 +412,6 @@ export class TerminalSession {
// normalisation. Writing to the socket directly skips both, which makes
// multi-line pastes execute line by line in any shell that expects it.
this.terminal.paste(text);
this.focus();
}

// ── Keyboard ────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -573,6 +574,8 @@ export class TerminalSession {

focus(): void { try { this.terminal.focus(); } catch { /* not attached */ } }

hasFocus(): boolean { return !!this.terminal.textarea && document.activeElement === this.terminal.textarea; }

show(): void {
this.el.hidden = false;
setTimeout(() => { this.fit(true); this.focus(); }, 20);
Expand Down