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/. */
4 
5 #ifndef nsTouchBar_h_
6 #define nsTouchBar_h_
7 
8 #import <Cocoa/Cocoa.h>
9 
10 #include "nsITouchBarHelper.h"
11 #include "nsTouchBarInput.h"
12 #include "nsTouchBarNativeAPIDefines.h"
13 
14 /**
15  * Our TouchBar is its own delegate. This is adequate for our purposes,
16  * since the current implementation only defines Touch Bar buttons for the
17  * main Firefox window. If modals and other windows were to have custom
18  * Touch Bar views, each window would have to be a NSTouchBarDelegate so
19  * they could define their own custom sets of buttons.
20  */
21 @interface nsTouchBar : NSTouchBar <NSTouchBarDelegate,
22                                     NSSharingServicePickerTouchBarItemDelegate,
23                                     NSSharingServiceDelegate> {
24   /**
25    * Link to the frontend API that determines which buttons appear
26    * in the Touch Bar
27    */
28   nsCOMPtr<nsITouchBarHelper> mTouchBarHelper;
29 }
30 
31 /**
32  * Contains TouchBarInput representations of the inputs currently in
33  * the Touch Bar. Populated in `init` and updated by nsITouchBarUpdater.
34  */
35 @property(strong) NSMutableDictionary<NSTouchBarItemIdentifier, TouchBarInput*>* mappedLayoutItems;
36 
37 /**
38  * Stores buttons displayed in a NSScrollView. They must be stored separately
39  * because they are untethered from the nsTouchBar. As such, they
40  * cannot be retrieved with [NSTouchBar itemForIdentifier].
41  */
42 @property(strong)
43     NSMutableDictionary<NSTouchBarItemIdentifier, NSCustomTouchBarItem*>* scrollViewButtons;
44 
45 /**
46  * Returns an instance of nsTouchBar based on implementation details
47  * fetched from the frontend through nsTouchBarHelper.
48  */
49 - (instancetype)init;
50 
51 /**
52  * If aInputs is not nil, a nsTouchBar containing the inputs specified is
53  * initialized. Otherwise, a nsTouchBar is initialized containing a default set
54  * of inputs.
55  */
56 - (instancetype)initWithInputs:(NSMutableArray<TouchBarInput*>*)aInputs;
57 
58 - (void)dealloc;
59 
60 /**
61  * Creates a new NSTouchBarItem and adds it to the Touch Bar.
62  * Reads the passed identifier and creates the
63  * appropriate item type (eg. NSCustomTouchBarItem).
64  * Required as a member of NSTouchBarDelegate.
65  */
66 - (NSTouchBarItem*)touchBar:(NSTouchBar*)aTouchBar
67       makeItemForIdentifier:(NSTouchBarItemIdentifier)aIdentifier;
68 
69 /**
70  * Updates an input on the Touch Bar by redirecting to one of the specific
71  * TouchBarItem types updaters.
72  * Returns true if the input was successfully updated.
73  */
74 - (bool)updateItem:(TouchBarInput*)aInput;
75 
76 /**
77  * Helper function for updateItem. Checks to see if a given input exists within
78  * any of this Touch Bar's popovers and updates it if it exists.
79  */
80 - (bool)maybeUpdatePopoverChild:(TouchBarInput*)aInput;
81 
82 /**
83  * Helper function for updateItem. Checks to see if a given input exists within
84  * any of this Touch Bar's scroll views and updates it if it exists.
85  */
86 - (bool)maybeUpdateScrollViewChild:(TouchBarInput*)aInput;
87 
88 /**
89  * Helper function for updateItem. Replaces an item in the
90  * self.mappedLayoutItems dictionary.
91  */
92 - (void)replaceMappedLayoutItem:(TouchBarInput*)aItem;
93 
94 /**
95  * Update or create various subclasses of TouchBarItem.
96  */
97 - (void)updateButton:(NSCustomTouchBarItem*)aButton
98       withIdentifier:(NSTouchBarItemIdentifier)aIdentifier;
99 - (void)updateMainButton:(NSCustomTouchBarItem*)aMainButton
100           withIdentifier:(NSTouchBarItemIdentifier)aIdentifier;
101 - (void)updatePopover:(NSPopoverTouchBarItem*)aPopoverItem
102        withIdentifier:(NSTouchBarItemIdentifier)aIdentifier;
103 - (void)updateScrollView:(NSCustomTouchBarItem*)aScrollViewItem
104           withIdentifier:(NSTouchBarItemIdentifier)aIdentifier;
105 - (void)updateLabel:(NSTextField*)aLabel withIdentifier:(NSTouchBarItemIdentifier)aIdentifier;
106 - (NSTouchBarItem*)makeShareScrubberForIdentifier:(NSTouchBarItemIdentifier)aIdentifier;
107 
108 /**
109  * If aShowing is true, aPopover is shown. Otherwise, it is hidden.
110  */
111 - (void)showPopover:(TouchBarInput*)aPopover showing:(bool)aShowing;
112 
113 /**
114  *  Redirects button actions to the appropriate handler.
115  */
116 - (void)touchBarAction:(id)aSender;
117 
118 /**
119  * Helper function to initialize a new nsTouchBarInputIcon and load an icon.
120  */
121 - (void)loadIconForInput:(TouchBarInput*)aInput forItem:(NSTouchBarItem*)aItem;
122 
123 - (NSArray*)itemsForSharingServicePickerTouchBarItem:
124     (NSSharingServicePickerTouchBarItem*)aPickerTouchBarItem;
125 
126 - (NSArray<NSSharingService*>*)sharingServicePicker:(NSSharingServicePicker*)aSharingServicePicker
127                             sharingServicesForItems:(NSArray*)aItems
128                             proposedSharingServices:(NSArray<NSSharingService*>*)aProposedServices;
129 
130 - (void)releaseJSObjects;
131 
132 @end  // nsTouchBar
133 
134 #endif  // nsTouchBar_h_
135