Skip to content

Commit

Permalink
Merge pull request #16 from libec/feature/add-swiftformat
Browse files Browse the repository at this point in the history
Feature/add swiftformat
  • Loading branch information
libec authored Feb 25, 2024
2 parents 7dd193c + 545b096 commit e01e69b
Show file tree
Hide file tree
Showing 106 changed files with 250 additions and 381 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ jobs:
runs-on: macos-13
steps:
- uses: actions/checkout@v3
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '15.2'
- name: Lint
run: swiftformat . --lint
- name: Run tests
run: xcodebuild test -project ClipNinja.xcodeproj -scheme ClipNinja
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.9
19 changes: 9 additions & 10 deletions App/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import SwiftUI
import Combine
import ClipNinjaPackage
import Combine
import SwiftUI

@main
class AppDelegate: NSObject, NSApplicationDelegate {

private let windowsController: WindowsController
private let menuBarController: MenuBarController
private let migrationController: MigrationController
Expand All @@ -14,24 +13,24 @@ class AppDelegate: NSObject, NSApplicationDelegate {
KeyboardHandlingAssembly(),
LaunchAtLoginAssembly(),
MigrationAssembly(),
WindowsAssembly()
WindowsAssembly(),
]).resolveDependencyGraph()
self.windowsController = instanceProvider.resolve(WindowsController.self)
self.menuBarController = instanceProvider.resolve(MenuBarController.self)
self.migrationController = instanceProvider.resolve(MigrationController.self)
windowsController = instanceProvider.resolve(WindowsController.self)
menuBarController = instanceProvider.resolve(MenuBarController.self)
migrationController = instanceProvider.resolve(MigrationController.self)
super.init()
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
func applicationDidFinishLaunching(_: Notification) {
migrationController.migrate()
menuBarController.setupMenuBar()
windowsController.startNavigation()
windowsController.openFirstWindow()
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { false }
func applicationShouldTerminateAfterLastWindowClosed(_: NSApplication) -> Bool { false }

func applicationWillResignActive(_ notification: Notification) {
func applicationWillResignActive(_: Notification) {
windowsController.resignActive()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import ClipNinjaPackage
import SwiftUI
import Swinject
import SwinjectAutoregistration
import SwiftUI

struct KeyboardHandlingAssembly: Assembly {

func assemble(container: Container) {
container.autoregister(SystemKeyboardObserver.self, initializer: SystemKeyboardObserver.init)
.implements(KeyboardObserver.self)
Expand Down
43 changes: 21 additions & 22 deletions App/Keyboard Handling/System/SystemKeyboardObserver.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import KeyboardShortcuts
import Combine
import ClipNinjaPackage
import Combine
import KeyboardShortcuts

final class SystemKeyboardObserver: KeyboardObserver {

private let keyPressSubject = PassthroughSubject<KeyboardEvent, Never>()

var keyPress: AnyPublisher<KeyboardEvent, Never> {
Expand All @@ -19,41 +18,41 @@ final class SystemKeyboardObserver: KeyboardObserver {
private func keyboardEvent(from shortcutsKeyPress: KeyboardShortcuts.Key) -> KeyboardEvent? {
switch shortcutsKeyPress {
case .downArrow:
return .down
.down
case .upArrow:
return .up
.up
case .rightArrow:
return .right
.right
case .leftArrow:
return .left
.left
case .delete:
return .delete
case .keypadEnter, .`return`:
return .enter
.delete
case .keypadEnter, .return:
.enter
case .space:
return .space
.space
case .escape:
return .escape
.escape
case .keypad1, .one:
return .number(number: 1)
.number(number: 1)
case .keypad2, .two:
return .number(number: 2)
.number(number: 2)
case .keypad3, .three:
return .number(number: 3)
.number(number: 3)
case .keypad4, .four:
return .number(number: 4)
.number(number: 4)
case .keypad5, .five:
return .number(number: 5)
.number(number: 5)
case .keypad6, .six:
return .number(number: 6)
.number(number: 6)
case .keypad7, .seven:
return .number(number: 7)
.number(number: 7)
case .keypad8, .eight:
return .number(number: 8)
.number(number: 8)
case .keypad9, .nine:
return .number(number: 9)
.number(number: 9)
default:
return nil
nil
}
}
}
7 changes: 3 additions & 4 deletions App/Keyboard Handling/System/SystemShortcutObserver.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import KeyboardShortcuts
import Combine
import ClipNinjaPackage
import Combine
import KeyboardShortcuts

extension KeyboardShortcuts.Name {
static let toggleClipNinja = Self("clipninja_shortcut", default: .init(.v, modifiers: [.command, .shift]))
}

final class SystemShortcutObserver: ShortcutObserver {

private let subject = PassthroughSubject<Void, Never>()

init() { }
init() {}

func observe() {
KeyboardShortcuts.onKeyUp(for: .toggleClipNinja) { [self] in
Expand Down
7 changes: 3 additions & 4 deletions App/Keyboard Handling/View/RecordShortcutView.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import ClipNinjaPackage
import SwiftUI
import KeyboardShortcuts
import SwiftUI

struct RecordShortcutView: View {
init() {}

init() { }

var body: some View {
HStack(alignment: .firstTextBaseline) {
Text(Strings.Settings.openAppShortcut)
KeyboardShortcuts.Recorder(for: .toggleClipNinja) { shortcut in
KeyboardShortcuts.Recorder(for: .toggleClipNinja) { _ in
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions App/Launch At Login/System/LaunchAtLoginSystemResource.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import ServiceManagement
import ClipNinjaPackage
import ServiceManagement

final class LaunchAtLoginSystemResource: LaunchAtLoginResource {

private let appService: SMAppService

init() {
self.appService = SMAppService.mainApp
appService = SMAppService.mainApp
}

var enabled: Bool {
Expand Down
3 changes: 1 addition & 2 deletions App/Migration/System/MigrationController.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Foundation
import ClipNinjaPackage
import Foundation

final class MigrationController {

private let legacyKey = "Clipboards"

func migrate() {
Expand Down
3 changes: 1 addition & 2 deletions App/Windows/Assembly/WindowsAssembly.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import ClipNinjaPackage
import SwiftUI
import Swinject
import SwinjectAutoregistration
import SwiftUI

struct WindowsAssembly: Assembly {

func assemble(container: Container) {
container.autoregister(WindowsController.self, initializer: WindowsController.init)
.inObjectScope(.container)
Expand Down
25 changes: 12 additions & 13 deletions App/Windows/System/MenuBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import ClipNinjaPackage
import Combine

final class MenuBarController: NSObject {

private let navigation: Navigation
private var statusItem: NSStatusItem?

Expand Down Expand Up @@ -32,25 +31,25 @@ final class MenuBarController: NSObject {
menu.addItem(.separator())
menu.addItem(quitItem)

self.statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
self.statusItem?.menu = menu
self.statusItem?.button?.image = NSImage(named: "MenuIcon")
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
statusItem?.menu = menu
statusItem?.button?.image = NSImage(named: "MenuIcon")
subscribeNavigation()
}

private func subscribeNavigation() {
navigation.navigationEvent
.receive(on: DispatchQueue.main)
.sink { [weak self] event in
switch event {
case .showTutorial:
self?.controlsItem?.target = nil
case .hideTutorial:
self?.controlsItem?.target = self
default:
break
}
}.store(in: &subscriptions)
switch event {
case .showTutorial:
self?.controlsItem?.target = nil
case .hideTutorial:
self?.controlsItem?.target = self
default:
break
}
}.store(in: &subscriptions)
}

@objc
Expand Down
5 changes: 0 additions & 5 deletions App/Windows/System/Windows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Combine
import KeyboardShortcuts

class StyledWindow: NSWindow {

var defaultsWindowKey = ""

init(contentRect: NSRect) {
Expand All @@ -30,8 +29,6 @@ class StyledWindow: NSWindow {
}

class SettingsWindow: StyledWindow {


init() {
super.init(contentRect: NSRect(x: 0, y: 0, width: 450, height: 300))
title = Strings.Settings.windowName
Expand All @@ -42,7 +39,6 @@ class SettingsWindow: StyledWindow {
}

class TutorialWindow: StyledWindow {

init() {
super.init(contentRect: NSRect(x: 0, y: 0, width: 500, height: 400))
setFrameAutosaveName("TutorialWindow")
Expand All @@ -52,7 +48,6 @@ class TutorialWindow: StyledWindow {
}

class ClipboardWindow: StyledWindow {

private let keyboardController: SystemKeyboardObserver

let keySubject = PassthroughSubject<KeyboardShortcuts.Key, Never>()
Expand Down
32 changes: 15 additions & 17 deletions App/Windows/System/WindowsController.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftUI
import Combine
import ClipNinjaPackage
import Combine
import SwiftUI

enum AppWindow {
case clips
Expand All @@ -9,7 +9,6 @@ enum AppWindow {
}

class WindowsController {

private let navigation: Navigation
private let windowFactory: WindowsFactory
private let resetViewPortUseCase: ResetViewPortUseCase
Expand Down Expand Up @@ -39,7 +38,7 @@ class WindowsController {

private func showModal(modalWindow: AppWindow) {
if self.modalWindow == nil {
let modalWindow = self.windowFactory.make(appWindow: modalWindow)
let modalWindow = windowFactory.make(appWindow: modalWindow)
self.modalWindow = modalWindow
activeWindow?.beginSheet(modalWindow)
}
Expand All @@ -58,7 +57,6 @@ class WindowsController {
}

func startNavigation() {

let delayedEvents = navigation.navigationEvent
.filter(\.delayedEvent)
.throttle(for: 0.3, scheduler: RunLoop.main, latest: true)
Expand All @@ -71,33 +69,33 @@ class WindowsController {
.sink { [unowned self] event in
log(message: "NavigationEvent: \(event)", category: .windows)
log(message: "App windows", category: .windows)
NSApp.windows.forEach { window in
for window in NSApp.windows {
log(message: "Window: \(window)", category: .windows)
}
switch event {
case .hideApp:
NSApp.hide(nil)
case .showClipboard:
self.closeSettingsWindows()
self.closeClipsWindows()
self.activate(appWindow: .clips)
closeSettingsWindows()
closeClipsWindows()
activate(appWindow: .clips)
case .showSettings:
self.closeSettingsWindows()
self.closeClipsWindows()
self.activate(appWindow: .settings)
closeSettingsWindows()
closeClipsWindows()
activate(appWindow: .settings)
case .showAppUsage:
let clipboardWindows = NSApp.windows.filter { $0 is ClipboardWindow }
if let clipboardWindow = clipboardWindows.first {
clipboardWindow.makeKeyAndOrderFront(nil)
self.activeWindow = clipboardWindow
activeWindow = clipboardWindow
} else {
self.activate(appWindow: .clips)
activate(appWindow: .clips)
}
self.showModal(modalWindow: .tutorial)
showModal(modalWindow: .tutorial)
case .showTutorial:
self.showModal(modalWindow: .tutorial)
showModal(modalWindow: .tutorial)
case .hideTutorial:
self.hideModal()
hideModal()
case .showSystemSettings:
let accessibilityUrl = Strings.Settings.PasteDirectly.accessibilityUrl
guard let url = URL(string: accessibilityUrl) else {
Expand Down
Loading

0 comments on commit e01e69b

Please sign in to comment.