Skip to content

Add global shortcuts to improve keyboard navigation #10551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
5 changes: 5 additions & 0 deletions localtypings/pxteditor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,8 @@ declare namespace pxt.editor {
zoomOut(): void;
resize(): void;
setScale(scale: number): void;
focusWorkspace(): void;
focusToolbox(): void;
}

export interface IFile {
Expand Down Expand Up @@ -819,6 +821,7 @@ declare namespace pxt.editor {
extensionsVisible?: boolean;
isMultiplayerGame?: boolean; // Arcade: Does the current project contain multiplayer blocks?
onboarding?: pxt.tour.BubbleStep[];
navigateRegions?: boolean;
feedback?: FeedbackState;
themePickerOpen?: boolean;
}
Expand Down Expand Up @@ -1056,6 +1059,8 @@ declare namespace pxt.editor {
hideLightbox(): void;
showOnboarding(): void;
hideOnboarding(): void;
showNavigateRegions(): void;
hideNavigateRegions(): void;
showKeymap(show: boolean): void;
toggleKeymap(): void;
signOutGithub(): void;
Expand Down
25 changes: 25 additions & 0 deletions pxtsim/accessibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ namespace pxsim.accessibility {
elem.setAttribute("tabindex", "0");
}

export function getKeyboardShortcutEditorAction(e: KeyboardEvent): pxsim.SimulatorAction | null {
const meta = e.metaKey || e.ctrlKey;
if (e.key === "Escape") {
e.preventDefault();
return "escape"
} else if (e.key === "b" && meta) {
e.preventDefault();
return "navigateregions"
}
return null
}

export function postKeyboardEvent() {
document.addEventListener("keydown", (e) => {
const action = getKeyboardShortcutEditorAction(e)
if (action) {
const message = {
type: "pxtsim",
action
} as pxsim.SimulatorActionMessage;
Runtime.postMessage(message)
}
});
}

export function enableKeyboardInteraction(elem: Element, handlerKeyDown?: () => void, handlerKeyUp?: () => void): void {
if (handlerKeyDown) {
elem.addEventListener('keydown', (e: KeyboardEvent) => {
Expand Down
6 changes: 6 additions & 0 deletions pxtsim/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ namespace pxsim {
url: string;
}

export type SimulatorAction = "escape" | "navigateregions";

export interface SimulatorActionMessage extends SimulatorMessage {
action: SimulatorAction;
}

export interface SimulatorStateMessage extends SimulatorMessage {
type: "status";
frameid?: string;
Expand Down
5 changes: 5 additions & 0 deletions theme/common.less
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ div.simframe > iframe {
top:0; left: 0; width:100%; height:100%;
}

#boardview:focus-visible {
outline: 3px solid var(--pxt-focus-border);
outline-offset: 3px;
}

.simHeadless {
height: 0 !important;
width: 0 !important;
Expand Down
72 changes: 72 additions & 0 deletions theme/navigateregions.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Import all components */
@import 'themes/default/globals/site.variables';
@import 'themes/pxt/globals/site.variables';

/* Reference import */
@import (reference) "semantic.less";

.navigate-regions-container {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0,0,0,0);
width: 100%;
height: 100%;
z-index: @modalDimmerZIndex;

.region-button {
position: absolute;
background-color: rgba(0, 0, 0, .6);
border-radius: 0;
border: 3px solid white;
padding: 0;

&.simulator-region {
z-index: 1;
}

&.simulator-collapsed {
border-start-end-radius: 100px;
border-end-end-radius: 100px;
}

&:focus-visible {
background-color: rgba(0, 0, 0, .3);

div {
border: 5px solid white;
background-color: black;
}

p {
font-weight: bold;
}
}

div {
background-color: rgba(0, 0, 0, .6);
border: 2px solid white;
width: fit-content;
margin: auto;
padding-right: 1em;
padding-left: 1em;
border-radius: 5px;

@media only screen and (max-width: @largestMobileScreen) {
padding-right: 0.5em;
padding-left: 0.5em;
}
}
p {
color: white;
font-size: 2rem;

@media only screen and (max-width: @largestTabletScreen),
only screen and (max-height: @tallEditorBreakpoint) and (min-width: @largestMobileScreen) {
font-size: 1.5rem;
}
}
}
}
1 change: 1 addition & 0 deletions theme/pxt.less
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
@import 'accessibility';
@import 'highcontrast';
@import 'greenscreen';
@import 'navigateregions';

@import 'extension';
@import 'extensionErrors';
Expand Down
17 changes: 17 additions & 0 deletions webapp/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import Util = pxt.Util;
import { HintManager } from "./hinttooltip";
import { mergeProjectCode, appendTemporaryAssets } from "./mergeProjects";
import { Tour } from "./components/onboarding/Tour";
import { NavigateRegionsOverlay } from "./components/NavigateRegionsOverlay";
import { parseTourStepsAsync } from "./onboarding";
import { initGitHubDb } from "./idbworkspace";
import { BlockDefinition, CategoryNameID } from "./toolbox";
Expand Down Expand Up @@ -5247,6 +5248,21 @@ export class ProjectView

}

///////////////////////////////////////////////////////////
//////////// Navigate regions /////////////
///////////////////////////////////////////////////////////

hideNavigateRegions() {
this.setState({ navigateRegions: false });
}

showNavigateRegions() {
const dialog = Array.from(document.querySelectorAll("[role=dialog]")).find(dialog => (dialog as any).checkVisibility());
if (!dialog) {
this.setState(state => state.home ? state : { navigateRegions: true })
}
}

///////////////////////////////////////////////////////////
//////////// Key map /////////////
///////////////////////////////////////////////////////////
Expand Down Expand Up @@ -5507,6 +5523,7 @@ export class ProjectView
{lightbox ? <sui.Dimmer isOpen={true} active={lightbox} portalClassName={'tutorial'} className={'ui modal'}
shouldFocusAfterRender={false} closable={true} onClose={this.hideLightbox} /> : undefined}
{this.state.onboarding && <Tour tourSteps={this.state.onboarding} onClose={this.hideOnboarding} />}
{accessibleBlocks && this.state.navigateRegions && <NavigateRegionsOverlay parent={this}/>}
{this.state.themePickerOpen && <ThemePickerModal themes={this.themeManager.getAllColorThemes()} onThemeClicked={theme => this.setColorThemeById(theme?.id, true)} onClose={this.hideThemePicker} />}
</div>
);
Expand Down
29 changes: 29 additions & 0 deletions webapp/src/blocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,35 @@ export class Editor extends toolboxeditor.ToolboxEditor {
return true
}
});

const triggerEditorAction = (action: pxsim.SimulatorAction) => {
switch (action) {
case "escape": {
this.parent.setSimulatorFullScreen(false);
return;
}
case "navigateregions" : {
this.parent.showNavigateRegions();
return
}
}
}

const simulatorOrigins = [
window.location.origin,
// Simulator deployed origin.
"https://trg-microbit.userpxt.io"
]
window.addEventListener("message", (e: MessageEvent) => {
// Listen to simulator iframe keydown post messages.
if (simulatorOrigins.includes(e.origin) && e.data.type === "pxtsim") {
triggerEditorAction((e.data as pxsim.SimulatorActionMessage).action)
}
}, false)
document.addEventListener("keydown", (e: KeyboardEvent) => {
const action = pxsim.accessibility.getKeyboardShortcutEditorAction(e)
triggerEditorAction(action)
});
}
}

Expand Down
Loading