aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/touchbar/touchbar_mac.mm
blob: 67c83aec0ce0578bec7160b44ecaa5686c8278c7 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "touchbar.h"
#include "touchbar_mac_p.h"
#include "touchbar_appdelegate_mac_p.h"

#include <utils/utilsicons.h>

#include <QtMac>

#import <AppKit/NSButton.h>
#import <AppKit/NSCustomTouchBarItem.h>
#import <AppKit/NSImage.h>
#import <AppKit/NSPopoverTouchBarItem.h>

namespace Utils {

TouchBar::TouchBar(const QByteArray &id, const QIcon &icon, const QString &title)
    : d(new Internal::TouchBarPrivate)
{
    d->m_id = id;
    d->m_action.setIcon(icon);
    d->m_action.setText(title);
    d->m_invalidate = []() {}; // safe-guard, do nothing by default
    d->m_delegate = [[TouchBarDelegate alloc] initWithParent:d];
}

TouchBar::~TouchBar()
{
    // clean up connections to the QActions
    clear();
    delete d;
}

TouchBar::TouchBar(const QByteArray &id, const QIcon &icon)
    : TouchBar(id, icon, {})
{
}

TouchBar::TouchBar(const QByteArray &id, const QString &title)
    : TouchBar(id, {}, title)
{
}

TouchBar::TouchBar(const QByteArray &id)
    : TouchBar(id, {}, {})
{
}

QByteArray TouchBar::id() const
{
    return d->m_id;
}

QAction *TouchBar::touchBarAction() const
{
    return &d->m_action;
}

void TouchBar::insertAction(QAction *before, const QByteArray &id, QAction *action)
{
    const QMetaObject::Connection connection = QObject::connect(action,
                                                                &QAction::changed,
                                                                [this]() { d->m_invalidate(); });
    const auto item = std::find_if(d->m_items.begin(), d->m_items.end(),
                                   [before](const Internal::TouchBarItem &item) {
                                       return item.action == before;
                                   });
    d->m_items.insert(item, {id, connection, action, nullptr});
    d->m_invalidate();
}

void TouchBar::insertTouchBar(QAction *before, TouchBar *touchBar)
{
    const QMetaObject::Connection connection = QObject::connect(touchBar->touchBarAction(),
                                                                &QAction::changed,
                                                                [this]() { d->m_invalidate(); });
    const auto item = std::find_if(d->m_items.begin(), d->m_items.end(),
                                   [before](const Internal::TouchBarItem &item) {
                                       return item.action == before;
                                   });
    d->m_items.insert(item, {touchBar->id(), connection, touchBar->touchBarAction(), touchBar->d});
    d->m_invalidate();
}

void TouchBar::removeAction(QAction *action)
{
    const auto item = std::find_if(d->m_items.begin(), d->m_items.end(),
                                   [action](const Internal::TouchBarItem &item) {
                                       return item.action == action;
                                   });
    if (item != d->m_items.end()) {
        QObject::disconnect(item->updateConnection);
        d->m_items.erase(item);
    }
    d->m_invalidate();
}

void TouchBar::removeTouchBar(TouchBar *touchBar)
{
    removeAction(touchBar->touchBarAction());
}

void TouchBar::clear()
{
    for (const Internal::TouchBarItem &item : d->m_items)
        QObject::disconnect(item.updateConnection);
    d->m_items.clear();
    d->m_invalidate();
}

void TouchBar::setApplicationTouchBar()
{
    Internal::ApplicationDelegate::instance()->setApplicationTouchBar(d);
}

} // namespace Utils

using namespace Utils::Internal;

static NSImage *iconToTemplateNSImage(const QIcon &icon)
{
    // touch bar icons are max 18-22 pts big. our are always 22 pts big.
    const QPixmap pixmap = icon.pixmap(22);
    NSImage *image = QtMac::toNSImage(pixmap);
    // toNSImage ignores devicePixelRatio, so fixup after the fact
    const CGFloat userWidth = pixmap.width() / pixmap.devicePixelRatio();
    const CGFloat userHeight = pixmap.height() / pixmap.devicePixelRatio();
    image.size = CGSizeMake(userWidth, userHeight); // scales the image
    [image setTemplate:YES];
    return image;
}

// NSButton that delegates trigger to QAction's trigger.
// State (enabled/visible) is not synced since that triggers a touch bar rebuild anyhow.

@interface QActionNSButton : NSButton
@property (readonly, atomic) QAction *qaction;
+ (QActionNSButton *)buttonWithQAction:(QAction *)qaction;
@end

@implementation QActionNSButton
@synthesize qaction = _qaction;

- (void)trigger:(id)sender
{
    Q_UNUSED(sender)
    self.qaction->trigger();
}

