Skip to content

Commit

Permalink
remove coupling between tabview and browserview.
Browse files Browse the repository at this point in the history
- call only through tabbarcontrol
- replace calls to tabbarcontrol.tabview with calls to tabbarcontrol if possible
  • Loading branch information
TAKeanice committed Apr 5, 2018
1 parent b36cce0 commit 533173a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 65 deletions.
5 changes: 2 additions & 3 deletions Interfaces/Base.lproj/MainWindowController.xib
Expand Up @@ -408,7 +408,7 @@
<popUpButton key="view" verticalHuggingPriority="750" id="DDQ-PY-F2c">
<rect key="frame" x="0.0" y="14" width="41" height="28"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<popUpButtonCell key="cell" type="roundTextured" bezelStyle="texturedRounded" imagePosition="only" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" pullsDown="YES" selectedItem="pdk-MS-QME" id="xAh-aB-Ek3">
<popUpButtonCell key="cell" type="roundTextured" bezelStyle="texturedRounded" image="NewFeedTemplate" imagePosition="only" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" pullsDown="YES" selectedItem="pdk-MS-QME" id="xAh-aB-Ek3">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
<menu key="menu" id="tQg-b5-Yiu">
Expand Down Expand Up @@ -633,7 +633,7 @@
<popUpButton key="view" verticalHuggingPriority="750" id="gOd-tT-XFQ">
<rect key="frame" x="0.0" y="14" width="41" height="28"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<popUpButtonCell key="cell" type="roundTextured" bezelStyle="texturedRounded" imagePosition="only" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" pullsDown="YES" selectedItem="lu2-BZ-Gxd" id="5CR-Qn-4pP">
<popUpButtonCell key="cell" type="roundTextured" bezelStyle="texturedRounded" image="StyleTemplate" imagePosition="only" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" pullsDown="YES" selectedItem="lu2-BZ-Gxd" id="5CR-Qn-4pP">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
<menu key="menu" id="mit-Vi-iR1">
Expand Down Expand Up @@ -824,7 +824,6 @@
<autoresizingMask key="autoresizingMask"/>
<connections>
<outlet property="tabBarControl" destination="1238" id="1243"/>
<outlet property="tabView" destination="1218" id="1230"/>
</connections>
</customView>
<customView translatesAutoresizingMaskIntoConstraints="NO" id="1288" customClass="EnclosureView">
Expand Down
127 changes: 65 additions & 62 deletions src/BrowserView.m
Expand Up @@ -26,25 +26,24 @@
#import <MMTabBarView/MMTabBarView.h>
#import "AppController.h"

@interface NSTabView (BrowserViewAdditions)
@interface MMTabBarView (BrowserViewAdditions)
-(NSTabViewItem *)tabViewItemWithIdentifier:(id)identifier;
@end

@implementation NSTabView (BrowserViewAdditions)
@implementation MMTabBarView (BrowserViewAdditions)

/* tabViewItemWithIdentifier
* Returns the tab view item that matches the specified identifier.
*/
-(NSTabViewItem *)tabViewItemWithIdentifier:(id)identifier
{
NSInteger i = [self indexOfTabViewItemWithIdentifier:identifier];
return (i != NSNotFound ? [self tabViewItemAtIndex:i] : nil);
NSInteger i = [self.tabView indexOfTabViewItemWithIdentifier:identifier];
return (i != NSNotFound ? [self.tabView tabViewItemAtIndex:i] : nil);
}
@end

@interface BrowserView () <MMTabBarViewDelegate>

@property (weak, nonatomic) IBOutlet NSTabView *tabView;
//queue for tab view items to select when current item is closed
@property NSMutableArray<NSTabViewItem *> *tabViewOrder;

Expand All @@ -70,8 +69,9 @@ @implementation BrowserView

-(void)awakeFromNib
{
[[self.tabView tabViewItemAtIndex:0] setLabel:NSLocalizedString(@"Articles", nil)];
[[tabBarControl.tabView tabViewItemAtIndex:0] setLabel:NSLocalizedString(@"Articles", nil)];
[self configureTabBar];
[self configureTabClosingBehavior];

self.tabViewOrder = [NSMutableArray array];
}
Expand Down Expand Up @@ -111,7 +111,7 @@ -(void)configureTabBar
*/
-(NSString *)view:(NSView *)view stringForToolTip:(NSToolTipTag)tag point:(NSPoint)point userData:(void *)userData
{
return [self.tabView tabViewItemWithIdentifier:(__bridge NSView *)userData].label;
return [tabBarControl tabViewItemWithIdentifier:(__bridge NSView *)userData].label;
}

