Skip to content

Commit 52550fc

Browse files
authored
Merge pull request #1510 from ychin/customizable-document-icon-setting
Add setting for whether to show document icon at title bar
2 parents 28e9af8 + b872678 commit 52550fc

File tree

7 files changed

+96
-14
lines changed

7 files changed

+96
-14
lines changed

src/MacVim/Base.lproj/Preferences.xib

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,11 @@
280280
<point key="canvasLocation" x="137.5" y="-17"/>
281281
</customView>
282282
<customView id="hr4-G4-3ZG" userLabel="Appearance">
283-
<rect key="frame" x="0.0" y="0.0" width="483" height="360"/>
283+
<rect key="frame" x="0.0" y="0.0" width="483" height="379"/>
284284
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
285285
<subviews>
286286
<customView id="fw0-VK-Nbz" userLabel="Dark mode selection">
287-
<rect key="frame" x="19" y="182" width="433" height="156"/>
287+
<rect key="frame" x="19" y="201" width="433" height="156"/>
288288
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
289289
<subviews>
290290
<textField focusRingType="none" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" id="T40-Os-PUf" userLabel="Dark mode selection">
@@ -345,11 +345,11 @@
345345
</subviews>
346346
</customView>
347347
<customView id="7af-iK-4r7" userLabel="Titlebar appearance">
348-
<rect key="frame" x="19" y="118" width="433" height="56"/>
348+
<rect key="frame" x="19" y="118" width="433" height="75"/>
349349
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
350350
<subviews>
351351
<textField focusRingType="none" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" id="9Rk-gT-kVC" userLabel="Titlebar appearance">
352-
<rect key="frame" x="-2" y="38" width="187" height="17"/>
352+
<rect key="frame" x="-2" y="57" width="187" height="17"/>
353353
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
354354
<textFieldCell key="cell" sendsActionOnEndEditing="YES" alignment="right" title="Window appearance:" id="HEH-Lo-v4I" userLabel="Titlebar appearance:">
355355
<font key="font" metaFont="system"/>
@@ -358,7 +358,7 @@
358358
</textFieldCell>
359359
</textField>
360360
<button id="7ie-0J-0Zr">
361-
<rect key="frame" x="189" y="37" width="244" height="18"/>
361+
<rect key="frame" x="189" y="56" width="244" height="18"/>
362362
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
363363
<buttonCell key="cell" type="check" title="Transparent title bar" bezelStyle="regularSquare" imagePosition="left" alignment="left" inset="2" id="hzd-hj-Pth">
364364
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
@@ -370,7 +370,7 @@
370370
</connections>
371371
</button>
372372
<button id="Hqh-Ql-2od">
373-
<rect key="frame" x="189" y="19" width="244" height="18"/>
373+
<rect key="frame" x="189" y="38" width="244" height="18"/>
374374
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
375375
<buttonCell key="cell" type="check" title="Hidden title bar" bezelStyle="regularSquare" imagePosition="left" alignment="left" inset="2" id="W40-cB-m1U">
376376
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
@@ -381,8 +381,20 @@
381381
<binding destination="58" name="value" keyPath="values.MMNoTitleBarWindow" id="CKL-mo-ZhZ"/>
382382
</connections>
383383
</button>
384+
<button id="PoE-cf-3kL" userLabel="Show document icon at title bar">
385+
<rect key="frame" x="189" y="19" width="244" height="18"/>
386+
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
387+
<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">
388+
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
389+
<font key="font" metaFont="system"/>
390+
</buttonCell>
391+
<connections>
392+
<action selector="appearanceChanged:" target="-2" id="e2x-W8-tpy"/>
393+
<binding destination="58" name="value" keyPath="values.MMTitlebarShowsDocumentIcon" id="svp-gP-mdE"/>
394+
</connections>
395+
</button>
384396
<button id="s2M-pj-U8e">
385-
<rect key="frame" x="189" y="1" width="244" height="18"/>
397+
<rect key="frame" x="189" y="-1" width="244" height="18"/>
386398
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
387399
<buttonCell key="cell" type="check" title="No drop shadows" bezelStyle="regularSquare" imagePosition="left" alignment="left" inset="2" id="VM8-tg-mVV">
388400
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
@@ -496,7 +508,7 @@
496508
</subviews>
497509
</customView>
498510
</subviews>
499-
<point key="canvasLocation" x="137.5" y="435"/>
511+
<point key="canvasLocation" x="137.5" y="444.5"/>
500512
</customView>
501513
<customView id="Bnq-Nx-GJH" userLabel="Input">
502514
<rect key="frame" x="0.0" y="0.0" width="483" height="110"/>

