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
43 changes: 43 additions & 0 deletions src/App.keyboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,46 @@ describe('one rail at a time on a tablet held upright', () => {
expect(saved.library).not.toBe(false);
});
});

describe('keys while a dialog is open', () => {
const share = () =>
document.querySelector<HTMLElement>('button[aria-label="Share"]')!;
const dialog = () => document.querySelector('[aria-modal="true"]');
const playLabel = () =>
document
.querySelector<HTMLElement>(
'button[aria-label="Pause"], button[aria-label="Play"]',
)
?.getAttribute('aria-label');

it('leaves the diagram and the panels alone behind it', () => {
mount();
press(node('mine-cache'));
press(share());
expect(dialog()).not.toBeNull();
const panels = body().value;
const running = playLabel();

key('Delete');
key('Backspace');
key('c');
key('m');
key(' ', { code: 'Space' } as KeyboardEventInit);
key('s');

expect(node('mine-cache')).not.toBeNull();
expect(body().value).toBe(panels);
expect(playLabel()).toBe(running);

key('Escape');
// It closes with a short animation.
act(() => {
vi.advanceTimersByTime(500);
});
expect(dialog()).toBeNull();
// With it closed, the same key reaches the diagram again.
press(node('mine-cache'));
key('Delete');
expect(node('mine-cache')).toBeNull();
});
});
13 changes: 13 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ import { buildCommands, type CommandActions, type CommandFacts } from './appComm
import { loadRecent, pushRecent, saveRecent, type Command } from './commands';
import {
canFullscreen,
dialogOpen,
toggleFullscreen,
useFocusedPart,
useFullscreen,
Expand Down Expand Up @@ -4465,6 +4466,17 @@ export default function App() {
}
}

// An open dialog owns the keys: Space on one of its buttons presses
// the button, not play, and C does not fold a panel behind it. The
// two keys that toggle a dialog still close their own.
if (dialogOpen()) {
const plain = !e.metaKey && !e.ctrlKey && !e.altKey;
const closesOwn =
(e.key === '?' && plain && glossaryOpen) ||
((e.ctrlKey || e.metaKey) && e.code === 'Slash' && shortcutsOpen);
if (!closesOwn) return;
}

/*
* Undo / redo. Bound by e.code, not e.key: KeyZ is the physical key in
* the Z position, so the chord works on layouts (Cyrillic, Greek) where
Expand Down Expand Up @@ -4631,6 +4643,7 @@ export default function App() {
handleDuplicate,
setSpeed,
glossaryOpen,
shortcutsOpen,
openGlossary,
closeGlossary,
toggleLibrary,
Expand Down
8 changes: 7 additions & 1 deletion src/components/Canvas.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { dialogOpen } from '../present';
import {
createElement,
memo,
Expand Down Expand Up @@ -646,8 +647,13 @@

const EMPTY_SELECTION: ReadonlySet<string> = new Set<string>();

/** True when the event should not be intercepted by a canvas key handler. */
/**
* True when the event should not be intercepted by a canvas key handler: the
* reader is typing in a field, or a dialog is open and owns the keys, so
* Delete in Share cannot remove the box selected behind it.
*/
function isTypingTarget(t: EventTarget | null): boolean {
if (dialogOpen()) return true;
const el = t as HTMLElement | null;
if (!el || typeof el.tagName !== 'string') return false;
const tag = el.tagName;
Expand Down Expand Up @@ -735,7 +741,7 @@
* The share of a blocking node's slots that are taken, working or waiting on
* a call it made, or `util` for a node that cannot wait (the field is absent).
*/
export function heldShare(s: NodeStats, cfg: NodeConfig, util: number): number {

Check warning on line 744 in src/components/Canvas.tsx

View workflow job for this annotation

GitHub Actions / check

react(only-export-components)

src/components/Canvas.tsx:744:17: Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
const waiting = s.waitingOnDownstream;
if (waiting === undefined) return util;
const slots =
Expand Down Expand Up @@ -773,7 +779,7 @@
*
* `route` is a client's own end-to-end figures, from `Engine.routes()`.
*/
export function readoutFor(

Check warning on line 782 in src/components/Canvas.tsx

View workflow job for this annotation

GitHub Actions / check

react(only-export-components)

src/components/Canvas.tsx:782:17: Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
kind: NodeKind,
s: NodeStats,
cfg: NodeConfig,
Expand Down Expand Up @@ -1544,7 +1550,7 @@
* Exported for App.tsx, whose sparkline recorder plots the same series the
* readout headlines.
*/
export function sourceBacklogs(

Check warning on line 1553 in src/components/Canvas.tsx

View workflow job for this annotation

GitHub Actions / check

react(only-export-components)

src/components/Canvas.tsx:1553:17: Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
topology: Topology,
nodes: Record<string, NodeStats>,
): Map<string, number> {
Expand Down Expand Up @@ -5376,7 +5382,7 @@
} else {
width = clamp(place(p.annRect.w + dx), NOTE_MIN_WIDTH, NOTE_MAX_WIDTH);
}
onResizeNote?.(p.hit.id!, x, width);

Check warning on line 5385 in src/components/Canvas.tsx

View workflow job for this annotation

GitHub Actions / check

react-hooks(exhaustive-deps)

src/components/Canvas.tsx:5385:9: React Hook useCallback has missing dependencies: 'onResizeNote', and 'onScaleNote'
return;
}

Expand Down Expand Up @@ -5692,25 +5698,25 @@

cancelGesture();
},
[
pendingLink,
nodeAt,
canLink,
onConnect,
onDeleteSelection,
onCreateNote,
onCreateSection,
onSetNoteSize,
onSetSectionTone,
onResizeNote,
onScaleNote,
onSetNoteStyle,
clearSelection,
selectOne,
setSelection,
toWorld,
cancelGesture,
],

Check warning on line 5719 in src/components/Canvas.tsx

View workflow job for this annotation

GitHub Actions / check

react-hooks(exhaustive-deps)

src/components/Canvas.tsx:5701:5: React Hook useCallback has unnecessary dependency: onScaleNote

Check warning on line 5719 in src/components/Canvas.tsx

View workflow job for this annotation

GitHub Actions / check

react-hooks(exhaustive-deps)

src/components/Canvas.tsx:5701:5: React Hook useCallback has unnecessary dependency: onResizeNote
);

