- added window position and size restoration to Cocoa backend

This commit is contained in:
alexey.lysiuk 2018-06-23 12:45:44 +03:00
parent 573758bc55
commit 009acf3fb2

View file

@ -49,7 +49,6 @@
#include "st_console.h" #include "st_console.h"
#include "v_text.h" #include "v_text.h"
#include "version.h" #include "version.h"
#include "videomodes.h"
#include "gl/renderer/gl_renderer.h" #include "gl/renderer/gl_renderer.h"
#include "gl/system/gl_framebuffer.h" #include "gl/system/gl_framebuffer.h"
@ -96,6 +95,11 @@ EXTERN_CVAR(Int, vid_defwidth)
EXTERN_CVAR(Int, vid_defheight) EXTERN_CVAR(Int, vid_defheight)
EXTERN_CVAR(Bool, fullscreen) EXTERN_CVAR(Bool, fullscreen)
CVAR(Int, win_x, -1, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
CVAR(Int, win_y, -1, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
CVAR(Int, win_w, -1, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
CVAR(Int, win_h, -1, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
CUSTOM_CVAR(Bool, vid_autoswitch, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL) CUSTOM_CVAR(Bool, vid_autoswitch, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL)
{ {
Printf("You must restart " GAMENAME " to apply graphics switching mode\n"); Printf("You must restart " GAMENAME " to apply graphics switching mode\n");
@ -154,6 +158,15 @@ namespace
[super setTitle:m_title]; [super setTitle:m_title];
} }
- (void)frameDidChange:(NSNotification*)notification
{
const NSRect frame = [self frame];
win_x = frame.origin.x;
win_y = frame.origin.y;
win_w = frame.size.width;
win_h = frame.size.height;
}
@end @end
@ -266,11 +279,8 @@ namespace
CocoaWindow* CreateCocoaWindow(const NSUInteger styleMask) CocoaWindow* CreateCocoaWindow(const NSUInteger styleMask)
{ {
static const CGFloat TEMP_WIDTH = VideoModes[0].width - 1;
static const CGFloat TEMP_HEIGHT = VideoModes[0].height - 1;
CocoaWindow* const window = [CocoaWindow alloc]; CocoaWindow* const window = [CocoaWindow alloc];
[window initWithContentRect:NSMakeRect(0, 0, TEMP_WIDTH, TEMP_HEIGHT) [window initWithContentRect:NSMakeRect(0, 0, vid_defwidth, vid_defheight)
styleMask:styleMask styleMask:styleMask
backing:NSBackingStoreBuffered backing:NSBackingStoreBuffered
defer:NO]; defer:NO];
@ -278,6 +288,16 @@ CocoaWindow* CreateCocoaWindow(const NSUInteger styleMask)
[window makeFirstResponder:appCtrl]; [window makeFirstResponder:appCtrl];
[window setAcceptsMouseMovedEvents:YES]; [window setAcceptsMouseMovedEvents:YES];
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc addObserver:window
selector:@selector(frameDidChange:)
name:NSWindowDidEndLiveResizeNotification
object:nil];
[nc addObserver:window
selector:@selector(frameDidChange:)
name:NSWindowDidMoveNotification
object:nil];
return window; return window;
} }
@ -441,13 +461,6 @@ void CocoaVideo::SetWindowTitle(const char* title)
void CocoaVideo::SetFullscreenMode() void CocoaVideo::SetFullscreenMode()
{ {
NSScreen* screen = [m_window screen];
const NSRect screenFrame = [screen frame];
const NSRect displayRect = vid_hidpi
? [screen convertRectToBacking:screenFrame]
: screenFrame;
if (!m_fullscreen) if (!m_fullscreen)
{ {
[m_window setLevel:LEVEL_FULLSCREEN]; [m_window setLevel:LEVEL_FULLSCREEN];
@ -456,16 +469,12 @@ void CocoaVideo::SetFullscreenMode()
[m_window setHidesOnDeactivate:YES]; [m_window setHidesOnDeactivate:YES];
} }
const NSRect screenFrame = [[m_window screen] frame];
[m_window setFrame:screenFrame display:YES]; [m_window setFrame:screenFrame display:YES];
} }
void CocoaVideo::SetWindowedMode() void CocoaVideo::SetWindowedMode()
{ {
const NSSize windowPixelSize = NSMakeSize(vid_defwidth, vid_defheight);
const NSSize windowSize = vid_hidpi
? [[m_window contentView] convertSizeFromBacking:windowPixelSize]
: windowPixelSize;
if (m_fullscreen) if (m_fullscreen)
{ {
[m_window setLevel:LEVEL_WINDOWED]; [m_window setLevel:LEVEL_WINDOWED];
@ -474,20 +483,18 @@ void CocoaVideo::SetWindowedMode()
[m_window setHidesOnDeactivate:NO]; [m_window setHidesOnDeactivate:NO];
} }
[m_window setContentSize:windowSize]; const bool isFrameValid = win_x >= 0 && win_y >= 0 && win_w > 320 && win_h > 200;
[m_window center]; const NSRect frameSize = isFrameValid
? NSMakeRect(win_x, win_y, win_w, win_h)
: NSMakeRect(0, 0, vid_defwidth, vid_defheight);
[m_window setFrame:frameSize display:YES];
[m_window enterFullscreenOnZoom]; [m_window enterFullscreenOnZoom];
[m_window exitAppOnClose]; [m_window exitAppOnClose];
} }
void CocoaVideo::SetMode(const bool fullscreen, const bool hiDPI) void CocoaVideo::SetMode(const bool fullscreen, const bool hiDPI)
{ {
if (fullscreen == m_fullscreen
&& hiDPI == m_hiDPI)
{
return;
}
NSOpenGLView* const glView = [m_window contentView]; NSOpenGLView* const glView = [m_window contentView];
[glView setWantsBestResolutionOpenGLSurface:hiDPI]; [glView setWantsBestResolutionOpenGLSurface:hiDPI];