feat: add tool and grid commands

This commit is contained in:
dscyrescotti
2024-07-15 22:09:32 +07:00
parent bd5701cf88
commit 2e64ebc570
6 changed files with 68 additions and 24 deletions

View File

@@ -181,6 +181,19 @@ extension Canvas {
try context.saveIfNeeded() try context.saveIfNeeded()
} }
} }
func toggleGridMode() {
let _gridMode: GridMode
switch gridMode {
case .none:
_gridMode = .point
case .point:
_gridMode = .line
case .line:
_gridMode = .none
}
setGridMode(_gridMode)
}
} }
// MARK: - Stroke // MARK: - Stroke

View File

@@ -35,6 +35,7 @@ final class Tool: NSObject, ObservableObject {
} }
func selectTool(_ selection: ToolSelection) { func selectTool(_ selection: ToolSelection) {
guard self.selection != selection else { return }
self.selection = selection self.selection = selection
withPersistence(\.viewContext) { [weak object] context in withPersistence(\.viewContext) { [weak object] context in
object?.selection = selection.rawValue object?.selection = selection.rawValue

View File

@@ -61,6 +61,8 @@ struct MemoView: View {
loadingIndicator("Loading photo...") loadingIndicator("Loading photo...")
} }
} }
.focusedSceneObject(tool)
.focusedSceneObject(canvas)
.focusedSceneObject(history) .focusedSceneObject(history)
.focusedSceneValue(\.activeSceneKey, .memo) .focusedSceneValue(\.activeSceneKey, .memo)
} }

View File

@@ -142,14 +142,7 @@ struct Toolbar: View {
private var gridModeControl: some View { private var gridModeControl: some View {
#if os(macOS) #if os(macOS)
Button { Button {
switch canvas.gridMode { canvas.toggleGridMode()
case .none:
canvas.gridMode = .point
case .point:
canvas.gridMode = .line
case .line:
canvas.gridMode = .none
}
} label: { } label: {
Image(systemName: canvas.gridMode.icon) Image(systemName: canvas.gridMode.icon)
.frame(width: size, height: size) .frame(width: size, height: size)

View File

@@ -10,25 +10,49 @@ import SwiftUI
struct EditCommands: Commands { struct EditCommands: Commands {
@FocusedValue(\.activeSceneKey) private var appScene @FocusedValue(\.activeSceneKey) private var appScene
@FocusedObject var tool: Tool?
@FocusedObject var history: History? @FocusedObject var history: History?
var body: some Commands { var body: some Commands {
CommandGroup(replacing: .undoRedo) { CommandGroup(replacing: .undoRedo) {
if appScene == .memo, let history { if appScene == .memo {
Button { if let history {
history.historyPublisher.send(.undo) Button {
} label: { history.historyPublisher.send(.undo)
Text("Undo") } label: {
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]) Divider()
.disabled(history.undoDisabled) if let tool {
Button { Button {
history.historyPublisher.send(.redo) tool.selectTool(.hand)
} label: { } label: {
Text("Redo") Text("Hand Tool")
}
.keyboardShortcut("h", modifiers: [.option])
Button {
tool.selectTool(.pen)
} label: {
Text("Pen Tool")
}
.keyboardShortcut("p", modifiers: [.option])
Button {
tool.selectTool(.photo)
} label: {
Text("Photo Tool")
}
.keyboardShortcut("p", modifiers: [.option, .shift])
} }
.keyboardShortcut("z", modifiers: [.command, .shift])
.disabled(history.redoDisabled)
} }
} }
CommandGroup(replacing: .pasteboard) { } CommandGroup(replacing: .pasteboard) { }

View File

@@ -9,23 +9,25 @@ import SwiftUI
struct ViewCommands: Commands { struct ViewCommands: Commands {
@ObservedObject private var application: Application @ObservedObject private var application: Application
@FocusedValue(\.activeSceneKey) private var appScene @FocusedValue(\.activeSceneKey) private var appScene
@FocusedObject var canvas: Canvas?
init(application: Application) { init(application: Application) {
self.application = application self.application = application
} }
var body: some Commands { var body: some Commands {
CommandGroup(replacing: .toolbar) { CommandGroup(replacing: .toolbar) {
if appScene == .memos || appScene == .trash { switch appScene {
case .memos, .trash:
Button { Button {
application.activateSearchBar() application.activateSearchBar()
} label: { } label: {
Text("Find Memo") Text("Find Memo")
} }
.keyboardShortcut("f", modifiers: [.command]) .keyboardShortcut("f", modifiers: [.command])
}
if appScene == .memos || appScene == .trash {
Button { Button {
application.toggleSidebar() application.toggleSidebar()
} label: { } label: {
@@ -37,6 +39,15 @@ struct ViewCommands: Commands {
} }
} }
.keyboardShortcut("o", modifiers: [.command]) .keyboardShortcut("o", modifiers: [.command])
case .memo:
Button {
canvas?.toggleGridMode()
} label: {
Text("Change Grid Layout")
}
.keyboardShortcut("g", modifiers: [.option])
default:
EmptyView()
} }
} }
} }