[PyCharm] PY-79133 minor refactoring in JupyterEditorGutterMouseListenerTest and DecoratedEditor

GitOrigin-RevId: c003c29dffa4f0c93d08d771efd7455fed91d2b5
This commit is contained in:
bogdan.kirilenko
2026-01-15 21:58:01 +00:00
committed by intellij-monorepo-bot
parent 4e43029a13
commit 65db41529c
3 changed files with 26 additions and 40 deletions
@@ -1,31 +0,0 @@
// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.notebooks.visualization.ui
import com.intellij.openapi.client.ClientSystemInfo
import java.awt.event.InputEvent
import java.awt.event.MouseEvent
data class ClickModifiers(
val isCtrlPressed: Boolean = false,
val isShiftPressed: Boolean = false,
val isAltPressed: Boolean = false,
val mouseButton: Int = MouseEvent.BUTTON1
) {
companion object {
fun fromEvent(e: MouseEvent) = ClickModifiers(
isCtrlPressed = e.isCtrlPressed(),
isShiftPressed = e.isShiftPressed(),
isAltPressed = e.isAltPressed(),
mouseButton = e.button
)
}
}
private fun MouseEvent.isCtrlPressed(): Boolean =
(modifiersEx and if (ClientSystemInfo.isMac()) InputEvent.META_DOWN_MASK else InputEvent.CTRL_DOWN_MASK) != 0
private fun MouseEvent.isShiftPressed(): Boolean =
(modifiersEx and InputEvent.SHIFT_DOWN_MASK) != 0
private fun MouseEvent.isAltPressed(): Boolean =
(modifiersEx and InputEvent.ALT_DOWN_MASK) != 0
@@ -67,7 +67,13 @@ class DecoratedEditor private constructor(
editorImpl.setMode(NotebookEditorMode.COMMAND)
}
updateSelectionAfterClick(hoveredCell.interval, ClickModifiers.fromEvent(event))
updateSelectionAfterClick(
hoveredCell.interval,
event.button,
event.isControlDown,
event.isShiftDown,
event.isAltDown
)
}
private fun sendMouseEventToNestedScroll(event: MouseEvent) {
@@ -82,18 +88,28 @@ class DecoratedEditor private constructor(
editorComponentWrapper.addEditorMouseWheelEvent { nestedScrollingSupport.processMouseWheelEvent(it) }
}
override fun inlayClicked(clickedCell: NotebookCellLines.Interval, clickModifiers: ClickModifiers) {
override fun inlayClicked(clickedCell: NotebookCellLines.Interval, event: MouseEvent) {
editorImpl.setMode(NotebookEditorMode.COMMAND)
updateSelectionAfterClick(clickedCell, clickModifiers)
updateSelectionAfterClick(
clickedCell,
event.button,
event.isControlDown,
event.isShiftDown,
event.isAltDown
)
}
fun updateSelectionAfterClick(
clickedCell: NotebookCellLines.Interval,
clickModifiers: ClickModifiers
mouseButton: Int,
isCtrlPressed: Boolean,
isShiftPressed: Boolean,
isAltPressed: Boolean,
) {
val model = editorImpl.cellSelectionModel!!
when {
clickModifiers.isCtrlPressed -> {
isCtrlPressed -> {
if (model.isSelectedCell(clickedCell)) {
model.removeSelection(clickedCell)
}
@@ -101,7 +117,7 @@ class DecoratedEditor private constructor(
model.selectCell(clickedCell, makePrimary = true)
}
}
clickModifiers.isShiftPressed -> {
isShiftPressed -> {
// select or deselect all cells from primary to the selected one
val primaryCell = model.primarySelectedCell
val line1 = primaryCell.lines.first
@@ -125,10 +141,10 @@ class DecoratedEditor private constructor(
}
}
}
clickModifiers.isAltPressed -> {
isAltPressed -> {
// don't change selection in this case
}
clickModifiers.mouseButton == MouseEvent.BUTTON1 && !model.isSelectedCell(clickedCell) -> {
mouseButton == MouseEvent.BUTTON1 && !model.isSelectedCell(clickedCell) -> {
model.selectSingleCell(clickedCell)
}
}
@@ -4,9 +4,10 @@ import com.intellij.notebooks.visualization.NotebookCellLines
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.observable.properties.AtomicProperty
import com.intellij.openapi.util.Key
import java.awt.event.MouseEvent
interface NotebookEditor {
fun inlayClicked(clickedCell: NotebookCellLines.Interval, clickModifiers: ClickModifiers)
fun inlayClicked(clickedCell: NotebookCellLines.Interval, event: MouseEvent)
val editorPositionKeeper: NotebookPositionKeeper
/** Updated by JupyterAboveCellToolbarManager. When set, we are hiding the cell action toolbar in the top right corner. */