/* setPrimaryTabItemView
Expand All @@ -125,11 +125,11 @@ -(void)setPrimaryTabItemView:(NSView<BaseView, WebUIDelegate, WebFrameLoadDelega
if (primaryTabItemView == nil)
{
// This should only be called on launch
item = [self.tabView tabViewItemAtIndex:0];
item = [tabBarControl.tabView tabViewItemAtIndex:0];
}
else
{
item = [self.tabView tabViewItemWithIdentifier:primaryTabItemView];
item = [tabBarControl tabViewItemWithIdentifier:primaryTabItemView];
}

[item setHasCloseButton:NO];
Expand All @@ -142,15 +142,15 @@ -(void)setPrimaryTabItemView:(NSView<BaseView, WebUIDelegate, WebFrameLoadDelega
[self setActiveTabToPrimaryTab];
//this call seems to be necessary manually here, no delegate call
//maybe setPrimaryTabItemView is called earlier than the delegate IBOutlet setup.
[self tabView:self.tabView didSelectTabViewItem:item];
[self tabView:tabBarControl.tabView didSelectTabViewItem:item];
}

/* activeTabItemView
* Returns the view associated with the active tab.
*/
-(NSView<BaseView> *)activeTabItemView
{
return self.tabView.selectedTabViewItem.identifier;
return tabBarControl.tabView.selectedTabViewItem.identifier;
}

/* setActiveTabToPrimaryTab
Expand All @@ -174,85 +174,63 @@ -(void)setActiveTabToPrimaryTab
*/
-(void)setTabItemViewTitle:(NSView *)inTabView title:(NSString *)newTitle
{
[self.tabView tabViewItemWithIdentifier:inTabView].label = newTitle;
[tabBarControl tabViewItemWithIdentifier:inTabView].label = newTitle;
}

/* tabTitle
* Returns the title of the specified tab. May be an empty string.
*/
-(NSString *)tabItemViewTitle:(NSView *)tabItemView
{
return [self.tabView tabViewItemWithIdentifier:tabItemView].label;
return [tabBarControl tabViewItemWithIdentifier:tabItemView].label;
}

/* closeAllTabs
* Close all tabs.
*/
-(void)closeAllTabs
{
NSInteger count = self.tabView.numberOfTabViewItems;
NSInteger count = tabBarControl.numberOfTabViewItems;
NSInteger i;
for ((i = (count - 1)); i >= 0; i--) {
NSTabViewItem * item = [self.tabView tabViewItemAtIndex:i];
NSTabViewItem * item = [tabBarControl.tabView tabViewItemAtIndex:i];
if (item.identifier != primaryTabItemView)
{
[self.tabViewOrder removeObject:item];
[self.tabView removeTabViewItem:item];
[tabBarControl removeTabViewItem:item];
}
}
}

/*
for manually closing tabs (not initiated from tabview)
*/
-(void)closeTabItemView:(NSView *)tabItemView
{
NSTabViewItem *tabViewItem = [self.tabView tabViewItemWithIdentifier:tabItemView];
[self closeTab:tabViewItem];
}

-(void)closeTab:(NSTabViewItem *)tabViewItem
{
//remove closing tab from tab order
[self.tabViewOrder removeObject:tabViewItem];

if (self.tabView.selectedTabViewItem == tabViewItem)
{
if (self.selectPreviousOnClose) {
//open most recently opened tab
[self.tabView selectTabViewItem:self.tabViewOrder.lastObject];
}
else if (self.selectRightItemFirst
&& [self.tabView indexOfTabViewItem:tabViewItem] < self.tabView.numberOfTabViewItems - 1)
{
//since tab is not the last, select the one right of it
[self.tabView selectTabViewItemAtIndex:[self.tabView indexOfTabViewItem:tabViewItem] + 1];
}
else if (self.canJumpToArticles == false
&& [self.tabView indexOfTabViewItem:tabViewItem] == 1
&& self.tabView.numberOfTabViewItems > 2)
{
//open tab to the right instead of article tab
[self.tabView selectTabViewItemAtIndex:2];
}
}
NSTabViewItem *tabViewItem = [tabBarControl tabViewItemWithIdentifier:tabItemView];

[self tabView:tabBarControl.tabView willCloseTabViewItem:tabViewItem];
//close tab to be closed
[self.tabView removeTabViewItem:tabViewItem];
[tabBarControl removeTabViewItem:tabViewItem];
[self tabView:tabBarControl.tabView didCloseTabViewItem:tabViewItem];
}

/* countOfTabs
* Returns the total number of tabs.
*/
-(NSInteger)countOfTabs
{
return self.tabView.numberOfTabViewItems;
return tabBarControl.numberOfTabViewItems;
}

