Skip to content

Commit

Permalink
Delete empty tasks on delete or backspace pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
diegotid committed Mar 23, 2024
1 parent c3457bd commit 7170075
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Tildone/Views/Common/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ enum Keyboard {
static let tabKey: Int = 48
static let arrowUp: Int = 126
static let arrowDown: Int = 125
static let delete: Int = 117
static let backSpace: Int = 51
}

enum Timeout {
Expand Down
37 changes: 30 additions & 7 deletions Tildone/Views/Note.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ private extension Note {
} else if event.keyCode == Keyboard.arrowDown {
handleMoveDown()
return nil
} else if event.keyCode == Keyboard.delete {
handleDelete()
return nil
} else if event.keyCode == Keyboard.backSpace {
handleDelete(isBackwards: true)
return nil
} else if event.modifierFlags.contains(.command),
event.charactersIgnoringModifiers == "w" {
NotificationCenter.default.post(name: .close, object: nil)
Expand Down Expand Up @@ -292,7 +298,7 @@ private extension Note {
}

func handleMoveUp() {
guard let focusIndex = sortedPendingTasks.map({ $0.created }).firstIndex(of: focusedTaskCreation) else {
guard let index = focusedIndex() else {
if focusedField == .newTask {
guard let list = self.list,
!list.items.isEmpty else {
Expand All @@ -305,15 +311,15 @@ private extension Note {
}
return
}
if focusIndex > 0 {
focusedTaskCreation = sortedPendingTasks[focusIndex - 1].created
if index > 0 {
focusedTaskCreation = sortedPendingTasks[index - 1].created
} else {
focusOnTopic()
}
}

func handleMoveDown() {
guard let focusIndex = sortedPendingTasks.map({ $0.created }).firstIndex(of: focusedTaskCreation) else {
guard let index = focusedIndex() else {
if focusedField == .topic {
guard let list = self.list,
!list.items.isEmpty else {
Expand All @@ -326,13 +332,27 @@ private extension Note {
}
return
}
if focusIndex < sortedPendingTasks.count - 1 {
focusedTaskCreation = sortedPendingTasks[focusIndex + 1].created
if index < sortedPendingTasks.count - 1 {
focusedTaskCreation = sortedPendingTasks[index + 1].created
} else {
focusOnNewTask()
}
}

func handleDelete(isBackwards: Bool = false) {
guard let index = focusedIndex(),
index > 0,
index < sortedPendingTasks.count - 1 else {
return
}
let task = sortedPendingTasks[index]
if task.what.isEmpty {
delete(task)
let newIndex = index - (isBackwards ? 1 : 0)
focusedTaskCreation = sortedPendingTasks[newIndex].created
}
}

func handleDisappearance() {
guard let list = self.list else {
return
Expand All @@ -355,7 +375,6 @@ private extension Note {
do {
try modelContext.save()
updateWindowClosability()
focusOnNewTask()
} catch {
fatalError("Error on task deletion: \(error)")
}
Expand All @@ -381,6 +400,10 @@ private extension Note {
self.focusedField = .newTask
}

func focusedIndex() -> Int? {
sortedPendingTasks.map({ $0.created }).firstIndex(of: focusedTaskCreation)
}

func placeCursor(forText value: String) {
placeCursor(forText: value, at: 0)
}
Expand Down

0 comments on commit 7170075

Please sign in to comment.