Skip to content

Commit

Permalink
Allow the Cocoa frontend to pick a monospace font
Browse files Browse the repository at this point in the history
  • Loading branch information
LIJI32 committed Nov 9, 2024
1 parent 41af62d commit f217613
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 65 deletions.
1 change: 1 addition & 0 deletions Cocoa/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@ enum model {
- (void) connectLinkCable:(NSMenuItem *)sender;
- (int)loadStateFile:(const char *)path noErrorOnNotFound:(bool)noErrorOnFileNotFound;
- (NSString *)captureOutputForBlock: (void (^)())block;
- (NSFont *)debuggerFontOfSize:(unsigned)size;
@end

86 changes: 82 additions & 4 deletions Cocoa/Document.m
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ - (void)initCommon
[self observeStandardDefaultsKey:@"GBRumbleMode" withBlock:^(NSNumber *value) {
GB_set_rumble_mode(gb, value.unsignedIntValue);
}];

[self observeStandardDefaultsKey:@"GBDebuggerFont" withBlock:^(NSString *value) {
[self updateFonts];
}];

[self observeStandardDefaultsKey:@"GBDebuggerFontSize" withBlock:^(NSString *value) {
[self updateFonts];
}];
}

- (void)updateMinSize
Expand Down Expand Up @@ -766,6 +774,65 @@ - (void)dealloc
}
}

- (NSFont *)debuggerFontOfSize:(unsigned)size
{
if (!size) {
size = [[NSUserDefaults standardUserDefaults] integerForKey:@"GBDebuggerFontSize"];
}

bool retry = false;

again:;
NSString *selectedFont = [[NSUserDefaults standardUserDefaults] stringForKey:@"GBDebuggerFont"];
if (@available(macOS 10.15, *)) {
if ([selectedFont isEqual:@"SF Mono"]) {
return [NSFont monospacedSystemFontOfSize:size weight:NSFontWeightRegular];
}
}

NSFont *ret = [NSFont fontWithName:selectedFont size:size];
if (ret) return ret;

if (retry) {
return [NSFont userFixedPitchFontOfSize:size];
}
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"GBDebuggerFont"];
retry = true;
goto again;
}


- (void)updateFonts
{
_hexController.font = [self debuggerFontOfSize:12];
[self.paletteView reloadData:self];
[self.objectView reloadData:self];

NSFont *newFont = [self debuggerFontOfSize:0];
NSFont *newBoldFont = [[NSFontManager sharedFontManager] convertFont:newFont toHaveTrait:NSBoldFontMask];
self.debuggerSideViewInput.font = newFont;
for (NSTextView *view in @[_debuggerSideView, _consoleOutput]) {
NSMutableAttributedString *newString = view.attributedString.mutableCopy;
[view.attributedString enumerateAttribute:NSFontAttributeName
inRange:NSMakeRange(0, view.attributedString.length)
options:0
usingBlock:^(NSFont *value, NSRange range, BOOL *stop) {
if ([[NSFontManager sharedFontManager] fontNamed:value.fontName hasTraits:NSBoldFontMask]) {
[newString addAttributes:@{
NSFontAttributeName: newBoldFont
} range:range];
}
else {
[newString addAttributes:@{
NSFontAttributeName: newFont
} range:range];
}
}];
[view.textStorage setAttributedString:newString];
}
[_consoleOutput scrollToEndOfDocument:nil];
}

- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
Expand All @@ -781,7 +848,7 @@ - (void)windowControllerDidLoadNib:(NSWindowController *)aController
NSMutableParagraphStyle *paragraph_style = [[NSMutableParagraphStyle alloc] init];
[paragraph_style setLineSpacing:2];

self.debuggerSideViewInput.font = [NSFont userFixedPitchFontOfSize:12];
self.debuggerSideViewInput.font = [self debuggerFontOfSize:0];
self.debuggerSideViewInput.textColor = [NSColor whiteColor];
self.debuggerSideViewInput.defaultParagraphStyle = paragraph_style;
[self.debuggerSideViewInput setString:@"registers\nbacktrace\n"];
Expand Down Expand Up @@ -907,9 +974,10 @@ - (void)windowControllerDidLoadNib:(NSWindowController *)aController
}
}

- (void) initMemoryView
- (void)initMemoryView
{
_hexController = [[HFController alloc] init];
_hexController.font = [self debuggerFontOfSize:12];
[_hexController setBytesPerColumn:1];
[_hexController setEditMode:HFOverwriteMode];

Expand Down Expand Up @@ -953,6 +1021,16 @@ - (void) initMemoryView
[self.memoryView addSubview:layoutView];
self.memoryView = layoutView;

CGSize contentSize = _memoryWindow.contentView.frame.size;
while (_hexController.bytesPerLine < 16) {
contentSize.width += 4;
[_memoryWindow setContentSize:contentSize];
}
while (_hexController.bytesPerLine > 16) {
contentSize.width -= 4;
[_memoryWindow setContentSize:contentSize];
}

self.memoryBankItem.enabled = false;
}

