Skip to content

Commit 6e51390

Browse files
committed
refactor(datagrid): handle cell-range selection in right-click and simplify delete
1 parent e3461f6 commit 6e51390

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
### Fixed
2222

2323
- Delete key now respects cell-range selection in the data grid, removing all rows covered by the selection instead of ignoring it.
24-
- Right-clicking a row inside a multi-row selection no longer collapses the selection before the context menu appears.
24+
- Right-clicking inside a multi-row or cell-range selection no longer collapses the selection before the context menu appears.
2525
- Oracle connections no longer crash the app during connect. A short or unexpected handshake packet from the server (such as session-setup metadata or an error) now surfaces the error or continues instead of trapping. (#1683)
2626
- MongoDB filters on `_id` and other ObjectId fields now match. A 24-character hex value is matched as an ObjectId as well as a string, so filtering by `_id` returns the row instead of nothing. (#1682)
2727
- The sidebar and inspector keep their width per connection, the sidebar keeps its collapsed state, and the inspector keeps its selected tab, when you quit and reopen the app.

TablePro/Views/Results/KeyHandlingTableView.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,7 @@ final class KeyHandlingTableView: NSTableView {
223223
@objc func delete(_ sender: Any?) {
224224
guard coordinator?.isEditable == true else { return }
225225
if let controller = gridSelection, !controller.isEmpty {
226-
let rows = controller.selection.affectedRows
227-
guard !rows.isEmpty else { return }
228-
coordinator?.delegate?.dataGridDeleteRows(Set(rows))
226+
coordinator?.delegate?.dataGridDeleteRows(Set(controller.selection.affectedRows))
229227
return
230228
}
231229
guard !selectedRowIndexes.isEmpty else { return }
@@ -531,7 +529,7 @@ final class KeyHandlingTableView: NSTableView {
531529
override func rightMouseDown(with event: NSEvent) {
532530
let point = convert(event.locationInWindow, from: nil)
533531
let clickedRow = row(at: point)
534-
if clickedRow >= 0, selectedRowIndexes.contains(clickedRow) {
532+
if clickedRow >= 0, clickIsInsideSelection(row: clickedRow, point: point) {
535533
window?.makeFirstResponder(self)
536534
if let menu = menu(for: event) {
537535
NSMenu.popUpContextMenu(menu, with: event, for: self)
@@ -541,6 +539,18 @@ final class KeyHandlingTableView: NSTableView {
541539
super.rightMouseDown(with: event)
542540
}
543541

542+
private func clickIsInsideSelection(row clickedRow: Int, point: NSPoint) -> Bool {
543+
if selectedRowIndexes.contains(clickedRow) { return true }
544+
guard let controller = gridSelection, !controller.isEmpty else { return false }
545+
let clickedColumn = column(at: point)
546+
guard clickedColumn >= 0,
547+
let schema = coordinator?.identitySchema,
548+
let dataColumn = DataGridView.dataColumnIndex(for: clickedColumn, in: self, schema: schema) else {
549+
return false
550+
}
551+
return controller.selection.contains(row: clickedRow, column: dataColumn)
552+
}
553+
544554
override func menu(for event: NSEvent) -> NSMenu? {
545555
let point = convert(event.locationInWindow, from: nil)
546556
let clickedRow = row(at: point)

TableProTests/Views/Results/CellSelectionTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ struct GridSelectionTests {
6363
#expect(selection.boundingRectangle == rect)
6464
}
6565

66+
@Test("a cell-range spanning rows reports every covered row")
67+
func cellRangeAffectsEveryCoveredRow() {
68+
let selection = GridSelection.single(
69+
GridRect(rows: 2...5, columns: 1...3),
70+
anchor: GridCoord(row: 2, column: 1),
71+
active: GridCoord(row: 5, column: 3)
72+
)
73+
#expect(selection.affectedRows == IndexSet(integersIn: 2...5))
74+
}
75+
6676
@Test("multiple rectangles report union of affected rows and columns")
6777
func multipleRectanglesUnion() {
6878
let selection = GridSelection(

0 commit comments

Comments
 (0)