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);
20 nsTouchBarUpdater::UpdateTouchBarInputs(
21 nsIBaseWindow* aWindow, const nsTArray<RefPtr<nsITouchBarInput>>& aInputs) {
22 if (!sTouchBarIsInitialized || !aWindow) {
26 BaseWindow* cocoaWin = nsTouchBarUpdater::GetCocoaWindow(aWindow);
28 return NS_ERROR_FAILURE;
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));
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.
44 isEqualToString:[TouchBarInput
45 nativeIdentifierWithType:@"scrubber"
50 TouchBarInput* convertedInput =
51 [[TouchBarInput alloc] initWithXPCOM:input];
52 [(nsTouchBar*)cocoaWin.touchBar updateItem:convertedInput];
60 nsTouchBarUpdater::ShowPopover(nsIBaseWindow* aWindow,
61 nsITouchBarInput* aPopover, bool aShowing) {
62 if (!sTouchBarIsInitialized || !aPopover || !aWindow) {
66 BaseWindow* cocoaWin = nsTouchBarUpdater::GetCocoaWindow(aWindow);
68 return NS_ERROR_FAILURE;
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];
87 nsTouchBarUpdater::EnterCustomizeMode() {
88 [NSApp toggleTouchBarCustomizationPalette:(id)this];
93 nsTouchBarUpdater::IsTouchBarInitialized(bool* aResult) {
94 *aResult = sTouchBarIsInitialized;
98 BaseWindow* nsTouchBarUpdater::GetCocoaWindow(nsIBaseWindow* aWindow) {
99 nsCOMPtr<nsIWidget> widget = nullptr;
100 aWindow->GetMainWidget(getter_AddRefs(widget));
104 BaseWindow* cocoaWin = (BaseWindow*)widget->GetNativeData(NS_NATIVE_WINDOW);
111 // NOTE: This method is for internal unit tests only.
113 nsTouchBarUpdater::SetTouchBarInitialized(bool aIsInitialized) {
114 sTouchBarIsInitialized = aIsInitialized;