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

add mac compatibility mode #15

Merged
merged 5 commits into from
Apr 4, 2024
Merged

add mac compatibility mode #15

merged 5 commits into from
Apr 4, 2024

Conversation

myandrienko
Copy link
Contributor

@myandrienko myandrienko commented Mar 6, 2024

This change is potentially controversial, so consider it RFC rather than something final :)

On Mac a common modifier for hotkeys is Meta (aka Cmd, aka ), while Ctrl is usually avoided. On the other hand, it's very uncommon for the Meta key to be used for app-level hotkeys on Windows or Linux.

To provide a familiar experience for users on all platforms, I suggest the Mac compatibility mode, which treats the Meta modifier as if it is Ctrl on Macs (unless both are used). So on Mac meta+b invokes ctrl+b, meta+ctrl+b still invokes meta+ctrl+b.

To keep the API minimal, I'm piggybacking on top of the overrides object, because basically this is a special kind of an override. A special opaque object MAC_COMPAT is exported, which can be mixed in with the rest of the overrides.

import { hotkeyKeyUX, startKeyUX, MAC_COMPAT } from 'keyux'

startKeyUX(window, [
  hotkeyKeyUX({ ...MAC_COMPAT })
])

getHotKeyHint also supports this and renders Ctrl as .

I'm open to all suggestions regarding the API and the idea in general.

@ai
Copy link
Owner

ai commented Mar 6, 2024

I thought about something this as well. I think we should have it, at least as an option.

I will think about architecture after I finish my task at the conference. Around next week

hotkey.js Outdated
@@ -59,14 +61,14 @@ function checkHotkey(where, code, overrides) {
)
}

function findHotKey(event, where, overrides) {
function findHotKey(event, window, where, overrides) {
Copy link
Owner

Choose a reason for hiding this comment

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

Can we use window.document and avoid where?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, done.

compat.js Outdated
const MAC_COMPAT_KEY = Symbol('macCompat')
export const MAC_COMPAT = { [MAC_COMPAT_KEY]: true }

export function applyCompat(code, window, overrides, dir = 'forward') {
Copy link
Owner

Choose a reason for hiding this comment

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

Since we are fighting for every bite of the library, let’s remove dir argument if we are not using it right now.

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 don't really like dir as well, but we need some kind of indication of the direction we're transforming in: either one way for hotkeys (meta -> ctrl), or in the reverse direction for hints (ctrl -> meta). I removed the default parameter value though.

README.md Outdated Show resolved Hide resolved
@myandrienko
Copy link
Contributor Author

Glad you're back from the conf :) Updated the API.

@myandrienko myandrienko requested a review from ai March 12, 2024 10:51
compat.js Outdated Show resolved Hide resolved
compat.js Outdated
return (code, window, dir) => {
if (
window.navigator.platform.indexOf('Mac') === 0 &&
code.indexOf('meta+ctrl') === -1
Copy link
Owner

Choose a reason for hiding this comment

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

Let's use includes() instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

index.d.ts Outdated
@@ -49,7 +55,10 @@ export type HotkeyOverride = Record<string, string>
* ])
* ```
*/
export function hotkeyKeyUX(overrides?: HotkeyOverride): KeyUXModule
export function hotkeyKeyUX(
overrides?: HotkeyOverride,
Copy link
Owner

Choose a reason for hiding this comment

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

Hm, can be overrides a transformer too?

hotkeyKeyUX([overrides(config), macCompact])

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I mentioned that earlier: #15 (comment) :)

Since it looks like you agree, I'll go ahead and implement this.

index.js Outdated
let realCode = code
for (let i in overrides) {
if (overrides[i] === code) {
realCode = i
break
}
}
transformers.forEach(
Copy link
Owner

Choose a reason for hiding this comment

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

Let’s write it as:

for (let transformer of transformers) {
  realCode = transform(realCode, window, 'r')
}

To avoid (x = 1)

@myandrienko
Copy link
Contributor Author

@ai Just letting you know, this PR is not abandoned, I will have time to revisit it later this week 👍

@myandrienko myandrienko mentioned this pull request Apr 4, 2024
@myandrienko myandrienko force-pushed the mac-compat branch 2 times, most recently from d313aca to f27a89b Compare April 4, 2024 14:13
@myandrienko
Copy link
Contributor Author

myandrienko commented Apr 4, 2024

I've rebased this PR to be on top of #20. Let's revisit it when (and if) #20 gets merged.

built on top of the transformers api
@myandrienko myandrienko requested a review from ai April 4, 2024 15:51
README.md Outdated

### Mac Compatibility Mode

It's common to use the Meta (<kbd>⌘</kbd>) modifier for hotkeys on Mac, while
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
It's common to use the Meta (<kbd>⌘</kbd>) modifier for hotkeys on Mac, while
Its common to use the Meta (<kbd>⌘</kbd>) modifier for hotkeys on Mac, while

README.md Outdated
### Mac Compatibility Mode

It's common to use the Meta (<kbd>⌘</kbd>) modifier for hotkeys on Mac, while
Window and Linux usually favor the Ctrl key. To provide familiar experience on
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
Window and Linux usually favor the Ctrl key. To provide familiar experience on
Window and Linux usually favor the <kbd>Ctrl</kbd>. To provide familiar experience on

README.md Outdated
getHotKeyHint(window, 'ctrl+b', [macCompat])
```

Hotkeys pressed with the Meta modifier will work as if the Ctrl modifier was
Copy link
Owner

Choose a reason for hiding this comment

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

Let’s use <kbd> on every key mentioning.

The more text modifiers make reading easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, looks nicer for sure.

compat.js Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@myandrienko myandrienko requested a review from ai April 4, 2024 16:04
@ai
Copy link
Owner

ai commented Apr 4, 2024

Nice. Thanks. I will release it after the shower.

Do you have Twitter account to mention you?

@myandrienko
Copy link
Contributor Author

Thanks for reviewing so quickly! I'm not really on Twitter, but if you're comfortable doing so, you can link my Telegram channel: https://t.me/smalldogenergy - although it's fine if you won't :)

@ai ai merged commit 022b05d into ai:main Apr 4, 2024
4 checks passed
@ai
Copy link
Owner

ai commented Apr 4, 2024

Thanks. Released in 0.7.

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