Skip to content

Commit

Permalink
macOS shortcuts: Only listen for command key, not control key
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal authored and kenpowers-signal committed Dec 17, 2019
1 parent d86e8ef commit 2c7baad
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 39 deletions.
40 changes: 21 additions & 19 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,9 @@
const { altKey, ctrlKey, key, metaKey, shiftKey } = event;

const optionOrAlt = altKey;
const ctrlOrCommand = metaKey || ctrlKey;
const commandKey = window.platform === 'darwin' && metaKey;
const controlKey = window.platform !== 'darwin' && ctrlKey;
const commandOrCtrl = commandKey || controlKey;

const state = store.getState();
const selectedId = state.conversations.selectedConversation;
Expand All @@ -715,7 +717,7 @@

// Show keyboard shortcuts - handled by Electron-managed keyboard shortcuts
// However, on linux Ctrl+/ selects all text, so we prevent that
if (ctrlOrCommand && key === '/') {
if (commandOrCtrl && key === '/') {
window.showKeyboardShortcuts();

event.stopPropagation();
Expand All @@ -725,7 +727,7 @@
}

// Navigate by section
if (ctrlOrCommand && !shiftKey && (key === 't' || key === 'T')) {
if (commandOrCtrl && !shiftKey && (key === 't' || key === 'T')) {
window.enterKeyboardMode();
const focusedElement = document.activeElement;

Expand Down Expand Up @@ -924,7 +926,7 @@
// Open the top-right menu for current conversation
if (
conversation &&
ctrlOrCommand &&
commandOrCtrl &&
shiftKey &&
(key === 'l' || key === 'L')
) {
Expand Down Expand Up @@ -965,7 +967,7 @@
}

// Search
if (ctrlOrCommand && !shiftKey && (key === 'f' || key === 'F')) {
if (commandOrCtrl && !shiftKey && (key === 'f' || key === 'F')) {
const { startSearch } = actions.search;
startSearch();

Expand All @@ -977,7 +979,7 @@
// Search in conversation
if (
conversation &&
ctrlOrCommand &&
commandOrCtrl &&
shiftKey &&
(key === 'f' || key === 'F')
) {
Expand All @@ -995,7 +997,7 @@
// Focus composer field
if (
conversation &&
ctrlOrCommand &&
commandOrCtrl &&
shiftKey &&
(key === 't' || key === 'T')
) {
Expand All @@ -1008,7 +1010,7 @@
// Open all media
if (
conversation &&
ctrlOrCommand &&
commandOrCtrl &&
shiftKey &&
(key === 'm' || key === 'M')
) {
Expand All @@ -1025,7 +1027,7 @@
// Begin recording voice note
if (
conversation &&
ctrlOrCommand &&
commandOrCtrl &&
shiftKey &&
(key === 'v' || key === 'V')
) {
Expand All @@ -1039,7 +1041,7 @@
if (
conversation &&
!conversation.get('isArchived') &&
ctrlOrCommand &&
commandOrCtrl &&
shiftKey &&
(key === 'a' || key === 'A')
) {
Expand Down Expand Up @@ -1074,7 +1076,7 @@
if (
conversation &&
conversation.get('isArchived') &&
ctrlOrCommand &&
commandOrCtrl &&
shiftKey &&
(key === 'u' || key === 'U')
) {
Expand All @@ -1096,7 +1098,7 @@
// Close conversation
if (
conversation &&
ctrlOrCommand &&
commandOrCtrl &&
shiftKey &&
(key === 'c' || key === 'C')
) {
Expand All @@ -1111,7 +1113,7 @@
// Show message details
if (
conversation &&
ctrlOrCommand &&
commandOrCtrl &&
!shiftKey &&
(key === 'd' || key === 'D')
) {
Expand All @@ -1129,7 +1131,7 @@
// Toggle reply to message
if (
conversation &&
ctrlOrCommand &&
commandOrCtrl &&
shiftKey &&
(key === 'r' || key === 'R')
) {
Expand All @@ -1144,7 +1146,7 @@
// Save attachment
if (
conversation &&
ctrlOrCommand &&
commandOrCtrl &&
!shiftKey &&
(key === 's' || key === 'S')
) {
Expand All @@ -1161,7 +1163,7 @@

if (
conversation &&
ctrlOrCommand &&
commandOrCtrl &&
shiftKey &&
(key === 'd' || key === 'D')
) {
Expand All @@ -1187,7 +1189,7 @@
// Attach file
if (
conversation &&
ctrlOrCommand &&
commandOrCtrl &&
!shiftKey &&
(key === 'u' || key === 'U')
) {
Expand All @@ -1201,7 +1203,7 @@
// Remove draft link preview
if (
conversation &&
ctrlOrCommand &&
commandOrCtrl &&
!shiftKey &&
(key === 'p' || key === 'P')
) {
Expand All @@ -1215,7 +1217,7 @@
// Attach file
if (
conversation &&
ctrlOrCommand &&
commandOrCtrl &&
shiftKey &&
(key === 'p' || key === 'P')
) {
Expand Down
9 changes: 6 additions & 3 deletions ts/components/CompositionArea.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { Editor } from 'draft-js';
import { noop } from 'lodash';
import { get, noop } from 'lodash';
import classNames from 'classnames';
import {
EmojiButton,
Expand Down Expand Up @@ -293,9 +293,12 @@ export const CompositionArea = ({
const { key, shiftKey, ctrlKey, metaKey } = e;
// When using the ctrl key, `key` is `'X'`. When using the cmd key, `key` is `'x'`
const xKey = key === 'x' || key === 'X';
const cmdOrCtrl = ctrlKey || metaKey;
const commandKey = get(window, 'platform') === 'darwin' && metaKey;
const controlKey = get(window, 'platform') !== 'darwin' && ctrlKey;
const commandOrCtrl = commandKey || controlKey;

// cmd/ctrl-shift-x
if (xKey && shiftKey && cmdOrCtrl) {
if (xKey && shiftKey && commandOrCtrl) {
e.preventDefault();
setLarge(x => !x);
}
Expand Down
5 changes: 4 additions & 1 deletion ts/components/CompositionInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -653,14 +653,17 @@ export const CompositionInput = ({
const editorKeybindingFn = React.useCallback(
// tslint:disable-next-line cyclomatic-complexity
(e: React.KeyboardEvent): CompositionInputEditorCommand | null => {
const commandKey = get(window, 'platform') === 'darwin' && e.metaKey;
const controlKey = get(window, 'platform') !== 'darwin' && e.ctrlKey;

if (e.key === 'Enter' && emojiResults.length > 0) {
e.preventDefault();

return 'enter-emoji';
}

if (e.key === 'Enter' && !e.shiftKey) {
if (large && !(e.ctrlKey || e.metaKey)) {
if (large && !(controlKey || commandKey)) {
return getDefaultKeyBinding(e);
}

Expand Down
6 changes: 4 additions & 2 deletions ts/components/LeftPane.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { AutoSizer, List } from 'react-virtualized';
import { debounce } from 'lodash';
import { debounce, get } from 'lodash';

import {
ConversationListItem,
Expand Down Expand Up @@ -128,7 +128,9 @@ export class LeftPane extends React.Component<PropsType> {
};

public handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
const commandOrCtrl = event.metaKey || event.ctrlKey;
const commandKey = get(window, 'platform') === 'darwin' && event.metaKey;
const controlKey = get(window, 'platform') !== 'darwin' && event.ctrlKey;
const commandOrCtrl = commandKey || controlKey;

if (commandOrCtrl && !event.shiftKey && event.key === 'ArrowUp') {
this.scrollToRow(0);
Expand Down
8 changes: 5 additions & 3 deletions ts/components/MainHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import classNames from 'classnames';
import { debounce } from 'lodash';
import { debounce, get } from 'lodash';
import { Manager, Popper, Reference } from 'react-popper';
import { createPortal } from 'react-dom';

Expand Down Expand Up @@ -222,10 +222,12 @@ export class MainHeader extends React.Component<PropsType, StateType> {
} = this.props;

const { ctrlKey, metaKey, key } = event;
const ctrlOrCommand = ctrlKey || metaKey;
const commandKey = get(window, 'platform') === 'darwin' && metaKey;
const controlKey = get(window, 'platform') !== 'darwin' && ctrlKey;
const commandOrCtrl = commandKey || controlKey;

// On linux, this keyboard combination selects all text
if (ctrlOrCommand && key === '/') {
if (commandOrCtrl && key === '/') {
event.preventDefault();
event.stopPropagation();

Expand Down
6 changes: 4 additions & 2 deletions ts/components/SearchResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
CellMeasurerCache,
List,
} from 'react-virtualized';
import { debounce, isNumber } from 'lodash';
import { debounce, get, isNumber } from 'lodash';

import { Intl } from './Intl';
import { Emojify } from './conversation/Emojify';
Expand Down Expand Up @@ -136,7 +136,9 @@ export class SearchResults extends React.Component<PropsType, StateType> {

public handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
const { items } = this.props;
const commandOrCtrl = event.metaKey || event.ctrlKey;
const commandKey = get(window, 'platform') === 'darwin' && event.metaKey;
const controlKey = get(window, 'platform') !== 'darwin' && event.ctrlKey;
const commandOrCtrl = commandKey || controlKey;

if (!items || items.length < 1) {
return;
Expand Down
6 changes: 4 additions & 2 deletions ts/components/conversation/Timeline.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debounce, isNumber } from 'lodash';
import { debounce, get, isNumber } from 'lodash';
import React from 'react';
import {
AutoSizer,
Expand Down Expand Up @@ -926,7 +926,9 @@ export class Timeline extends React.PureComponent<Props, State> {

public handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
const { selectMessage, selectedMessageId, items, id } = this.props;
const commandOrCtrl = event.metaKey || event.ctrlKey;
const commandKey = get(window, 'platform') === 'darwin' && event.metaKey;
const controlKey = get(window, 'platform') !== 'darwin' && event.ctrlKey;
const commandOrCtrl = commandKey || controlKey;

if (!items || items.length < 1) {
return;
Expand Down
8 changes: 5 additions & 3 deletions ts/components/emoji/EmojiButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import classNames from 'classnames';
import { noop } from 'lodash';
import { get, noop } from 'lodash';
import { Manager, Popper, Reference } from 'react-popper';
import { createPortal } from 'react-dom';
import {
Expand Down Expand Up @@ -86,15 +86,17 @@ export const EmojiButton = React.memo(
() => {
const handleKeydown = (event: KeyboardEvent) => {
const { ctrlKey, key, metaKey, shiftKey } = event;
const ctrlOrCommand = metaKey || ctrlKey;
const commandKey = get(window, 'platform') === 'darwin' && metaKey;
const controlKey = get(window, 'platform') !== 'darwin' && ctrlKey;
const commandOrCtrl = commandKey || controlKey;

// We don't want to open up if the conversation has any panels open
const panels = document.querySelectorAll('.conversation .panel');
if (panels && panels.length > 1) {
return;
}

if (ctrlOrCommand && shiftKey && (key === 'j' || key === 'J')) {
if (commandOrCtrl && shiftKey && (key === 'j' || key === 'J')) {
event.stopPropagation();
event.preventDefault();

Expand Down
8 changes: 5 additions & 3 deletions ts/components/stickers/StickerButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import classNames from 'classnames';
import { noop } from 'lodash';
import { get, noop } from 'lodash';
import { Manager, Popper, Reference } from 'react-popper';
import { createPortal } from 'react-dom';
import { StickerPicker } from './StickerPicker';
Expand Down Expand Up @@ -152,15 +152,17 @@ export const StickerButton = React.memo(
() => {
const handleKeydown = (event: KeyboardEvent) => {
const { ctrlKey, key, metaKey, shiftKey } = event;
const ctrlOrCommand = metaKey || ctrlKey;
const commandKey = get(window, 'platform') === 'darwin' && metaKey;
const controlKey = get(window, 'platform') !== 'darwin' && ctrlKey;
const commandOrCtrl = commandKey || controlKey;

// We don't want to open up if the conversation has any panels open
const panels = document.querySelectorAll('.conversation .panel');
if (panels && panels.length > 1) {
return;
}

if (ctrlOrCommand && shiftKey && (key === 's' || key === 'S')) {
if (commandOrCtrl && shiftKey && (key === 's' || key === 'S')) {
event.stopPropagation();
event.preventDefault();

Expand Down
2 changes: 1 addition & 1 deletion ts/util/lint/exceptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -7577,7 +7577,7 @@
"rule": "React-createRef",
"path": "ts/components/MainHeader.js",
"line": " this.inputRef = react_1.default.createRef();",
"lineNumber": 142,
"lineNumber": 144,
"reasonCategory": "usageTrusted",
"updated": "2019-08-09T21:17:57.798Z",
"reasonDetail": "Used only to set focus"
Expand Down

0 comments on commit 2c7baad

Please sign in to comment.