Skip to content

Commit

Permalink
Add ability to hide label (#32)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
rcobelli and sindresorhus authored Mar 19, 2021
1 parent e73c5a6 commit bd6ae4a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
27 changes: 23 additions & 4 deletions Sources/CircularProgress/CircularProgress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public final class CircularProgress: NSView {
self.progressCircle.progress = self._progress
})

progressLabel.isHidden = progress == 0 && isIndeterminate ? cancelButton.isHidden : !cancelButton.isHidden
progressLabel.isHidden = isLabelHidden || (progress == 0 && isIndeterminate ? cancelButton.isHidden : !cancelButton.isHidden)

if !progressLabel.isHidden {
progressLabel.string = "\(Int(_progress * 100))%"
Expand Down Expand Up @@ -280,6 +280,7 @@ public final class CircularProgress: NSView {

progressCircle.isHidden = isIndeterminate
indeterminateCircle.isHidden = !isIndeterminate
progressLabel.isHidden = isLabelHidden
}

/**
Expand All @@ -299,6 +300,7 @@ public final class CircularProgress: NSView {

progressCircle.resetProgress()
progressLabel.string = "0%"
progressLabel.isHidden = isLabelHidden

successView.isHidden = true

Expand Down Expand Up @@ -451,7 +453,7 @@ public final class CircularProgress: NSView {
}

override public func mouseExited(with event: NSEvent) {
progressLabel.isHidden = isIndeterminate && progress == 0
progressLabel.isHidden = _isLabelHidden || (isIndeterminate && progress == 0)
cancelButton.isHidden = true
successView.isHidden = shouldHideSuccessView
}
Expand Down Expand Up @@ -486,13 +488,30 @@ public final class CircularProgress: NSView {
private func startIndeterminateState() {
progressCircle.isHidden = true
indeterminateCircle.isHidden = false
progressLabel.isHidden = progress == 0 && isIndeterminate && cancelButton.isHidden
progressLabel.isHidden = _isLabelHidden || (progress == 0 && isIndeterminate && cancelButton.isHidden)
}

private func stopIndeterminateState() {
indeterminateCircle.isHidden = true
progressCircle.isHidden = false
progressLabel.isHidden = !cancelButton.isHidden
progressLabel.isHidden = _isLabelHidden || !cancelButton.isHidden
}

private var _isLabelHidden = false
/**
Returns whether the progress label is hidden.
*/
@IBInspectable public var isLabelHidden: Bool {
get { _isLabelHidden }
set {
assertMainThread()

willChangeValue(for: \.isLabelHidden)
_isLabelHidden = newValue
didChangeValue(for: \.isLabelHidden)

progressLabel.isHidden = _isLabelHidden
}
}

private var shouldHideSuccessView: Bool {
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ Note that the `.progress` property and `.isIndeterminate` are not tied together.

If you use the `.progressInstance` property, the [`isIndeterminate`](https://developer.apple.com/documentation/foundation/progress/1412871-isindeterminate) property will automatically be observed. The view will then switch back and forth to the indeterminate state when appropriate.

### Hidden progress label

<img src="screenshot-hiddenLabel.gif" width="118" align="right">

Displays a spinner without a percentage indicator in the center.

This is accomplished by setting the `.isLabelHidden` property to `true`. The default state is `false` (the label is displayed).

## API

```swift
Expand Down
Binary file added screenshot-hiddenLabel.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bd6ae4a

Please sign in to comment.