summaryrefslogtreecommitdiffstats
path: root/widget/cocoa/nsTouchBarUpdater.mm
blob: ed0b78c6d1801070ef0679f3ea535eaba89171cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* 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/. */

#import <Cocoa/Cocoa.h>

#include "nsTouchBar.h"
#include "nsTouchBarInput.h"
#include "nsTouchBarUpdater.h"

#include "nsIBaseWindow.h"
#include "nsIWidget.h"

// defined in nsCocoaWindow.mm.
extern BOOL sTouchBarIsInitialized;

NS_IMPL_ISUPPORTS(nsTouchBarUpdater, nsITouchBarUpdater);

NS_IMETHODIMP
nsTouchBarUpdater::UpdateTouchBarInputs(
    nsIBaseWindow* aWindow, const nsTArray<RefPtr<nsITouchBarInput>>& aInputs) {
  if (!sTouchBarIsInitialized || !aWindow) {
    return NS_OK;
  }

  BaseWindow* cocoaWin = nsTouchBarUpdater::GetCocoaWindow(aWindow);
  if (!cocoaWin) {
    return NS_ERROR_FAILURE;
  }

  if ([cocoaWin respondsToSelector:@selector(touchBar)]) {
    size_t itemCount = aInputs.Length();
    for (size_t i = 0; i < itemCount; ++i) {
      nsCOMPtr<nsITouchBarInput> input(aInputs.ElementAt(i));
      if (!input) {
        continue;
      }

      NSTouchBarItemIdentifier newIdentifier =
          [TouchBarInput nativeIdentifierWithXPCOM:input];
      // We don't support updating the Share scrubber since it's a special
      // Apple-made component that behaves differently from the other inputs.
      if ([newIdentifier
              isEqualToString:[TouchBarInput
                                  nativeIdentifierWithType:@"scrubber"
                                                   withKey:@"share"]]) {
        continue;
      }

      TouchBarInput* convertedInput =
          [[TouchBarInput alloc] initWithXPCOM:input];
      [(nsTouchBar*)cocoaWin.touchBar updateItem:convertedInput];
    }
  }

  return NS_OK;
}

NS_IMETHODIMP
nsTouchBarUpdater::ShowPopover(nsIBaseWindow* aWindow,
                               nsITouchBarInput* aPopover, bool aShowing) {
  if (!sTouchBarIsInitialized || !aPopover || !aWindow) {
    return NS_OK;
  }

  BaseWindow* cocoaWin = nsTouchBarUpdater::GetCocoaWindow(aWindow);
  if (!cocoaWin) {
    return NS_ERROR_FAILURE;
  }

  if ([cocoaWin respondsToSelector:@selector(touchBar)]) {
    // We don't need to completely reinitialize the popover. We only need its
    // identifier to look it up in [nsTouchBar mappedLayoutItems].
    NSTouchBarItemIdentifier popoverIdentifier =
        [TouchBarInput nativeIdentifierWithXPCOM:aPopover];

    TouchBarInput* popoverItem =
        [[(nsTouchBar*)cocoaWin.touchBar mappedLayoutItems]
            objectForKey:popoverIdentifier];

    [(nsTouchBar*)cocoaWin.touchBar showPopover:popoverItem showing:aShowing];
  }
  return NS_OK;
}

NS_IMETHODIMP
nsTouchBarUpdater::EnterCustomizeMode() {
  [NSApp toggleTouchBarCustomizationPalette:(id)this];
  return NS_OK;
}

NS_IMETHODIMP
nsTouchBarUpdater::IsTouchBarInitialized(bool* aResult) {
  *aResult = sTouchBarIsInitialized;
  return NS_OK;
}

BaseWindow* nsTouchBarUpdater::GetCocoaWindow(nsIBaseWindow* aWindow) {
  nsCOMPtr<nsIWidget> widget = nullptr;
  aWindow->GetMainWidget(getter_AddRefs(widget));
  if (!widget) {
    return nil;
  }
  BaseWindow* cocoaWin = (BaseWindow*)widget->GetNativeData(NS_NATIVE_WINDOW);
  if (!cocoaWin) {
    return nil;
  }
  return cocoaWin;
}

// NOTE: This method is for internal unit tests only.
NS_IMETHODIMP
nsTouchBarUpdater::SetTouchBarInitialized(bool aIsInitialized) {
  sTouchBarIsInitialized = aIsInitialized;
  return NS_OK;
}