├── README.md ├── main.mm └── qt-touchbar.pro /README.md: -------------------------------------------------------------------------------- 1 | Qt Touch Bar 2 | ============ 3 | 4 | This example shows how to create a global touch bar with standard 5 | and custom items for a Qt-based application. 6 | -------------------------------------------------------------------------------- /main.mm: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | ** 3 | ** Copyright (C) 2017 The Qt Company Ltd. 4 | ** Contact: https://www.qt.io/licensing/ 5 | ** 6 | ** Commercial License Usage 7 | ** Licensees holding valid commercial Qt licenses may use this file in 8 | ** accordance with the commercial license agreement provided with the 9 | ** Software or, alternatively, in accordance with the terms contained in 10 | ** a written agreement between you and The Qt Company. For licensing terms 11 | ** and conditions see https://www.qt.io/terms-conditions. For further 12 | ** information use the contact form at https://www.qt.io/contact-us. 13 | ** 14 | ** BSD License Usage 15 | ** Alternatively, you may use this file under the terms of the BSD license 16 | ** as follows: 17 | ** 18 | ** "Redistribution and use in source and binary forms, with or without 19 | ** modification, are permitted provided that the following conditions are 20 | ** met: 21 | ** * Redistributions of source code must retain the above copyright 22 | ** notice, this list of conditions and the following disclaimer. 23 | ** * Redistributions in binary form must reproduce the above copyright 24 | ** notice, this list of conditions and the following disclaimer in 25 | ** the documentation and/or other materials provided with the 26 | ** distribution. 27 | ** * Neither the name of The Qt Company Ltd nor the names of its 28 | ** contributors may be used to endorse or promote products derived 29 | ** from this software without specific prior written permission. 30 | ** 31 | ** 32 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 33 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 34 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 35 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 36 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 37 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 38 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 39 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 40 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 41 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 42 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 43 | ** 44 | ** 45 | ****************************************************************************/ 46 | 47 | #include 48 | #import 49 | 50 | // This example shows how to create and populate touch bars for Qt applications. 51 | // Two approaches are demonstrated: creating a global touch bar for the entire 52 | // application via the NSApplication delegate, and creating per-window touch bars 53 | // via the NSWindow delegate. Applications may use either or both of these, for example 54 | // to provide global base touch bar with window specific additions. Refer to the 55 | // NSTouchBar documentation for further details. 56 | 57 | // The TouchBarProvider class implements the NSTouchBarDelegate protocol, as 58 | // well as app and window delegate protocols. 59 | @interface TouchBarProvider: NSResponder 60 | 61 | @property (strong) NSCustomTouchBarItem *touchBarItem1; 62 | @property (strong) NSCustomTouchBarItem *touchBarItem2; 63 | @property (strong) NSButton *touchBarButton1; 64 | @property (strong) NSButton *touchBarButton2; 65 | 66 | @property (strong) NSObject *qtDelegate; 67 | 68 | @end 69 | 70 | // Create identifiers for two button items. 71 | static NSTouchBarItemIdentifier Button1Identifier = @"com.myapp.Button1Identifier"; 72 | static NSTouchBarItemIdentifier Button2Identifier = @"com.myapp.Button2Identifier"; 73 | 74 | @implementation TouchBarProvider 75 | 76 | - (NSTouchBar *)makeTouchBar 77 | { 78 | // Create the touch bar with this instance as its delegate 79 | NSTouchBar *bar = [[NSTouchBar alloc] init]; 80 | bar.delegate = self; 81 | 82 | // Add touch bar items: first, the very important emoji picker, followed 83 | // by two buttons. Note that no further handling of the emoji picker 84 | // is needed (emojii are automatically routed to any active text edit). Button 85 | // actions handlers are set up in makeItemForIdentifier below. 86 | bar.defaultItemIdentifiers = @[NSTouchBarItemIdentifierCharacterPicker, 87 | Button1Identifier, Button2Identifier]; 88 | 89 | return bar; 90 | } 91 | 92 | - (NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier 93 | { 94 | Q_UNUSED(touchBar); 95 | 96 | // Create touch bar items as NSCustomTouchBarItems which can contain any NSView. 97 | if ([identifier isEqualToString:Button1Identifier]) { 98 | QString title = "B1"; 99 | self.touchBarItem1 = [[[NSCustomTouchBarItem alloc] initWithIdentifier:identifier] autorelease]; 100 | self.touchBarButton1 = [[NSButton buttonWithTitle:title.toNSString() target:self 101 | action:@selector(button1Clicked)] autorelease]; 102 | self.touchBarItem1.view = self.touchBarButton1; 103 | return self.touchBarItem1; 104 | } else if ([identifier isEqualToString:Button2Identifier]) { 105 | QString title = "B2"; 106 | self.touchBarItem2 = [[[NSCustomTouchBarItem alloc] initWithIdentifier:identifier] autorelease]; 107 | self.touchBarButton2 = [[NSButton buttonWithTitle:title.toNSString() target:self 108 | action:@selector(button2Clicked)] autorelease]; 109 | self.touchBarItem2.view = self.touchBarButton2; 110 | return self.touchBarItem2; 111 | } 112 | return nil; 113 | } 114 | 115 | - (void)installAsDelegateForWindow:(NSWindow *)window 116 | { 117 | _qtDelegate = window.delegate; // Save current delegate for forwarding 118 | window.delegate = self; 119 | } 120 | 121 | - (void)installAsDelegateForApplication:(NSApplication *)application 122 | { 123 | _qtDelegate = application.delegate; // Save current delegate for forwarding 124 | application.delegate = self; 125 | } 126 | 127 | - (BOOL)respondsToSelector:(SEL)aSelector 128 | { 129 | // We want to forward to the qt delegate. Respond to selectors it 130 | // responds to in addition to selectors this instance resonds to. 131 | return [_qtDelegate respondsToSelector:aSelector] || [super respondsToSelector:aSelector]; 132 | } 133 | 134 | - (void)forwardInvocation:(NSInvocation *)anInvocation 135 | { 136 | // Forward to the existing delegate. This function is only called for selectors 137 | // this instance does not responds to, which means that the qt delegate 138 | // must respond to it (due to the respondsToSelector implementation above). 139 | [anInvocation invokeWithTarget:_qtDelegate]; 140 | } 141 | 142 | - (void)button1Clicked 143 | { 144 | qDebug() << "button1Clicked"; 145 | } 146 | 147 | - (void)button2Clicked 148 | { 149 | qDebug() << "button2Clicked"; 150 | } 151 | 152 | @end 153 | 154 | int main(int argc, char **argv) 155 | { 156 | QApplication app(argc, argv); 157 | 158 | { 159 | // Install TouchBarProvider as application delegate 160 | TouchBarProvider *touchBarProvider = [[TouchBarProvider alloc] init]; 161 | [touchBarProvider installAsDelegateForApplication:[NSApplication sharedApplication]]; 162 | } 163 | 164 | QTextEdit textEdit; 165 | textEdit.show(); 166 | 167 | { 168 | // Install TouchBarProvider as window delegate 169 | NSView *view = reinterpret_cast(textEdit.winId()); 170 | TouchBarProvider *touchBarProvider = [[TouchBarProvider alloc] init]; 171 | [touchBarProvider installAsDelegateForWindow:view.window]; 172 | } 173 | 174 | return app.exec(); 175 | } 176 | 177 | -------------------------------------------------------------------------------- /qt-touchbar.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | TARGET = qt-and-touchbar 3 | INCLUDEPATH += . 4 | QT += widgets 5 | 6 | # Input 7 | OBJECTIVE_SOURCES += main.mm 8 | LIBS += -framework AppKit --------------------------------------------------------------------------------