Menu items and shortcut to resize the window in the Cocoa frontend

This commit is contained in:
Lior Halphon
2023-12-04 00:34:44 +02:00
parent bce413821d
commit eba273d89c
3 changed files with 118 additions and 23 deletions

View File

@@ -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;
}
enum GBWindowResizeAction
{
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);
*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;
}
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;
}
- (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)newFrame
{
if (_fullScreen) {
return newFrame;
}
size_t width = GB_get_screen_width(&_gb),
height = GB_get_screen_height(&_gb);
NSRect rect = window.contentView.frame;
[self newRect:&newFrame forWindow:window action:GBWindowResizeActionZoom];
return newFrame;
}
unsigned titlebarSize = window.contentView.superview.frame.size.height - rect.size.height;
unsigned step = width / [[window screen] backingScaleFactor];
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;
- (IBAction)increaseWindowSize:(id)sender
{
NSRect rect;
if ([self newRect:&rect forWindow:_mainWindow action:GBWindowResizeActionIncrease]) {
[_mainWindow setFrame:rect display:true animate:true];
}
else if (rect.size.height > newFrame.size.height) {
rect.size.width = width;
rect.size.height = height + titlebarSize;
}
- (IBAction)decreaseWindowSize:(id)sender
{
NSRect rect;
if ([self newRect:&rect forWindow:_mainWindow action:GBWindowResizeActionDecrease]) {
[_mainWindow setFrame:rect display:true animate:true];
}
rect.origin = window.frame.origin;
rect.origin.y -= rect.size.height - window.frame.size.height;
return rect;
}
- (void) appendPendingOutput

View File

@@ -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"/>

View File

@@ -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>