Skip to content

Commit

Permalink
Fix that status line doesn't show correctly in Xcode 6.4 and 7.
Browse files Browse the repository at this point in the history
This fix completely rewrite the status line implementation.
It uses autolayout and binding to show a status line at right position.
This may fix the issue #830 since now it uses color from the theme.
  • Loading branch information
JugglerShu committed Sep 20, 2015
1 parent 732f3c7 commit 125c207
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 173 deletions.
4 changes: 1 addition & 3 deletions XVim/IDEEditor+XVim.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@

@interface IDEEditor(XVim)
+ (void)xvim_initialize;
- (void)xvim_didSetupEditor;
- (void)xvim_primitiveInvalidate;
@end
@end
53 changes: 2 additions & 51 deletions XVim/IDEEditor+XVim.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,17 @@
#import "IDESourceEditor.h"
#import "Logger.h"
#import "XVim.h"
#import "XVimOptions.h"
#import "NSObject+ExtraData.h"
#import "XVimStatusLine.h"
#import <objc/runtime.h>
#import "NSObject+XVimAdditions.h"

#define DID_REGISTER_OBSERVER_KEY "net.JugglerShu.IDEEditorHook._didRegisterObserver"

@implementation IDEEditor(XVim)

+ (void)xvim_initialize{
[self xvim_swizzleInstanceMethod:@selector(didSetupEditor) with:@selector(xvim_didSetupEditor)];
[self xvim_swizzleInstanceMethod:@selector(primitiveInvalidate) with:@selector(xvim_primitiveInvalidate)];
}

