Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scroll wheel sends escape sequences in alternate screen mode #282

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 27 additions & 15 deletions sources/PTYTextView.m
Expand Up @@ -1546,7 +1546,7 @@ - (void)rightMouseDragged:(NSEvent *)event
[super rightMouseDragged:event];
}

- (BOOL)scrollWheelShouldSendArrowForEvent:(NSEvent *)event at:(NSPoint)point {
- (BOOL)scrollWheelShouldSendAuxForEvent:(NSEvent *)event at:(NSPoint)point upData:(NSData**)upData downData:(NSData**)downData {
Copy link
Owner

Choose a reason for hiding this comment

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

Let's separate computing the data to send from determining if data should be sent. That means two methods:

- (BOOL)scrollWheelShouldSendDataForEvent:(NSEvent *)event

(by not taking a point we're doing a touch of cleanup here since the point can be computed from the event and the caller doesn't need it for anything but passing to this method)

and

- (NSData *)dataToSendForScrollEvent:(NSEvent *)event

which calls enclosingScrollView and accumulateVerticalScrollFromEvent, and then returns the appropriate data.

NSRect liveRect = [self liveRect];
if (!NSPointInRect(point, liveRect)) {
return NO;
Expand All @@ -1560,40 +1560,52 @@ - (BOOL)scrollWheelShouldSendArrowForEvent:(NSEvent *)event at:(NSPoint)point {
return NO;
}
BOOL alternateMouseScroll = [iTermAdvancedSettingsModel alternateMouseScroll];
NSString* alternateMouseScrollEscapeUp = [iTermAdvancedSettingsModel alternateMouseScrollEscapeUp];
Copy link
Owner

Choose a reason for hiding this comment

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

(style nitpick) Put the space before the *

NSString* alternateMouseScrollEscapeDown = [iTermAdvancedSettingsModel alternateMouseScrollEscapeDown];
BOOL showingAlternateScreen = [self.dataSource showingAlternateScreen];
if (!alternateMouseScroll && showingAlternateScreen) {
[_altScreenMouseScrollInferer scrollWheel:event];
}
if (!alternateMouseScroll) {
if (!showingAlternateScreen) {
return NO;
}
if (!showingAlternateScreen) {

if (alternateMouseScroll) {
*upData = [_dataSource.terminal.output keyArrowUp:event.modifierFlags];
*downData = [_dataSource.terminal.output keyArrowDown:event.modifierFlags];
return YES;
} else if (alternateMouseScrollEscapeUp.length && alternateMouseScrollEscapeDown.length) {
Copy link
Owner

Choose a reason for hiding this comment

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

Seems like this could cause confusion if a user intentionally sets only one to experiment with it.


NSString* e_ups = [NSString stringWithFormat:@"\e%@", alternateMouseScrollEscapeUp ];
Copy link
Owner

Choose a reason for hiding this comment

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

This should probably send multiple repeats of the code when you scroll fast. Also, don't prepend \e. Instead, use -[NSString stringByExpandingVimSpecialCharacters], which converts a \e into a esc.

NSString* e_dps = [NSString stringWithFormat:@"\e%@", alternateMouseScrollEscapeDown ];

*upData = [ e_ups dataUsingEncoding:_dataSource.terminal.encoding ];
*downData = [ e_dps dataUsingEncoding:_dataSource.terminal.encoding ];
Copy link
Owner

Choose a reason for hiding this comment

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

Use _delegate.textViewEncoding instead of reaching through to the terminal.

return YES;
} else {
[_altScreenMouseScrollInferer scrollWheel:event];
return NO;
}
return YES;
}


- (void)scrollWheel:(NSEvent *)event {
DLog(@"scrollWheel:%@", event);

NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];

if ([self scrollWheelShouldSendArrowForEvent:event at:point]) {
NSData* upData = nil;
NSData* downData = nil;
if ([self scrollWheelShouldSendAuxForEvent:event at:point upData:&upData downData:&downData]) {
DLog(@"Scroll wheel sending arrow key");

PTYScrollView *scrollView = (PTYScrollView *)self.enclosingScrollView;
CGFloat deltaY = [scrollView accumulateVerticalScrollFromEvent:event];

NSData *arrowKeyData = nil;
NSData* payload = nil;
if (deltaY > 0) {
arrowKeyData = [_dataSource.terminal.output keyArrowUp:event.modifierFlags];
payload = upData;
} else if (deltaY < 0) {
arrowKeyData = [_dataSource.terminal.output keyArrowDown:event.modifierFlags];
payload = downData;
}
if (arrowKeyData) {
if (payload) {
for (int i = 0; i < ceil(fabs(deltaY)); i++) {
[_delegate writeTask:arrowKeyData];
[_delegate writeTask:payload];
}
}
} else if (![self reportMouseEvent:event]) {
Expand Down
2 changes: 2 additions & 0 deletions sources/iTermAdvancedSettingsModel.h
Expand Up @@ -15,6 +15,8 @@
+ (int)minCompactTabWidth;
+ (int)optimumTabWidth;
+ (BOOL)alternateMouseScroll;
+ (NSString *)alternateMouseScrollEscapeUp;
+ (NSString *)alternateMouseScrollEscapeDown;
+ (BOOL)traditionalVisualBell;
+ (double)hotkeyTermAnimationDuration;
+ (BOOL)hotkeyWindowFloatsAboveOtherWindows;
Expand Down
2 changes: 2 additions & 0 deletions sources/iTermAdvancedSettingsModel.m
Expand Up @@ -64,6 +64,8 @@ + (NSString *)name { \
DEFINE_BOOL(allowDragOfTabIntoNewWindow, YES, @"Tabs: Allow a tab to be dragged and dropped outside any existing tab bar to create a new window.");

#pragma mark Mouse
DEFINE_STRING(alternateMouseScrollEscapeUp, @"", @"Mouse: Scroll wheel sends the specified escape sequence in alternate screen mode (up)");
Copy link
Owner

Choose a reason for hiding this comment

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

Add a newline and explain that the string should use vim syntax, such as \e for escape.

DEFINE_STRING(alternateMouseScrollEscapeDown, @"", @"Mouse: Scroll wheel sends the specified escape sequence in alternate screen mode (down)");
DEFINE_BOOL(alternateMouseScroll, NO, @"Mouse: Scroll wheel sends arrow keys when in alternate screen mode.");
DEFINE_BOOL(pinchToChangeFontSizeDisabled, NO, @"Mouse: Disable changing font size in response to a pinch gesture.");
DEFINE_BOOL(useSystemCursorWhenPossible, NO, @"Mouse: Use system cursor icons when possible.");
Expand Down