2013-07-10 18:08:55 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "NativeKeyBindings.h"
|
|
|
|
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsCocoaUtils.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2013-09-25 15:21:19 +04:00
|
|
|
#include "mozilla/TextEvents.h"
|
2013-07-10 18:08:55 +04:00
|
|
|
|
2014-03-14 17:13:32 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace widget {
|
2013-07-10 18:08:55 +04:00
|
|
|
|
2016-05-18 22:55:42 +03:00
|
|
|
static LazyLogModule gNativeKeyBindingsLog("NativeKeyBindings");
|
2013-07-10 18:08:55 +04:00
|
|
|
|
2014-03-14 17:13:31 +04:00
|
|
|
NativeKeyBindings* NativeKeyBindings::sInstanceForSingleLineEditor = nullptr;
|
|
|
|
NativeKeyBindings* NativeKeyBindings::sInstanceForMultiLineEditor = nullptr;
|
|
|
|
|
|
|
|
// static
|
|
|
|
NativeKeyBindings* NativeKeyBindings::GetInstance(NativeKeyBindingsType aType) {
|
|
|
|
switch (aType) {
|
|
|
|
case nsIWidget::NativeKeyBindingsForSingleLineEditor:
|
|
|
|
if (!sInstanceForSingleLineEditor) {
|
2014-03-14 17:13:32 +04:00
|
|
|
sInstanceForSingleLineEditor = new NativeKeyBindings();
|
2014-03-14 17:13:31 +04:00
|
|
|
sInstanceForSingleLineEditor->Init(aType);
|
|
|
|
}
|
|
|
|
return sInstanceForSingleLineEditor;
|
|
|
|
case nsIWidget::NativeKeyBindingsForMultiLineEditor:
|
|
|
|
case nsIWidget::NativeKeyBindingsForRichTextEditor:
|
|
|
|
if (!sInstanceForMultiLineEditor) {
|
2014-03-14 17:13:32 +04:00
|
|
|
sInstanceForMultiLineEditor = new NativeKeyBindings();
|
2014-03-14 17:13:31 +04:00
|
|
|
sInstanceForMultiLineEditor->Init(aType);
|
|
|
|
}
|
|
|
|
return sInstanceForMultiLineEditor;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Not implemented");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void NativeKeyBindings::Shutdown() {
|
2014-03-14 17:13:32 +04:00
|
|
|
delete sInstanceForSingleLineEditor;
|
|
|
|
sInstanceForSingleLineEditor = nullptr;
|
|
|
|
delete sInstanceForMultiLineEditor;
|
|
|
|
sInstanceForMultiLineEditor = nullptr;
|
2014-03-14 17:13:31 +04:00
|
|
|
}
|
|
|
|
|
2013-07-10 18:08:55 +04:00
|
|
|
NativeKeyBindings::NativeKeyBindings() {}
|
|
|
|
|
|
|
|
#define SEL_TO_COMMAND(aSel, aCommand) \
|
|
|
|
mSelectorToCommand.Put(reinterpret_cast<struct objc_selector*>(@selector(aSel)), aCommand)
|
|
|
|
|
|
|
|
void NativeKeyBindings::Init(NativeKeyBindingsType aType) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info, ("%p NativeKeyBindings::Init", this));
|
2013-07-10 18:08:55 +04:00
|
|
|
|
|
|
|
// Many selectors have a one-to-one mapping to a Gecko command. Those mappings
|
|
|
|
// are registered in mSelectorToCommand.
|
|
|
|
|
|
|
|
// Selectors from NSResponder's "Responding to Action Messages" section and
|
|
|
|
// from NSText's "Action Methods for Editing" section
|
|
|
|
|
|
|
|
// TODO: Improves correctness of left / right meaning
|
|
|
|
// TODO: Add real paragraph motions
|
|
|
|
|
|
|
|
// SEL_TO_COMMAND(cancelOperation:, );
|
|
|
|
// SEL_TO_COMMAND(capitalizeWord:, );
|
|
|
|
// SEL_TO_COMMAND(centerSelectionInVisibleArea:, );
|
|
|
|
// SEL_TO_COMMAND(changeCaseOfLetter:, );
|
|
|
|
// SEL_TO_COMMAND(complete:, );
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(copy:, Command::Copy);
|
2013-07-10 18:08:55 +04:00
|
|
|
// SEL_TO_COMMAND(copyFont:, );
|
|
|
|
// SEL_TO_COMMAND(copyRuler:, );
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(cut:, Command::Cut);
|
|
|
|
SEL_TO_COMMAND(delete:, Command::Delete);
|
|
|
|
SEL_TO_COMMAND(deleteBackward:, Command::DeleteCharBackward);
|
2013-07-10 18:08:55 +04:00
|
|
|
// SEL_TO_COMMAND(deleteBackwardByDecomposingPreviousCharacter:, );
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(deleteForward:, Command::DeleteCharForward);
|
2013-07-10 18:08:55 +04:00
|
|
|
|
|
|
|
// TODO: deleteTo* selectors are also supposed to add text to a kill buffer
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(deleteToBeginningOfLine:, Command::DeleteToBeginningOfLine);
|
|
|
|
SEL_TO_COMMAND(deleteToBeginningOfParagraph:, Command::DeleteToBeginningOfLine);
|
|
|
|
SEL_TO_COMMAND(deleteToEndOfLine:, Command::DeleteToEndOfLine);
|
|
|
|
SEL_TO_COMMAND(deleteToEndOfParagraph:, Command::DeleteToEndOfLine);
|
2013-07-10 18:08:55 +04:00
|
|
|
// SEL_TO_COMMAND(deleteToMark:, );
|
|
|
|
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(deleteWordBackward:, Command::DeleteWordBackward);
|
|
|
|
SEL_TO_COMMAND(deleteWordForward:, Command::DeleteWordForward);
|
2013-07-10 18:08:55 +04:00
|
|
|
// SEL_TO_COMMAND(indent:, );
|
|
|
|
// SEL_TO_COMMAND(insertBacktab:, );
|
|
|
|
// SEL_TO_COMMAND(insertContainerBreak:, );
|
|
|
|
// SEL_TO_COMMAND(insertLineBreak:, );
|
|
|
|
// SEL_TO_COMMAND(insertNewline:, );
|
|
|
|
// SEL_TO_COMMAND(insertNewlineIgnoringFieldEditor:, );
|
|
|
|
// SEL_TO_COMMAND(insertParagraphSeparator:, );
|
|
|
|
// SEL_TO_COMMAND(insertTab:, );
|
|
|
|
// SEL_TO_COMMAND(insertTabIgnoringFieldEditor:, );
|
|
|
|
// SEL_TO_COMMAND(insertDoubleQuoteIgnoringSubstitution:, );
|
|
|
|
// SEL_TO_COMMAND(insertSingleQuoteIgnoringSubstitution:, );
|
|
|
|
// SEL_TO_COMMAND(lowercaseWord:, );
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(moveBackward:, Command::CharPrevious);
|
|
|
|
SEL_TO_COMMAND(moveBackwardAndModifySelection:, Command::SelectCharPrevious);
|
2014-03-14 17:13:31 +04:00
|
|
|
if (aType == nsIWidget::NativeKeyBindingsForSingleLineEditor) {
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(moveDown:, Command::EndLine);
|
2013-07-10 18:08:55 +04:00
|
|
|
} else {
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(moveDown:, Command::LineNext);
|
2013-07-10 18:08:55 +04:00
|
|
|
}
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(moveDownAndModifySelection:, Command::SelectLineNext);
|
|
|
|
SEL_TO_COMMAND(moveForward:, Command::CharNext);
|
|
|
|
SEL_TO_COMMAND(moveForwardAndModifySelection:, Command::SelectCharNext);
|
|
|
|
SEL_TO_COMMAND(moveLeft:, Command::CharPrevious);
|
|
|
|
SEL_TO_COMMAND(moveLeftAndModifySelection:, Command::SelectCharPrevious);
|
|
|
|
SEL_TO_COMMAND(moveParagraphBackwardAndModifySelection:, Command::SelectBeginLine);
|
|
|
|
SEL_TO_COMMAND(moveParagraphForwardAndModifySelection:, Command::SelectEndLine);
|
|
|
|
SEL_TO_COMMAND(moveRight:, Command::CharNext);
|
|
|
|
SEL_TO_COMMAND(moveRightAndModifySelection:, Command::SelectCharNext);
|
|
|
|
SEL_TO_COMMAND(moveToBeginningOfDocument:, Command::MoveTop);
|
|
|
|
SEL_TO_COMMAND(moveToBeginningOfDocumentAndModifySelection:, Command::SelectTop);
|
|
|
|
SEL_TO_COMMAND(moveToBeginningOfLine:, Command::BeginLine);
|
|
|
|
SEL_TO_COMMAND(moveToBeginningOfLineAndModifySelection:, Command::SelectBeginLine);
|
|
|
|
SEL_TO_COMMAND(moveToBeginningOfParagraph:, Command::BeginLine);
|
|
|
|
SEL_TO_COMMAND(moveToBeginningOfParagraphAndModifySelection:, Command::SelectBeginLine);
|
|
|
|
SEL_TO_COMMAND(moveToEndOfDocument:, Command::MoveBottom);
|
|
|
|
SEL_TO_COMMAND(moveToEndOfDocumentAndModifySelection:, Command::SelectBottom);
|
|
|
|
SEL_TO_COMMAND(moveToEndOfLine:, Command::EndLine);
|
|
|
|
SEL_TO_COMMAND(moveToEndOfLineAndModifySelection:, Command::SelectEndLine);
|
|
|
|
SEL_TO_COMMAND(moveToEndOfParagraph:, Command::EndLine);
|
|
|
|
SEL_TO_COMMAND(moveToEndOfParagraphAndModifySelection:, Command::SelectEndLine);
|
|
|
|
SEL_TO_COMMAND(moveToLeftEndOfLine:, Command::BeginLine);
|
|
|
|
SEL_TO_COMMAND(moveToLeftEndOfLineAndModifySelection:, Command::SelectBeginLine);
|
|
|
|
SEL_TO_COMMAND(moveToRightEndOfLine:, Command::EndLine);
|
|
|
|
SEL_TO_COMMAND(moveToRightEndOfLineAndModifySelection:, Command::SelectEndLine);
|
2014-03-14 17:13:31 +04:00
|
|
|
if (aType == nsIWidget::NativeKeyBindingsForSingleLineEditor) {
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(moveUp:, Command::BeginLine);
|
2013-07-10 18:08:55 +04:00
|
|
|
} else {
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(moveUp:, Command::LinePrevious);
|
2013-07-10 18:08:55 +04:00
|
|
|
}
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(moveUpAndModifySelection:, Command::SelectLinePrevious);
|
|
|
|
SEL_TO_COMMAND(moveWordBackward:, Command::WordPrevious);
|
|
|
|
SEL_TO_COMMAND(moveWordBackwardAndModifySelection:, Command::SelectWordPrevious);
|
|
|
|
SEL_TO_COMMAND(moveWordForward:, Command::WordNext);
|
|
|
|
SEL_TO_COMMAND(moveWordForwardAndModifySelection:, Command::SelectWordNext);
|
|
|
|
SEL_TO_COMMAND(moveWordLeft:, Command::WordPrevious);
|
|
|
|
SEL_TO_COMMAND(moveWordLeftAndModifySelection:, Command::SelectWordPrevious);
|
|
|
|
SEL_TO_COMMAND(moveWordRight:, Command::WordNext);
|
|
|
|
SEL_TO_COMMAND(moveWordRightAndModifySelection:, Command::SelectWordNext);
|
|
|
|
SEL_TO_COMMAND(pageDown:, Command::MovePageDown);
|
|
|
|
SEL_TO_COMMAND(pageDownAndModifySelection:, Command::SelectPageDown);
|
|
|
|
SEL_TO_COMMAND(pageUp:, Command::MovePageUp);
|
|
|
|
SEL_TO_COMMAND(pageUpAndModifySelection:, Command::SelectPageUp);
|
|
|
|
SEL_TO_COMMAND(paste:, Command::Paste);
|
2013-07-10 18:08:55 +04:00
|
|
|
// SEL_TO_COMMAND(pasteFont:, );
|
|
|
|
// SEL_TO_COMMAND(pasteRuler:, );
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(scrollLineDown:, Command::ScrollLineDown);
|
|
|
|
SEL_TO_COMMAND(scrollLineUp:, Command::ScrollLineUp);
|
|
|
|
SEL_TO_COMMAND(scrollPageDown:, Command::ScrollPageDown);
|
|
|
|
SEL_TO_COMMAND(scrollPageUp:, Command::ScrollPageUp);
|
|
|
|
SEL_TO_COMMAND(scrollToBeginningOfDocument:, Command::ScrollTop);
|
|
|
|
SEL_TO_COMMAND(scrollToEndOfDocument:, Command::ScrollBottom);
|
|
|
|
SEL_TO_COMMAND(selectAll:, Command::SelectAll);
|
2013-07-10 18:08:55 +04:00
|
|
|
// selectLine: is complex, see KeyDown
|
2014-03-14 17:13:31 +04:00
|
|
|
if (aType == nsIWidget::NativeKeyBindingsForSingleLineEditor) {
|
2019-04-30 07:23:24 +03:00
|
|
|
SEL_TO_COMMAND(selectParagraph:, Command::SelectAll);
|
2013-07-10 18:08:55 +04:00
|
|
|
}
|
|
|
|
// SEL_TO_COMMAND(selectToMark:, );
|
|
|
|
// selectWord: is complex, see KeyDown
|
|
|
|
// SEL_TO_COMMAND(setMark:, );
|
|
|
|
// SEL_TO_COMMAND(showContextHelp:, );
|
|
|
|
// SEL_TO_COMMAND(supplementalTargetForAction:sender:, );
|
|
|
|
// SEL_TO_COMMAND(swapWithMark:, );
|
|
|
|
// SEL_TO_COMMAND(transpose:, );
|
|
|
|
// SEL_TO_COMMAND(transposeWords:, );
|
|
|
|
// SEL_TO_COMMAND(uppercaseWord:, );
|
|
|
|
// SEL_TO_COMMAND(yank:, );
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SEL_TO_COMMAND
|
|
|
|
|
2017-05-19 11:49:41 +03:00
|
|
|
void NativeKeyBindings::GetEditCommands(const WidgetKeyboardEvent& aEvent,
|
|
|
|
nsTArray<CommandInt>& aCommands) {
|
|
|
|
MOZ_ASSERT(aCommands.IsEmpty());
|
|
|
|
|
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info, ("%p NativeKeyBindings::GetEditCommands", this));
|
2013-07-10 18:08:55 +04:00
|
|
|
|
|
|
|
// Recover the current event, which should always be the key down we are
|
|
|
|
// responding to.
|
|
|
|
|
2013-08-24 11:24:32 +04:00
|
|
|
NSEvent* cocoaEvent = reinterpret_cast<NSEvent*>(aEvent.mNativeKeyEvent);
|
2013-07-10 18:08:55 +04:00
|
|
|
|
|
|
|
if (!cocoaEvent || [cocoaEvent type] != NSKeyDown) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2017-05-19 11:49:41 +03:00
|
|
|
("%p NativeKeyBindings::GetEditCommands, no Cocoa key down event", this));
|
2013-07-10 18:08:55 +04:00
|
|
|
|
2017-05-19 11:49:41 +03:00
|
|
|
return;
|
2013-07-10 18:08:55 +04:00
|
|
|
}
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2017-05-19 11:49:41 +03:00
|
|
|
("%p NativeKeyBindings::GetEditCommands, interpreting", this));
|
2013-07-10 18:08:55 +04:00
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<KeyBindingsCommand, 2> bindingCommands;
|
2013-07-10 18:08:55 +04:00
|
|
|
nsCocoaUtils::GetCommandsFromKeyEvent(cocoaEvent, bindingCommands);
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2017-07-26 23:03:57 +03:00
|
|
|
("%p NativeKeyBindings::GetEditCommands, bindingCommands=%zu", this,
|
2013-07-10 18:08:55 +04:00
|
|
|
bindingCommands.Length()));
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < bindingCommands.Length(); i++) {
|
|
|
|
SEL selector = bindingCommands[i].selector;
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
if (MOZ_LOG_TEST(gNativeKeyBindingsLog, LogLevel::Info)) {
|
2013-07-10 18:08:55 +04:00
|
|
|
NSString* selectorString = NSStringFromSelector(selector);
|
|
|
|
nsAutoString nsSelectorString;
|
|
|
|
nsCocoaUtils::GetStringForNSString(selectorString, nsSelectorString);
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2017-05-19 11:49:41 +03:00
|
|
|
("%p NativeKeyBindings::GetEditCommands, selector=%s", this,
|
2015-05-07 19:43:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(nsSelectorString).get()));
|
2013-07-10 18:08:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find a simple mapping in the hashtable
|
2019-04-30 07:23:24 +03:00
|
|
|
Command geckoCommand = Command::DoNothing;
|
|
|
|
if (mSelectorToCommand.Get(reinterpret_cast<struct objc_selector*>(selector), &geckoCommand) &&
|
|
|
|
geckoCommand != Command::DoNothing) {
|
|
|
|
aCommands.AppendElement(static_cast<CommandInt>(geckoCommand));
|
2013-07-10 18:08:55 +04:00
|
|
|
} else if (selector == @selector(selectLine:)) {
|
|
|
|
// This is functional, but Cocoa's version is direction-less in that
|
|
|
|
// selection direction is not determined until some future directed action
|
|
|
|
// is taken. See bug 282097, comment 79 for more details.
|
2019-04-30 07:23:24 +03:00
|
|
|
aCommands.AppendElement(static_cast<CommandInt>(Command::BeginLine));
|
|
|
|
aCommands.AppendElement(static_cast<CommandInt>(Command::SelectEndLine));
|
2013-07-10 18:08:55 +04:00
|
|
|
} else if (selector == @selector(selectWord:)) {
|
|
|
|
// This is functional, but Cocoa's version is direction-less in that
|
|
|
|
// selection direction is not determined until some future directed action
|
|
|
|
// is taken. See bug 282097, comment 79 for more details.
|
2019-04-30 07:23:24 +03:00
|
|
|
aCommands.AppendElement(static_cast<CommandInt>(Command::WordPrevious));
|
|
|
|
aCommands.AppendElement(static_cast<CommandInt>(Command::SelectWordNext));
|
2013-07-10 18:08:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 11:49:41 +03:00
|
|
|
if (!MOZ_LOG_TEST(gNativeKeyBindingsLog, LogLevel::Info)) {
|
|
|
|
return;
|
2013-07-10 18:08:55 +04:00
|
|
|
}
|
|
|
|
|
2017-05-19 11:49:41 +03:00
|
|
|
if (aCommands.IsEmpty()) {
|
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
|
|
|
("%p NativeKeyBindings::GetEditCommands, handled=false", this));
|
|
|
|
return;
|
|
|
|
}
|
2013-07-10 18:08:55 +04:00
|
|
|
|
2017-05-19 11:49:41 +03:00
|
|
|
for (CommandInt commandInt : aCommands) {
|
|
|
|
Command geckoCommand = static_cast<Command>(commandInt);
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gNativeKeyBindingsLog, LogLevel::Info,
|
2017-05-19 11:49:41 +03:00
|
|
|
("%p NativeKeyBindings::GetEditCommands, command=%s", this,
|
2014-11-22 17:39:03 +03:00
|
|
|
WidgetKeyboardEvent::GetCommandStr(geckoCommand)));
|
2013-07-10 18:08:55 +04:00
|
|
|
}
|
|
|
|
}
|
2014-03-14 17:13:32 +04:00
|
|
|
|
|
|
|
} // namespace widget
|
|
|
|
} // namespace mozilla
|