Skip to content

Commit

Permalink
NXTTabView: create box and set it as conten view for item view.
Browse files Browse the repository at this point in the history
  • Loading branch information
trunkmaster committed Nov 11, 2024
1 parent 25c4908 commit 2b9a498
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
9 changes: 9 additions & 0 deletions Frameworks/DesktopKit/NXTTabView.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
#import "NXTTabViewItem.h"

@interface NXTTabView : NSTabView
{
NSBox *_itemContentView;

CGFloat _tabHeight;
CGFloat _subviewTopLineHeight;
CGFloat _itemLeftOffset;
CGFloat _itemBottomOffset;
}
@property (readwrite) CGFloat topTabOffset;
@property (readwrite, copy) NSColor *unselectedBackgroundColor;
@property (readwrite, copy) NSColor *selectedBackgroundColor;
@end
45 changes: 33 additions & 12 deletions Frameworks/DesktopKit/NXTTabView.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,34 @@
#import <DesktopKit/Utilities.h>
#import "NXTTabView.h"

#define TAB_OFFSET 6

@implementation NXTTabView

#pragma mark - Overridings

- (id)initWithFrame:(NSRect)frameRect
{
[super initWithFrame:frameRect];

_topTabOffset = 6;
_itemLeftOffset = 1;
_itemBottomOffset = 2;
_tabHeight = [_font defaultLineHeightForFont] + _topTabOffset;
_subviewTopLineHeight = 1;

frameRect.size.width -= 3;
frameRect.size.height -= (_topTabOffset + _tabHeight + _subviewTopLineHeight + _itemBottomOffset);
frameRect.origin = NSMakePoint(_itemLeftOffset, _itemBottomOffset);
_itemContentView = [[NSBox alloc] initWithFrame:frameRect];
[_itemContentView setTitlePosition:NSNoTitle];
[_itemContentView setBorderType:NSNoBorder];
[_itemContentView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[_itemContentView setContentViewMargins:NSMakeSize(0, 0)];
[self addSubview:_itemContentView];
[_itemContentView release];

return self;
}

- (BOOL)isFlipped
{
return NO;
Expand Down Expand Up @@ -55,7 +77,7 @@ - (void)selectTabViewItem:(NSTabViewItem *)tabViewItem
if (_selected != nil) {
[_selected _setTabState:NSBackgroundTab];
// NB: If [_selected view] is nil this does nothing, which is fine.
[[_selected view] removeFromSuperview];
// [[_selected view] removeFromSuperview];
}

_selected = tabViewItem;
Expand All @@ -64,7 +86,8 @@ - (void)selectTabViewItem:(NSTabViewItem *)tabViewItem
if (selectedView != nil) {
NSView *firstResponder;

[self addSubview:selectedView];
// [self addSubview:selectedView];
[_itemContentView setContentView:selectedView];

firstResponder = [_selected initialFirstResponder];
if (firstResponder == nil) {
Expand Down Expand Up @@ -220,8 +243,6 @@ - (void)drawTabForItem:(NSTabViewItem *)item
- (void)drawRect:(NSRect)rect
{
NSGraphicsContext *ctxt = GSCurrentContext();
CGFloat tabHeight = [_font defaultLineHeightForFont] + TAB_OFFSET;
CGFloat subviewTopLineHeight = 1;
CGFloat tabOverlap = 25;

// NSLog(@"NXTabView: line height is: %f", [_font defaultLineHeightForFont]);
Expand All @@ -234,16 +255,16 @@ - (void)drawRect:(NSRect)rect

// Fill subview background
DPSsetgray(ctxt, [_selectedBackgroundColor whiteComponent]);
DPSrectfill(ctxt, 0, 0, rect.size.width, rect.size.height - TAB_OFFSET - tabHeight);
DPSrectfill(ctxt, 0, 0, rect.size.width, rect.size.height - _topTabOffset - _tabHeight);

DPSstroke(ctxt);

// Draw unselected
NSUInteger tabCount = [_items count];
if (tabCount > 0) {
CGFloat tabWidth = roundf(([self frame].size.width + (tabOverlap * (tabCount - 1))) / tabCount);
NSRect tabRect = NSMakeRect(0, (_frame.size.height - TAB_OFFSET - tabHeight - subviewTopLineHeight),
tabWidth, tabHeight);
NSRect tabRect = NSMakeRect(0, (_frame.size.height - _topTabOffset - _tabHeight - _subviewTopLineHeight),
tabWidth, _tabHeight);
NXTTabViewItem *item;
NSUInteger selectedTabIndex = 0;

Expand All @@ -262,7 +283,7 @@ - (void)drawRect:(NSRect)rect
}

// White line between views and tabs
NSDrawButton(NSMakeRect(0, 0, _frame.size.width, (_frame.size.height - TAB_OFFSET - tabHeight)),
NSDrawButton(NSMakeRect(0, 0, _frame.size.width, (_frame.size.height - _topTabOffset - _tabHeight)),
rect);

// Draw selected
Expand All @@ -280,9 +301,9 @@ - (void)drawRect:(NSRect)rect
CGFloat msgWidth = [msgFont widthOfString:message];
CGFloat msgHeight = [msgFont defaultLineHeightForFont];
NSPoint msgPoint = NSMakePoint((_frame.size.width - msgWidth) / 2,
(_frame.size.height - TAB_OFFSET - tabHeight - msgHeight) / 2);
(_frame.size.height - _topTabOffset - _tabHeight - msgHeight) / 2);
// White line between views and tabs
NSDrawButton(NSMakeRect(0, 0, _frame.size.width, (_frame.size.height - TAB_OFFSET - tabHeight)),
NSDrawButton(NSMakeRect(0, 0, _frame.size.width, (_frame.size.height - _topTabOffset - _tabHeight)),
rect);
[message drawAtPoint:msgPoint
withAttributes:@{
Expand Down

0 comments on commit 2b9a498

Please sign in to comment.