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
-4
View File
@@ -15,17 +15,13 @@ 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
} }
} }
}
extension Application { extension Application {
func activateSearchBar() { func activateSearchBar() {
@@ -19,6 +19,21 @@ struct DashboardView: View {
var body: some View { var body: some View {
#if os(macOS) #if os(macOS)
ZStack {
if let memo = application.memoObject {
NavigationStack {
MemoView(memo: memo)
.onDisappear {
withPersistence(\.viewContext) { context in
try context.saveIfNeeded()
context.refreshAllObjects()
}
}
.navigationTitle("")
}
.transition(.opacity)
.matchedGeometryEffect(id: "pop-up", in: namespace)
} else {
NavigationSplitView(columnVisibility: $columnVisibility) { NavigationSplitView(columnVisibility: $columnVisibility) {
Sidebar(sidebarItem: $sidebarItem, horizontalSizeClass: horizontalSizeClass) Sidebar(sidebarItem: $sidebarItem, horizontalSizeClass: horizontalSizeClass)
} detail: { } detail: {
@@ -31,20 +46,13 @@ struct DashboardView: View {
MemosView() 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 {
+10 -3
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]) .keyboardShortcut("z", modifiers: [.command])
.disabled(history.undoDisabled)
Button { Button {
history.historyPublisher.send(.redo)
} label: { } label: {
Text("Redo") Text("Redo")
} }
.keyboardShortcut("z", modifiers: [.command, .shift]) .keyboardShortcut("z", modifiers: [.command, .shift])
.disabled(history.redoDisabled)
}
} }
CommandGroup(replacing: .pasteboard) { } CommandGroup(replacing: .pasteboard) { }
} }