Expand Down Expand Up @@ -1456,7 +1534,7 @@ - (void) appendPendingOutput
[_consoleOutputLock unlock];
}

- (void) log: (const char *) string withAttributes: (GB_log_attributes) attributes
- (void)log:(const char *)string withAttributes:(GB_log_attributes)attributes
{
NSString *nsstring = @(string); // For ref-counting
if (_capturedOutput) {
Expand All @@ -1465,7 +1543,7 @@ - (void) log: (const char *) string withAttributes: (GB_log_attributes) attribut
}


NSFont *font = [NSFont userFixedPitchFontOfSize:12];
NSFont *font = [self debuggerFontOfSize:0];
NSUnderlineStyle underline = NSUnderlineStyleNone;
if (attributes & GB_LOG_BOLD) {
font = [[NSFontManager sharedFontManager] convertFont:font toHaveTrait:NSBoldFontMask];
Expand Down
24 changes: 12 additions & 12 deletions Cocoa/Document.xib
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,18 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<scrollView fixedFrame="YES" borderType="none" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" scrollerKnobStyle="dark" translatesAutoresizingMaskIntoConstraints="NO" id="V9U-Ua-F4z">
<rect key="frame" x="0.0" y="354" width="329" height="47"/>
<rect key="frame" x="0.0" y="338" width="329" height="63"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<clipView key="contentView" drawsBackground="NO" copiesOnScroll="NO" id="YHx-TM-zIC">
<rect key="frame" x="0.0" y="0.0" width="329" height="47"/>
<rect key="frame" x="0.0" y="0.0" width="329" height="63"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textView drawsBackground="NO" importsGraphics="NO" richText="NO" verticallyResizable="YES" allowsCharacterPickerTouchBarItem="NO" allowsUndo="YES" allowsNonContiguousLayout="YES" textCompletion="NO" spellingCorrection="YES" id="w0g-eK-jM4">
<rect key="frame" x="0.0" y="0.0" width="329" height="47"/>
<rect key="frame" x="0.0" y="0.0" width="329" height="63"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
<size key="minSize" width="329" height="47"/>
<size key="minSize" width="329" height="63"/>
<size key="maxSize" width="463" height="10000000"/>
<color key="insertionPointColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<allowedInputSourceLocales>
Expand All @@ -266,27 +266,27 @@
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" wantsLayer="YES" verticalHuggingPriority="750" doubleValue="1" horizontal="NO" id="qgN-F8-fdB">
<rect key="frame" x="313" y="0.0" width="16" height="47"/>
<rect key="frame" x="313" y="0.0" width="16" height="63"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
<box verticalHuggingPriority="750" fixedFrame="YES" boxType="separator" translatesAutoresizingMaskIntoConstraints="NO" id="5qI-qZ-nkh">
<rect key="frame" x="0.0" y="352" width="329" height="5"/>
<rect key="frame" x="0.0" y="336" width="329" height="5"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
</box>
<scrollView fixedFrame="YES" borderType="none" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="vts-CC-ZjQ">
<rect key="frame" x="0.0" y="0.0" width="329" height="354"/>
<rect key="frame" x="0.0" y="0.0" width="329" height="338"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<clipView key="contentView" drawsBackground="NO" copiesOnScroll="NO" id="Cs9-3x-ATg">
<rect key="frame" x="0.0" y="0.0" width="329" height="354"/>
<rect key="frame" x="0.0" y="0.0" width="329" height="338"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textView editable="NO" drawsBackground="NO" importsGraphics="NO" richText="NO" verticallyResizable="YES" baseWritingDirection="leftToRight" findStyle="bar" allowsNonContiguousLayout="YES" spellingCorrection="YES" id="JgV-7E-iwp">
<rect key="frame" x="0.0" y="0.0" width="329" height="354"/>
<rect key="frame" x="0.0" y="0.0" width="329" height="338"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" red="0.14901960780000001" green="0.14901960780000001" blue="0.14901960780000001" alpha="1" colorSpace="calibratedRGB"/>
<size key="minSize" width="329" height="354"/>
<size key="minSize" width="329" height="338"/>
<size key="maxSize" width="1160" height="10000000"/>
<color key="insertionPointColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<allowedInputSourceLocales>
Expand All @@ -301,7 +301,7 @@
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="4jm-Gm-D2R">
<rect key="frame" x="313" y="0.0" width="16" height="354"/>
<rect key="frame" x="313" y="0.0" width="16" height="338"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
Expand Down Expand Up @@ -746,7 +746,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<clipView key="contentView" id="mzf-yu-RID">
<rect key="frame" x="1" y="1" width="604" height="257"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="none" alternatingRowBackgroundColors="YES" columnReordering="NO" columnResizing="NO" multipleSelection="NO" emptySelection="NO" autosaveColumns="NO" typeSelect="NO" rowHeight="24" headerView="pvX-uJ-qK5" id="tA3-8T-bxb">
<rect key="frame" x="0.0" y="0.0" width="604" height="240"/>
Expand Down
8 changes: 8 additions & 0 deletions Cocoa/GBApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ - (void) applicationDidFinishLaunching:(NSNotification *)notification
[defaults removeObjectForKey:button_to_preference_name(i, 0)];
}
}

