Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextView scrolling behavior and selecting behavior

I have an NSTextView that I'm outputting text from NSTask. Everything works as expected except the scrolling and selecting behaviors.

1: If I try to scroll up, the position of my scroll snaps back to the bottom instantly after I let go. Any ideas? I've looked through quite a bit of documentation about this and can't find anything about it.

2: If I select text, it removes it. I just want it to select so I can copy and paste. Lost on this one too.

Any tips or pointers would be most welcome. Thanks.

- (id)init
{
    [super init];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(readPipe:)
                                                 name:NSFileHandleReadCompletionNotification 
                                               object:nil];

    return self;
}

- (void)kicked
{
    task = [[NSTask alloc] init];

    [task setLaunchPath:[self.kickLocationTextField stringValue]];
    [task setArguments:kickBuild];

    NSPipe *pipe = [[NSPipe alloc] init];
    fileHandle = [pipe fileHandleForReading];
    [fileHandle readInBackgroundAndNotify];

    [task setStandardOutput:pipe];
    [task setStandardError:pipe];

    [task launch];

    [task release];
    [pipe release];
}


- (void)readPipe:(NSNotification *)notification
{
    NSData *data;
    NSString *text;

    if( [notification object] != fileHandle )
    {
        return;
    }

    data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
    text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

    [nsTaskOutput insertText:text];

    [text release];
    if (data != 0)
    {
        [fileHandle readInBackgroundAndNotify];
    }
}
like image 931
crewshin Avatar asked Nov 28 '25 13:11

crewshin


1 Answers

Try this instead of insertText::

NSScroller *scroller = nTaskOutput.enclosingScrollView.verticalScroller;
BOOL shouldScrollToBottom = scroller.doubleValue == 1.0;
NSTextStorage *ts = nTaskOutput.textStorage;
[ts replaceCharactersInRange:NSMakeRange(ts.length, 0) withString:text];
if (shouldScrollToBottom) {
    NSRect bounds = nTaskOutput.bounds;
    [nTaskOutput scrollPoint:NSMakePoint(NSMaxX(bounds), NSMaxY(bounds))];
}
like image 196
rob mayoff Avatar answered Nov 30 '25 06:11

rob mayoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!