no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / widget / cocoa / nsTouchBarUpdater.mm
blobed0b78c6d1801070ef0679f3ea535eaba89171cf
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #import <Cocoa/Cocoa.h>
7 #include "nsTouchBar.h"
8 #include "nsTouchBarInput.h"
9 #include "nsTouchBarUpdater.h"
11 #include "nsIBaseWindow.h"
12 #include "nsIWidget.h"
14 // defined in nsCocoaWindow.mm.
15 extern BOOL sTouchBarIsInitialized;
17 NS_IMPL_ISUPPORTS(nsTouchBarUpdater, nsITouchBarUpdater);
19 NS_IMETHODIMP
20 nsTouchBarUpdater::UpdateTouchBarInputs(
21     nsIBaseWindow* aWindow, const nsTArray<RefPtr<nsITouchBarInput>>& aInputs) {
22   if (!sTouchBarIsInitialized || !aWindow) {
23     return NS_OK;
24   }
26   BaseWindow* cocoaWin = nsTouchBarUpdater::GetCocoaWindow(aWindow);
27   if (!cocoaWin) {
28     return NS_ERROR_FAILURE;
29   }
31   if ([cocoaWin respondsToSelector:@selector(touchBar)]) {
32     size_t itemCount = aInputs.Length();
33     for (size_t i = 0; i < itemCount; ++i) {
34       nsCOMPtr<nsITouchBarInput> input(aInputs.ElementAt(i));
35       if (!input) {
36         continue;
37       }
39       NSTouchBarItemIdentifier newIdentifier =
40           [TouchBarInput nativeIdentifierWithXPCOM:input];
41       // We don't support updating the Share scrubber since it's a special
42       // Apple-made component that behaves differently from the other inputs.
43       if ([newIdentifier
44               isEqualToString:[TouchBarInput
45                                   nativeIdentifierWithType:@"scrubber"
46                                                    withKey:@"share"]]) {
47         continue;
48       }
50       TouchBarInput* convertedInput =
51           [[TouchBarInput alloc] initWithXPCOM:input];
52       [(nsTouchBar*)cocoaWin.touchBar updateItem:convertedInput];
53     }
54   }
56   return NS_OK;
59 NS_IMETHODIMP
60 nsTouchBarUpdater::ShowPopover(nsIBaseWindow* aWindow,
61                                nsITouchBarInput* aPopover, bool aShowing) {
62   if (!sTouchBarIsInitialized || !aPopover || !aWindow) {
63     return NS_OK;
64   }
66   BaseWindow* cocoaWin = nsTouchBarUpdater::GetCocoaWindow(aWindow);
67   if (!cocoaWin) {
68     return NS_ERROR_FAILURE;
69   }
71   if ([cocoaWin respondsToSelector:@selector(touchBar)]) {
72     // We don't need to completely reinitialize the popover. We only need its
73     // identifier to look it up in [nsTouchBar mappedLayoutItems].
74     NSTouchBarItemIdentifier popoverIdentifier =
75         [TouchBarInput nativeIdentifierWithXPCOM:aPopover];
77     TouchBarInput* popoverItem =
78         [[(nsTouchBar*)cocoaWin.touchBar mappedLayoutItems]
79             objectForKey:popoverIdentifier];
81     [(nsTouchBar*)cocoaWin.touchBar showPopover:popoverItem showing:aShowing];
82   }
83   return NS_OK;
86 NS_IMETHODIMP
87 nsTouchBarUpdater::EnterCustomizeMode() {
88   [NSApp toggleTouchBarCustomizationPalette:(id)this];
89   return NS_OK;
92 NS_IMETHODIMP
93 nsTouchBarUpdater::IsTouchBarInitialized(bool* aResult) {
94   *aResult = sTouchBarIsInitialized;
95   return NS_OK;
98 BaseWindow* nsTouchBarUpdater::GetCocoaWindow(nsIBaseWindow* aWindow) {
99   nsCOMPtr<nsIWidget> widget = nullptr;
100   aWindow->GetMainWidget(getter_AddRefs(widget));
101   if (!widget) {
102     return nil;
103   }
104   BaseWindow* cocoaWin = (BaseWindow*)widget->GetNativeData(NS_NATIVE_WINDOW);
105   if (!cocoaWin) {
106     return nil;
107   }
108   return cocoaWin;
111 // NOTE: This method is for internal unit tests only.
112 NS_IMETHODIMP
113 nsTouchBarUpdater::SetTouchBarInitialized(bool aIsInitialized) {
114   sTouchBarIsInitialized = aIsInitialized;
115   return NS_OK;