From 455e24bc2288e06131c33965ac74a0e8e55c1d63 Mon Sep 17 00:00:00 2001 From: Takayama Fumihiko Date: Tue, 31 Oct 2023 23:40:05 +0900 Subject: [PATCH] Update DevicesView to use ConnectedDevices in ForEach instead of $settings.connectedDeviceSettings --- .../src/View/DevicesGamePadSettingsView.swift | 227 +++++++++--------- .../SettingsWindow/src/View/DevicesView.swift | 47 ++-- 2 files changed, 146 insertions(+), 128 deletions(-) diff --git a/src/apps/SettingsWindow/src/View/DevicesGamePadSettingsView.swift b/src/apps/SettingsWindow/src/View/DevicesGamePadSettingsView.swift index 240d1abd9..6e1f27e7e 100644 --- a/src/apps/SettingsWindow/src/View/DevicesGamePadSettingsView.swift +++ b/src/apps/SettingsWindow/src/View/DevicesGamePadSettingsView.swift @@ -1,109 +1,114 @@ import SwiftUI struct DevicesGamePadSettingsView: View { - @Binding var connectedDeviceSetting: LibKrbn.ConnectedDeviceSetting + let connectedDevice: LibKrbn.ConnectedDevice @Binding var showing: Bool + @ObservedObject private var settings = LibKrbn.Settings.shared + @State var connectedDeviceSetting: LibKrbn.ConnectedDeviceSetting? var body: some View { ZStack(alignment: .topLeading) { VStack(alignment: .leading, spacing: 12.0) { - Text( - "\(connectedDeviceSetting.connectedDevice.productName) (\(connectedDeviceSetting.connectedDevice.manufacturerName))" - ) - .padding(.leading, 40) - .padding(.top, 20) - - GroupBox(label: Text("Deadzone")) { - VStack(alignment: .leading) { - HStack { - Toggle(isOn: $connectedDeviceSetting.gamePadOverwriteXYStickDeadzone) { - Text("Overwrite XY stick deadzone: ") - .frame(maxWidth: .infinity, alignment: .leading) + if let s = connectedDeviceSetting { + let binding = Binding { + s + } set: { + connectedDeviceSetting = $0 + } + Text("\(connectedDevice.productName) (\(connectedDevice.manufacturerName))") + .padding(.leading, 40) + .padding(.top, 20) + + GroupBox(label: Text("Deadzone")) { + VStack(alignment: .leading) { + HStack { + Toggle(isOn: binding.gamePadOverwriteXYStickDeadzone) { + Text("Overwrite XY stick deadzone: ") + .frame(maxWidth: .infinity, alignment: .leading) + } + .switchToggleStyle(controlSize: .mini, font: .callout) + .frame(width: 250.0) + + HStack(alignment: .center, spacing: 8.0) { + Text("Deadzone:") + + DoubleTextField( + value: binding.gamePadXYStickDeadzone, + range: 0.05...1, + step: 0.01, + maximumFractionDigits: 3, + width: 60) + + Text("(Default: 0.1)") + } + .padding(.leading, 20) + .disabled(!s.gamePadOverwriteXYStickDeadzone) + + Spacer() } - .switchToggleStyle(controlSize: .mini, font: .callout) - .frame(width: 250.0) - - HStack(alignment: .center, spacing: 8.0) { - Text("Deadzone:") - - DoubleTextField( - value: $connectedDeviceSetting.gamePadXYStickDeadzone, - range: 0.05...1, - step: 0.01, - maximumFractionDigits: 3, - width: 60) - Text("(Default: 0.1)") + HStack { + Toggle(isOn: binding.gamePadOverwriteWheelsStickDeadzone) { + Text("Overwrite Wheels stick deadzone: ") + .frame(maxWidth: .infinity, alignment: .leading) + } + .switchToggleStyle(controlSize: .mini, font: .callout) + .frame(width: 250.0) + + HStack(alignment: .center, spacing: 8.0) { + Text("Deadzone:") + + DoubleTextField( + value: binding.gamePadWheelsStickDeadzone, + range: 0.05...1, + step: 0.01, + maximumFractionDigits: 3, + width: 60) + + Text("(Default: 0.1)") + } + .padding(.leading, 20) + .disabled(!s.gamePadOverwriteWheelsStickDeadzone) } - .padding(.leading, 20) - .disabled(!connectedDeviceSetting.gamePadOverwriteXYStickDeadzone) - - Spacer() - } + }.padding() + } - HStack { - Toggle(isOn: $connectedDeviceSetting.gamePadOverwriteWheelsStickDeadzone) { - Text("Overwrite Wheels stick deadzone: ") - .frame(maxWidth: .infinity, alignment: .leading) + GroupBox(label: Text("Stick parameters")) { + VStack(alignment: .leading) { + HStack { + Toggle( + isOn: binding + .gamePadOverwriteStickStrokeAccelerationTransitionDurationMilliseconds + ) { + Text("Overwrite stick stroke acceleration transition duration: ") + .frame(maxWidth: .infinity, alignment: .leading) + } + .switchToggleStyle(controlSize: .mini, font: .callout) + .frame(width: 250.0) + + HStack(alignment: .center, spacing: 8.0) { + Text("Duration:") + + IntTextField( + value: binding.gamePadStickStrokeAccelerationTransitionDurationMilliseconds, + range: 0...10000, + step: 100, + width: 60) + + Text("milliseconds (Default: 500)") + } + .padding(.leading, 20) + .disabled( + !s.gamePadOverwriteStickStrokeAccelerationTransitionDurationMilliseconds) + + Spacer() } - .switchToggleStyle(controlSize: .mini, font: .callout) - .frame(width: 250.0) - - HStack(alignment: .center, spacing: 8.0) { - Text("Deadzone:") - - DoubleTextField( - value: $connectedDeviceSetting.gamePadWheelsStickDeadzone, - range: 0.05...1, - step: 0.01, - maximumFractionDigits: 3, - width: 60) - Text("(Default: 0.1)") - } - .padding(.leading, 20) - .disabled(!connectedDeviceSetting.gamePadOverwriteWheelsStickDeadzone) - } - }.padding() - } - - GroupBox(label: Text("Stick parameters")) { - VStack(alignment: .leading) { - HStack { - Toggle( - isOn: $connectedDeviceSetting - .gamePadOverwriteStickStrokeAccelerationTransitionDurationMilliseconds - ) { - Text("Overwrite stick stroke acceleration transition duration: ") - .frame(maxWidth: .infinity, alignment: .leading) + Toggle(isOn: binding.gamePadSwapSticks) { + Text("Swap gamepad XY and wheels sticks") } .switchToggleStyle(controlSize: .mini, font: .callout) - .frame(width: 250.0) - - HStack(alignment: .center, spacing: 8.0) { - Text("Duration:") - - IntTextField( - value: $connectedDeviceSetting - .gamePadStickStrokeAccelerationTransitionDurationMilliseconds, - range: 0...10000, - step: 100, - width: 60) - - Text("milliseconds (Default: 500)") - } - .padding(.leading, 20) - .disabled( - !connectedDeviceSetting - .gamePadOverwriteStickStrokeAccelerationTransitionDurationMilliseconds) - - Spacer() } - - Toggle(isOn: $connectedDeviceSetting.gamePadSwapSticks) { - Text("Swap gamepad XY and wheels sticks") - } - .switchToggleStyle(controlSize: .mini, font: .callout) } } @@ -116,33 +121,41 @@ struct DevicesGamePadSettingsView: View { } .padding() .frame(width: 1000, height: 600) + .onAppear { + setConnectedDeviceSetting() + } + .onChange(of: settings.connectedDeviceSettings) { _ in + setConnectedDeviceSetting() + } + } + + private func setConnectedDeviceSetting() { + connectedDeviceSetting = settings.findConnectedDeviceSetting(connectedDevice) } } struct DevicesGamePadSettingsView_Previews: PreviewProvider { - @State static var connectedDeviceSetting = LibKrbn.ConnectedDeviceSetting( - LibKrbn.ConnectedDevice( - index: 0, - manufacturerName: "", - productName: "", - transport: "", - vendorId: 0, - productId: 0, - deviceAddress: nil, - isKeyboard: false, - isPointingDevice: false, - isGamePad: true, - isBuiltInKeyboard: false, - isBuiltInTrackpad: false, - isBuiltInTouchBar: false, - isAppleDevice: false - ) + @State static var connectedDevice = LibKrbn.ConnectedDevice( + index: 0, + manufacturerName: "", + productName: "", + transport: "", + vendorId: 0, + productId: 0, + deviceAddress: nil, + isKeyboard: false, + isPointingDevice: false, + isGamePad: true, + isBuiltInKeyboard: false, + isBuiltInTrackpad: false, + isBuiltInTouchBar: false, + isAppleDevice: false ) @State static var showing = true static var previews: some View { DevicesGamePadSettingsView( - connectedDeviceSetting: $connectedDeviceSetting, + connectedDevice: connectedDevice, showing: $showing) } } diff --git a/src/apps/SettingsWindow/src/View/DevicesView.swift b/src/apps/SettingsWindow/src/View/DevicesView.swift index 9ff9c83bd..e1d5ba265 100644 --- a/src/apps/SettingsWindow/src/View/DevicesView.swift +++ b/src/apps/SettingsWindow/src/View/DevicesView.swift @@ -7,19 +7,19 @@ struct DevicesView: View { VStack(alignment: .leading, spacing: 12.0) { List { VStack(alignment: .leading, spacing: 0.0) { - ForEach($settings.connectedDeviceSettings) { $connectedDeviceSetting in + ForEach(LibKrbn.ConnectedDevices.shared.connectedDevices) { connectedDevice in VStack(alignment: .leading, spacing: 0.0) { - DeviceName(connectedDevice: connectedDeviceSetting.connectedDevice) + DeviceName(connectedDevice: connectedDevice) VStack(alignment: .leading, spacing: 0.0) { - ModifyEventsSetting(connectedDevice: connectedDeviceSetting.connectedDevice) + ModifyEventsSetting(connectedDevice: connectedDevice) VStack(alignment: .leading, spacing: 12.0) { - KeyboardSettings(connectedDevice: connectedDeviceSetting.connectedDevice) + KeyboardSettings(connectedDevice: connectedDevice) - MouseSettings(connectedDevice: connectedDeviceSetting.connectedDevice) + MouseSettings(connectedDevice: connectedDevice) - GamePadSettings(connectedDeviceSetting: $connectedDeviceSetting) + GamePadSettings(connectedDevice: connectedDevice) } .padding(.leading, 20.0) .padding(.top, 8.0) @@ -33,7 +33,9 @@ struct DevicesView: View { RoundedRectangle(cornerRadius: 8) .stroke( Color(NSColor.selectedControlColor), - lineWidth: connectedDeviceSetting.modifyEvents ? 3 : 0 + lineWidth: settings.findConnectedDeviceSetting(connectedDevice)?.modifyEvents + ?? false + ? 3 : 0 ) .padding(2) ) @@ -304,24 +306,27 @@ struct DevicesView: View { } struct GamePadSettings: View { - @Binding var connectedDeviceSetting: LibKrbn.ConnectedDeviceSetting + let connectedDevice: LibKrbn.ConnectedDevice @State var showing = false var body: some View { - if connectedDeviceSetting.connectedDevice.isGamePad { - Button( - action: { - showing = true - }, - label: { - Label("Open game pad settings", systemImage: "gamecontroller") - .buttonLabelStyle() - } - ) - .sheet(isPresented: $showing) { - DevicesGamePadSettingsView( - connectedDeviceSetting: $connectedDeviceSetting, showing: $showing + VStack { + if connectedDevice.isGamePad { + Button( + action: { + showing = true + }, + label: { + Label("Open game pad settings", systemImage: "gamecontroller") + .buttonLabelStyle() + } ) + .sheet(isPresented: $showing) { + DevicesGamePadSettingsView( + connectedDevice: connectedDevice, + showing: $showing + ) + } } } }