Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: registering hotkeys after system reboot #49

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions FlashSpace/FlashSpaceApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,37 @@
// Created by Wojciech Kulik on 19/01/2025.
//

import Combine
import SwiftUI

final class AppDelegate: NSObject, NSApplicationDelegate {
@Environment(\.openWindow) private var openWindow

private var cancellables = Set<AnyCancellable>()

func applicationDidFinishLaunching(_ notification: Notification) {
AppDependencies.shared.hotKeysManager.enableAll()

NotificationCenter.default
.publisher(for: .openMainWindow)
.sink { [weak self] _ in
self?.openWindow(id: "main")
NSApp.activate(ignoringOtherApps: true)
}
.store(in: &cancellables)
}
}

@main
struct FlashSpaceApp: App {
@StateObject
private var workspaceManager = AppDependencies.shared.workspaceManager

@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@Environment(\.openWindow) private var openWindow

@StateObject private var workspaceManager = AppDependencies.shared.workspaceManager

var body: some Scene {
Window("⚡ FlashSpace v\(AppConstants.version)", id: "main") {
MainView()
.onReceive(NotificationCenter.default.publisher(for: .openMainWindow)) { _ in
openWindow(id: "main")
NSApp.activate(ignoringOtherApps: true)
}
}
.windowResizability(.contentSize)

Expand Down
6 changes: 0 additions & 6 deletions FlashSpace/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import SymbolPicker

struct MainView: View {
@Environment(\.openWindow) var openWindow
@Environment(\.dismissWindow) var dismissWindow

@StateObject var viewModel = MainViewModel()
@StateObject var profilesRepository = AppDependencies.shared.profilesRepository
Expand All @@ -25,11 +24,6 @@ struct MainView: View {
.padding()
.fixedSize()
.onAppear {
if viewModel.dismissOnLaunch {
viewModel.dismissOnLaunch = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { dismissWindow() }
}

Task { await UpdatesManager.shared.autoCheckForUpdates() }
}
.sheet(isPresented: $viewModel.isInputDialogPresented) {
Expand Down
17 changes: 0 additions & 17 deletions FlashSpace/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import ShortcutRecorder
import SwiftUI

final class MainViewModel: ObservableObject {
@AppStorage("afterFirstLaunch") var afterFirstLaunch = false

@Published var workspaces: [Workspace] = []
@Published var workspaceApps: [String]?

Expand Down Expand Up @@ -40,7 +38,6 @@ final class MainViewModel: ObservableObject {
@Published var isSymbolPickerPresented = false
@Published var isInputDialogPresented = false
@Published var userInput = ""
@Published var dismissOnLaunch = false

var focusAppOptions: [String] {
[AppConstants.lastFocusedOption] + (workspaceApps ?? [])
Expand Down Expand Up @@ -81,26 +78,12 @@ final class MainViewModel: ObservableObject {

private let workspaceManager = AppDependencies.shared.workspaceManager
private let workspaceRepository = AppDependencies.shared.workspaceRepository
private let hotKeysManager = AppDependencies.shared.hotKeysManager

init() {
self.workspaces = workspaceRepository.workspaces
self.workspaceDisplay = NSScreen.main?.localizedName ?? ""

hotKeysManager.enableAll()
observe()
checkIfFirstLaunch()
}

private func checkIfFirstLaunch() {
if afterFirstLaunch {
dismissOnLaunch = true
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
NSApp.activate(ignoringOtherApps: true)
}
}
afterFirstLaunch = true
}

private func observe() {
Expand Down