Skip to content

Commit

Permalink
Add eraseNotConnectedDeviceSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jul 28, 2024
1 parent 94b21d3 commit 1573d70
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/apps/SettingsWindow/src/View/DevicesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import SwiftUI
struct DevicesView: View {
@ObservedObject private var settings = LibKrbn.Settings.shared
@ObservedObject private var connectedDevices = LibKrbn.ConnectedDevices.shared
@State private var showEraseNotConnectedDeviceSettingsButton = false

var body: some View {
VStack(alignment: .leading, spacing: 12.0) {
Expand Down Expand Up @@ -53,6 +54,46 @@ struct DevicesView: View {
}
}
.background(Color(NSColor.textBackgroundColor))

if settings.notConnectedDeviceSettingsCount > 0 {
HStack {
Text(
"There are \(settings.notConnectedDeviceSettingsCount) other settings for devices that are not currently connected"
)

Spacer()

if !showEraseNotConnectedDeviceSettingsButton {
Button(
action: {
showEraseNotConnectedDeviceSettingsButton = true
},
label: {
Image(systemName: "trash")
.buttonLabelStyle()
}
)
} else {
Button(
role: .destructive,
action: {
settings.eraseNotConnectedDeviceSettings()
},
label: {
Label(
"Remove settings for \(settings.notConnectedDeviceSettingsCount) devices",
systemImage: "trash"
)
.buttonLabelStyle()
}
)
.deleteButtonStyle()
}
}
.padding()
.foregroundColor(Color.infoForeground)
.background(Color.infoBackground)
}
}
.padding()
}
Expand Down
12 changes: 12 additions & 0 deletions src/apps/share/swift/LibKrbn/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ extension LibKrbn {
//

@Published var connectedDeviceSettings: [ConnectedDeviceSetting] = []
@Published var notConnectedDeviceSettingsCount: Int = 0

private func updateConnectedDeviceSettings() {
var newConnectedDeviceSettings: [ConnectedDeviceSetting] = []
Expand All @@ -528,6 +529,9 @@ extension LibKrbn {
}

connectedDeviceSettings = newConnectedDeviceSettings

notConnectedDeviceSettingsCount =
libkrbn_core_configuration_get_selected_profile_not_connected_devices_count()
}

public func findConnectedDeviceSetting(_ connectedDevice: ConnectedDevice)
Expand All @@ -541,6 +545,14 @@ extension LibKrbn {
return nil
}

public func eraseNotConnectedDeviceSettings() {
libkrbn_core_configuration_erase_selected_profile_not_connected_devices()

updateConnectedDeviceSettings()

save()
}

@Published var delayMillisecondsBeforeOpenDevice: Int = 0 {
didSet {
if didSetEnabled {
Expand Down
1 change: 1 addition & 0 deletions src/lib/libkrbn/include/libkrbn/libkrbn.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ bool libkrbn_core_configuration_get_selected_profile_device_game_pad_swap_sticks
void libkrbn_core_configuration_set_selected_profile_device_game_pad_swap_sticks(const libkrbn_device_identifiers* device_identifiers,
bool value);

size_t libkrbn_core_configuration_get_selected_profile_not_connected_devices_count(void);
void libkrbn_core_configuration_erase_selected_profile_not_connected_devices(void);

// game_pad_xy_stick_continued_movement_absolute_magnitude_threshold
Expand Down
11 changes: 11 additions & 0 deletions src/lib/libkrbn/src/libkrbn_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,17 @@ void libkrbn_core_configuration_set_selected_profile_device_game_pad_swap_sticks
d->set_game_pad_swap_sticks(value);
}

size_t libkrbn_core_configuration_get_selected_profile_not_connected_devices_count(void) {
if (auto manager = libkrbn_cpp::get_components_manager()) {
if (auto connected_devices = manager->get_current_connected_devices()) {
auto c = get_current_core_configuration();
return c->get_selected_profile().not_connected_devices_count(*connected_devices);
}
}

return 0;
}

void libkrbn_core_configuration_erase_selected_profile_not_connected_devices(void) {
if (auto manager = libkrbn_cpp::get_components_manager()) {
if (auto connected_devices = manager->get_current_connected_devices()) {
Expand Down

0 comments on commit 1573d70

Please sign in to comment.