Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ API between minor releases.
- Change or reset the viewport background color from the display menu.
- Saved viewpoints can restore clipping, hidden elements and linked-file checkboxes. The viewpoint list and the
tour both use the saved state.
- Keyboard navigation and visible focus in the model tree and context menu.

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fails when it differs from the committed file. The end-to-end tests need the
.NET SDK and a Chromium for Playwright (`npx playwright install chromium`).
Fixtures are in `tests/fixtures/viewer`.

CI also runs the smoke, selection and section tests in Firefox and builds `tests/fixtures/viewer/generate-fixtures.cs`
CI also runs the smoke, selection, section and keyboard-accessibility tests in Firefox and builds `tests/fixtures/viewer/generate-fixtures.cs`
and `tools/SyntheticModel/SyntheticModel.cs`. To run the Firefox project locally, build the viewer, install Firefox
with `npx playwright install firefox` and run `npx playwright test --project=firefox`. On Linux, install browser
system dependencies with `npx playwright install --with-deps chromium firefox` and prefix the Firefox command with
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Built clean-room under the [IOExtend](https://github.com/IOExtend) organization.
| Middle or right drag | Pan |
| Wheel | Zoom towards the cursor |
| Right click | Context menu |
| Menu key / Shift+F10 | Open the context menu. In the model tree, use the focused element |
| Up / Down, Enter in the context menu | Move between actions and run the focused action |
| Up / Down, Left / Right in the model tree | Move between rows, collapse or expand a category, or move to a parent or child |
| Home / End, Enter in the model tree | Jump to the first or last row, or select and zoom to the focused element |
| H / I / Shift+H | Hide selected / isolate selected / show all |
| F | Zoom to the selection, or fit the whole model when nothing is selected |
| Esc | Close the open menu or popup, or clear the measurement; otherwise clear the selection |
Expand Down
356 changes: 178 additions & 178 deletions src/IOViewer/Viewer/dist/viewer.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/IOViewer/Viewer/playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineConfig({
{ name: 'chromium', use: { browserName: 'chromium' } },
{
name: 'firefox',
testMatch: ['**/smoke.spec.js', '**/selection.spec.js', '**/sections.spec.js'],
testMatch: ['**/smoke.spec.js', '**/selection.spec.js', '**/sections.spec.js', '**/accessibility.spec.js'],
use: {
browserName: 'firefox',
// Under Linux, Xvfb supplies the display Firefox uses to create a WebGL context in CI.
Expand Down
2 changes: 2 additions & 0 deletions src/IOViewer/Viewer/src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const dictionaries = {
'tool.grids': 'Оси',
'tool.opacity': 'Прозрачность',
'tool.fullscreen': 'Полный экран',
'contextMenu': 'Контекстное меню',
'menu.properties': 'Свойства',
'menu.hide': 'Скрыть',
'menu.isolate': 'Изолировать',
Expand Down Expand Up @@ -150,6 +151,7 @@ const dictionaries = {
'tool.grids': 'Grids',
'tool.opacity': 'Transparency',
'tool.fullscreen': 'Full screen',
'contextMenu': 'Context menu',
'menu.properties': 'Properties',
'menu.hide': 'Hide',
'menu.isolate': 'Isolate',
Expand Down
13 changes: 13 additions & 0 deletions src/IOViewer/Viewer/src/ui/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function mountUI({ root, data, scene, controls, picker, selection, measur
elements: meta.elements,
selection,
scene,
openKeyboardMenu,
activate: (i) => {
selection.set([i]);
controls.zoomToBox(scene.elementBox(i));
Expand Down Expand Up @@ -97,6 +98,17 @@ export function mountUI({ root, data, scene, controls, picker, selection, measur
}

const contextMenu = createContextMenu(root);
function openKeyboardMenu(anchor, index = null, restore = () => anchor.focus({ preventScroll: true })) {
if (contextMenu.isOpen) return;
const fromCanvas = anchor === document.body;
const rect = (fromCanvas ? controls.dom : anchor).getBoundingClientRect();
if (index !== null && !selection.has(index)) selection.set([index]);
const [first] = selection.values();
const target = index ?? first ?? null;
const x = fromCanvas ? rect.left + rect.width / 2 : rect.left;
const y = fromCanvas ? rect.top + rect.height / 2 : rect.bottom;
contextMenu.open(target === null ? 'empty' : 'element', x, y, target, restore);
}
contextMenu.setItem('properties', { handler: (i) => selection.set([i]) });
for (const id of ['hide', 'isolate', 'showAll', 'zoomTo', 'fitAll']) contextMenu.setItem(id, { handler: actions[id] });
contextMenu.setItem('copyId', {
Expand All @@ -123,6 +135,7 @@ export function mountUI({ root, data, scene, controls, picker, selection, measur
const escapeHandlers = [];
installHotkeys(window, {
...actions,
contextMenu: () => openKeyboardMenu(document.activeElement ?? document.body),
// Esc closes an open menu and keeps the selection; otherwise a feature may use it; otherwise it clears the selection.
escape: () => {
if (contextMenu.isOpen) return contextMenu.close();
Expand Down
52 changes: 48 additions & 4 deletions src/IOViewer/Viewer/src/ui/contextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const shortcuts = { hide: 'H', isolate: 'I', showAll: 'Shift+H', zoomTo: 'F' };
const MARGIN = 4; // px kept free between the menu and the window edge

/**
* Right-click menu. open(kind, x, y, target) shows menus[kind] at client (x, y), kept inside the window;
* Context menu. open(kind, x, y, target) shows menus[kind] at client (x, y), kept inside the window;
* an item calls its handler with `target` and closes the menu. setItem(id, { enabled, handler }) configures
* an item; items without a handler are shown disabled. Closes on wheel, resize, page scroll (not on scrolling inside
* the UI panels, which cannot move the menu) and on a press outside it; that press only dismisses the menu,
Expand All @@ -19,18 +19,23 @@ const MARGIN = 4; // px kept free between the menu and the window edge
export function createContextMenu(root) {
const menu = el('div', 'ioviewer-context-menu');
menu.setAttribute('role', 'menu');
menu.setAttribute('aria-label', t('contextMenu'));
menu.hidden = true;
root.append(menu);
const items = new Map(Object.values(menus).flat().filter((id) => id !== '-').map((id) => [id, { enabled: false, handler: null }]));
let returnFocus = null;

function close() {
if (menu.hidden) return;
const restore = menu.contains(document.activeElement) ? returnFocus : null;
returnFocus = null;
menu.hidden = true;
menu.replaceChildren();
window.removeEventListener('pointerdown', outside, true);
window.removeEventListener('wheel', close, true);
window.removeEventListener('scroll', pageScroll, true);
window.removeEventListener('resize', close);
restore?.();
}

function pageScroll(e) {
Expand All @@ -44,23 +49,28 @@ export function createContextMenu(root) {
swallowPress(e);
}

function open(kind, x, y, target) {
function open(kind, x, y, target, restoreFocus = null) {
const layout = menus[kind];
if (!layout) throw new RangeError(`Unknown menu: ${kind}`);
close();
returnFocus = restoreFocus;
for (const id of layout) {
if (id === '-') {
menu.append(el('div', 'ioviewer-menu-separator'));
const separator = el('div', 'ioviewer-menu-separator');
separator.setAttribute('role', 'separator');
menu.append(separator);
continue;
}
const item = items.get(id);
const button = el('button', 'ioviewer-menu-item');
button.type = 'button';
button.tabIndex = -1;
button.dataset.item = id;
button.setAttribute('role', 'menuitem');
button.disabled = !item.enabled;
button.setAttribute('aria-disabled', String(!item.enabled));
button.append(el('span', '', t(`menu.${id}`)), el('kbd', '', shortcuts[id] ?? ''));
button.addEventListener('click', () => {
if (!item.enabled) return;
close();
item.handler(target);
});
Expand All @@ -74,8 +84,42 @@ export function createContextMenu(root) {
window.addEventListener('wheel', close, true);
window.addEventListener('scroll', pageScroll, true);
window.addEventListener('resize', close);
if (restoreFocus) menu.querySelector('button')?.focus({ preventScroll: true });
}

menu.addEventListener('keydown', (e) => {
if (e.key === 'Tab') {
close();
return;
}
if (e.key === 'Escape') {
e.preventDefault();
close();
return;
}
const buttons = [...menu.querySelectorAll('button')];
const current = buttons.indexOf(document.activeElement);
let next;
switch (e.key) {
case 'ArrowDown':
next = (current + 1) % buttons.length;
break;
case 'ArrowUp':
next = (current + buttons.length - 1) % buttons.length;
break;
case 'Home':
next = 0;
break;
case 'End':
next = buttons.length - 1;
break;
default:
return;
}
e.preventDefault();
buttons[next]?.focus({ preventScroll: true });
});

function setItem(id, { enabled = true, handler }) {
const item = items.get(id);
if (!item) throw new RangeError(`Unknown menu item: ${id}`);
Expand Down
6 changes: 6 additions & 0 deletions src/IOViewer/Viewer/src/ui/hotkeys.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const FIELDS = 'input, textarea, select, [contenteditable]';

export function isContextMenuKey(e) {
return e.key === 'ContextMenu' || e.code === 'ContextMenu' || (e.shiftKey && (e.key === 'F10' || e.code === 'F10'));
}

// Action for a keydown, or null. Keys are matched by physical position (KeyboardEvent.code), so H/I/F work in any
// layout, e.g. Russian. Ignored with Ctrl/Alt/Meta (browser shortcuts), on auto-repeat and while typing in a field.
export function hotkeyAction(e) {
if (e.ctrlKey || e.altKey || e.metaKey || e.repeat || e.target?.closest?.(FIELDS)) return null;
if (isContextMenuKey(e)) return 'contextMenu';
switch (e.code) {
case 'KeyH':
return e.shiftKey ? 'showAll' : 'hide';
Expand All @@ -21,6 +26,7 @@ export function hotkeyAction(e) {
// Calls actions[hotkeyAction(e)] for keydowns on `target` (window).
export function installHotkeys(target, actions) {
target.addEventListener('keydown', (e) => {
if (e.defaultPrevented) return;
const action = hotkeyAction(e);
if (!action) return;
e.preventDefault();
Expand Down
Loading
Loading