Skip to content

Commit

Permalink
Add setting for whether to show document icon at title bar
Browse files Browse the repository at this point in the history
Don't automatically hide the icon while in transparent title bar mode,
since it's a little confusing to the user. Instead, just use an extra
setting for that.

Also, when hiding the icon, properly hide it now by clearing it.
Previously it was just making an invisible icon which was still
draggable and didn't behave properly. Make sure after we set the setting
it's refreshed correctly.

Fix #1503
  • Loading branch information
ychin committed Nov 4, 2024
1 parent 28e9af8 commit b872678
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 14 deletions.
28 changes: 20 additions & 8 deletions src/MacVim/Base.lproj/Preferences.xib
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,11 @@
<point key="canvasLocation" x="137.5" y="-17"/>
</customView>
<customView id="hr4-G4-3ZG" userLabel="Appearance">
<rect key="frame" x="0.0" y="0.0" width="483" height="360"/>
<rect key="frame" x="0.0" y="0.0" width="483" height="379"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<customView id="fw0-VK-Nbz" userLabel="Dark mode selection">
<rect key="frame" x="19" y="182" width="433" height="156"/>
<rect key="frame" x="19" y="201" width="433" height="156"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField focusRingType="none" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" id="T40-Os-PUf" userLabel="Dark mode selection">
Expand Down Expand Up @@ -345,11 +345,11 @@
</subviews>
</customView>
<customView id="7af-iK-4r7" userLabel="Titlebar appearance">
<rect key="frame" x="19" y="118" width="433" height="56"/>
<rect key="frame" x="19" y="118" width="433" height="75"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField focusRingType="none" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" id="9Rk-gT-kVC" userLabel="Titlebar appearance">
<rect key="frame" x="-2" y="38" width="187" height="17"/>
<rect key="frame" x="-2" y="57" width="187" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
<textFieldCell key="cell" sendsActionOnEndEditing="YES" alignment="right" title="Window appearance:" id="HEH-Lo-v4I" userLabel="Titlebar appearance:">
<font key="font" metaFont="system"/>
Expand All @@ -358,7 +358,7 @@
</textFieldCell>
</textField>
<button id="7ie-0J-0Zr">
<rect key="frame" x="189" y="37" width="244" height="18"/>
<rect key="frame" x="189" y="56" width="244" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Transparent title bar" bezelStyle="regularSquare" imagePosition="left" alignment="left" inset="2" id="hzd-hj-Pth">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand All @@ -370,7 +370,7 @@
</connections>
</button>
<button id="Hqh-Ql-2od">
<rect key="frame" x="189" y="19" width="244" height="18"/>
<rect key="frame" x="189" y="38" width="244" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Hidden title bar" bezelStyle="regularSquare" imagePosition="left" alignment="left" inset="2" id="W40-cB-m1U">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand All @@ -381,8 +381,20 @@
<binding destination="58" name="value" keyPath="values.MMNoTitleBarWindow" id="CKL-mo-ZhZ"/>
</connections>
</button>
<button id="PoE-cf-3kL" userLabel="Show document icon at title bar">
<rect key="frame" x="189" y="19" width="244" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Show document icon at title bar" bezelStyle="regularSquare" imagePosition="left" alignment="left" inset="2" id="AvU-Lc-YNn" userLabel="Show document icon at title bar">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="appearanceChanged:" target="-2" id="e2x-W8-tpy"/>
<binding destination="58" name="value" keyPath="values.MMTitlebarShowsDocumentIcon" id="svp-gP-mdE"/>
</connections>
</button>
<button id="s2M-pj-U8e">
<rect key="frame" x="189" y="1" width="244" height="18"/>
<rect key="frame" x="189" y="-1" width="244" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="No drop shadows" bezelStyle="regularSquare" imagePosition="left" alignment="left" inset="2" id="VM8-tg-mVV">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand Down Expand Up @@ -496,7 +508,7 @@
</subviews>
</customView>
</subviews>
<point key="canvasLocation" x="137.5" y="435"/>
<point key="canvasLocation" x="137.5" y="444.5"/>
</customView>
<customView id="Bnq-Nx-GJH" userLabel="Input">
<rect key="frame" x="0.0" y="0.0" width="483" height="110"/>
Expand Down
1 change: 1 addition & 0 deletions src/MacVim/MMAppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ + (void)registerDefaults
[NSNumber numberWithInt:0], MMAppearanceModeSelectionKey,
[NSNumber numberWithBool:NO], MMNoTitleBarWindowKey,
[NSNumber numberWithBool:NO], MMTitlebarAppearsTransparentKey,
[NSNumber numberWithBool:YES], MMTitlebarShowsDocumentIconKey,
[NSNumber numberWithBool:NO], MMZoomBothKey,
@"", MMLoginShellCommandKey,
@"", MMLoginShellArgumentKey,
Expand Down
3 changes: 3 additions & 0 deletions src/MacVim/MMWindowController.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
NSRect preFullScreenFrame;
MMWindow *decoratedWindow;
NSString *lastSetTitle;
NSString *documentFilename; ///< File name of document being edited, used for the icon at the title bar.
int userRows;
int userCols;
NSPoint userTopLeft;
Expand All @@ -57,6 +58,7 @@
- (id)initWithVimController:(MMVimController *)controller;
- (MMVimController *)vimController;
- (MMVimView *)vimView;
- (NSWindow *)window;
- (NSString *)windowAutosaveKey;
- (void)setWindowAutosaveKey:(NSString *)key;
- (void)cleanup;
Expand All @@ -72,6 +74,7 @@
- (void)zoomWithRows:(int)rows columns:(int)cols state:(int)state;
- (void)setTitle:(NSString *)title;
- (void)setDocumentFilename:(NSString *)filename;
- (void)updateDocumentFilename;
- (void)setToolbar:(NSToolbar *)toolbar;
- (void)createScrollbarWithIdentifier:(int32_t)ident type:(int)type;
- (BOOL)destroyScrollbarWithIdentifier:(int32_t)ident;
Expand Down
28 changes: 22 additions & 6 deletions src/MacVim/MMWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ - (void)dealloc
// in case processAfterWindowPresentedQueue wasn't called
[afterWindowPresentedQueue release]; afterWindowPresentedQueue = nil;
[lastSetTitle release]; lastSetTitle = nil;
[documentFilename release]; documentFilename = nil;

