| 1 |
/* |
| 2 |
* Copyright (C) 2023- Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#import "WindowControllerMacOS.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "application/AppInboundProtocol.h" |
| 13 |
#include "application/Application.h" |
| 14 |
#include "interfaces/AnnouncementManager.h" |
| 15 |
#include "messaging/ApplicationMessenger.h" |
| 16 |
#include "settings/DisplaySettings.h" |
| 17 |
#include "settings/Settings.h" |
| 18 |
#include "settings/SettingsComponent.h" |
| 19 |
#import "windowing/osx/OpenGL/OSXGLView.h" |
| 20 |
#include "windowing/osx/WinEventsOSX.h" |
| 21 |
#import "windowing/osx/WinSystemOSX.h" |
| 22 |
|
| 23 |
@implementation XBMCWindowControllerMacOS |
| 24 |
|
| 25 |
- (nullable instancetype)initWithTitle:(NSString*)title defaultSize:(NSSize)size |
| 26 |
{ |
| 27 |
auto frame = NSMakeRect(0, 0, size.width, size.height); |
| 28 |
const NSWindowStyleMask style = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | |
| 29 |
NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable; |
| 30 |
|
| 31 |
auto window = [[NSWindow alloc] initWithContentRect:frame |
| 32 |
styleMask:style |
| 33 |
backing:NSBackingStoreBuffered |
| 34 |
defer:YES]; |
| 35 |
window.backgroundColor = NSColor.blackColor; |
| 36 |
window.title = title; |
| 37 |
window.titlebarAppearsTransparent = YES; |
| 38 |
window.titleVisibility = NSWindowTitleHidden; |
| 39 |
|
| 40 |
window.collectionBehavior = NSWindowCollectionBehaviorFullScreenPrimary | |
| 41 |
NSWindowCollectionBehaviorFullScreenDisallowsTiling; |
| 42 |
window.tabbingMode = NSWindowTabbingModeDisallowed; |
| 43 |
|
| 44 |
if ((self = [super initWithWindow:window])) |
| 45 |
{ |
| 46 |
self.windowFrameAutosaveName = @"OSXGLWindowPositionHeightWidth"; |
| 47 |
self.shouldCascadeWindows = NO; |
| 48 |
window.delegate = self; |
| 49 |
window.contentView = [[OSXGLView alloc] initWithFrame:NSZeroRect]; |
| 50 |
window.acceptsMouseMovedEvents = YES; |
| 51 |
} |
| 52 |
g_application.m_AppFocused = true; |
| 53 |
return self; |
| 54 |
} |
| 55 |
|
| 56 |
- (void)windowDidResize:(NSNotification*)aNotification |
| 57 |
{ |
| 58 |
if ((self.window.styleMask & NSWindowStyleMaskFullScreen) != NSWindowStyleMaskFullScreen) |
| 59 |
{ |
| 60 |
NSRect rect = [self.window.contentView |
| 61 |
convertRectToBacking:[self.window contentRectForFrameRect:self.window.frame]]; |
| 62 |
|
| 63 |
XBMC_Event newEvent = {}; |
| 64 |
newEvent.type = XBMC_VIDEORESIZE; |
| 65 |
newEvent.resize.width = static_cast<int>(rect.size.width); |
| 66 |
newEvent.resize.height = static_cast<int>(rect.size.height); |
| 67 |
newEvent.resize.scale = 1.0; |
| 68 |
|
| 69 |
// check for valid sizes cause in some cases |
| 70 |
// we are hit during fullscreen transition from macos |
| 71 |
// and might be technically "zero" sized |
| 72 |
if (newEvent.resize.width != 0 && newEvent.resize.height != 0) |
| 73 |
{ |
| 74 |
std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort(); |
| 75 |
if (appPort) |
| 76 |
appPort->OnEvent(newEvent); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
- (void)windowWillStartLiveResize:(NSNotification*)notification |
| 82 |
{ |
| 83 |
std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort(); |
| 84 |
if (appPort) |
| 85 |
{ |
| 86 |
appPort->SetRenderGUI(false); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
- (void)windowDidEndLiveResize:(NSNotification*)notification |
| 91 |
{ |
| 92 |
std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort(); |
| 93 |
if (appPort) |
| 94 |
{ |
| 95 |
appPort->SetRenderGUI(true); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
- (void)windowDidMiniaturize:(NSNotification*)aNotification |
| 100 |
{ |
| 101 |
g_application.m_AppFocused = false; |
| 102 |
} |
| 103 |
|
| 104 |
- (void)windowDidDeminiaturize:(NSNotification*)aNotification |
| 105 |
{ |
| 106 |
g_application.m_AppFocused = true; |
| 107 |
} |
| 108 |
|
| 109 |
- (void)windowDidBecomeKey:(NSNotification*)aNotification |
| 110 |
{ |
| 111 |
g_application.m_AppFocused = true; |
| 112 |
CServiceBroker::GetAnnouncementManager()->Announce(ANNOUNCEMENT::GUI, "WindowFocused"); |
| 113 |
|
| 114 |
auto winSystem = dynamic_cast<CWinSystemOSX*>(CServiceBroker::GetWinSystem()); |
| 115 |
if (winSystem) |
| 116 |
{ |
| 117 |
winSystem->enableInputEvents(); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
- (void)windowDidResignKey:(NSNotification*)aNotification |
| 122 |
{ |
| 123 |
g_application.m_AppFocused = false; |
| 124 |
CServiceBroker::GetAnnouncementManager()->Announce(ANNOUNCEMENT::GUI, "WindowUnfocused"); |
| 125 |
|
| 126 |
auto winSystem = dynamic_cast<CWinSystemOSX*>(CServiceBroker::GetWinSystem()); |
| 127 |
if (winSystem) |
| 128 |
{ |
| 129 |
winSystem->disableInputEvents(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
- (BOOL)windowShouldClose:(id)sender |
| 134 |
{ |
| 135 |
if (!g_application.m_bStop) |
| 136 |
CServiceBroker::GetAppMessenger()->PostMsg(TMSG_QUIT); |
| 137 |
|
| 138 |
return NO; |
| 139 |
} |
| 140 |
|
| 141 |
- (void)windowDidExpose:(NSNotification*)aNotification |
| 142 |
{ |
| 143 |
g_application.m_AppFocused = true; |
| 144 |
} |
| 145 |
|
| 146 |
- (void)windowDidMove:(NSNotification*)aNotification |
| 147 |
{ |
| 148 |
if (self.window.contentView) |
| 149 |
{ |
| 150 |
NSPoint window_origin = [self.window.contentView frame].origin; |
| 151 |
XBMC_Event newEvent = {}; |
| 152 |
newEvent.type = XBMC_VIDEOMOVE; |
| 153 |
newEvent.move.x = window_origin.x; |
| 154 |
newEvent.move.y = window_origin.y; |
| 155 |
std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort(); |
| 156 |
if (appPort) |
| 157 |
{ |
| 158 |
appPort->OnEvent(newEvent); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
- (void)windowDidChangeScreen:(NSNotification*)notification |
| 164 |
{ |
| 165 |
// window has moved to a different screen |
| 166 |
if (self.window) |
| 167 |
{ |
| 168 |
XBMC_Event newEvent = {}; |
| 169 |
newEvent.type = XBMC_SCREENCHANGE; |
| 170 |
newEvent.screen.screenIdx = |
| 171 |
static_cast<unsigned int>([NSScreen.screens indexOfObject:self.window.screen]); |
| 172 |
std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort(); |
| 173 |
if (appPort) |
| 174 |
{ |
| 175 |
appPort->OnEvent(newEvent); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
- (void)windowDidChangeBackingProperties:(NSNotification*)notification |
| 181 |
{ |
| 182 |
// if the backing scale changes, we need to force resize the window |
| 183 |
//! @TODO resize only when the backing scale changes, this can also be triggered when the colorspace changes (e.g. HDR/EDR) |
| 184 |
[self windowDidResize:notification]; |
| 185 |
} |
| 186 |
|
| 187 |
- (NSSize)windowWillResize:(NSWindow*)sender toSize:(NSSize)frameSize |
| 188 |
{ |
| 189 |
return frameSize; |
| 190 |
} |
| 191 |
|
| 192 |
- (void)windowWillEnterFullScreen:(NSNotification*)pNotification |
| 193 |
{ |
| 194 |
CWinSystemOSX* winSystem = dynamic_cast<CWinSystemOSX*>(CServiceBroker::GetWinSystem()); |
| 195 |
if (!winSystem) |
| 196 |
return; |
| 197 |
|
| 198 |
// if osx is the issuer of the toggle |
| 199 |
// call Kodi's toggle function |
| 200 |
if (!winSystem->GetFullscreenWillToggle()) |
| 201 |
{ |
| 202 |
// indicate that we are toggling |
| 203 |
// flag will be reset in SetFullscreen once its |
| 204 |
// called from Kodi's gui thread |
| 205 |
winSystem->SetFullscreenWillToggle(true); |
| 206 |
|
| 207 |
CServiceBroker::GetAppMessenger()->PostMsg(TMSG_TOGGLEFULLSCREEN); |
| 208 |
} |
| 209 |
else |
| 210 |
{ |
| 211 |
// in this case we are just called because |
| 212 |
// of Kodi did a toggle - just reset the flag |
| 213 |
// we don't need to do anything else |
| 214 |
winSystem->SetFullscreenWillToggle(false); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
- (NSApplicationPresentationOptions)window:(NSWindow*)window |
| 219 |
willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions |
| 220 |
{ |
| 221 |
// customize our appearance when entering full screen: |
| 222 |
// we don't want the dock to appear but we want the menubar to hide/show automatically |
| 223 |
// |
| 224 |
return (NSApplicationPresentationFullScreen | // support full screen for this window (required) |
| 225 |
NSApplicationPresentationHideDock | // completely hide the dock |
| 226 |
NSApplicationPresentationAutoHideMenuBar); // yes we want the menu bar to show/hide |
| 227 |
} |
| 228 |
|
| 229 |
- (void)windowDidExitFullScreen:(NSNotification*)pNotification |
| 230 |
{ |
| 231 |
auto winSystem = dynamic_cast<CWinSystemOSX*>(CServiceBroker::GetWinSystem()); |
| 232 |
if (!winSystem) |
| 233 |
return; |
| 234 |
|
| 235 |
// if osx is the issuer of the toggle |
| 236 |
// call Kodi's toggle function |
| 237 |
if (!winSystem->GetFullscreenWillToggle()) |
| 238 |
{ |
| 239 |
// indicate that we are toggling |
| 240 |
// flag will be reset in SetFullscreen once its |
| 241 |
// called from Kodi's gui thread |
| 242 |
winSystem->SetFullscreenWillToggle(true); |
| 243 |
CServiceBroker::GetAppMessenger()->PostMsg(TMSG_TOGGLEFULLSCREEN); |
| 244 |
} |
| 245 |
else |
| 246 |
{ |
| 247 |
// in this case we are just called because |
| 248 |
// of Kodi did a toggle - just reset the flag |
| 249 |
// we don't need to do anything else |
| 250 |
winSystem->SetFullscreenWillToggle(false); |
| 251 |
} |
| 252 |
winSystem->SignalFullScreenStateChanged(false); |
| 253 |
} |
| 254 |
|
| 255 |
- (void)windowDidEnterFullScreen:(NSNotification*)notification |
| 256 |
{ |
| 257 |
auto winSystem = dynamic_cast<CWinSystemOSX*>(CServiceBroker::GetWinSystem()); |
| 258 |
if (!winSystem) |
| 259 |
return; |
| 260 |
winSystem->SignalFullScreenStateChanged(true); |
| 261 |
} |
| 262 |
@end |