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
52 changes: 32 additions & 20 deletions apple/TablePro/Core/Menu/QueryMenuBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,44 @@ enum QueryMenuBuilder {
shortcut: .saveAsFavorite,
keyboard: keyboard
),
MenuItemFactory.item(
String(localized: "Open Value Editor"),
action: #selector(MainSplitViewController.openValueEditor(_:)),
shortcut: .openValueEditor,
keyboard: keyboard
),
MenuItemFactory.item(
String(localized: "Preview FK Reference"),
action: #selector(MainSplitViewController.previewFKReference(_:)),
shortcut: .previewFKReference,
keyboard: keyboard
),
MenuItemFactory.separator
] + pageItems(keyboard: keyboard) + [
MenuItemFactory.separator,
MenuItemFactory.item(
String(localized: "Review with AI"),
action: #selector(MainSplitViewController.reviewQueryWithAI(_:)),
shortcut: .aiReviewQuery,
keyboard: keyboard
),
MenuItemFactory.item(
String(localized: "Explain with AI"),
action: #selector(MainSplitViewController.explainQueryWithAI(_:)),
shortcut: .aiExplainQuery,
keyboard: keyboard
),
MenuItemFactory.item(
String(localized: "Optimize with AI"),
action: #selector(MainSplitViewController.optimizeQueryWithAI(_:)),
shortcut: .aiOptimizeQuery,
keyboard: keyboard
)
])
}