[super dealloc];
}
Expand All @@ -290,6 +291,11 @@ - (MMVimView *)vimView
return vimView;
}

- (NSWindow *)window
{
return decoratedWindow;
}

- (NSString *)windowAutosaveKey
{
return windowAutosaveKey;
Expand Down Expand Up @@ -503,6 +509,8 @@ - (void)setTitle:(NSString *)title
}
}

/// Set the currently edited document's file path, passed in from Vim. Buffers with
/// no file paths will be passed in as empty strings.
- (void)setDocumentFilename:(NSString *)filename
{
if (!filename)
Expand All @@ -513,15 +521,22 @@ - (void)setDocumentFilename:(NSString *)filename
if (![[NSFileManager defaultManager] fileExistsAtPath:filename])
filename = @"";

[filename retain];
[documentFilename release];
documentFilename = filename;

[self updateDocumentFilename];
}

- (void)updateDocumentFilename
{
if (documentFilename == nil)
return;
const bool showDocumentIcon = [[NSUserDefaults standardUserDefaults] boolForKey:MMTitlebarShowsDocumentIconKey];
NSString *filename = showDocumentIcon ? documentFilename : @"";
[decoratedWindow setRepresentedFilename:filename];
[fullScreenWindow setRepresentedFilename:filename];

if ([[NSUserDefaults standardUserDefaults] boolForKey:MMTitlebarAppearsTransparentKey]) {
// Remove the draggable file icon in the title bar for a clean look
// when we are in transparent titlebar mode.
[[decoratedWindow standardWindowButton:NSWindowDocumentIconButton] setImage: nil];
[[fullScreenWindow standardWindowButton:NSWindowDocumentIconButton] setImage: nil];
}
}

