Skip to content

Fix fontSizeUp/Down actions to preserve -monospace- font alias #1544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/MacVim/MMCoreTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,15 @@ - (void)changeFont:(id)sender

if (newFont) {
NSString *name = [newFont fontName];

// If this is a system monospace font, retrieve the user-friendly
// name before we send it to Vim. We don't want to expose the OS-
// specific system font name which is confusing to the user.
NSString *userMonospaceFontName = [[self vimController] systemFontNamesToAlias][name];
if (userMonospaceFontName != nil) {
name = userMonospaceFontName;
}

unsigned len = (unsigned)[name lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
if (len > 0) {
NSMutableData *data = [NSMutableData data];
Expand Down
4 changes: 4 additions & 0 deletions src/MacVim/MMVimController.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
BOOL hasModifiedBuffer;
}

/// Mapping from internal names for system monospace font to user-visible one.
/// E.g. ".AppleSystemUIFontMonospaced-Medium" -> "-monospace-Medium"
@property (nonatomic, readonly) NSMutableDictionary<NSString*, NSString*>* systemFontNamesToAlias;

- (id)initWithBackend:(id)backend pid:(int)processIdentifier;
- (void)uninitialize;
- (unsigned long)vimControllerId;
Expand Down
10 changes: 10 additions & 0 deletions src/MacVim/MMVimController.m
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ - (void)dealloc
[mainMenu release]; mainMenu = nil;
[creationDate release]; creationDate = nil;

[_systemFontNamesToAlias release]; _systemFontNamesToAlias = nil;

[super dealloc];
}

Expand Down Expand Up @@ -927,6 +929,14 @@ - (void)handleMessage:(int)msgid data:(NSData *)data
fontWeight = NSFontWeightBlack;
}
font = [NSFont monospacedSystemFontOfSize:size weight:fontWeight];

// We cache the internal name -> user-facing alias mapping
// to allow fontSizeUp/Down actions to be able to retain
// the user-facing font name in 'guifont'.
if (_systemFontNamesToAlias == nil) {
_systemFontNamesToAlias = [[NSMutableDictionary alloc] initWithCapacity:9];
}
_systemFontNamesToAlias[font.fontName] = name;
}
else
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/MacVim/MMWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1239,12 +1239,14 @@ - (IBAction)vimTouchbarItemAction:(id)sender

- (IBAction)fontSizeUp:(id)sender
{
// This creates a new font and triggers text view's changeFont: callback
[[NSFontManager sharedFontManager] modifyFont:
[NSNumber numberWithInt:NSSizeUpFontAction]];
}

- (IBAction)fontSizeDown:(id)sender
{
// This creates a new font and triggers text view's changeFont: callback
[[NSFontManager sharedFontManager] modifyFont:
[NSNumber numberWithInt:NSSizeDownFontAction]];
}
Expand Down
2 changes: 1 addition & 1 deletion src/MacVim/MacVim.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ enum {

extern NSString *VimFindPboardType;

// Alias for system monospace font name
/// Alias for system monospace font name
extern NSString *MMSystemFontAlias;


Expand Down
2 changes: 2 additions & 0 deletions src/MacVim/MacVimTests/MacVimTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,12 @@ - (void) testGuifontSystemMonospace {
[[[app keyVimController] windowController] fontSizeUp:nil];
[self waitForEventHandlingAndVimProcess];
XCTAssertEqualObjects([textView font], [NSFont monospacedSystemFontOfSize:13 weight:NSFontWeightHeavy]);
XCTAssertEqualObjects([[app keyVimController] evaluateVimExpression:@"&guifont"], @"-monospace-Heavy:h13");

[[[app keyVimController] windowController] fontSizeDown:nil];
[self waitForEventHandlingAndVimProcess];
XCTAssertEqualObjects([textView font], [NSFont monospacedSystemFontOfSize:12 weight:NSFontWeightHeavy]);
XCTAssertEqualObjects([[app keyVimController] evaluateVimExpression:@"&guifont"], @"-monospace-Heavy:h12");
}

/// Test that dark mode settings work and the corresponding Vim bindings are functional.
Expand Down