mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-02 06:19:28 +08:00
* Implement key combination holds for enabling/disabling keyboard or trackpad * Improve keyboard/trackpad connection status HUD
57 lines
1.8 KiB
Swift
57 lines
1.8 KiB
Swift
import SwiftUI
|
|
|
|
/// The contents of the HUD that tells the user about the state of an input device.
|
|
struct VMInputStatusHUD: Equatable {
|
|
enum Phase: Equatable {
|
|
/// The shortcut that toggles the device is being held down,
|
|
/// the toggle takes effect if it's held down for the remaining time.
|
|
case holding(remaining: TimeInterval)
|
|
/// The device has just been connected to or disconnected from the virtual machine.
|
|
case changed
|
|
}
|
|
|
|
var device: VMInputDevice
|
|
/// Whether the device's events are currently being delivered to the virtual machine.
|
|
var isConnected: Bool
|
|
var phase: Phase
|
|
}
|
|
|
|
/// Decides when the input status HUD is on screen and what it shows.
|
|
@MainActor
|
|
@Observable
|
|
final class VMInputStatusHUDPresenter {
|
|
/// How long the HUD stays on screen after an input device is connected or disconnected.
|
|
static let displayDuration: TimeInterval = 2.5
|
|
|
|
private(set) var hud: VMInputStatusHUD?
|
|
|
|
@ObservationIgnored
|
|
private var dismissTask: Task<Void, Never>?
|
|
|
|
func holdBegan(for device: VMInputDevice, isConnected: Bool, remaining: TimeInterval) {
|
|
dismissTask?.cancel()
|
|
dismissTask = nil
|
|
|
|
hud = VMInputStatusHUD(device: device, isConnected: isConnected, phase: .holding(remaining: remaining))
|
|
}
|
|
|
|
func holdCancelled() {
|
|
guard case .holding = hud?.phase else { return }
|
|
|
|
hud = nil
|
|
}
|
|
|
|
func statusChanged(for device: VMInputDevice, isConnected: Bool) {
|
|
hud = VMInputStatusHUD(device: device, isConnected: isConnected, phase: .changed)
|
|
|
|
dismissTask?.cancel()
|
|
dismissTask = Task { [weak self] in
|
|
try? await Task.sleep(for: .seconds(Self.displayDuration))
|
|
|
|
guard !Task.isCancelled else { return }
|
|
|
|
self?.hud = nil
|
|
}
|
|
}
|
|
}
|