src/MacVim/MMAppController.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ + (void)registerDefaults
217217
[NSNumber numberWithInt:0], MMAppearanceModeSelectionKey,
218218
[NSNumber numberWithBool:NO], MMNoTitleBarWindowKey,
219219
[NSNumber numberWithBool:NO], MMTitlebarAppearsTransparentKey,
220+
[NSNumber numberWithBool:YES], MMTitlebarShowsDocumentIconKey,
220221
[NSNumber numberWithBool:NO], MMZoomBothKey,
221222
@"", MMLoginShellCommandKey,
222223
@"", MMLoginShellArgumentKey,

src/MacVim/MMWindowController.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
NSRect preFullScreenFrame;
4343
MMWindow *decoratedWindow;
4444
NSString *lastSetTitle;
45+
NSString *documentFilename; ///< File name of document being edited, used for the icon at the title bar.
4546
int userRows;
4647
int userCols;
4748
NSPoint userTopLeft;
@@ -57,6 +58,7 @@
5758
- (id)initWithVimController:(MMVimController *)controller;
5859
- (MMVimController *)vimController;
5960
- (MMVimView *)vimView;
61+
- (NSWindow *)window;
6062
- (NSString *)windowAutosaveKey;
6163
- (void)setWindowAutosaveKey:(NSString *)key;
6264
- (void)cleanup;
@@ -72,6 +74,7 @@
7274
- (void)zoomWithRows:(int)rows columns:(int)cols state:(int)state;
7375
- (void)setTitle:(NSString *)title;
7476
- (void)setDocumentFilename:(NSString *)filename;
77+
- (void)updateDocumentFilename;
7578
- (void)setToolbar:(NSToolbar *)toolbar;
7679
- (void)createScrollbarWithIdentifier:(int32_t)ident type:(int)type;
7780
- (BOOL)destroyScrollbarWithIdentifier:(int32_t)ident;

src/MacVim/MMWindowController.m

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ - (void)dealloc
268268
// in case processAfterWindowPresentedQueue wasn't called
269269
[afterWindowPresentedQueue release]; afterWindowPresentedQueue = nil;
270270
[lastSetTitle release]; lastSetTitle = nil;
271+
[documentFilename release]; documentFilename = nil;
271272

272273
[super dealloc];
273274
}
@@ -290,6 +291,11 @@ - (MMVimView *)vimView
290291
return vimView;
291292
}
292293

294+
- (NSWindow *)window
295+
{
296+
return decoratedWindow;
297+
}
298+
293299
- (NSString *)windowAutosaveKey
294300
{
295301
return windowAutosaveKey;
@@ -503,6 +509,8 @@ - (void)setTitle:(NSString *)title
503509
}
504510
}
505511

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

