-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e126e7
commit 9f8352b
Showing
3 changed files
with
53 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
OpenHaystack/OpenHaystack/HaystackApp/Views/Slider+LogScale.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// OpenHaystack – Tracking personal Bluetooth devices via Apple's Find My network | ||
// | ||
// Copyright © 2021 Secure Mobile Networking Lab (SEEMOO) | ||
// Copyright © 2021 The Open Wireless Link Project | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
// | ||
|
||
import SwiftUI | ||
|
||
extension Binding where Value == Double { | ||
func logarithmic(base: Double = 10.0) -> Binding<Double> { | ||
Binding( | ||
get: { | ||
logC(self.wrappedValue, forBase: base) | ||
}, | ||
set: { (newValue) in | ||
self.wrappedValue = pow(base, newValue) | ||
}) | ||
} | ||
} | ||
|
||
extension Slider { | ||
static func withLogScale( | ||
base: Double = 10.0, | ||
value: Binding<Double>, | ||
in inRange: ClosedRange<Double>, | ||
minimumValueLabel: ValueLabel = EmptyView() as! ValueLabel, | ||
maximumValueLabel: ValueLabel = EmptyView() as! ValueLabel, | ||
label: () -> Label = { EmptyView() as! Label }, | ||
onEditingChanged: @escaping (Bool) -> Void = { _ in } | ||
) -> Slider where Label: View, ValueLabel: View { | ||
return self.init( | ||
value: value.logarithmic(base: base), | ||
in: logC(inRange.lowerBound, forBase: base)...logC(inRange.upperBound, forBase: base), | ||
onEditingChanged: onEditingChanged, minimumValueLabel: minimumValueLabel, | ||
maximumValueLabel: maximumValueLabel, | ||
label: label) | ||
} | ||
} | ||
|
||
private func logC(_ value: Double, forBase base: Double) -> Double { | ||
return log(value) / log(base) | ||
} |