- (void)setToolbar:(NSToolbar *)theToolbar
Expand Down Expand Up @@ -613,6 +628,7 @@ - (void)refreshApperanceMode

// Title may have been lost if we hid the title-bar. Reset it.
[self setTitle:lastSetTitle];
[self updateDocumentFilename];

// Dark mode only works on 10.14+ because that's when dark mode was
// introduced.
Expand Down
48 changes: 48 additions & 0 deletions src/MacVim/MacVimTests/MacVimTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,52 @@ - (void) testGuifontSystemMonospace {
[self waitForVimClose];
}

/// Test that document icon is shown in title bar when enabled.
- (void) testTitlebarDocumentIcon {
MMAppController *app = MMAppController.sharedInstance;

[app openNewWindow:NewWindowClean activate:YES];
[self waitForVimOpenAndMessages];

NSWindow *win = [[[app keyVimController] windowController] window];

// Untitled documents have no icons
XCTAssertEqualObjects(@"", win.representedFilename);

// Test that the document icon is shown when a file (gui_mac.txt) is opened by querying "representedFilename"
[self sendStringToVim:@":help macvim\n" withMods:0];
[self waitForEventHandlingAndVimProcess];
NSString *gui_mac_path = [[NSBundle mainBundle] pathForResource:@"gui_mac.txt" ofType:nil inDirectory:@"vim/runtime/doc"];
XCTAssertEqualObjects(gui_mac_path, win.representedFilename);

// Change setting to hide the document icon
NSUserDefaults *ud = NSUserDefaults.standardUserDefaults;
NSDictionary<NSString *, id> *defaults = [ud volatileDomainForName:NSArgumentDomain];
NSMutableDictionary<NSString *, id> *newDefaults = [defaults mutableCopy];
newDefaults[MMTitlebarShowsDocumentIconKey] = @NO;
[ud setVolatileDomain:newDefaults forName:NSArgumentDomain];

// Test that there is no document icon shown
[app refreshAllAppearances];
XCTAssertEqualObjects(@"", win.representedFilename);

// Change setting back to show the document icon. Test that the path was remembered and icon is shown.
newDefaults[MMTitlebarShowsDocumentIconKey] = @YES;
[ud setVolatileDomain:newDefaults forName:NSArgumentDomain];
[app refreshAllAppearances];
XCTAssertEqualObjects(gui_mac_path, win.representedFilename);

// Close the file to go back to untitled document and make sure no icon is shown
[self sendStringToVim:@":q\n" withMods:0];
[self waitForEventHandlingAndVimProcess];
XCTAssertEqualObjects(@"", win.representedFilename);

// Restore settings to test defaults
[ud setVolatileDomain:defaults forName:NSArgumentDomain];

// Clean up
[[app keyVimController] sendMessage:VimShouldCloseMsgID data:nil];
[self waitForVimClose];
}

@end
1 change: 1 addition & 0 deletions src/MacVim/Miscellaneous.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extern NSString *MMFontPreserveLineSpacingKey;
extern NSString *MMAppearanceModeSelectionKey;
extern NSString *MMNoTitleBarWindowKey;
extern NSString *MMTitlebarAppearsTransparentKey;
extern NSString *MMTitlebarShowsDocumentIconKey;
extern NSString *MMNoWindowShadowKey;
extern NSString *MMDisableLaunchAnimationKey;
extern NSString *MMLoginShellKey;
Expand Down
1 change: 1 addition & 0 deletions src/MacVim/Miscellaneous.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
NSString *MMAppearanceModeSelectionKey = @"MMAppearanceModeSelection";
NSString *MMNoTitleBarWindowKey = @"MMNoTitleBarWindow";
NSString *MMTitlebarAppearsTransparentKey = @"MMTitlebarAppearsTransparent";
NSString *MMTitlebarShowsDocumentIconKey = @"MMTitlebarShowsDocumentIcon";
NSString *MMNoWindowShadowKey = @"MMNoWindowShadow";
NSString *MMDisableLaunchAnimationKey = @"MMDisableLaunchAnimation";
NSString *MMLoginShellKey = @"MMLoginShell";
Expand Down

0 comments on commit b872678

Please sign in to comment.