Skip to content

Commit ee474a9

Browse files
committed
Cleanup on new tabs code and make them compatible on old macOS versions
1 parent 90714ff commit ee474a9

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

runtime/doc/gui_mac.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ KEY VALUE ~
313313
*MMShowTabScrollButtons* enable tab scroll buttons on tabline [bool]
314314
*MMTabMinWidth* minimum width of a tab [int]
315315
*MMTabOptimumWidth* default width of a tab [int]
316+
*MMDefaultTablineColors* use default colors instead of colorscheme for tabs [bool]
316317
*MMTextInsetBottom* text area offset in pixels [int]
317318
*MMTextInsetLeft* text area offset in pixels [int]
318319
*MMTextInsetRight* text area offset in pixels [int]

runtime/doc/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5634,6 +5634,7 @@ MMAllowForceClickLookUp gui_mac.txt /*MMAllowForceClickLookUp*
56345634
MMAppearanceModeSelection gui_mac.txt /*MMAppearanceModeSelection*
56355635
MMCellWidthMultiplier gui_mac.txt /*MMCellWidthMultiplier*
56365636
MMCmdLineAlignBottom gui_mac.txt /*MMCmdLineAlignBottom*
5637+
MMDefaultTablineColors gui_mac.txt /*MMDefaultTablineColors*
56375638
MMDialogsTrackPwd gui_mac.txt /*MMDialogsTrackPwd*
56385639
MMDisableLaunchAnimation gui_mac.txt /*MMDisableLaunchAnimation*
56395640
MMFontPreserveLineSpacing gui_mac.txt /*MMFontPreserveLineSpacing*

src/MacVim/MMTabline/MMTab.m

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#import "MMTabline.h"
44
#import "MMHoverButton.h"
55

6+
#import "MacVim.h" // for availability macros
7+
8+
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_13
9+
typedef NSString * NSAnimatablePropertyKey;
10+
#endif
11+
612
@interface MMTab ()
713
@property (nonatomic) NSColor *fillColor;
814
@end
@@ -38,8 +44,14 @@ - (instancetype)initWithFrame:(NSRect)frameRect tabline:(MMTabline *)tabline
3844
[self addSubview:_closeButton];
3945

4046
_titleLabel = [NSTextField new];
41-
_titleLabel.stringValue = @"[No Name]";
42-
_titleLabel.font = [NSFont systemFontOfSize:11 weight:NSFontWeightSemibold];
47+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_11
48+
if (AVAILABLE_MAC_OS(10,11)) {
49+
_titleLabel.font = [NSFont systemFontOfSize:NSFont.smallSystemFontSize weight:NSFontWeightSemibold];
50+
} else
51+
#endif
52+
{
53+
_titleLabel.font = [NSFont systemFontOfSize:NSFont.smallSystemFontSize];
54+
}
4355
_titleLabel.textColor = NSColor.controlTextColor;
4456
_titleLabel.editable = NO;
4557
_titleLabel.selectable = NO;
@@ -122,12 +134,8 @@ - (void)setState:(MMTabState)state
122134
}
123135

124136
- (NSColor *)unselectedHoverColor
125-
{ // stackoverflow.com/a/52516863/111418
126-
NSAppearance *currentAppearance = NSAppearance.currentAppearance;
127-
NSAppearance.currentAppearance = self.effectiveAppearance;
128-
NSColor *c = [_tabline.tablineSelBgColor blendedColorWithFraction:0.6 ofColor:_tabline.tablineBgColor];
129-
NSAppearance.currentAppearance = currentAppearance;
130-
return c;
137+
{
138+
return [_tabline.tablineSelBgColor blendedColorWithFraction:0.6 ofColor:_tabline.tablineBgColor];
131139
}
132140

133141
- (void)drawRect:(NSRect)dirtyRect
@@ -148,7 +156,7 @@ - (void)drawRect:(NSRect)dirtyRect
148156
// line between the top of the tab and the window's title bar.
149157
// It looks better given the new way macOS 11 draws title bars.
150158
// Older macOS versions don't need this.
151-
if (@available(macOS 11.0, *)) {
159+
if (AVAILABLE_MAC_OS(11,0)) {
152160
NSAffineTransform *transform = [NSAffineTransform new];
153161
[transform translateXBy:0 yBy:-1.0];
154162
[p transformUsingAffineTransform:transform];

src/MacVim/MMTabline/MMTabline.m

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
const CGFloat MinimumTabWidth = 100;
1313
const CGFloat TabOverlap = 6;
1414

15-
MMHoverButton* MakeHoverButton(MMTabline *tabline, NSString *imageName, NSString *tooltip, SEL action, BOOL continuous) {
15+
static MMHoverButton* MakeHoverButton(MMTabline *tabline, NSString *imageName, NSString *tooltip, SEL action, BOOL continuous) {
1616
MMHoverButton *button = [MMHoverButton new];
1717
button.image = [MMHoverButton imageNamed:imageName];
1818
button.translatesAutoresizingMaskIntoConstraints = NO;
@@ -25,6 +25,11 @@
2525
return button;
2626
}
2727

28+
static BOOL isDarkMode(NSAppearance *appearance) {
29+
int flags = getCurrentAppearance(appearance);
30+
return (flags == 1 || flags == 3);
31+
}
32+
2833
@implementation MMTabline
2934
{
3035
NSView *_tabsContainer;
@@ -185,7 +190,7 @@ - (void)setShowsTabScrollButtons:(BOOL)showsTabScrollButtons
185190

186191
- (NSColor *)tablineBgColor
187192
{
188-
return _tablineBgColor ?: getCurrentAppearance(self.effectiveAppearance)
193+
return _tablineBgColor ?: isDarkMode(self.effectiveAppearance)
189194
? [NSColor colorWithWhite:0.2 alpha:1]
190195
: [NSColor colorWithWhite:0.8 alpha:1];
191196
}
@@ -209,7 +214,7 @@ - (void)setTablineFgColor:(NSColor *)color
209214

210215
- (NSColor *)tablineSelBgColor
211216
{
212-
return _tablineSelBgColor ?: getCurrentAppearance(self.effectiveAppearance)
217+
return _tablineSelBgColor ?: isDarkMode(self.effectiveAppearance)
213218
? [NSColor colorWithWhite:0.4 alpha:1]
214219
: NSColor.whiteColor;
215220
}
@@ -236,7 +241,7 @@ - (void)setTablineSelFgColor:(NSColor *)color
236241

237242
- (NSColor *)tablineFillFgColor
238243
{
239-
return _tablineFillFgColor ?: getCurrentAppearance(self.effectiveAppearance)
244+
return _tablineFillFgColor ?: isDarkMode(self.effectiveAppearance)
240245
? [NSColor colorWithWhite:0.2 alpha:1]
241246
: [NSColor colorWithWhite:0.8 alpha:1];
242247
}
@@ -427,7 +432,7 @@ - (void)fixupTabZOrder
427432
- (void)fixupLayoutWithAnimation:(BOOL)shouldAnimate
428433
{
429434
if (_tabs.count == 0) return;
430-
435+
431436
TabWidth t = [self tabWidthForTabs:_tabs.count];
432437
for (NSInteger i = 0; i < _tabs.count; i++) {
433438
MMTab *tab = _tabs[i];
@@ -440,8 +445,10 @@ - (void)fixupLayoutWithAnimation:(BOOL)shouldAnimate
440445
context.allowsImplicitAnimation = YES;
441446
tab.animator.frame = frame;
442447
[tab layout];
443-
}];
444-
} else tab.frame = frame;
448+
} completionHandler:nil];
449+
} else {
450+
tab.frame = frame;
451+
}
445452
}
446453
// _tabsContainer expands to fit tabs, is at least as wide as _scrollView.
447454
NSRect frame = _tabsContainer.frame;
@@ -597,19 +604,25 @@ - (void)updateTabScrollButtonsEnabledState
597604
- (void)scrollTabToVisibleAtIndex:(NSInteger)index
598605
{
599606
if (_tabs.count == 0) return;
600-
607+
601608
// Get the amount of time elapsed between the previous invocation
602609
// of this method and now. Use this elapsed time to set the animation
603610
// duration such that rapid invocations of this method result in
604611
// faster animations. For example, the user might hold down the tab
605612
// scrolling buttons (causing them to repeatedly fire) or they might
606613
// rapidly click them.
614+
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
607615
static NSTimeInterval lastTime = 0;
608616
struct timespec t;
609617
clock_gettime(CLOCK_MONOTONIC, &t);
610618
NSTimeInterval currentTime = t.tv_sec + t.tv_nsec * 1.e-9;
611619
NSTimeInterval elapsedTime = currentTime - lastTime;
612620
lastTime = currentTime;
621+
#else
622+
// clock_gettime was only added to macOS 10.12. Just use a fixed value. No
623+
// need to find an alternative API for legacy macOS versions.
624+
NSTimeInterval elapsedTime = 0.1;
625+
#endif
613626

614627
NSRect tabFrame = _tabs[index].frame;
615628
NSRect clipBounds = _scrollView.contentView.bounds;

src/MacVim/MMWindow.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ - (id)initWithContentRect:(NSRect)rect
7979
tablineSeparator = [[NSBox alloc] initWithFrame:tabSepRect];
8080

8181
[tablineSeparator setBoxType:NSBoxSeparator];
82-
[tablineSeparator setFillColor:NSColor.secondaryLabelColor];
8382
[tablineSeparator setHidden:YES];
8483
[tablineSeparator setAutoresizingMask:NSViewWidthSizable|NSViewMinYMargin];
8584

0 commit comments

Comments
 (0)