/* ---------------- pointercancel: the browser took the pointer ----------
Expand Down
15 changes: 15 additions & 0 deletions src/learn/bottleneck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ describe('findBottleneck', () => {
expect(wrong).toEqual([]);
}, 60_000);

it('says "its only slot" for a part with one slot, not "all 1 slots"', () => {
const t = preset('single-server');
const db = idOf(t, 'Database');
const one: Topology = {
...t,
nodes: t.nodes.map((n) =>
n.id === db ? { ...n, config: { ...n.config, capacity: 1, instances: 1 } } : n,
),
};
const run = runFor(one, 10);
expect(problemAt(run)).toBe(db);
expect(run.verdict.text).toContain('Database: its only slot busy');
expect(run.verdict.text).not.toContain('all 1 ');
});

it('names the database in the single server example at 270 a second', () => {
const run = runFor(scaled(preset('single-server'), 5.4), 10);
const db = idOf(run.topology, 'Database');
Expand Down
15 changes: 11 additions & 4 deletions src/learn/bottleneck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ export function findBottleneck(topology: Topology, snapshot: SimSnapshot): Verdi

/* ---------------- the sentences ---------------- */

/** "all 6 slots", or "its only slot" when there is one: "all 1 slots" is not English. */
function allOf(n: number, one: string, many: string): string {
return n === 1 ? `its only ${one}` : `all ${formatCount(n)} ${many}`;
}

/** "all 6 slots busy, 34 waiting in line", or the closest honest phrase for the kind. */
export function stateClause(
r: Reading,
Expand All @@ -341,18 +346,20 @@ export function stateClause(
// Full only because calls hold slots: the box reads busy as the working
// share alone, so a bare "all busy" would contradict it.
parts.push(
`all ${formatCount(slots)} slots taken, ${formatCount(r.s.inFlight)} working and ${formatCount(held)} waiting on its calls`,
`${allOf(slots, 'slot', 'slots')} taken, ${formatCount(r.s.inFlight)} working and ${formatCount(held)} waiting on its calls`,
);
} else if (slots !== null) {
parts.push(`all ${formatCount(slots)} slots busy`);
parts.push(`${allOf(slots, 'slot', 'slots')} busy`);
} else if (r.node.kind === 'shard') {
parts.push(`its busiest partition is at ${formatPct(r.fill)}`);
} else if (r.node.kind === 'lambda') {
parts.push(
`all ${formatCount(r.node.config.maxConcurrency ?? 0)} copies running`,
`${allOf(r.node.config.maxConcurrency ?? 0, 'copy', 'copies')} running`,
);
} else if (r.node.kind === 'websocket') {
parts.push(`all ${formatCount(r.s.maxConnections ?? 0)} connections in use`);
parts.push(
`${allOf(r.s.maxConnections ?? 0, 'connection', 'connections')} in use`,
);
} else {
parts.push(`${formatPct(r.fill)} busy`);
}
Expand Down
11 changes: 10 additions & 1 deletion src/present.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ export function isTypingTarget(t: EventTarget | null): boolean {
);
}

/**
* Whether a dialog (Share, Settings, the palette, the glossary...) is open.
* It owns the keyboard then: a letter, Space or Delete pressed in it must
* not reach the diagram behind it.
*/
export function dialogOpen(): boolean {
return document.querySelector('[aria-modal="true"]') !== null;
}

/** What a key press means here, or null for a key this mode leaves alone. */
export function presentKeyAction(
e: Pick<
Expand Down Expand Up @@ -130,7 +139,7 @@ export function usePresenting(
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
// A dialog (the palette, the shortcuts, a picker) owns the keys.
if (document.querySelector('[aria-modal="true"]')) return;
if (dialogOpen()) return;
const act = presentKeyAction(e, live.current);
if (act === null) return;
e.preventDefault();
Expand Down
Loading