SameBoy | Accurate GB/GBC emulator |
| download: https://git.y1.nz/archives/sameboy.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit eba273d89cfebb9fe557200d7987a885257e0557 parent bce413821d4de61a04f82adc76e5b04222248aa0 Author: Lior Halphon <LIJI32@gmail.com> Date: Mon, 4 Dec 2023 00:34:44 +0200 Menu items and shortcut to resize the window in the Cocoa frontend Diffstat:
| M | Cocoa/Document.m | 125 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------- |
| M | Cocoa/Document.xib | 2 | +- |
| M | Cocoa/MainMenu.xib | 14 | ++++++++++++-- |
3 files changed, 118 insertions(+), 23 deletions(-)
diff --git a/Cocoa/Document.m b/Cocoa/Document.m @@ -1259,6 +1259,12 @@ static bool is_path_writeable(const char *path) else if ([anItem action] == @selector(toggleAudioChannel:)) { [(NSMenuItem *)anItem setState:!GB_is_channel_muted(&_gb, [anItem tag])]; } + else if ([anItem action] == @selector(increaseWindowSize:)) { + return [self newRect:NULL forWindow:_mainWindow action:GBWindowResizeActionIncrease]; + } + else if ([anItem action] == @selector(decreaseWindowSize:)) { + return [self newRect:NULL forWindow:_mainWindow action:GBWindowResizeActionDecrease]; + } return [super validateUserInterfaceItem:anItem]; } @@ -1276,35 +1282,114 @@ static bool is_path_writeable(const char *path) self.view.mouseHidingEnabled = false; } -- (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)newFrame +enum GBWindowResizeAction { - if (_fullScreen) { - return newFrame; + GBWindowResizeActionZoom, + GBWindowResizeActionIncrease, + GBWindowResizeActionDecrease, +}; + +- (bool)newRect:(NSRect *)rect forWindow:(NSWindow *)window action:(enum GBWindowResizeAction)action +{ + if (_fullScreen) return false; + if (!rect) { + rect = alloca(sizeof(*rect)); } + size_t width = GB_get_screen_width(&_gb), - height = GB_get_screen_height(&_gb); + height = GB_get_screen_height(&_gb); + + *rect = window.contentView.frame; + + unsigned titlebarSize = window.contentView.superview.frame.size.height - rect->size.height; + + unsigned stepX = width / [[window screen] backingScaleFactor]; + unsigned stepY = height / [[window screen] backingScaleFactor]; + + if (action == GBWindowResizeActionDecrease) { + if (rect->size.width <= width || rect->size.height <= height) { + return false; + } + } + + typeof(floor) *roundFunc = action == GBWindowResizeActionDecrease? ceil : floor; + unsigned currentFactor = MIN(roundFunc(rect->size.width / stepX), roundFunc(rect->size.height / stepY)); + + rect->size.width = currentFactor * stepX; + rect->size.height = currentFactor * stepY + titlebarSize; + + if (action == GBWindowResizeActionDecrease) { + rect->size.width -= stepX; + rect->size.height -= stepY; + } + else { + rect->size.width += stepX; + rect->size.height += stepY; + } + + NSRect maxRect = [_mainWindow screen].visibleFrame; + + if (rect->size.width > maxRect.size.width || + rect->size.height > maxRect.size.height) { + if (action == GBWindowResizeActionIncrease) { + return false; + } + rect->size.width = width; + rect->size.height = height + titlebarSize; + } - NSRect rect = window.contentView.frame; + rect->origin = window.frame.origin; + if (action == GBWindowResizeActionZoom) { + rect->origin.y -= rect->size.height - window.frame.size.height; + } + else { + rect->origin.y -= (rect->size.height - window.frame.size.height) / 2; + rect->origin.x -= (rect->size.width - window.frame.size.width) / 2; + } + + if (rect->origin.x < maxRect.origin.x) { + rect->origin.x = maxRect.origin.x; + } + + if (rect->origin.y < maxRect.origin.y) { + rect->origin.y = maxRect.origin.y; + } + + if (rect->origin.x + rect->size.width > maxRect.origin.x + maxRect.size.width) { + rect->origin.x = maxRect.origin.x + maxRect.size.width - rect->size.width; + } + + if (rect->origin.y + rect->size.height > maxRect.origin.y + maxRect.size.height) { + rect->origin.y = maxRect.origin.y + maxRect.size.height - rect->size.height; + } + + return true; +} - unsigned titlebarSize = window.contentView.superview.frame.size.height - rect.size.height; - unsigned step = width / [[window screen] backingScaleFactor]; +- (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)newFrame +{ + if (_fullScreen) { + return newFrame; + } + [self newRect:&newFrame forWindow:window action:GBWindowResizeActionZoom]; + return newFrame; +} - rect.size.width = floor(rect.size.width / step) * step + step; - rect.size.height = rect.size.width * height / width + titlebarSize; - if (rect.size.width > newFrame.size.width) { - rect.size.width = width; - rect.size.height = height + titlebarSize; - } - else if (rect.size.height > newFrame.size.height) { - rect.size.width = width; - rect.size.height = height + titlebarSize; +- (IBAction)increaseWindowSize:(id)sender +{ + NSRect rect; + if ([self newRect:&rect forWindow:_mainWindow action:GBWindowResizeActionIncrease]) { + [_mainWindow setFrame:rect display:true animate:true]; } +} - rect.origin = window.frame.origin; - rect.origin.y -= rect.size.height - window.frame.size.height; - - return rect; +- (IBAction)decreaseWindowSize:(id)sender +{ + NSRect rect; + if ([self newRect:&rect forWindow:_mainWindow action:GBWindowResizeActionDecrease]) { + [_mainWindow setFrame:rect display:true animate:true]; + } } - (void) appendPendingOutput diff --git a/Cocoa/Document.xib b/Cocoa/Document.xib @@ -52,7 +52,7 @@ </customObject> <customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/> <customObject id="-3" userLabel="Application" customClass="NSObject"/> - <window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="xOd-HO-29H" userLabel="Window"> + <window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" tabbingMode="disallowed" id="xOd-HO-29H" userLabel="Window"> <windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/> <windowCollectionBehavior key="collectionBehavior" fullScreenPrimary="YES"/> <rect key="contentRect" x="0.0" y="0.0" width="160" height="144"/> diff --git a/Cocoa/MainMenu.xib b/Cocoa/MainMenu.xib @@ -1,8 +1,8 @@ <?xml version="1.0" encoding="UTF-8"?> -<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14868" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct"> +<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21507" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct"> <dependencies> <deployment identifier="macosx"/> - <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14868"/> + <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21507"/> </dependencies> <objects> <customObject id="-2" userLabel="File's Owner" customClass="NSApplication"> @@ -553,6 +553,16 @@ <action selector="performMiniaturize:" target="-1" id="VwT-WD-YPe"/> </connections> </menuItem> + <menuItem title="Increase Window Size" keyEquivalent="+" id="jh7-RG-JP7"> + <connections> + <action selector="increaseWindowSize:" target="-1" id="yXg-4S-0VI"/> + </connections> + </menuItem> + <menuItem title="Decrease Window Size" keyEquivalent="-" id="wsI-Is-iNn"> + <connections> + <action selector="decreaseWindowSize:" target="-1" id="sFZ-b4-gIp"/> + </connections> + </menuItem> <menuItem title="Zoom" id="R4o-n2-Eq4"> <modifierMask key="keyEquivalentModifierMask"/> <connections>
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.