Skip to content

Commit

Permalink
Polish demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed May 6, 2024
2 parents 212af59 + 3cee13f commit 9618985
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 22 additions & 6 deletions Sources/Adwaita/Model/Data Flow/Idle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Tests/IdleDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 9618985

Please sign in to comment.