Skip to content
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

Meta+PageUp/PageDown increments the ViewerTab #3055

Merged
merged 11 commits into from
Feb 9, 2024
Merged

Meta+PageUp/PageDown increments the ViewerTab #3055

merged 11 commits into from
Feb 9, 2024

Conversation

OAGr
Copy link
Contributor

@OAGr OAGr commented Feb 6, 2024

It would be nice to focus() on the first item in the tab, but that state is all the way in the ViewerProvider. Getting this to all work would be a bit annoying, so leaving for now.

@OAGr OAGr requested a review from berekuk as a code owner February 6, 2024 22:27
Copy link

changeset-bot bot commented Feb 6, 2024

🦋 Changeset detected

Latest commit: 2ac5be1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages
Name Type
@quri/squiggle-components Patch
@quri/ui Patch
@quri/versioned-squiggle-components Patch
vscode-squiggle Patch
@quri/squiggle-lang Patch
@quri/prettier-plugin-squiggle Patch
@quri/squiggle-textmate-grammar Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Feb 6, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
quri-hub ✅ Ready (Inspect) Visit Preview Feb 9, 2024 7:23pm
quri-ui ✅ Ready (Inspect) Visit Preview Feb 9, 2024 7:23pm
squiggle-components ✅ Ready (Inspect) Visit Preview Feb 9, 2024 7:23pm
squiggle-website ✅ Ready (Inspect) Visit Preview Feb 9, 2024 7:23pm


export type Shortcut = {
metaKey?: boolean;
shiftKey?: boolean;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't end up using, but figure we might later

return true;
}

export function useGlobalShortcut(shortcut: Shortcut, act: () => void) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could remove this, as we have "useGlobalShortcut(s)" now, but I could imagine times when this would be a (bit) better (especially if useGlobalShortcut always needs memoization) . Not sure though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this hook, but can we express it by calling useGlobalShortcuts (possibly with useMemo for an array, though see my comment below)?

packages/ui/src/hooks/useGlobalShortcut.ts Outdated Show resolved Hide resolved
return true;
}

export function useGlobalShortcut(shortcut: Shortcut, act: () => void) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this hook, but can we express it by calling useGlobalShortcuts (possibly with useMemo for an array, though see my comment below)?

if (
(shortcut.shiftKey && !event.shiftKey) ||
(shortcut.metaKey && !event.metaKey) ||
event.key.toLowerCase() !== shortcut.key.toLowerCase()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically, if the shortcut is e.g. Cmd+1, then Cmd+Shift+1 or Cmd+Option+1 won't work. So it's not enough to check that the event has all modifiers that are required; you should also check that event doesn't have any extra modifiers.

It also means that you should check for altKey and ctrlKey here, which I forgot to do in the previous version.


import { ViewerTab } from "../../lib/utility.js";

const tabs = ["Imports", "Variables", "Exports", "Result", "AST"] as const;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This list is too far from the menu code and could easily get out of sync.

One solution that I like is to write a single custom hook, useViewerTabs:

const [viewerTab, setViewerTab] = useViewerTab({
  output: simulation.output,
  enableGlobalShortcuts: ...
})

That hook would know the ViewerTab shape, so it would be easier to keep the list in sync.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's not enough. Your code already has a problem that I can switch to non-existent items (we show "Variables", "Imports" and "Exports" conditionally).

Also, the source of truth for both which items are present and for the order of items is the dropdown menu, which is not even rendered fully on most cases.

The ideal solution is quite complicated, I obviously don't recommend you do it now, but it'd be something like:

  • Dropdown (or some specialized version of it) would expose an imperative handle with selectNext/selectPrev
  • which means action items would have to register themselves somehow on that list, with stable ids
  • selectNext and selectPrev methods would have to call render() (?) and analyze the list to detect the next value... and this part is especially messy and difficult to do

Ok, another idea:

  • go with useViewerTab idea above
  • return something like { viewerTab: ViewerTab, allViewerTabs: ViewerTab[], setViewerTab: (tab: ViewerTab) => void } from it
  • move the logic for detecting visible tabs from <ViewerMenu> to this hook
  • now you can implement shortcuts in useViewerTab code correctly
  • add another component, <ViewerMenuTabItem tab={viewerTab} output={output} />
  • in ViewerMenu's DropdownMenu, render allViewerTabs.map(tab => <ViewerMenuTabItem tab={tab} output={output} />

Uh, <DropdownMenuHeader>Debugging</DropdownMenuHeader> is a problem... I guess allViewerTabs could have sections, but the boundary between hooks and components could get awkward.

() => [shortcut, act],
[shortcut, act]
);
useGlobalShortcuts([shortCut]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shortCut] creates a new array every time, so memoization is not working. Should be useMemo(() => [[shortcut, act]], ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants