feat: add redo and undo commands

This commit is contained in:
dscyrescotti
2024-07-14 18:16:13 +07:00
parent c1e2fa227e
commit bd5701cf88
4 changed files with 53 additions and 40 deletions
+2 -6
View File
@@ -15,15 +15,11 @@ final class Application: NSObject, ObservableObject {
extension Application { extension Application {
func openMemo(_ memoObject: MemoObject?) { func openMemo(_ memoObject: MemoObject?) {
withAnimation(.easeOut) { self.memoObject = memoObject
self.memoObject = memoObject
}
} }
func closeMemo() { func closeMemo() {
withAnimation(.easeOut) { self.memoObject = nil
self.memoObject = nil
}
} }
} }
@@ -19,32 +19,40 @@ struct DashboardView: View {
var body: some View { var body: some View {
#if os(macOS) #if os(macOS)
NavigationSplitView(columnVisibility: $columnVisibility) { ZStack {
Sidebar(sidebarItem: $sidebarItem, horizontalSizeClass: horizontalSizeClass) if let memo = application.memoObject {
} detail: { NavigationStack {
switch sidebarItem { MemoView(memo: memo)
case .memos: .onDisappear {
MemosView() withPersistence(\.viewContext) { context in
case .trash: try context.saveIfNeeded()
TrashView(sidebarItem: $sidebarItem) context.refreshAllObjects()
default: }
MemosView() }
.navigationTitle("")
}
.transition(.opacity)
.matchedGeometryEffect(id: "pop-up", in: namespace)
} else {
NavigationSplitView(columnVisibility: $columnVisibility) {
Sidebar(sidebarItem: $sidebarItem, horizontalSizeClass: horizontalSizeClass)
} detail: {
switch sidebarItem {
case .memos:
MemosView()
case .trash:
TrashView(sidebarItem: $sidebarItem)
default:
MemosView()
}
}
.transition(.opacity)
.matchedGeometryEffect(id: "pop-up", in: namespace)
} }
} }
.animation(.easeIn, value: application.memoObject)
.toolbar(application.memoObject == nil ? .visible : .hidden, for: .windowToolbar) .toolbar(application.memoObject == nil ? .visible : .hidden, for: .windowToolbar)
.toolbarBackground(application.memoObject == nil ? .clear : Color(nsColor: .windowBackgroundColor), for: .windowToolbar) .toolbarBackground(application.memoObject == nil ? .clear : Color(nsColor: .windowBackgroundColor), for: .windowToolbar)
.overlay {
if let memo = application.memoObject {
MemoView(memo: memo)
.onDisappear {
withPersistence(\.viewContext) { context in
try context.saveIfNeeded()
context.refreshAllObjects()
}
}
.transition(.move(edge: .bottom))
}
}
.onChange(of: columnVisibility) { oldValue, newValue in .onChange(of: columnVisibility) { oldValue, newValue in
application.changeSidebarVisibility(newValue == .all ? .shown : .hidden) application.changeSidebarVisibility(newValue == .all ? .shown : .hidden)
} }
+2
View File
@@ -61,6 +61,8 @@ struct MemoView: View {
loadingIndicator("Loading photo...") loadingIndicator("Loading photo...")
} }
} }
.focusedSceneObject(history)
.focusedSceneValue(\.activeSceneKey, .memo)
} }
private var canvasView: some View { private var canvasView: some View {
+19 -12
View File
@@ -8,21 +8,28 @@
import SwiftUI import SwiftUI
struct EditCommands: Commands { struct EditCommands: Commands {
@FocusedValue(\.activeSceneKey) private var appScene
@FocusedObject var history: History?
var body: some Commands { var body: some Commands {
CommandGroup(replacing: .undoRedo) { CommandGroup(replacing: .undoRedo) {
// memo view if appScene == .memo, let history {
Button { Button {
history.historyPublisher.send(.undo)
} label: { } label: {
Text("Undo") Text("Undo")
}
.keyboardShortcut("z", modifiers: [.command])
.disabled(history.undoDisabled)
Button {
history.historyPublisher.send(.redo)
} label: {
Text("Redo")
}
.keyboardShortcut("z", modifiers: [.command, .shift])
.disabled(history.redoDisabled)
} }
.keyboardShortcut("z", modifiers: [.command])
Button {
} label: {
Text("Redo")
}
.keyboardShortcut("z", modifiers: [.command, .shift])
} }
CommandGroup(replacing: .pasteboard) { } CommandGroup(replacing: .pasteboard) { }
} }