From 2e64ebc5705a13c92ea05d000a2043f06d5e3cc1 Mon Sep 17 00:00:00 2001 From: dscyrescotti Date: Mon, 15 Jul 2024 22:09:32 +0700 Subject: [PATCH] feat: add tool and grid commands --- Memola/Canvas/Core/Canvas.swift | 13 ++++++ Memola/Canvas/Tool/Core/Tool.swift | 1 + Memola/Features/Memo/Memo/MemoView.swift | 2 + Memola/Features/Memo/Toolbar/Toolbar.swift | 9 +--- Memola/Shortcut/Commands/EditCommands.swift | 50 +++++++++++++++------ Memola/Shortcut/Commands/ViewCommands.swift | 17 +++++-- 6 files changed, 68 insertions(+), 24 deletions(-) diff --git a/Memola/Canvas/Core/Canvas.swift b/Memola/Canvas/Core/Canvas.swift index 589be72..ddb9f3b 100644 --- a/Memola/Canvas/Core/Canvas.swift +++ b/Memola/Canvas/Core/Canvas.swift @@ -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 diff --git a/Memola/Canvas/Tool/Core/Tool.swift b/Memola/Canvas/Tool/Core/Tool.swift index f7911c2..12b1e2d 100644 --- a/Memola/Canvas/Tool/Core/Tool.swift +++ b/Memola/Canvas/Tool/Core/Tool.swift @@ -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 diff --git a/Memola/Features/Memo/Memo/MemoView.swift b/Memola/Features/Memo/Memo/MemoView.swift index 2f6385d..2030334 100644 --- a/Memola/Features/Memo/Memo/MemoView.swift +++ b/Memola/Features/Memo/Memo/MemoView.swift @@ -61,6 +61,8 @@ struct MemoView: View { loadingIndicator("Loading photo...") } } + .focusedSceneObject(tool) + .focusedSceneObject(canvas) .focusedSceneObject(history) .focusedSceneValue(\.activeSceneKey, .memo) } diff --git a/Memola/Features/Memo/Toolbar/Toolbar.swift b/Memola/Features/Memo/Toolbar/Toolbar.swift index 52cf9c9..64d214d 100644 --- a/Memola/Features/Memo/Toolbar/Toolbar.swift +++ b/Memola/Features/Memo/Toolbar/Toolbar.swift @@ -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) diff --git a/Memola/Shortcut/Commands/EditCommands.swift b/Memola/Shortcut/Commands/EditCommands.swift index 12d00d1..a11c1ba 100644 --- a/Memola/Shortcut/Commands/EditCommands.swift +++ b/Memola/Shortcut/Commands/EditCommands.swift @@ -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) { } diff --git a/Memola/Shortcut/Commands/ViewCommands.swift b/Memola/Shortcut/Commands/ViewCommands.swift index beb2bc0..12290e0 100644 --- a/Memola/Shortcut/Commands/ViewCommands.swift +++ b/Memola/Shortcut/Commands/ViewCommands.swift @@ -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() } } }