Skip to content

Commit

Permalink
NXTTabView: trancate tab titles if it's allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
trunkmaster committed Oct 31, 2024
1 parent a9bca44 commit 4c18276
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
11 changes: 8 additions & 3 deletions Frameworks/DesktopKit/NXTTabView.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
//

#import <DesktopKit/Utilities.h>
#import "NXTTabView.h"
#include "Foundation/NSObjCRuntime.h"

#define TAB_OFFSET 6

Expand Down Expand Up @@ -155,18 +155,23 @@ - (void)drawTabTitle:(NSString *)title
{
NSPoint titlePosition;
NSDictionary *titleAttributes;
NSString *label = title;

// Fill text background
[background set];
NSRectFill(titleRect);

if (title) {
// TODO: `_trancated_label` is weird name. Use `_allowsTrancatedLabels` instead.
if (_truncated_label != NO) {
label = NXTShortenString(title, titleRect.size.width, _font, NXSymbolElement, NXTDotsAtRight);
}
titlePosition = NSMakePoint(
titleRect.origin.x + (titleRect.size.width - [_font widthOfString:title]) / 2,
titleRect.origin.x + (titleRect.size.width - [_font widthOfString:label]) / 2,
titleRect.origin.y + floorf((titleRect.size.height - [_font defaultLineHeightForFont]) / 2));
titleAttributes =
@{NSForegroundColorAttributeName : foreground, NSFontAttributeName : _font};
[title drawAtPoint:titlePosition withAttributes:titleAttributes];
[label drawAtPoint:titlePosition withAttributes:titleAttributes];
}
}

Expand Down
30 changes: 18 additions & 12 deletions Tests/DesktopKit/TabViewTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#import <AppKit/AppKit.h>
#include "AppKit/NSTextField.h"
#import <DesktopKit/NXTTabView.h>
#import "TabViewTest.h"

Expand All @@ -41,31 +42,36 @@ - (id)init
NXTTabView *tabView = [[NXTTabView alloc] initWithFrame:NSMakeRect(-1, -2, 359, 242)];
tabView.unselectedBackgroundColor = [NSColor grayColor];
tabView.selectedBackgroundColor = [NSColor lightGrayColor];
// [tabView setFont:[NSFont boldSystemFontOfSize:12]];
[tabView setAllowsTruncatedLabels:YES];
[tabView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

NXTTabViewItem *item;
item = [[NXTTabViewItem alloc] initWithIdentifier:@"Model"];
item.label = @"Model";
item = [[NXTTabViewItem alloc] initWithIdentifier:@"0"];
item.label = @"Instances";
NSTextField *subview = [[NSTextField alloc] initWithFrame:NSMakeRect(20, 100, (359 - 40), 21)];
[item setView:subview];
[subview release];
[tabView addTabViewItem:item];

item = [[NXTTabViewItem alloc] initWithIdentifier:@"Repeat"];
item.label = @"Repeat";
item = [[NXTTabViewItem alloc] initWithIdentifier:@"1"];
item.label = @"Classes";
[tabView addTabViewItem:item];

item = [[NXTTabViewItem alloc] initWithIdentifier:@"Layouts"];
item.label = @"Layouts";
item = [[NXTTabViewItem alloc] initWithIdentifier:@"2"];
item.label = @"Images";
[tabView addTabViewItem:item];

item = [[NXTTabViewItem alloc] initWithIdentifier:@"Shortcuts"];
item.label = @"Shortcuts";
item = [[NXTTabViewItem alloc] initWithIdentifier:@"3"];
item.label = @"Sounds";
[tabView addTabViewItem:item];

item = [[NXTTabViewItem alloc] initWithIdentifier:@"Keypad"];
item.label = @"Keypad";
item = [[NXTTabViewItem alloc] initWithIdentifier:@"4"];
item.label = @"LogngTitle";
[tabView addTabViewItem:item];

item = [[NXTTabViewItem alloc] initWithIdentifier:@"Modifiers"];
item.label = @"Modifiers";
item = [[NXTTabViewItem alloc] initWithIdentifier:@"5"];
item.label = @"VeryLogngTitle";
[tabView addTabViewItem:item];

[[window contentView] addSubview:tabView];
Expand Down

0 comments on commit 4c18276

Please sign in to comment.