Vuo 2.4.4
Loading...
Searching...
No Matches
VuoToolbar.mm
Go to the documentation of this file.
1
10#include "VuoToolbar.hh"
11
13#include <AppKit/AppKit.h>
14#include <objc/message.h>
15
16#include "VuoEditor.hh"
18
23static NSRect windowWillPositionSheetUsingRect(id self, SEL _cmd, NSWindow *window, NSWindow *sheet, NSRect rect)
24{
25 float titleAndToolbarHeight = window.frame.size.height - [window contentRectForFrameRect:window.frame].size.height;
26
27#if VUO_PRO
28 if ([NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){10,14,0}])
29 {
30 // On macOS 10.14, we need to adjust the position by different amounts when toolbar labels are visible and hidden.
31 if (static_cast<VuoEditor *>(qApp)->areToolbarLabelsVisible())
32 rect.origin.y += titleAndToolbarHeight;
33 else
34 rect.origin.y += titleAndToolbarHeight * 2;
35 }
36 else if (!static_cast<VuoEditor *>(qApp)->areToolbarLabelsVisible())
37 // On macOS 10.12 and 10.13, we only need to adjust the position when toolbar labels are hidden.
38 rect.origin.y += titleAndToolbarHeight;
39#else
40 if ([NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){10,14,0}])
41 rect.origin.y += titleAndToolbarHeight;
42#endif
43
44 return rect;
45}
46
50VuoToolbar::VuoToolbar(QMainWindow *window)
51 : QObject(window)
52{
53 this->window = window;
54 qtToolbar = nullptr;
55 nsWindow = nil;
56 titleView = nil;
57 titleViewController = nil;
58}
59
67{
68 NSView *nsView = (NSView *)window->winId();
69 nsWindow = [nsView window];
70
71 titleView = [NSTextField new];
72 titleView.cell = [VuoToolbarTitleCell new];
73 titleView.font = [NSFont titleBarFontOfSize:0];
74 titleView.selectable = NO;
75 titleViewController = nil;
76
77 qtToolbar = new QMacToolBar(window);
78 {
79 NSToolbar *tb = qtToolbar->nativeToolbar();
80
81 // Workaround for apparent Qt bug, where QCocoaIntegration::clearToolbars() releases the toolbar after it's already been deallocated.
82 [tb retain];
83
84 [tb setAllowsUserCustomization:NO];
85 }
86
87 addToolbarItems(); // Toolbar items must be added before attachToWindow() or they won't appear.
88
89 qtToolbar->attachToWindow(window->windowHandle());
90 window->setUnifiedTitleAndToolBarOnMac(true);
91
92 auto editor = static_cast<VuoEditor *>(qApp);
94 updateColor(editor->isInterfaceDark());
95
96 class_addMethod(nsWindow.delegate.class,
97 @selector(window:willPositionSheet:usingRect:),
99 "{CGRect={CGPoint=dd}{CGSize=dd}}@:@@{CGRect={CGPoint=dd}{CGSize=dd}}");
100
101#if VUO_PRO
102 setUp_Pro();
103#endif
104}
105
109void VuoToolbar::updateColor(bool isDark)
110{
111 {
112 // Request a transparent titlebar (and toolbar) so the window's background color (set below) shows through.
113 // "Previously NSWindow would make the titlebar transparent for certain windows with NSWindowStyleMaskTexturedBackground set,
114 // even if titlebarAppearsTransparent was NO. When linking against the 10.13 SDK, textured windows must
115 // explicitly set titlebarAppearsTransparent to YES for this behavior."
116 // - https://developer.apple.com/library/content/releasenotes/AppKit/RN-AppKit/
117 nsWindow.titlebarAppearsTransparent = YES;
118
120 {
121 // "A window with titlebarAppearsTransparent is normally opted-out of automatic window tabbing."
122 // - https://developer.apple.com/library/archive/releasenotes/AppKit/RN-AppKit/index.html
123 // …but it works fine for us, so reenable it.
124 // https://b33p.net/kosada/node/15412
125 nsWindow.tabbingMode = NSWindowTabbingModeAutomatic;
126 nsWindow.tabbingIdentifier = getTabbingIdentifier();
127 }
128 }
129
130 if (isDark)
131 [nsWindow setBackgroundColor:[NSColor colorWithCalibratedWhite:.47 alpha:1]];
132 else
133 [nsWindow setBackgroundColor:[NSColor colorWithCalibratedWhite:.92 alpha:1]];
134}