524+
[filename retain];
525+
[documentFilename release];
526+
documentFilename = filename;
527+
528+
[self updateDocumentFilename];
529+
}
530+
531+
- (void)updateDocumentFilename
532+
{
533+
if (documentFilename == nil)
534+
return;
535+
const bool showDocumentIcon = [[NSUserDefaults standardUserDefaults] boolForKey:MMTitlebarShowsDocumentIconKey];
536+
NSString *filename = showDocumentIcon ? documentFilename : @"";
516537
[decoratedWindow setRepresentedFilename:filename];
517538
[fullScreenWindow setRepresentedFilename:filename];
518539

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

527542
- (void)setToolbar:(NSToolbar *)theToolbar
@@ -613,6 +628,7 @@ - (void)refreshApperanceMode
613628

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

617633
// Dark mode only works on 10.14+ because that's when dark mode was
618634
// introduced.

src/MacVim/MacVimTests/MacVimTests.m

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,4 +458,52 @@ - (void) testGuifontSystemMonospace {
458458
[self waitForVimClose];
459459
}
460460

461+
/// Test that document icon is shown in title bar when enabled.
462+
- (void) testTitlebarDocumentIcon {
463+
MMAppController *app = MMAppController.sharedInstance;
464+
465+
[app openNewWindow:NewWindowClean activate:YES];
466+
[self waitForVimOpenAndMessages];
467+
468+
NSWindow *win = [[[app keyVimController] windowController] window];
469+
470+
// Untitled documents have no icons
471+
XCTAssertEqualObjects(@"", win.representedFilename);
472+
473+
// Test that the document icon is shown when a file (gui_mac.txt) is opened by querying "representedFilename"
474+
[self sendStringToVim:@":help macvim\n" withMods:0];
475+
[self waitForEventHandlingAndVimProcess];
476+
NSString *gui_mac_path = [[NSBundle mainBundle] pathForResource:@"gui_mac.txt" ofType:nil inDirectory:@"vim/runtime/doc"];
477+
XCTAssertEqualObjects(gui_mac_path, win.representedFilename);
478+
479+
// Change setting to hide the document icon
480+
NSUserDefaults *ud = NSUserDefaults.standardUserDefaults;
481+
NSDictionary<NSString *, id> *defaults = [ud volatileDomainForName:NSArgumentDomain];
482+
NSMutableDictionary<NSString *, id> *newDefaults = [defaults mutableCopy];
483+
newDefaults[MMTitlebarShowsDocumentIconKey] = @NO;
484+
[ud setVolatileDomain:newDefaults forName:NSArgumentDomain];
485+
486+
// Test that there is no document icon shown
487+
[app refreshAllAppearances];
488+
XCTAssertEqualObjects(@"", win.representedFilename);
489+
490+
// Change setting back to show the document icon. Test that the path was remembered and icon is shown.
491+
newDefaults[MMTitlebarShowsDocumentIconKey] = @YES;
492+
[ud setVolatileDomain:newDefaults forName:NSArgumentDomain];
493+
[app refreshAllAppearances];
494+
XCTAssertEqualObjects(gui_mac_path, win.representedFilename);
495+
496+
// Close the file to go back to untitled document and make sure no icon is shown
497+
[self sendStringToVim:@":q\n" withMods:0];
498+
[self waitForEventHandlingAndVimProcess];
499+
XCTAssertEqualObjects(@"", win.representedFilename);
500+
501+
// Restore settings to test defaults
502+
[ud setVolatileDomain:defaults forName:NSArgumentDomain];
503+
504+
// Clean up
505+
[[app keyVimController] sendMessage:VimShouldCloseMsgID data:nil];
506+
[self waitForVimClose];
507+
}
508+
461509
@end

src/MacVim/Miscellaneous.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ extern NSString *MMFontPreserveLineSpacingKey;
3737
extern NSString *MMAppearanceModeSelectionKey;
3838
extern NSString *MMNoTitleBarWindowKey;
3939
extern NSString *MMTitlebarAppearsTransparentKey;
40+
extern NSString *MMTitlebarShowsDocumentIconKey;
4041
extern NSString *MMNoWindowShadowKey;
4142
extern NSString *MMDisableLaunchAnimationKey;
4243
extern NSString *MMLoginShellKey;

src/MacVim/Miscellaneous.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
NSString *MMAppearanceModeSelectionKey = @"MMAppearanceModeSelection";
3434
NSString *MMNoTitleBarWindowKey = @"MMNoTitleBarWindow";
3535
NSString *MMTitlebarAppearsTransparentKey = @"MMTitlebarAppearsTransparent";
36+
NSString *MMTitlebarShowsDocumentIconKey = @"MMTitlebarShowsDocumentIcon";
3637
NSString *MMNoWindowShadowKey = @"MMNoWindowShadow";
3738
NSString *MMDisableLaunchAnimationKey = @"MMDisableLaunchAnimation";
3839
NSString *MMLoginShellKey = @"MMLoginShell";

0 commit comments

Comments
 (0)