- (id)initWithQAction:(QAction *)qaction
{
    self = [super init];
    [self setButtonType:NSButtonTypeMomentaryPushIn];
    self.bezelStyle = NSRoundedBezelStyle;
    self.target = self;
    self.action = @selector(trigger:);
    _qaction = qaction;
    if (!self.qaction->text().isEmpty())
        self.title = self.qaction->text().toNSString();
    if (!self.qaction->icon().isNull())
        self.image = iconToTemplateNSImage(self.qaction->icon());
    self.enabled = self.qaction->isEnabled();
    return self;
}

+ (QActionNSButton *)buttonWithQAction:(QAction *)qaction
{
    return [[[QActionNSButton alloc] initWithQAction:qaction] autorelease];
}

@end

@interface PopoverButton : NSButton
@property (readonly, atomic) TouchBarPrivate *parent;
+ (PopoverButton *)buttonWithTouchBar:(TouchBarPrivate *)parent;
@end

@implementation PopoverButton
@synthesize parent = _parent;

- (PopoverButton *)initWithTouchBar:(TouchBarPrivate *)parent
{
    self = [super init];
    [self setButtonType:NSButtonTypeMomentaryPushIn];
    self.bezelStyle = NSRoundedBezelStyle;
    self.target = self;
    self.action = @selector(trigger:);
    _parent = parent;
    if (!self.parent->m_action.text().isEmpty())
        self.title = self.parent->m_action.text().toNSString();
    if (!self.parent->m_action.icon().isNull())
        self.image = iconToTemplateNSImage(self.parent->m_action.icon());
    return self;
}

- (void)trigger:(id)sender
{
    Q_UNUSED(sender)
    ApplicationDelegate::instance()->pushTouchBar(self.parent);
}

+ (PopoverButton *)buttonWithTouchBar:(TouchBarPrivate *)parent
{
    return [[[PopoverButton alloc] initWithTouchBar:parent] autorelease];
}

@end

@implementation TouchBarDelegate
@synthesize parent = _parent;
@synthesize closeButtonIdentifier;

- (id)initWithParent:(TouchBarPrivate *)parent
{
    self = [super init];
    _parent = parent;
    const QByteArray closeIdentifier = parent->m_id + ".close";
    self.closeButtonIdentifier = QString::fromUtf8(closeIdentifier).toNSString();
    return self;
}

- (NSTouchBar *)makeTouchBar
{
    NSTouchBar *touchBar = [[NSTouchBar alloc] init];
    touchBar.delegate = self;
    NSMutableArray<NSTouchBarItemIdentifier> *items
        = [[[NSMutableArray<NSTouchBarItemIdentifier> alloc] init] autorelease];
    if (self.parent->m_isSubBar) {
        // add a close button
        [items addObject:self.closeButtonIdentifier];
    }
    for (const TouchBarItem &item : self.parent->m_items) {
        // Touch bar items that are hidden still take up space in the touch bar, so
        // only include actions that are visible
        if (item.action && item.action->isVisible()) {
            [items addObject:QString::fromUtf8(item.id).toNSString()];
        }
    }
    touchBar.defaultItemIdentifiers = items;
    return touchBar;
}

- (void)popTouchBar:(id)sender
{
    Q_UNUSED(sender)
    ApplicationDelegate::instance()->popTouchBar();
}

- (NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar
       makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
{
    Q_UNUSED(touchBar)
    if ([identifier isEqualToString:self.closeButtonIdentifier]) {
        NSCustomTouchBarItem *item = [[[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]
                                                                                       autorelease];
        NSButton *button = [NSButton buttonWithImage:iconToTemplateNSImage(Utils::Icons::MACOS_TOUCHBAR_CLEAR.icon())
                                              target:self
                                              action:@selector(popTouchBar:)];
        button.bordered = NO;
        item.view = button;
        return item;
    }
    const auto itemId = QString::fromNSString(identifier).toUtf8();
    const std::vector<TouchBarItem> &items = self.parent->m_items;
    const auto actionPos = std::find_if(items.begin(), items.end(),
                                        [itemId](const TouchBarItem &item) {
                                           return item.id == itemId;
                                        });
    Q_ASSERT(actionPos != items.end());
    if (actionPos->touchBar) {
        // The default popover item does not work for us, because it closes when the touch bar
        // is rebuilt on context changes.
        // Create a custom popover item that actually replaces the application touch bar with the
        // sub bar when activated
        actionPos->touchBar->m_isSubBar = true;
        NSCustomTouchBarItem *item = [[[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]
                                                                                       autorelease];
        PopoverButton *button = [PopoverButton buttonWithTouchBar:actionPos->touchBar];
        item.view = button;
        // keep buttons for subcontainers visible
        item.visibilityPriority = NSTouchBarItemPriorityHigh;
        return item;
    } else if (actionPos->action) {
        NSCustomTouchBarItem *item = [[[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]
                                                                                       autorelease];
        QActionNSButton *button = [QActionNSButton buttonWithQAction:actionPos->action];
        item.view = button;
        return item;
    }
    return nil;
}

@end