From 48dcf5a5efeb9efa5ca84a72919941df3347bf4b Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Thu, 28 Aug 2008 19:50:33 +0200 Subject: Listener: Keep the output view on the bottom by scrolling. --- MLKListenerController.m | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/MLKListenerController.m b/MLKListenerController.m index fb9fb5c..680365f 100644 --- a/MLKListenerController.m +++ b/MLKListenerController.m @@ -45,6 +45,9 @@ id object; NSDictionary *attrs; NSString *input = [inputField stringValue]; + float originalScrollPosition = [[[outputTextView enclosingScrollView] + verticalScroller] + floatValue]; [submitButton setEnabled:NO]; @@ -92,6 +95,12 @@ [statusText setStringValue:@"Compiling and executing."]; + if (originalScrollPosition == 1.0) + { + NSRange range = NSMakeRange ([text length], 0); + [outputTextView scrollRangeToVisible:range]; + } + [NSThread detachNewThreadSelector:@selector(evalObject:) toTarget:self withObject:nullify(object)]; @@ -104,6 +113,9 @@ NSMutableAttributedString *text = [outputTextView textStorage]; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; BOOL waitp = NO; + float originalScrollPosition = [[[outputTextView enclosingScrollView] + verticalScroller] + floatValue]; object = denullify(object); @@ -196,6 +208,13 @@ withObject:self waitUntilDone:NO]; + if (originalScrollPosition == 1.0) + { + [self performSelectorOnMainThread:@selector(scrollDown:) + withObject:self + waitUntilDone:NO]; + } + [pool release]; } @@ -211,6 +230,10 @@ - (void)writeString:(NSString *)string { + float originalScrollPosition = [[[outputTextView enclosingScrollView] + verticalScroller] + floatValue]; + NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys: [NSColor brownColor], NSForegroundColorAttributeName, @@ -222,5 +245,20 @@ performSelectorOnMainThread:@selector(appendAttributedString:) withObject:output waitUntilDone:YES]; + + if (originalScrollPosition == 1.0) + { + [self performSelectorOnMainThread:@selector(scrollDown:) + withObject:self + waitUntilDone:NO]; + } +} + +- (void)scrollDown:(id)sender +{ + // FIXME: This is slow. Investigate NSClipView#-scrollToPoint as a + // possible alternative. + NSRange range = NSMakeRange ([[outputTextView textStorage] length], 0); + [outputTextView scrollRangeToVisible:range]; } @end -- cgit v1.2.3