From 7bfad4e7032b3fc244a1511a502411c343890ba2 Mon Sep 17 00:00:00 2001 From: Takayama Fumihiko Date: Tue, 31 Oct 2023 21:39:28 +0900 Subject: [PATCH] Update ModifyEventsSetting to use ConnectedDevice --- .../SettingsWindow/src/View/DevicesView.swift | 52 +++++++++++++------ src/apps/share/swift/LibKrbn/Settings.swift | 2 +- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/apps/SettingsWindow/src/View/DevicesView.swift b/src/apps/SettingsWindow/src/View/DevicesView.swift index 8202af4a6..2f61f8e68 100644 --- a/src/apps/SettingsWindow/src/View/DevicesView.swift +++ b/src/apps/SettingsWindow/src/View/DevicesView.swift @@ -12,7 +12,7 @@ struct DevicesView: View { DeviceName(connectedDevice: connectedDeviceSetting.connectedDevice) VStack(alignment: .leading, spacing: 0.0) { - ModifyEventsSetting(connectedDeviceSetting: $connectedDeviceSetting) + ModifyEventsSetting(connectedDevice: connectedDeviceSetting.connectedDevice) VStack(alignment: .leading, spacing: 12.0) { KeyboardSettings(connectedDeviceSetting: $connectedDeviceSetting) @@ -77,31 +77,39 @@ struct DevicesView: View { } struct ModifyEventsSetting: View { - @Binding var connectedDeviceSetting: LibKrbn.ConnectedDeviceSetting + let connectedDevice: LibKrbn.ConnectedDevice @ObservedObject private var settings = LibKrbn.Settings.shared + @State var connectedDeviceSetting: LibKrbn.ConnectedDeviceSetting? var body: some View { HStack(alignment: .top) { - if connectedDeviceSetting.connectedDevice.isAppleDevice, - !connectedDeviceSetting.connectedDevice.isKeyboard, + if connectedDevice.isAppleDevice, + !connectedDevice.isKeyboard, !settings.unsafeUI { Text("Apple pointing devices are not supported") .foregroundColor(Color(NSColor.placeholderTextColor)) .frame(maxWidth: .infinity, alignment: .leading) } else { - Toggle(isOn: $connectedDeviceSetting.modifyEvents) { - Text("Modify events") + if let c = connectedDeviceSetting { + let binding = Binding { + c + } set: { + connectedDeviceSetting = $0 + } + Toggle(isOn: binding.modifyEvents) { + Text("Modify events") + } + .switchToggleStyle() + .frame(width: 140.0) } - .switchToggleStyle() - .frame(width: 140.0) } Spacer() - if connectedDeviceSetting.connectedDevice.transport != "FIFO" { + if connectedDevice.transport != "FIFO" { VStack(alignment: .trailing, spacing: 4.0) { - if connectedDeviceSetting.connectedDevice.vendorId != 0 { + if connectedDevice.vendorId != 0 { HStack(alignment: .firstTextBaseline, spacing: 0) { Spacer() @@ -110,13 +118,13 @@ struct DevicesView: View { Text( String( format: "%5d (0x%04x)", - connectedDeviceSetting.connectedDevice.vendorId, - connectedDeviceSetting.connectedDevice.vendorId) + connectedDevice.vendorId, + connectedDevice.vendorId) ) } } - if connectedDeviceSetting.connectedDevice.productId != 0 { + if connectedDevice.productId != 0 { HStack(alignment: .center, spacing: 0) { Spacer() @@ -125,25 +133,35 @@ struct DevicesView: View { Text( String( format: "%5d (0x%04x)", - connectedDeviceSetting.connectedDevice.productId, - connectedDeviceSetting.connectedDevice.productId) + connectedDevice.productId, + connectedDevice.productId) ) } } - if !connectedDeviceSetting.connectedDevice.deviceAddress.isEmpty { + if !connectedDevice.deviceAddress.isEmpty { HStack(alignment: .center, spacing: 0) { Spacer() Text("Device Address: ") - Text(connectedDeviceSetting.connectedDevice.deviceAddress) + Text(connectedDevice.deviceAddress) } } } .font(.custom("Menlo", size: 12.0)) } } + .onAppear { + setConnectedDeviceSetting() + } + .onChange(of: settings.connectedDeviceSettings) { _ in + setConnectedDeviceSetting() + } + } + + private func setConnectedDeviceSetting() { + connectedDeviceSetting = settings.findConnectedDeviceSetting(connectedDevice) } } diff --git a/src/apps/share/swift/LibKrbn/Settings.swift b/src/apps/share/swift/LibKrbn/Settings.swift index ee3bf27ed..9e295358c 100644 --- a/src/apps/share/swift/LibKrbn/Settings.swift +++ b/src/apps/share/swift/LibKrbn/Settings.swift @@ -504,7 +504,7 @@ extension LibKrbn { -> ConnectedDeviceSetting? { for connectedDeviceSetting in connectedDeviceSettings - where connectedDeviceSetting.connectedDevice.id == connectedDevice.id { + where connectedDeviceSetting.connectedDevice == connectedDevice { return connectedDeviceSetting }