Skip to content

Commit

Permalink
Update DevicesView to use ConnectedDevices in ForEach instead of $set…
Browse files Browse the repository at this point in the history
…tings.connectedDeviceSettings
  • Loading branch information
tekezo committed Oct 31, 2023
1 parent efbde30 commit 455e24b
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 128 deletions.
227 changes: 120 additions & 107 deletions src/apps/SettingsWindow/src/View/DevicesGamePadSettingsView.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

Expand All @@ -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)
}
}
47 changes: 26 additions & 21 deletions src/apps/SettingsWindow/src/View/DevicesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
)
Expand Down Expand Up @@ -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
)
}
}
}
}
Expand Down

0 comments on commit 455e24b

Please sign in to comment.