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 issue with selection commands #427

Merged
merged 1 commit into from
Oct 25, 2023
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
6 changes: 3 additions & 3 deletions App/Sources/Core/Controllers/NSEventController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import Foundation
import SwiftUI

final class NSEventController: ObservableObject {
@Published var keyDown: Bool = false
@Published var repeatingKeyDown: Bool = false

static let shared: NSEventController = .init()

fileprivate init() {
NSEvent.addLocalMonitorForEvents(matching: [.keyUp, .keyDown]) { [weak self] event in
guard let self else { return event }
if event.type == .keyDown, event.isARepeat {
keyDown = true
repeatingKeyDown = true
} else {
keyDown = false
repeatingKeyDown = false
}
return event
}
Expand Down
11 changes: 8 additions & 3 deletions App/Sources/UI/Views/Focus/FocusPublisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ final class FocusPublisher<Element>: ObservableObject where Element: Equatable,
Element: Hashable,
Element: Identifiable,
Element.ID: CustomStringConvertible {
private let debouncer: FocusPublisherDebouncer<Element> = .init(milliseconds: 50) { elementID in
private lazy var debouncer: FocusPublisherDebouncer<Element> = .init(milliseconds: 50) { elementID in
FocusableProxy<Element>.post(elementID)
self.focus = .elementID(elementID)
}

private(set) var focus: Focused<Element> = .elementID("-1" as! Element.ID)
Expand All @@ -42,7 +43,11 @@ final class FocusPublisher<Element>: ObservableObject where Element: Equatable,
}

func publish(_ elementID: Element.ID) {
debouncer.process(elementID)
focus = .elementID(elementID)
if NSEventController.shared.repeatingKeyDown {
debouncer.process(elementID)
} else {
FocusableProxy<Element>.post(elementID)
focus = .elementID(elementID)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ final class DebounceSelectionManager<Snapshot: DebounceSnapshot> {
self.onUpdate = onUpdate
self.subscription = subject
.dropFirst()
.removeDuplicates()
.debounce(for: .milliseconds(milliseconds), scheduler: DispatchQueue.main)
.sink { onUpdate($0) }
.removeDuplicates()
.sink {
onUpdate($0)
}
}

func process(_ snapshot: Snapshot) {
if NSEventController.shared.keyDown {
if NSEventController.shared.repeatingKeyDown {
subject.send(snapshot)
} else {
onUpdate(snapshot)
Expand Down
Loading