/* showTabItemView
* Makes the specified tab active if not already and post a notification.
*/
-(void)showTabItemView:(NSView *)theTabView
{
if ([self.tabView tabViewItemWithIdentifier:theTabView]) {
[self.tabView selectTabViewItemWithIdentifier:theTabView];
NSTabViewItem *tabViewItem = [tabBarControl tabViewItemWithIdentifier:theTabView];
if (tabViewItem) {
[tabBarControl selectTabViewItem:tabViewItem];
}
}

Expand All @@ -262,10 +240,10 @@ -(void)showTabItemView:(NSView *)theTabView
*/
-(void)showPreviousTab
{
if ([self.tabView indexOfTabViewItem:self.tabView.selectedTabViewItem] == 0)
[self.tabView selectLastTabViewItem:self];
if ([tabBarControl indexOfTabViewItem:tabBarControl.selectedTabViewItem] == 0)
[tabBarControl.tabView selectLastTabViewItem:self];
else
[self.tabView selectPreviousTabViewItem:self];
[tabBarControl.tabView selectPreviousTabViewItem:self];
}

/* showNextTab
Expand All @@ -274,10 +252,10 @@ -(void)showPreviousTab
*/
-(void)showNextTab
{
if ([self.tabView indexOfTabViewItem:self.tabView.selectedTabViewItem] == (self.tabView.numberOfTabViewItems - 1))
[self.tabView selectFirstTabViewItem:self];
if ([tabBarControl indexOfTabViewItem:tabBarControl.tabView.selectedTabViewItem] == (tabBarControl.tabView.numberOfTabViewItems - 1))
[tabBarControl.tabView selectFirstTabViewItem:self];
else
[self.tabView selectNextTabViewItem:self];
[tabBarControl.tabView selectNextTabViewItem:self];
}

#pragma mark - TabBarDelegate
Expand All @@ -290,9 +268,34 @@ -(void)showNextTab
*/
- (BOOL)tabView:(NSTabView *)aTabView shouldCloseTabViewItem:(NSTabViewItem *)tabViewItem
{
[self closeTab:tabViewItem];
//TODO: let tab bar do the work. return tabViewItem.identifier != primaryTabItemView;
return NO;
return tabViewItem.identifier != primaryTabItemView;
}

- (void)tabView:(NSTabView *)aTabView willCloseTabViewItem:(NSTabViewItem *)tabViewItem
{
//remove closing tab from tab order
[self.tabViewOrder removeObject:tabViewItem];

if (tabBarControl.selectedTabViewItem == tabViewItem)
{
if (self.selectPreviousOnClose) {
//open most recently opened tab
[tabBarControl.tabView selectTabViewItem:self.tabViewOrder.lastObject];
}
else if (self.selectRightItemFirst
&& [tabBarControl.tabView indexOfTabViewItem:tabViewItem] < tabBarControl.numberOfTabViewItems - 1)
{
//since tab is not the last, select the one right of it
[tabBarControl.tabView selectTabViewItemAtIndex:[tabBarControl.tabView indexOfTabViewItem:tabViewItem] + 1];
}
else if (self.canJumpToArticles == false
&& [tabBarControl.tabView indexOfTabViewItem:tabViewItem] == 1
&& tabBarControl.tabView.numberOfTabViewItems > 2)
{
//open tab to the right instead of article tab
[tabBarControl.tabView selectTabViewItemAtIndex:2];
}
}
}

// Closing behavior
Expand Down Expand Up @@ -340,8 +343,8 @@ -(void)tabView:(NSTabView *)inTabView didSelectTabViewItem:(NSTabViewItem *)inTa
[[NSNotificationCenter defaultCenter] postNotificationName:@"MA_Notify_TabChanged" object:inTabViewItem.identifier];
if (self.canJumpToArticles || inTabViewItem.identifier != primaryTabItemView)
{
[self.tabViewOrder removeObject:self.tabView.selectedTabViewItem];
[self.tabViewOrder addObject:self.tabView.selectedTabViewItem];
[self.tabViewOrder removeObject:tabBarControl.tabView.selectedTabViewItem];
[self.tabViewOrder addObject:tabBarControl.tabView.selectedTabViewItem];
}
}

Expand Down Expand Up @@ -395,7 +398,7 @@ -(void)saveOpenTabs
NSMutableArray *tabLinks = [NSMutableArray arrayWithCapacity:self.countOfTabs];
NSMutableDictionary *tabTitles = [NSMutableDictionary dictionaryWithCapacity:self.countOfTabs];

for (NSTabViewItem * tabViewItem in self.tabView.tabViewItems)
for (NSTabViewItem * tabViewItem in tabBarControl.tabView.tabViewItems)
{
NSView<BaseView> * theView = tabViewItem.identifier;
NSString * tabLink = theView.viewLink;
Expand Down Expand Up @@ -480,7 +483,7 @@ -(void)createNewTabWithView:(NSView<BaseView> *)newTabView makeKey:(BOOL)keyIt
NSTabViewItem *tabViewItem = [[NSTabViewItem alloc] initWithIdentifier:newTabView];
tabViewItem.view = newTabView;

[self.tabView addTabViewItem:tabViewItem];
[tabBarControl.tabView addTabViewItem:tabViewItem];

//newly created item will be selected first or last to be selected
if (self.selectNewItemFirst)
Expand Down

0 comments on commit 533173a

Please sign in to comment.