Backed out changeset 0a133d5fd155 (bug 1864534) for causing screenshot related failur...
[gecko.git] / widget / headless / HeadlessKeyBindingsCocoa.mm
blob27ed07c461a6f227022cc6de11ab81bed924eace
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "HeadlessKeyBindings.h"
7 #import <Cocoa/Cocoa.h>
8 #include "nsCocoaUtils.h"
9 #include "NativeKeyBindings.h"
10 #include "mozilla/ClearOnShutdown.h"
11 #include "mozilla/Maybe.h"
12 #include "mozilla/NativeKeyBindingsType.h"
13 #include "mozilla/WritingModes.h"
15 namespace mozilla {
16 namespace widget {
18 HeadlessKeyBindings& HeadlessKeyBindings::GetInstance() {
19   static UniquePtr<HeadlessKeyBindings> sInstance;
20   if (!sInstance) {
21     sInstance.reset(new HeadlessKeyBindings());
22     ClearOnShutdown(&sInstance);
23   }
24   return *sInstance;
27 nsresult HeadlessKeyBindings::AttachNativeKeyEvent(
28     WidgetKeyboardEvent& aEvent) {
29   NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
31   aEvent.mNativeKeyEvent =
32       nsCocoaUtils::MakeNewCococaEventFromWidgetEvent(aEvent, 0, nil);
34   return NS_OK;
36   NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
39 void HeadlessKeyBindings::GetEditCommands(
40     NativeKeyBindingsType aType, const WidgetKeyboardEvent& aEvent,
41     const Maybe<WritingMode>& aWritingMode, nsTArray<CommandInt>& aCommands) {
42   // Convert the widget keyboard into a cocoa event so it can be translated
43   // into commands in the NativeKeyBindings.
44   WidgetKeyboardEvent modifiedEvent(aEvent);
45   modifiedEvent.mNativeKeyEvent =
46       nsCocoaUtils::MakeNewCococaEventFromWidgetEvent(aEvent, 0, nil);
48   NativeKeyBindings* keyBindings = NativeKeyBindings::GetInstance(aType);
49   keyBindings->GetEditCommands(modifiedEvent, aWritingMode, aCommands);
52 }  // namespace widget
53 }  // namespace mozilla