Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/cli/src/ui/contexts/MouseContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ export function MouseProvider({
!handled &&
event.name === 'move' &&
event.col >= 0 &&
event.row >= 0
event.row >= 0 &&
event.button === 'left'
) {
// Terminal apps only receive mouse move events when the mouse is down
// so this always indicates a mouse drag that the user was expecting
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/src/ui/contexts/ScrollProvider.drag.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ describe('ScrollProvider Drag', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand All @@ -125,6 +126,7 @@ describe('ScrollProvider Drag', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand All @@ -142,6 +144,7 @@ describe('ScrollProvider Drag', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand All @@ -159,6 +162,7 @@ describe('ScrollProvider Drag', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand All @@ -172,6 +176,7 @@ describe('ScrollProvider Drag', () => {
shift: false,
ctrl: false,
meta: false,
button: 'none',
});
}
expect(scrollBy).not.toHaveBeenCalled();
Expand Down Expand Up @@ -209,6 +214,7 @@ describe('ScrollProvider Drag', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand All @@ -231,6 +237,7 @@ describe('ScrollProvider Drag', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand Down Expand Up @@ -268,6 +275,7 @@ describe('ScrollProvider Drag', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand Down Expand Up @@ -305,6 +313,7 @@ describe('ScrollProvider Drag', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand Down Expand Up @@ -342,6 +351,7 @@ describe('ScrollProvider Drag', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand Down Expand Up @@ -407,6 +417,7 @@ describe('ScrollProvider Drag', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand All @@ -424,6 +435,7 @@ describe('ScrollProvider Drag', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}
// Expect scrollTo to be called with target and duration 0
Expand Down
17 changes: 17 additions & 0 deletions packages/cli/src/ui/contexts/ScrollProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'none',
}) === true
) {
handled = true;
Expand Down Expand Up @@ -147,6 +148,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'none',
}) === true
) {
handled = true;
Expand Down Expand Up @@ -193,6 +195,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand Down Expand Up @@ -226,6 +229,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand Down Expand Up @@ -258,6 +262,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'none',
};
for (const callback of mockUseMouseCallbacks) {
callback(mouseEvent);
Expand Down Expand Up @@ -303,6 +308,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'none',
});
callback({
name: 'scroll-down',
Expand All @@ -311,6 +317,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'none',
});
callback({
name: 'scroll-up',
Expand All @@ -319,6 +326,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'none',
});
}

Expand Down Expand Up @@ -358,6 +366,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'none',
});
callback({
name: 'scroll-down',
Expand All @@ -366,6 +375,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'none',
});
callback({
name: 'scroll-down',
Expand All @@ -374,6 +384,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'none',
});
}

Expand Down Expand Up @@ -417,6 +428,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand All @@ -429,6 +441,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand All @@ -441,6 +454,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand Down Expand Up @@ -475,6 +489,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand All @@ -487,6 +502,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand All @@ -498,6 +514,7 @@ describe('ScrollProvider', () => {
shift: false,
ctrl: false,
meta: false,
button: 'left',
});
}

Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/ui/utils/mouse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('mouse utils', () => {
shift: false,
meta: false,
ctrl: false,
button: 'left',
});
expect(result!.length).toBe(input.length);
});
Expand All @@ -43,6 +44,7 @@ describe('mouse utils', () => {
shift: false,
meta: false,
ctrl: false,
button: 'left',
});
});

Expand All @@ -58,6 +60,7 @@ describe('mouse utils', () => {
shift: true,
meta: true,
ctrl: true,
button: 'left',
});
});

Expand All @@ -67,6 +70,7 @@ describe('mouse utils', () => {
const result = parseSGRMouseEvent(input);
expect(result).not.toBeNull();
expect(result!.event.name).toBe('move');
expect(result!.event.button).toBe('left');
});

it('parses SGR scroll events', () => {
Expand Down Expand Up @@ -100,6 +104,7 @@ describe('mouse utils', () => {
shift: false,
meta: false,
ctrl: false,
button: 'left',
});
expect(result!.length).toBe(6);
});
Expand Down
31 changes: 30 additions & 1 deletion packages/cli/src/ui/utils/mouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface MouseEvent {
shift: boolean;
meta: boolean;
ctrl: boolean;
button: 'left' | 'middle' | 'right' | 'none';
}

export type MouseHandler = (event: MouseEvent) => void | boolean;
Expand Down Expand Up @@ -71,6 +72,20 @@ export function getMouseEventName(
}
}

function getButtonFromCode(code: number): MouseEvent['button'] {
const button = code & 3;
switch (button) {
case 0:
return 'left';
case 1:
return 'middle';
case 2:
return 'right';
default:
return 'none';
}
}

export function parseSGRMouseEvent(
buffer: string,
): { event: MouseEvent; length: number } | null {
Expand Down Expand Up @@ -98,6 +113,7 @@ export function parseSGRMouseEvent(
shift,
col,
row,
button: getButtonFromCode(buttonCode),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

For scroll events, the button property is being incorrectly assigned based on the event code. For example, a scroll-up event (code 64) will result in button: 'left' because 64 & 3 is 0. Scroll events should have button: 'none'. You should also update the parses SGR scroll events test in packages/cli/src/ui/utils/mouse.test.ts to assert that event.button is 'none'.

Suggested change
button: getButtonFromCode(buttonCode),
button: name.startsWith('scroll-') ? 'none' : getButtonFromCode(buttonCode),

},
length: match[0].length,
};
Expand Down Expand Up @@ -165,8 +181,21 @@ export function parseX11MouseEvent(
}

if (name) {
let button = getButtonFromCode(b);
if (name === 'left-release' && button === 'none') {
button = 'left';
}
Comment on lines +184 to +187

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Similar to the SGR parsing, scroll events in X11 mode are being assigned a button incorrectly. For example, a scroll-up event will be assigned button: 'left'. Scroll events should have button: 'none'. You should also consider adding a test for X11 scroll events in packages/cli/src/ui/utils/mouse.test.ts and assert that event.button is 'none'.

    let button: MouseEvent['button'];
    if (name.startsWith('scroll-')) {
      button = 'none';
    } else {
      button = getButtonFromCode(b);
      if (name === 'left-release' && button === 'none') {
        button = 'left';
      }
    }


return {
event: { name, ctrl, meta, shift, col, row },
event: {
name,
ctrl,
meta,
shift,
col,
row,
button,
},
length: match[0].length,
};
}
Expand Down