summaryrefslogtreecommitdiff
path: root/macos/Sources/Helpers/Cursor.swift
blob: fe4a148b5a17418c38ee1c6241cc40aefc557855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Cocoa

/// This helps manage the stateful nature of NSCursor hiding and unhiding. 
class Cursor {
    private static var counter: UInt = 0

    static var isVisible: Bool {
        counter == 0
    }

    static func hide() {
        counter += 1
        NSCursor.hide()
    }

    /// Unhide the cursor. Returns true if the cursor was previously hidden.
    static func unhide() -> Bool {
        // Its always safe to call unhide when the counter is zero because it
        // won't go negative.
        NSCursor.unhide()

        if (counter > 0) {
            counter -= 1
            return true
        }

        return false
    }

    static func unhideCompletely() -> UInt {
        let counter = self.counter
        for _ in 0..<counter {
            assert(unhide())
        }
        assert(self.counter == 0)
        return counter
    }
}