RAMSlider.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Copyright © 2021 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import SwiftUI
  17. struct RAMSlider: View {
  18. let validMemoryValues = [32, 64, 128, 256, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 10240, 12288, 14336, 16384, 32768, 65536]
  19. let validateMemorySize: (Bool) -> Void
  20. @Binding var systemMemory: NSNumber?
  21. @State private var memorySizeIndex: Float = 0
  22. var memorySizeIndexObserver: Binding<Float> {
  23. Binding<Float>(
  24. get: {
  25. return memorySizePickerIndex(size: systemMemory)
  26. },
  27. set: {
  28. systemMemory = memorySize(pickerIndex: $0)
  29. }
  30. )
  31. }
  32. init(systemMemory: Binding<NSNumber?>, onValidate: @escaping (Bool) -> Void = { _ in }) {
  33. validateMemorySize = onValidate
  34. _systemMemory = systemMemory
  35. }
  36. init<T: FixedWidthInteger>(systemMemory: Binding<T>, onValidate: @escaping (Bool) -> Void = { _ in }) {
  37. validateMemorySize = onValidate
  38. _systemMemory = Binding<NSNumber?>(get: {
  39. UInt64(systemMemory.wrappedValue) as NSNumber
  40. }, set: { newValue in
  41. systemMemory.wrappedValue = T(newValue?.uint64Value ?? 0)
  42. })
  43. }
  44. var body: some View {
  45. GeometryReader { geo in
  46. HStack {
  47. Slider(value: memorySizeIndexObserver, in: 0...Float(validMemoryValues.count-1), step: 1) { start in
  48. if !start {
  49. validateMemorySize(false)
  50. }
  51. } label: {
  52. Text("")
  53. }
  54. NumberTextField("", number: $systemMemory, prompt: "Size", onEditingChanged: validateMemorySize)
  55. .frame(width: 80)
  56. Text("MiB")
  57. }
  58. }.frame(height: 30)
  59. }
  60. func memorySizePickerIndex(size: NSNumber?) -> Float {
  61. guard let sizeUnwrap = size else {
  62. return 0
  63. }
  64. for (i, s) in validMemoryValues.enumerated() {
  65. if s >= Int(truncating: sizeUnwrap) {
  66. return Float(i)
  67. }
  68. }
  69. return Float(validMemoryValues.count - 1)
  70. }
  71. func memorySize(pickerIndex: Float) -> NSNumber {
  72. let i = Int(pickerIndex)
  73. guard i >= 0 && i < validMemoryValues.count else {
  74. return 0
  75. }
  76. return validMemoryValues[i] as NSNumber
  77. }
  78. }
  79. struct RAMSlider_Previews: PreviewProvider {
  80. static var previews: some View {
  81. RAMSlider(systemMemory: .constant(1024)) { _ in
  82. }
  83. }
  84. }