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()
}
}
func toggleGridMode() {
let _gridMode: GridMode
switch gridMode {
case .none:
_gridMode = .point
case .point:
_gridMode = .line
case .line:
_gridMode = .none
}
setGridMode(_gridMode)
}
}
// MARK: - Stroke

View File

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

View File

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

View File

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

View File

@@ -10,25 +10,49 @@ import SwiftUI
struct EditCommands: Commands {
@FocusedValue(\.activeSceneKey) private var appScene
@FocusedObject var tool: Tool?
@FocusedObject var history: History?
var body: some Commands {
CommandGroup(replacing: .undoRedo) {
if appScene == .memo, let history {
Button {
history.historyPublisher.send(.undo)
} label: {
Text("Undo")
if appScene == .memo {
if let history {
Button {
history.historyPublisher.send(.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])
.disabled(history.undoDisabled)
Button {
history.historyPublisher.send(.redo)
} label: {
Text("Redo")
Divider()
if let tool {
Button {
tool.selectTool(.hand)
} label: {
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) { }

View File

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