Skip to content

Commit

Permalink
✨ Make tab and shift+tab move to the next or previous row
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkulman committed Dec 26, 2023
1 parent d3f1a0d commit 5e3e0bf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sources/LocalizationEditor/UI/Cells/LocalizationCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Cocoa

protocol LocalizationCellDelegate: AnyObject {
func controlTextDidEndEditing(_ obj: Notification)
func userDidUpdateLocalizationString(language: String, key: String, with value: String, message: String?)
}

Expand Down Expand Up @@ -58,7 +59,8 @@ final class LocalizationCell: NSTableCellView {
// MARK: - Delegate

extension LocalizationCell: NSTextFieldDelegate {
func controlTextDidEndEditing(_: Notification) {
func controlTextDidEndEditing(_ obj: Notification) {
delegate?.controlTextDidEndEditing(obj)
guard let language = language, let value = value else {
return
}
Expand Down
43 changes: 43 additions & 0 deletions sources/LocalizationEditor/UI/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,49 @@ extension ViewController: LocalizationCellDelegate {
func userDidUpdateLocalizationString(language: String, key: String, with value: String, message: String?) {
dataSource.updateLocalization(language: language, key: key, with: value, message: message)
}

func controlTextDidEndEditing(_ obj: Notification) {
guard let view = obj.object as? NSView, let textMovementInt = obj.userInfo?["NSTextMovement"] as? Int, let textMovement = NSTextMovement(rawValue: textMovementInt) else {
return
}

let columnIndex = tableView.column(for: view)
let rowIndex = tableView.row(for: view)

let newRowIndex: Int
let newColumnIndex: Int

switch textMovement {
case .tab:
if columnIndex + 1 >= tableView.numberOfColumns - 1 {
newRowIndex = rowIndex + 1
newColumnIndex = 1
} else {
newColumnIndex = columnIndex + 1
newRowIndex = rowIndex
}
if newRowIndex >= tableView.numberOfRows {
return
}
case .backtab:
if columnIndex - 1 <= 0 {
newRowIndex = rowIndex - 1
newColumnIndex = tableView.numberOfColumns - 2
} else {
newColumnIndex = columnIndex - 1
newRowIndex = rowIndex
}
if newRowIndex < 0 {
return
}
default:
return
}

DispatchQueue.main.async { [weak self] in
self?.tableView.editColumn(newColumnIndex, row: newRowIndex, with: nil, select: true)
}
}
}

// MARK: - ActionsCellDelegate
Expand Down

0 comments on commit 5e3e0bf

Please sign in to comment.