diff --git a/Sources/Adwaita/Model/Data Flow/Idle.swift b/Sources/Adwaita/Model/Data Flow/Idle.swift index 7f41e3e..e922c23 100644 --- a/Sources/Adwaita/Model/Data Flow/Idle.swift +++ b/Sources/Adwaita/Model/Data Flow/Idle.swift @@ -31,10 +31,30 @@ public struct Idle { /// - priority: The task's priority, default priority by default. /// - closure: The closure to run. Return if you want to exit the loop. @discardableResult + @available(macOS, introduced: 13) public init( delay: Duration, priority: Priority = .defaultIdle, closure: @escaping () -> Bool + ) { + let secondsToMilliseconds: Int64 = 1_000 + let attosecondsToMilliseconds: Int64 = 1_000_000_000_000_000 + let milliseconds = delay.components.seconds * secondsToMilliseconds + + (delay.components.attoseconds / attosecondsToMilliseconds) + self.init(delay: milliseconds, priority: priority, closure: closure) + } + + /// Repeat a function with a certain delay. + /// - Parameters: + /// - delay: The delay between the repetitions in milliseconds. + /// - priority: The task's priority, default priority by default. + /// - closure: The closure to run. Return if you want to exit the loop. + @discardableResult + @available(macOS, deprecated: 13) + public init( + delay: Int64, + priority: Priority = .defaultIdle, + closure: @escaping () -> Bool ) { Self.handler.add(closure, priority: .init(priority.rawValue), delay: delay) } @@ -60,15 +80,11 @@ public struct Idle { /// Add a function to be called whenever there are no higher priority events pending to the default main loop. /// - Parameter closure: The function. - func add(_ closure: @escaping () -> Bool, priority: Int32, delay: Duration? = nil) { + func add(_ closure: @escaping () -> Bool, priority: Int32, delay: Int64? = nil) { let context = UnsafeMutableRawPointer(Unmanaged.passRetained(ClosureContainer(closure: closure)).toOpaque()) - let secondsToMilliseconds: Int64 = 1_000 - let attosecondsToMilliseconds: Int64 = 1_000_000_000_000_000 if let delay { - let milliseconds = delay.components.seconds * secondsToMilliseconds - + (delay.components.attoseconds / attosecondsToMilliseconds) // swiftlint:disable prefer_self_in_static_references - g_timeout_add_full(priority, .init(milliseconds), { IdleHandler.run(pointer: $0) }, context, nil) + g_timeout_add_full(priority, .init(delay), { IdleHandler.run(pointer: $0) }, context, nil) // swiftlint:enable prefer_self_in_static_references } else { // swiftlint:disable prefer_self_in_static_references diff --git a/Tests/IdleDemo.swift b/Tests/IdleDemo.swift index 74fa02c..96eca41 100644 --- a/Tests/IdleDemo.swift +++ b/Tests/IdleDemo.swift @@ -14,7 +14,7 @@ struct IdleDemo: View { @State private var progress = 0.0 @State private var activeProcess = false let max = 500.0 - let delayFactor = 5.0 + let delayFactor = 5_000.0 let maxWidth = 300 var view: Body { @@ -26,7 +26,7 @@ struct IdleDemo: View { activeProcess = true progress = 0 Task { - Idle(delay: .seconds(delayFactor / max)) { + Idle(delay: .init(delayFactor / max)) { progress += 1 let done = progress == max if done {