bool hasSFMono = false;
if (@available(macOS 10.15, *)) {
hasSFMono = [[NSFont monospacedSystemFontOfSize:12 weight:NSFontWeightRegular].displayName containsString:@"SF"];
}
[[NSUserDefaults standardUserDefaults] registerDefaults:@{
@"GBRight": @(kVK_RightArrow),
@"GBLeft": @(kVK_LeftArrow),
Expand Down Expand Up @@ -85,6 +90,9 @@ - (void) applicationDidFinishLaunching:(NSNotification *)notification

@"GBEmulatedModel": @(MODEL_AUTO),

@"GBDebuggerFont": hasSFMono? @"SF Mono" : @"Menlo",
@"GBDebuggerFontSize": @12,

// Default themes
@"GBThemes": @{
@"Canyon": @{
Expand Down
10 changes: 10 additions & 0 deletions Cocoa/GBObjectView.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,23 @@ - (void)reloadData:(Document *)document
uint8_t length = document.oamCount;
bool cgb = GB_is_cgb(document.gb);
uint8_t height = document.oamHeight;
NSFont *font = [document debuggerFontOfSize:11];
NSFont *boldFont = [[NSFontManager sharedFontManager] convertFont:font toHaveTrait:NSBoldFontMask];

for (unsigned i = 0; i < 40; i++) {
GBObjectViewItem *item = _items[i];
if (i >= length) {
item.view.hidden = true;
}
else {
item.view.hidden = false;

item.oamAddress.font = boldFont;
item.position.font = font;
item.attributes.font = font;
item.tile.font = font;
item.tileAddress.font = font;

item.oamAddress.stringValue = [NSString stringWithFormat:@"$%04X", info[i].oam_addr];
item.position.stringValue = [NSString stringWithFormat:@"(%d, %d)",
((signed)(unsigned)info[i].x) - 8,
Expand Down
4 changes: 2 additions & 2 deletions Cocoa/GBObjectViewItem.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"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down
2 changes: 2 additions & 0 deletions Cocoa/GBPaletteView.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ - (void)reloadData:(Document *)document
GB_gameboy_t *gb = document.gb;
uint8_t *bg = GB_get_direct_access(gb, GB_DIRECT_ACCESS_BGP, NULL, NULL);
uint8_t *obj = GB_get_direct_access(gb, GB_DIRECT_ACCESS_OBP, NULL, NULL);
NSFont *font = [document debuggerFontOfSize:13];

for (unsigned i = 0; i < 4 * 8 * 2; i++) {
uint8_t index = i % (4 * 8);
Expand All @@ -58,6 +59,7 @@ - (void)reloadData:(Document *)document
field.stringValue = [NSString stringWithFormat:@"$%04X", color];
field.textColor = r * 3 + g * 4 + b * 2 > 120? [NSColor blackColor] : [NSColor whiteColor];
field.toolTip = [NSString stringWithFormat:@"Red: %d, Green: %d, Blue: %d", r, g, b];
field.font = font;
field.backgroundColor = [NSColor colorWithRed:(nativeColor & 0xFF) / 255.0
green:((nativeColor >> 8) & 0xFF) / 255.0
blue:((nativeColor >> 16) & 0xFF) / 255.0
Expand Down
4 changes: 2 additions & 2 deletions Cocoa/GBPaletteViewRow.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"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down
4 changes: 4 additions & 0 deletions Cocoa/GBPreferencesWindow.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import <Cocoa/Cocoa.h>
#import <JoyKit/JoyKit.h>
#import "GBPaletteEditorController.h"
#import "GBTitledPopUpButton.h"

@interface GBPreferencesWindow : NSWindow <NSTableViewDelegate, NSTableViewDataSource, JOYListener>
@property IBOutlet NSTableView *controlsTableView;
Expand All @@ -18,4 +19,7 @@
@property IBOutlet NSPopUpButton *colorPalettePopupButton;
@property IBOutlet NSPopUpButton *hotkey1PopupButton;
@property IBOutlet NSPopUpButton *hotkey2PopupButton;

@property IBOutlet GBTitledPopUpButton *fontPopupButton;
@property IBOutlet NSStepper *fontSizeStepper;
@end
Loading

0 comments on commit f217613

Please sign in to comment.