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 @@ -14,6 +14,7 @@ API between minor releases.
- 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.
- Select multiple elements in the model tree and open the viewer context menu from its rows.

### Fixed

Expand Down
5 changes: 3 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ 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, 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
CI also runs the smoke, selection, section, keyboard-accessibility and model-tree selection tests in Firefox.
It 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
`xvfb-run -a` on machines without a display.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ 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 |
| Ctrl / Cmd + click or Space in the model tree | Toggle an element or all elements in a category |
| Shift + click or Shift+Space in the model tree | Select a range of expanded or filtered rows from the last individually selected or toggled row |
| Ctrl / Cmd + Shift + click in the model tree | Add a range to the current selection |
| 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 |
Expand Down
62 changes: 31 additions & 31 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', '**/accessibility.spec.js'],
testMatch: ['**/smoke.spec.js', '**/selection.spec.js', '**/sections.spec.js', '**/accessibility.spec.js', '**/tree-selection.spec.js'],
use: {
browserName: 'firefox',
// Under Linux, Xvfb supplies the display Firefox uses to create a WebGL context in CI.
Expand Down
24 changes: 10 additions & 14 deletions src/IOViewer/Viewer/src/ui/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ 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));
},
openContextMenu,
zoomToElement: (i) => controls.zoomToBox(scene.elementBox(i)),
}),
});
const properties = document.createElement('aside');
Expand Down Expand Up @@ -98,16 +95,16 @@ export function mountUI({ root, data, scene, controls, picker, selection, measur
}

const contextMenu = createContextMenu(root);
function openKeyboardMenu(anchor, index = null, restore = () => anchor.focus({ preventScroll: true })) {
function openContextMenu(x, y, target, restoreFocus) {
contextMenu.open(target === null ? 'empty' : 'element', x, y, target, restoreFocus);
}
function openKeyboardMenu(anchor) {
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);
openContextMenu(x, y, selection.values().next().value ?? null, () => anchor.focus({ preventScroll: true }));
}
contextMenu.setItem('properties', { handler: (i) => selection.set([i]) });
for (const id of ['hide', 'isolate', 'showAll', 'zoomTo', 'fitAll']) contextMenu.setItem(id, { handler: actions[id] });
Expand All @@ -123,13 +120,12 @@ export function mountUI({ root, data, scene, controls, picker, selection, measur
controls.addEventListener('contextmenu', (e) => {
const { x, y } = e.detail;
if (measure.active) {
const [first] = selection.values();
return first === undefined ? contextMenu.open('empty', x, y, null) : contextMenu.open('element', x, y, first);
return openContextMenu(x, y, selection.values().next().value ?? null);
}
const hit = picker.pick(x, y);
if (!hit || hit.element < 0) return contextMenu.open('empty', x, y, null);
if (!hit || hit.element < 0) return openContextMenu(x, y, null);
if (!selection.has(hit.element)) selection.set([hit.element]);
contextMenu.open('element', x, y, hit.element);
openContextMenu(x, y, hit.element);
});

const escapeHandlers = [];
Expand Down
10 changes: 7 additions & 3 deletions src/IOViewer/Viewer/src/ui/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@ function swallow(e) {
e.stopPropagation();
}

// Swallows a whole press from its capturing pointerdown `e`: that pointerdown, its pointerup and the click that
// follows, so a press that dismisses a menu or popover never reaches the model or the panels.
// Swallows the pointerdown, pointerup and resulting click or context menu of a dismissing press.
export function swallowPress(e) {
swallow(e);
const context = e.button === 2;
const release = () => {
window.removeEventListener('pointerup', swallowUp, true);
setTimeout(() => window.removeEventListener('click', swallow, true)); // the click, if any, comes in this task
setTimeout(() => {
window.removeEventListener('click', swallow, true);
if (context) window.removeEventListener('contextmenu', swallow, true);
}); // the activation, if any, comes in this task
};
const swallowUp = (up) => {
swallow(up);
release();
};
window.addEventListener('pointerup', swallowUp, true);
window.addEventListener('click', swallow, true);
if (context) window.addEventListener('contextmenu', swallow, true);
}

// Puts a screen-space label (position: absolute in the full-window #ioviewer-ui) over the scene point `point`;
Expand Down
104 changes: 89 additions & 15 deletions src/IOViewer/Viewer/src/ui/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,23 @@ export function groupByCategory(elements) {

/**
* Model tree in `container`: collapsed categories with counts, then elements (name, muted type); a search flattens
* matches. Rows in view and the focused row exist in the DOM. Activating an element calls activate(index);
* matches. Rows in view and the focused row exist in the DOM. Activating an element selects and zooms to it;
* selecting elsewhere scrolls to and highlights the last selected row, expanding its category. Hidden elements are dimmed.
*/
export function mountTree(container, { elements, selection, scene, activate, openKeyboardMenu }) {
export function mountTree(container, { elements, selection, scene, zoomToElement, openContextMenu }) {
const groups = groupByCategory(elements);
const categoryOf = new Int32Array(elements.length);
groups.forEach((group, c) => group.indices.forEach((i) => (categoryOf[i] = c)));
const expanded = new Uint8Array(groups.length);
const groupRows = new Int32Array(groups.length);
const selectedCounts = new Uint32Array(groups.length);
const search = createSearch(elements);
let query = '';
let rows = new Int32Array(0); // element index, or -(category + 1) for a category header
let tabRow = 0;
let pendingRow = -1; // row to scroll to once the tree has a size (it is laid out only while its panel is open)
let rangeAnchor = null; // row value, stable across filtering, expansion and scrolling
let selectingInTree = false;

const input = el('input', 'ioviewer-tree-search');
input.type = 'search';
Expand Down Expand Up @@ -110,14 +113,17 @@ export function mountTree(container, { elements, selection, scene, activate, ope

function row(r) {
const value = rows[r];
const c = value < 0 ? -value - 1 : categoryOf[value];
const selected = value < 0 ? selectedCounts[c] === groups[c].indices.length : selection.has(value);
const node = el('div', 'tree-row');
node.style.top = `${r * ROW_HEIGHT}px`;
node.style.height = `${ROW_HEIGHT}px`;
node.dataset.row = r;
node.tabIndex = r === tabRow ? 0 : -1;
node.setAttribute('role', 'treeitem');
node.classList.toggle('selected', selected);
node.setAttribute('aria-selected', String(selected));
if (value < 0) {
const c = -value - 1;
node.classList.add('tree-category');
node.setAttribute('aria-level', '1');
node.setAttribute('aria-posinset', String(c + 1));
Expand All @@ -129,16 +135,12 @@ export function mountTree(container, { elements, selection, scene, activate, ope
node.append(chevron, el('span', 'tree-name', name || t('noCategory')), el('span', 'tree-count', String(indices.length)));
} else {
const element = elements[value];
const selected = selection.has(value);
const c = categoryOf[value];
node.classList.add('tree-element');
node.setAttribute('aria-level', query ? '1' : '2');
node.setAttribute('aria-posinset', String(query ? r + 1 : r - groupRows[c]));
node.setAttribute('aria-setsize', String(query ? rows.length : groups[c].indices.length));
node.classList.toggle('tree-child', !query);
node.classList.toggle('selected', selected);
node.classList.toggle('tree-hidden', !scene.isVisible(value));
node.setAttribute('aria-selected', String(selected));
node.append(el('span', 'tree-name', element.name || `#${element.id}`), el('span', 'tree-type', element.type ?? ''));
}
return node;
Expand Down Expand Up @@ -175,6 +177,51 @@ export function mountTree(container, { elements, selection, scene, activate, ope
render();
}

function selectInTree(indices) {
selectingInTree = true;
try {
selection.set(indices);
} finally {
selectingInTree = false;
}
}

function selectRange(r, additive) {
let anchor = rows.indexOf(rangeAnchor);
if (anchor < 0) {
anchor = r;
rangeAnchor = rows[r];
}
const range = rows.subarray(Math.min(anchor, r), Math.max(anchor, r) + 1).filter((value) => value >= 0);
if (range.length > 0) selectInTree(additive ? [...selection.values(), ...range] : range);
}

function toggleRow(value) {
rangeAnchor = value;
const c = -value - 1;
const targets = value < 0 ? groups[c].indices : [value];
const remove = value < 0 ? selectedCounts[c] === targets.length : selection.has(value);
const selected = new Set(selection.values());
for (const i of targets) {
if (remove) selected.delete(i);
else selected.add(i);
}
selectInTree(selected);
}

function openRowMenu(value, x, y) {
if (value >= 0 && !selection.has(value)) {
rangeAnchor = value;
selectInTree([value]);
}
const target = value >= 0 ? value : selection.values().next().value ?? null;
openContextMenu(x, y, target, () => {
const current = rows.indexOf(value);
if (current >= 0) focusRow(current);
else input.focus();
});
}

// Tab can follow opening the panel before the ResizeObserver delivers its first visible size.
input.addEventListener('focus', () => {
if (!spacer.firstElementChild) syncLayout();
Expand Down Expand Up @@ -202,11 +249,16 @@ export function mountTree(container, { elements, selection, scene, activate, ope
if (isContextMenuKey(e)) {
e.preventDefault();
if (!e.repeat) {
openKeyboardMenu(node, value >= 0 ? value : null, () => {
const current = rows.indexOf(value);
if (current >= 0) focusRow(current);
else input.focus();
});
const { left, bottom } = node.getBoundingClientRect();
openRowMenu(value, left, bottom);
}
return;
}
if (e.key === ' ') {
e.preventDefault();
if (!e.repeat) {
if (e.shiftKey) selectRange(r, false);
else toggleRow(value);
}
return;
}
Expand Down Expand Up @@ -253,7 +305,9 @@ export function mountTree(container, { elements, selection, scene, activate, ope

function activateRow(value) {
if (value >= 0) {
activate(value);
rangeAnchor = value;
selectInTree([value]);
zoomToElement(value);
return;
}
expanded[-value - 1] ^= 1;
Expand All @@ -262,7 +316,18 @@ export function mountTree(container, { elements, selection, scene, activate, ope

viewport.addEventListener('click', (e) => {
const node = e.target.closest('.tree-row');
if (node) activateRow(rows[Number(node.dataset.row)]);
if (!node) return;
const r = Number(node.dataset.row);
const additive = e.ctrlKey || e.metaKey;
if (e.shiftKey) selectRange(r, additive);
else if (additive) toggleRow(rows[r]);
else activateRow(rows[r]);
});
viewport.addEventListener('contextmenu', (e) => {
const node = e.target.closest('.tree-row');
if (!node) return;
e.preventDefault();
openRowMenu(rows[Number(node.dataset.row)], e.clientX, e.clientY);
});
function syncLayout() {
if (pendingRow >= 0 && viewport.clientHeight > 0) {
Expand All @@ -274,7 +339,16 @@ export function mountTree(container, { elements, selection, scene, activate, ope
new ResizeObserver(syncLayout).observe(viewport);
selection.addEventListener('change', () => {
let last = -1;
for (const i of selection.values()) last = i;
selectedCounts.fill(0);
for (const i of selection.values()) {
selectedCounts[categoryOf[i]]++;
last = i;
}
if (selectingInTree) {
render();
return;
}
rangeAnchor = null;
if (last >= 0) reveal(last);
else render();
});
Expand Down
6 changes: 5 additions & 1 deletion src/IOViewer/Viewer/styles/viewer.css
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,15 @@ input:focus-visible {
color: #64748b;
}

.tree-element.selected {
.tree-row.selected {
background: #1e3a8a;
color: #bfdbfe;
}

.tree-category.selected :is(.tree-chevron, .tree-count) {
color: inherit;
}

.tree-hidden {
opacity: 0.45;
}
Expand Down
Loading
Loading