forked from aparoksha/adwaita-swift
Polish demo app
This commit is contained in:
commit
9618985d15
@ -31,10 +31,30 @@ public struct Idle {
|
|||||||
/// - priority: The task's priority, default priority by default.
|
/// - priority: The task's priority, default priority by default.
|
||||||
/// - closure: The closure to run. Return if you want to exit the loop.
|
/// - closure: The closure to run. Return if you want to exit the loop.
|
||||||
@discardableResult
|
@discardableResult
|
||||||
|
@available(macOS, introduced: 13)
|
||||||
public init(
|
public init(
|
||||||
delay: Duration,
|
delay: Duration,
|
||||||
priority: Priority = .defaultIdle,
|
priority: Priority = .defaultIdle,
|
||||||
closure: @escaping () -> Bool
|
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)
|
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.
|
/// Add a function to be called whenever there are no higher priority events pending to the default main loop.
|
||||||
/// - Parameter closure: The function.
|
/// - 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 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 {
|
if let delay {
|
||||||
let milliseconds = delay.components.seconds * secondsToMilliseconds
|
|
||||||
+ (delay.components.attoseconds / attosecondsToMilliseconds)
|
|
||||||
// swiftlint:disable prefer_self_in_static_references
|
// 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
|
// swiftlint:enable prefer_self_in_static_references
|
||||||
} else {
|
} else {
|
||||||
// swiftlint:disable prefer_self_in_static_references
|
// swiftlint:disable prefer_self_in_static_references
|
||||||
|
|||||||
@ -14,7 +14,7 @@ struct IdleDemo: View {
|
|||||||
@State private var progress = 0.0
|
@State private var progress = 0.0
|
||||||
@State private var activeProcess = false
|
@State private var activeProcess = false
|
||||||
let max = 500.0
|
let max = 500.0
|
||||||
let delayFactor = 5.0
|
let delayFactor = 5_000.0
|
||||||
let maxWidth = 300
|
let maxWidth = 300
|
||||||
|
|
||||||
var view: Body {
|
var view: Body {
|
||||||
@ -26,7 +26,7 @@ struct IdleDemo: View {
|
|||||||
activeProcess = true
|
activeProcess = true
|
||||||
progress = 0
|
progress = 0
|
||||||
Task {
|
Task {
|
||||||
Idle(delay: .seconds(delayFactor / max)) {
|
Idle(delay: .init(delayFactor / max)) {
|
||||||
progress += 1
|
progress += 1
|
||||||
let done = progress == max
|
let done = progress == max
|
||||||
if done {
|
if done {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user