- (void)xvim_didSetupEditor{

[self xvim_didSetupEditor];

// If you do not like status line comment out folloing.
// ---- FROM HERE ----
NSView* container = nil;
if( [NSStringFromClass([self class]) isEqualToString:@"IDESourceCodeComparisonEditor"] ){
container = [(IDESourceCodeComparisonEditor*)self layoutView];
}
else if( [NSStringFromClass([self class]) isEqualToString:@"IDESourceCodeEditor"] ){
container = [(IDESourceCodeEditor*)self containerView];
}else{
return;
}

if (container != nil) {
XVimStatusLine *status = [XVimStatusLine associateOf:container];
if (status == nil) {
// Insert status line
[container setPostsFrameChangedNotifications:YES];
status = [[XVimStatusLine alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
[container addSubview:status];
[status associateWith:container];
status.editor = self;

// Layout
[[NSNotificationCenter defaultCenter] addObserver:status selector:@selector(didContainerFrameChanged:) name:NSViewFrameDidChangeNotification object:container];
[status layoutStatus:container];
[container performSelector:@selector(invalidateLayout)];

// For % register and to notify contents of editor is changed
[self addObserver:[XVim instance] forKeyPath:@"document" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:(__bridge void*)self];
objc_setAssociatedObject(self, DID_REGISTER_OBSERVER_KEY, [NSNumber numberWithBool:YES], OBJC_ASSOCIATION_RETAIN);
}
}
//---- TO HERE ----
}

- (void)xvim_primitiveInvalidate {
IDEEditor *editor = (IDEEditor *)self;
NSNumber *didRegisterObserver = objc_getAssociatedObject(editor, DID_REGISTER_OBSERVER_KEY);
if ([didRegisterObserver boolValue]) {
[editor removeObserver:[XVim instance] forKeyPath:@"document"];
}

[self xvim_primitiveInvalidate];
}

@end
1 change: 1 addition & 0 deletions XVim/IDESourceCodeEditor+XVim.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
@interface IDESourceCodeEditor(XVim)
+ (void)xvim_initialize;
- (NSArray*) xvim_textView:(NSTextView *)textView willChangeSelectionFromCharacterRanges:(NSArray *)oldSelectedCharRanges toCharacterRanges:(NSArray *)newSelectedCharRanges;
- (id)xvim_initWithNibName:(NSString*)arg1 bundle:(NSBundle*)arg2 document:(IDEEditorDocument*)arg3;
@end
79 changes: 79 additions & 0 deletions XVim/IDESourceCodeEditor+XVim.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,98 @@

#import "IDESourceCodeEditor+XVim.h"
#import "IDEKit.h"
#import "DVTFoundation.h"
#import "XVimWindow.h"
#import "Logger.h"
#import "XVimStatusLine.h"
#import "XVim.h"
#import "NSObject+XVimAdditions.h"
#import "NSobject+ExtraData.h"

@implementation IDESourceCodeEditor(XVim)
+ (void)xvim_initialize{
[self xvim_swizzleInstanceMethod:@selector(textView:willChangeSelectionFromCharacterRanges:toCharacterRanges:) with:@selector(xvim_textView:willChangeSelectionFromCharacterRanges:toCharacterRanges:)];
[self xvim_swizzleInstanceMethod:@selector(initWithNibName:bundle:document:) with:@selector(xvim_initWithNibName:bundle:document:)];
}

- (NSArray*) xvim_textView:(NSTextView *)textView willChangeSelectionFromCharacterRanges:(NSArray *)oldSelectedCharRanges toCharacterRanges:(NSArray *)newSelectedCharRanges
{
return newSelectedCharRanges;
}

- (id)xvim_initWithNibName:(NSString*)name bundle:(NSBundle*)bundle document:(IDEEditorDocument*)doc{
id obj = [self xvim_initWithNibName:name bundle:bundle document:doc];
NSView* container = [[obj view] contentView];

// Insert status line
if( nil != container ){
// TODO: Observe DVTFontAndColorSourceTextSettingsChangedNotification to change color of status bar
DVTSourceTextScrollView* scrollView = [self mainScrollView];
[scrollView setTranslatesAutoresizingMaskIntoConstraints:NO]; // To use autolayout we need set this NO

// Add status view
XVimStatusLine* status = [[XVimStatusLine alloc] initWithString:doc.filePath.pathString];
[status setTranslatesAutoresizingMaskIntoConstraints:NO];
[container addSubview:status];

// Bind its visibility to 'laststatus'
XVimLaststatusTransformer* transformer = [[XVimLaststatusTransformer alloc] init];
[status bind:@"hidden" toObject:[[XVim instance] options] withKeyPath:@"laststatus" options:@{NSValueTransformerBindingOption:transformer}];


// View autolayout constraints (for the source view and status bar)

// Same width with the parent
[container addConstraint:[NSLayoutConstraint constraintWithItem:scrollView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:container
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];

// ScrollView's left position is 0
[container addConstraint:[NSLayoutConstraint constraintWithItem:scrollView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:container
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0.0]];
// Position scrollView above the status bar
[container addConstraint:[NSLayoutConstraint constraintWithItem:scrollView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:status
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0]];
// ScrollView fills to top of the container view
[container addConstraint:[NSLayoutConstraint constraintWithItem:scrollView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:container
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
// Place Status line at bottom edge
[container addConstraint:[NSLayoutConstraint constraintWithItem:status
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:container
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
// Status line width fills the container
[container addConstraint:[NSLayoutConstraint constraintWithItem:status
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:container
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0]];
}

return obj;
}
@end
8 changes: 6 additions & 2 deletions XVim/Logger.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "Logger.h"
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
#import "DVTKit.h"

#define LOGGER_DEFAULT_NAME @"LoggerDefaultName"

Expand Down Expand Up @@ -235,14 +236,17 @@ + (void) traceViewInfo:(NSView*)obj subView:(BOOL)sub{
[Logger traceViewInfoImpl:obj subView:sub prefix:@""];
}


+ (void)traceView:(NSView*)view depth:(NSUInteger)depth{
NSMutableString* str = [[NSMutableString alloc] init];
for( NSUInteger i = 0 ; i < depth; i++ ){
[str appendString:@" "];
}
[str appendString:@"%p:%@ (Tag:%d)"];
NSLog(str, view, NSStringFromClass([view class]),
[view tag]);
if( [view isKindOfClass:NSClassFromString(@"DVTControllerContentView")]){
[str appendFormat:@" <--- %@", [(DVTControllerContentView*)view viewController].description];
}
NSLog(str, view, NSStringFromClass([view class]), [view tag]);
for(NSView* v in [view subviews] ){
[self traceView:v depth:depth+1];
}
Expand Down
9 changes: 0 additions & 9 deletions XVim/XVim.m
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,6 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
}else{
[[Logger defaultLogger] setLogFile:nil];
}
} else if( [keyPath isEqualToString:@"document"] ){
NSString *documentPath = [[[object document] fileURL] path];
self.document = documentPath;
IDEEditor* editor = (__bridge IDEEditor*)context;

if (documentPath != nil) {
NSDictionary *userInfo = @{XVimDocumentPathKey:documentPath, XVimStatusLineIDEEditorKey:editor};
[[NSNotificationCenter defaultCenter] postNotificationName:XVimDocumentChangedNotification object:nil userInfo:userInfo];
}
}
}

Expand Down
13 changes: 4 additions & 9 deletions XVim/XVimStatusLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@

#import <Cocoa/Cocoa.h>

extern NSString* const XVimStatusLineIDEEditorKey;

@class IDEEditor;

@interface XVimStatusLine : NSView
@property (weak,nonatomic) IDEEditor* editor;
- (void)layoutStatus:(NSView*)container;
@interface XVimLaststatusTransformer : NSValueTransformer
@end

+ (XVimStatusLine*)associateOf:(id)object;
- (void)associateWith:(id)object;
@interface XVimStatusLine : NSTextField
- (id)initWithString:(NSString*)str;
@end
Loading

3 comments on commit 125c207

@bbudzon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @JugglerShu
This commit seems to have broken the branch.

/Volumes/git/XVim_temp/XVim/IDESourceCodeEditor+XVim.m:32:37: error: no visible @interface for 'NSView' declares the selector 'contentView'
    NSView* container = [[obj view] contentView];
                         ~~~~~~~~~~ ^~~~~~~~~~~
1 error generated.

My apologies for my github etiquette. Long time developer, new to github. If I understood the codebase better I'd submit a patch/pull request. I hope to learn the code base better as I love this plugin and would like to help where I can.

@bbudzon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still trying to figure out the intention here so that I can pull the HEAD of develop into my fork, test, and submit a few pull requests (currently testing everything on the revision before this). I know very little about how this code is working and hopefully we can open up some discussion to get this resolved.

@bbudzon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bug should be fixed with pull request #855

Please sign in to comment.