Fix custom title on macOS.

This commit is contained in:
John Preston 2019-10-18 20:56:17 +04:00
parent 414eee35c2
commit e4b23cad1b

View file

@ -17,19 +17,22 @@
@interface WindowObserver : NSObject { @interface WindowObserver : NSObject {
} }
- (id) init:(Fn<void(bool)>)toggleCustomTitleVisibility; - (id) initWithToggle:(Fn<void(bool)>)toggleCustomTitleVisibility enforce:(Fn<void()>)enforceCorrectStyle;
- (void) windowWillEnterFullScreen:(NSNotification *)aNotification; - (void) windowWillEnterFullScreen:(NSNotification *)aNotification;
- (void) windowWillExitFullScreen:(NSNotification *)aNotification; - (void) windowWillExitFullScreen:(NSNotification *)aNotification;
- (void) windowDidExitFullScreen:(NSNotification *)aNotification;
@end // @interface WindowObserver @end // @interface WindowObserver
@implementation WindowObserver { @implementation WindowObserver {
Fn<void(bool)> _toggleCustomTitleVisibility; Fn<void(bool)> _toggleCustomTitleVisibility;
Fn<void()> _enforceCorrectStyle;
} }
- (id) init:(Fn<void(bool)>)toggleCustomTitleVisibility { - (id) initWithToggle:(Fn<void(bool)>)toggleCustomTitleVisibility enforce:(Fn<void()>)enforceCorrectStyle {
if (self = [super init]) { if (self = [super init]) {
_toggleCustomTitleVisibility = toggleCustomTitleVisibility; _toggleCustomTitleVisibility = toggleCustomTitleVisibility;
_enforceCorrectStyle = enforceCorrectStyle;
} }
return self; return self;
} }
@ -39,9 +42,14 @@
} }
- (void) windowWillExitFullScreen:(NSNotification *)aNotification { - (void) windowWillExitFullScreen:(NSNotification *)aNotification {
_enforceCorrectStyle();
_toggleCustomTitleVisibility(true); _toggleCustomTitleVisibility(true);
} }
- (void) windowDidExitFullScreen:(NSNotification *)aNotification {
_enforceCorrectStyle();
}
@end // @implementation MainWindowObserver @end // @implementation MainWindowObserver
namespace Ui { namespace Ui {
@ -85,6 +93,7 @@ private:
void initCustomTitle(); void initCustomTitle();
[[nodiscard]] Fn<void(bool)> toggleCustomTitleCallback(); [[nodiscard]] Fn<void(bool)> toggleCustomTitleCallback();
[[nodiscard]] Fn<void()> enforceStyleCallback();
const not_null<WindowHelper*> _owner; const not_null<WindowHelper*> _owner;
const WindowObserver *_observer = nullptr; const WindowObserver *_observer = nullptr;
@ -100,7 +109,7 @@ private:
WindowHelper::Private::Private(not_null<WindowHelper*> owner) WindowHelper::Private::Private(not_null<WindowHelper*> owner)
: _owner(owner) : _owner(owner)
, _observer([[WindowObserver alloc] init:toggleCustomTitleCallback()]) { , _observer([[WindowObserver alloc] initWithToggle:toggleCustomTitleCallback() enforce:enforceStyleCallback()]) {
init(); init();
} }
@ -114,6 +123,14 @@ Fn<void(bool)> WindowHelper::Private::toggleCustomTitleCallback() {
}; };
} }
Fn<void()> WindowHelper::Private::enforceStyleCallback() {
return [=] {
if (_nativeWindow && _customTitleHeight > 0) {
[_nativeWindow setStyleMask:[_nativeWindow styleMask] | NSFullSizeContentViewWindowMask];
}
};
}
void WindowHelper::Private::initOpenGL() { void WindowHelper::Private::initOpenGL() {
auto forceOpenGL = std::make_unique<QOpenGLWidget>(_owner->_window); auto forceOpenGL = std::make_unique<QOpenGLWidget>(_owner->_window);
} }
@ -132,10 +149,12 @@ void WindowHelper::Private::initCustomTitle() {
|| ![_nativeWindow respondsToSelector:@selector(setTitlebarAppearsTransparent:)]) { || ![_nativeWindow respondsToSelector:@selector(setTitlebarAppearsTransparent:)]) {
return; return;
} }
[_nativeWindow setTitlebarAppearsTransparent:YES]; [_nativeWindow setTitlebarAppearsTransparent:YES];
[[NSNotificationCenter defaultCenter] addObserver:_observer selector:@selector(windowWillEnterFullScreen:) name:NSWindowWillEnterFullScreenNotification object:_nativeWindow]; [[NSNotificationCenter defaultCenter] addObserver:_observer selector:@selector(windowWillEnterFullScreen:) name:NSWindowWillEnterFullScreenNotification object:_nativeWindow];
[[NSNotificationCenter defaultCenter] addObserver:_observer selector:@selector(windowWillExitFullScreen:) name:NSWindowWillExitFullScreenNotification object:_nativeWindow]; [[NSNotificationCenter defaultCenter] addObserver:_observer selector:@selector(windowWillExitFullScreen:) name:NSWindowWillExitFullScreenNotification object:_nativeWindow];
[[NSNotificationCenter defaultCenter] addObserver:_observer selector:@selector(windowDidExitFullScreen:) name:NSWindowDidExitFullScreenNotification object:_nativeWindow];
// Qt has bug with layer-backed widgets containing QOpenGLWidgets. // Qt has bug with layer-backed widgets containing QOpenGLWidgets.
// See https://bugreports.qt.io/browse/QTBUG-64494 // See https://bugreports.qt.io/browse/QTBUG-64494