From b28533141dc492883ec7617f465c8eb0e4b34a65 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Tue, 19 May 2026 19:37:17 +0700 Subject: [PATCH] refactor(datagrid): make Cmd+C and Cmd+Shift+C robust when focus left the grid --- TablePro/TableProApp.swift | 10 ++++++++-- TablePro/Views/Main/Child/MainEditorContentView.swift | 3 ++- TablePro/Views/Structure/CreateTableView.swift | 10 +++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/TablePro/TableProApp.swift b/TablePro/TableProApp.swift index 9e1d43350..d4be35c93 100644 --- a/TablePro/TableProApp.swift +++ b/TablePro/TableProApp.swift @@ -43,8 +43,12 @@ struct PasteboardCommands: Commands { hasTableSelection: actions?.hasTableSelection ?? false ) switch action { - case .textCopy, .copyRows: + case .textCopy: NSApp.sendAction(#selector(NSText.copy(_:)), to: nil, from: nil) + case .copyRows: + if !NSApp.sendAction(#selector(NSText.copy(_:)), to: nil, from: nil) { + actions?.copySelectedRows() + } case .copyTableNames: actions?.copyTableNames() } @@ -52,7 +56,9 @@ struct PasteboardCommands: Commands { .optionalKeyboardShortcut(shortcut(for: .copy)) Button("Copy Rows") { - NSApp.sendAction(#selector(KeyHandlingTableView.copyRowsAsTSV(_:)), to: nil, from: nil) + if !NSApp.sendAction(#selector(KeyHandlingTableView.copyRowsAsTSV(_:)), to: nil, from: nil) { + actions?.copySelectedRows() + } } .optionalKeyboardShortcut(shortcut(for: .copyRowsExplicit)) .disabled(!(actions?.hasRowSelection ?? false)) diff --git a/TablePro/Views/Main/Child/MainEditorContentView.swift b/TablePro/Views/Main/Child/MainEditorContentView.swift index 7933bcaf1..9b79be674 100644 --- a/TablePro/Views/Main/Child/MainEditorContentView.swift +++ b/TablePro/Views/Main/Child/MainEditorContentView.swift @@ -209,7 +209,8 @@ struct MainEditorContentView: View { case .createTable: CreateTableView( connection: connection, - coordinator: coordinator + coordinator: coordinator, + selectionState: selectionState ) case .erDiagram: erDiagramContent(tab: tab) diff --git a/TablePro/Views/Structure/CreateTableView.swift b/TablePro/Views/Structure/CreateTableView.swift index 02ca4d7c9..e9adbffd3 100644 --- a/TablePro/Views/Structure/CreateTableView.swift +++ b/TablePro/Views/Structure/CreateTableView.swift @@ -33,6 +33,7 @@ struct CreateTableView: View { let connection: DatabaseConnection var coordinator: MainContentCoordinator? + let selectionState: GridSelectionState @State private var structureChangeManager: StructureChangeManager @State private var wrappedChangeManager: AnyChangeManager @@ -50,9 +51,14 @@ struct CreateTableView: View { @State private var sortState = SortState() @State private var columnLayout = ColumnLayoutState() - init(connection: DatabaseConnection, coordinator: MainContentCoordinator?) { + init( + connection: DatabaseConnection, + coordinator: MainContentCoordinator?, + selectionState: GridSelectionState + ) { self.connection = connection self.coordinator = coordinator + self.selectionState = selectionState let manager = StructureChangeManager() _structureChangeManager = State(wrappedValue: manager) @@ -80,6 +86,8 @@ struct CreateTableView: View { structureChangeManager.addNewColumn() } } + .onDisappear { selectionState.indices = [] } + .onChange(of: selectedRows) { _, newRows in selectionState.indices = newRows } .onChange(of: selectedTab) { updateGridDelegate() } .alert(String(localized: "Create Table Failed"), isPresented: $showError) { Button("OK") {}