Skip to content

Commit

Permalink
Menu items and shortcut to resize the window in the Cocoa frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
LIJI32 committed Dec 3, 2023
1 parent bce4138 commit eba273d
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 23 deletions.
125 changes: 105 additions & 20 deletions Cocoa/Document.m
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,12 @@ - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)anItem
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];
}
Expand All @@ -1276,35 +1282,114 @@ - (void) windowWillExitFullScreen:(NSNotification *)notification
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
Expand Down
2 changes: 1 addition & 1 deletion Cocoa/Document.xib
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down
14 changes: 12 additions & 2 deletions Cocoa/MainMenu.xib
Original file line number Diff line number Diff line change
@@ -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">
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit eba273d

Please sign in to comment.