private static func pageItems(keyboard: KeyboardSettings) -> [NSMenuItem] {
[
MenuItemFactory.item(
String(localized: "First Page"),
action: #selector(MainSplitViewController.goToFirstPage(_:)),
Expand All @@ -146,26 +177,7 @@ enum QueryMenuBuilder {
action: #selector(MainSplitViewController.goToLastPage(_:)),
shortcut: .lastPage,
keyboard: keyboard
),
MenuItemFactory.separator,
MenuItemFactory.item(
String(localized: "Review with AI"),
action: #selector(MainSplitViewController.reviewQueryWithAI(_:)),
shortcut: .aiReviewQuery,
keyboard: keyboard
),
MenuItemFactory.item(
String(localized: "Explain with AI"),
action: #selector(MainSplitViewController.explainQueryWithAI(_:)),
shortcut: .aiExplainQuery,
keyboard: keyboard
),
MenuItemFactory.item(
String(localized: "Optimize with AI"),
action: #selector(MainSplitViewController.optimizeQueryWithAI(_:)),
shortcut: .aiOptimizeQuery,
keyboard: keyboard
)
])
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ extension MainSplitViewController: NSMenuItemValidation {
#selector(showERDiagram(_:)):
return context.isConnected

// The focused data grid answers and validates this itself; reaching the window means no grid has focus.
case #selector(previewFKReference(_:)):
// The focused data grid answers and validates these itself; reaching the window means no grid has focus.
case #selector(previewFKReference(_:)), #selector(openValueEditor(_:)):
return false

// Agent mode hides the tab strip; on `isConnected` alone these switched tabs behind the conversation or did nothing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ extension MainSplitViewController {
// Declared for the menu item; the focused data grid implements the same selector closer to the first responder.
@objc func previewFKReference(_ sender: Any?) {}

@objc func openValueEditor(_ sender: Any?) {}

@objc func goToFirstPage(_ sender: Any?) {
commandActions?.goToFirstPage()
}
Expand Down
29 changes: 23 additions & 6 deletions apple/TablePro/Models/UI/KeyboardShortcutModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable {
case truncateTable
case toggleHeaderRow
case previewFKReference
case openValueEditor
case saveAsFavorite
case previousPage
case nextPage
Expand Down Expand Up @@ -214,7 +215,7 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable {
.paste, .delete, .selectAll, .clearSelection, .addRow, .duplicateRow,
.setValueEmpty, .setValueNull, .setValueDefault, .generateUUIDv4, .generateUUIDv7, .setCellsToValue,
.restorePreviousValues,
.truncateTable, .toggleHeaderRow, .previewFKReference, .saveAsFavorite, .previousPage,
.truncateTable, .toggleHeaderRow, .previewFKReference, .openValueEditor, .saveAsFavorite, .previousPage,
.nextPage, .firstPage, .lastPage, .refresh, .export, .exportResults, .importData, .jumpToColumn:
return .dataGrid
case .navigateBack, .navigateForward,
Expand Down Expand Up @@ -266,7 +267,7 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable {
return .editor
case .previousPage, .nextPage, .firstPage, .lastPage, .addRow, .duplicateRow,
.setValueEmpty, .setValueNull, .setValueDefault, .generateUUIDv4, .generateUUIDv7, .setCellsToValue,
.delete, .truncateTable, .previewFKReference, .saveAsFavorite,
.delete, .truncateTable, .previewFKReference, .openValueEditor, .saveAsFavorite,
.copyRowsExplicit, .copyWithHeaders, .copyAsJson, .toggleFilters, .jumpToColumn,
.copyCellValue, .copyAsCsv, .copyAsCsvWithHeaders, .copyAsMarkdown, .copyAsInClause,
.copyAsInsert, .copyAsUpdate,
Expand All @@ -284,6 +285,18 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable {
self == .switchToRecentTab || self == .switchToLeastRecentTab
}

/// The focused grid reads this action's key from `keyDown`, whatever its modifiers, so its menu
/// item never carries a key equivalent. Its default, Option-Return, breaks a line in a text
/// field, and AppKit hands an equivalent to the menu before the field sees the key.
var isReadByFocusedGrid: Bool {
self == .openValueEditor
}

// A key the focused grid reads loses to any menu key equivalent, whatever surface that item acts on.
func contends(with other: ShortcutAction) -> Bool {
isReadByFocusedGrid || other.isReadByFocusedGrid || context.overlaps(other.context)
}

var allowsBareKey: Bool {
switch self {
case .previewFKReference, .clearSelection, .delete:
Expand Down Expand Up @@ -378,6 +391,7 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable {
case .truncateTable: return String(localized: "Truncate Table")
case .toggleHeaderRow: return String(localized: "Switch First Row Between Header/Data")
case .previewFKReference: return String(localized: "Preview FK Reference")
case .openValueEditor: return String(localized: "Open Value Editor")
case .saveAsFavorite: return String(localized: "Save as Favorite")
case .toggleTableBrowser: return String(localized: "Toggle Table Browser")
case .showTablesList: return String(localized: "Show Tables")
Expand Down Expand Up @@ -570,7 +584,7 @@ struct KeyboardSettings: Codable, Equatable {
guard let other = ShortcutAction(rawValue: rawValue), other != action, !override.isCleared else {
continue
}
guard override == key, other.context.overlaps(action.context) else { continue }
guard override == key, other.contends(with: action) else { continue }
if let module = other.module, !AppModule.storedEnabledModules().contains(module) {
continue
}
Expand All @@ -596,7 +610,7 @@ struct KeyboardSettings: Codable, Equatable {
) -> ShortcutAction? {
guard !key.isCleared else { return nil }
for other in ShortcutAction.allCases where other != action && other.isAvailable(enabledModules: enabledModules) {
guard action.context.overlaps(other.context),
guard action.contends(with: other),
let otherKey = shortcut(for: other), !otherKey.isCleared,
otherKey == key else { continue }
return other
Expand Down Expand Up @@ -636,7 +650,8 @@ struct KeyboardSettings: Codable, Equatable {
/// bare (modifier-less) key. Bare keys dispatch through the responder chain in
/// the focused view, not through a global menu key-equivalent.
func keyboardShortcut(for action: ShortcutAction) -> KeyboardShortcut? {
guard let key = shortcut(for: action), !key.isCleared, key.hasModifier || key.isFunctionKey,
guard !action.isReadByFocusedGrid,
let key = shortcut(for: action), !key.isCleared, key.hasModifier || key.isFunctionKey,
let equivalent = key.swiftUIKeyEquivalent else {
return nil
}
Expand All @@ -648,7 +663,8 @@ struct KeyboardSettings: Codable, Equatable {
/// a cleared binding, an unrepresentable key, or a bare key, which reaches the
/// focused responder directly rather than through a global menu key-equivalent.
func menuKeyEquivalent(for action: ShortcutAction) -> (characters: String, modifiers: NSEvent.ModifierFlags)? {
guard let key = shortcut(for: action), !key.isCleared, key.hasModifier || key.isFunctionKey,
guard !action.isReadByFocusedGrid,
let key = shortcut(for: action), !key.isCleared, key.hasModifier || key.isFunctionKey,
let characters = key.menuKeyEquivalent else {
return nil
}
Expand Down Expand Up @@ -724,6 +740,7 @@ struct KeyboardSettings: Codable, Equatable {
.truncateTable: .special(.delete, option: true),
.toggleHeaderRow: .character("h", command: true, shift: true),
.previewFKReference: .special(.space),
.openValueEditor: .special(.return, option: true),
.saveAsFavorite: .character("d", command: true),
.previousPage: .character("[", command: true),
.nextPage: .character("]", command: true),
Expand Down
6 changes: 6 additions & 0 deletions apple/TablePro/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -131906,6 +131906,9 @@
},
"Open transaction in “%@”" : {

},
"Open Value Editor" : {

},
"Open Zed and click the Agent Panel icon in the right side of the title bar" : {
"localizations" : {
Expand Down Expand Up @@ -219897,6 +219900,9 @@
}
}
}
},
"View Value" : {

},
"View Vector…" : {

Expand Down
4 changes: 2 additions & 2 deletions apple/TablePro/Views/DataGrid/Commands/DataGridCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ struct DataGridCommands {
return model.commit(edits, actionName: actionName)
}

// What typing could edit: a read-only or server-owned column, a deleted row, a value not loaded and a binary value stay out.
// What typing could edit: a read-only or server-owned column, a deleted row, a value not loaded, a binary value and a checkbox stay out.
func typingCanEdit(_ cell: DataGridCommandCell) -> Bool {
guard configuration.isEditable, model.isColumnWritable(cell.column), !isRowDeleted(cell.rowID),
let row = storedRow(cell.rowID) else { return false }
let dataIndex = cell.column.dataIndex
guard dataIndex < row.values.count, model.rows.unloadedByteCount(row.id, column: dataIndex) == nil else {
return false
}
if cell.column.type?.isBlobType == true { return false }
if cell.column.type?.isBlobType == true || configuration.checkboxColumns.contains(dataIndex) { return false }
return row.values[dataIndex].asBytes == nil
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ extension DataGridViewController {

private func resultItems(context: DataGridMenuContext, cell: DataGridCommandCell?) -> [NSMenuItem] {
var items: [NSMenuItem] = []
if cell != nil, let focus = context.scope(in: model).focus, let editor = valueEditor(at: focus) {
items.append(menuItem(Self.valueEditorTitle(editor), #selector(openValueEditorFromMenu(_:)), context))
}
if gridCommands.offers(.showRowsAsJSON) {
items.append(menuItem(String(localized: "Show Row as JSON"), #selector(showRowsAsJSONFromMenu(_:)), context))
}
Expand Down Expand Up @@ -234,6 +237,7 @@ extension DataGridViewController {
private static let rowMenuActions: Set<Selector> = [
#selector(DataGridViewController.undoDeleteFromMenu(_:)),
#selector(DataGridViewController.showRowsAsJSONFromMenu(_:)),
#selector(DataGridViewController.openValueEditorFromMenu(_:)),
#selector(DataGridViewController.chooseForeignKeyRowFromMenu(_:)),
#selector(DataGridViewController.previewForeignKeyFromMenu(_:)),
#selector(DataGridViewController.openForeignKeyFromMenu(_:)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ extension DataGridViewController {
toggleForeignKeyPreview()
return true
}
return false
return handleValueEditorKey(event)
}

// MARK: - Checkboxes
Expand Down
93 changes: 93 additions & 0 deletions apple/TablePro/Views/DataGrid/Editing/DataGridValueEditor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// DataGridValueEditor.swift
// TablePro
//

import Foundation

enum DataGridChevronEditor: Equatable {
case dropdown
case typePicker
case boolean
case array
case vector
case set
case enumeration
case json
case blob
case datePicker
case inlineText
}

enum DataGridValueEditor: Equatable {
case chevron(DataGridChevronEditor)
case mode(CellInteractionMode)
case jsonViewer
case unloaded(edits: Bool)

var edits: Bool {
switch self {
case .chevron:
return true
case .jsonViewer:
return false
case .unloaded(let edits):
return edits
case .mode(let mode):
switch mode {
case .editInline, .editText, .editForeignKey, .editJson, .editBlob, .editSvg, .editVector:
return true
case .viewText, .viewJson, .viewBlob, .viewPhpSerialized, .viewBinaryValue, .viewSvg, .viewVector, .blocked:
return false
}
}
}

// nil where a cell opens nothing beyond its inline field. `chevron` is nil on a cell the grid cannot write.
static func resolve(
mode: CellInteractionMode,
chevron: DataGridChevronEditor?,
isJsonColumn: Bool,
isNull: Bool,
isUnloaded: Bool,
isWritable: Bool,
isCheckbox: Bool
) -> DataGridValueEditor? {
guard !isCheckbox, mode != .blocked else { return nil }
if isUnloaded { return .unloaded(edits: isWritable) }
let editor = loaded(mode: mode, chevron: chevron, isJsonColumn: isJsonColumn)
// A NULL has nothing to view; an editor is still how a value gets into it.
if isNull, editor?.edits == false { return nil }
return editor
}

private static func loaded(
mode: CellInteractionMode,
chevron: DataGridChevronEditor?,
isJsonColumn: Bool
) -> DataGridValueEditor? {
switch mode {
case .viewJson, .editJson, .viewBlob, .editBlob, .viewPhpSerialized, .viewBinaryValue,
.viewSvg, .editSvg, .viewVector, .editVector:
// Ahead of the chevron, which follows the column type and would drop a Display As choice.
return .mode(mode)
case .viewText, .editInline, .editText, .editForeignKey:
if let chevron, chevron != .inlineText { return .chevron(chevron) }
if isJsonColumn { return .jsonViewer }
return longText(mode)
case .blocked:
return nil
}
}

private static func longText(_ mode: CellInteractionMode) -> DataGridValueEditor? {
switch mode {
case .editText:
return .mode(mode)
case .viewText(let value):
return CellInteractionResolver.editsInPopover(value) ? .mode(mode) : nil
default:
return nil
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,22 @@ extension DataGridViewController {
guard model.configuration.isEditable,
!isRowDeleted(cell.row.id),
model.isColumnWritable(cell.column),
!model.configuration.checkboxColumns.contains(cell.column.dataIndex),
!cell.isUnloaded,
cell.column.type?.isBlobType != true,
cell.value.asBytes == nil else { return nil }
return cell.value.asText ?? ""
}

func cellContext(for cell: DataGridEditCell) -> CellContext {
func cellContext(for cell: DataGridEditCell, locksAsReadOnly: Bool = false) -> CellContext {
let name = cell.column.name
let rows = model.rows
let value = cell.value
// A column the owner locked is not read-only content: it refuses with a beep instead (#2726).
let serverOwned = model.configuration.traits.immutableColumns.contains(name)
|| model.changeState?.generatedColumns.contains(name) == true
let serverOwned = locksAsReadOnly
? !model.isColumnWritable(cell.column)
: model.configuration.traits.immutableColumns.contains(name)
|| model.changeState?.generatedColumns.contains(name) == true
return CellContext(
columnType: cell.column.type,
value: value.asText,
Expand Down Expand Up @@ -169,7 +172,12 @@ extension DataGridViewController {
guard let cell = editCell(at: coord) else { return }
let target = cell.target
guard loadsUnloadedValue(of: cell, then: { $0.openEditor(for: target) }) else { return }
let mode = CellInteractionResolver().resolve(cellContext(for: cell))
// A checkbox cell is its mark: a field over it would type text into a flag.
guard !model.configuration.checkboxColumns.contains(cell.column.dataIndex) else { return }
present(CellInteractionResolver().resolve(cellContext(for: cell)), for: cell)
}

func present(_ mode: CellInteractionMode, for cell: DataGridEditCell) {
switch mode {
case .blocked:
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ extension DataGridViewController {

// validateMenuItem asks this first; nil leaves the item to the other commands.
func validateEditingCommand(_ item: NSValidatedUserInterfaceItem) -> Bool? {
if item.action == #selector(openValueEditor(_:)) { return canOpenValueEditorForActiveCell }
guard item.action == #selector(previewFKReference(_:)) else { return nil }
if editingState.foreignKeyPreview?.isShown == true { return true }
guard let active = model.selection.activeCell else { return false }
Expand Down
Loading
Loading