Skip to content

Commit

Permalink
Use external keyboard to toggle widgets on/off.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Feb 3, 2025
1 parent e05b240 commit 8b06516
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Moblin/Various/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5992,6 +5992,15 @@ final class Model: NSObject, ObservableObject, @unchecked Sendable {
}
}

private func toggleWidgetOnOff(id: UUID) {
guard let widget = findWidget(id: id) else {
return
}
widget.enabled!.toggle()
reloadSpeechToText()
sceneUpdated()
}

func sceneUpdated(imageEffectChanged: Bool = false, attachCamera: Bool = false) {
if imageEffectChanged {
reloadImageEffects()
Expand Down Expand Up @@ -6107,6 +6116,8 @@ final class Model: NSObject, ObservableObject, @unchecked Sendable {
toggleBlackScreen()
case .scene:
selectScene(id: key.sceneId)
case .widget:
toggleWidgetOnOff(id: key.widgetId!)
}
updateButtonStates()
return .handled
Expand Down
10 changes: 10 additions & 0 deletions Moblin/Various/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2330,6 +2330,7 @@ enum SettingsKeyboardKeyFunction: String, Codable, CaseIterable {
case torch = "Torch"
case blackScreen = "Black screen"
case scene = "Scene"
case widget = "Widget"

public init(from decoder: Decoder) throws {
var value = try decoder.singleValueContainer().decode(RawValue.self)
Expand All @@ -2355,6 +2356,8 @@ enum SettingsKeyboardKeyFunction: String, Codable, CaseIterable {
return .blackScreen
case String(localized: "Scene"):
return .scene
case String(localized: "Widget"):
return .widget
default:
return .unused
}
Expand All @@ -2376,6 +2379,8 @@ enum SettingsKeyboardKeyFunction: String, Codable, CaseIterable {
return String(localized: "Black screen")
case .scene:
return String(localized: "Scene")
case .widget:
return String(localized: "Widget")
}
}
}
Expand All @@ -2387,6 +2392,7 @@ class SettingsKeyboardKey: Codable, Identifiable {
var key: String = ""
var function: SettingsKeyboardKeyFunction = .unused
var sceneId: UUID = .init()
var widgetId: UUID? = .init()
}

class SettingsKeyboard: Codable {
Expand Down Expand Up @@ -4546,5 +4552,9 @@ final class Settings {
scene.overrideVideoStabilizationMode = false
store()
}
for key in realDatabase.keyboard!.keys where key.widgetId == nil {
key.widgetId = .init()
store()
}
}
}
19 changes: 19 additions & 0 deletions Moblin/View/Settings/Keyboard/KeyboardKeySettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ struct KeyboardKeySettingsView: View {
var key: SettingsKeyboardKey
@State var selection: String
@State var sceneSelection: UUID
@State var widgetSelection: UUID

private func onFunctionChange(function: String) {
selection = function
Expand Down Expand Up @@ -49,6 +50,24 @@ struct KeyboardKeySettingsView: View {
Text("Scene")
}
}
if key.function == .widget {
Section {
Picker("", selection: $widgetSelection) {
ForEach(model.database.widgets) { widget in
Text(widget.name)
.tag(widget.id)
}
}
.onChange(of: widgetSelection) { widgetId in
key.widgetId = widgetId
model.objectWillChange.send()
}
.pickerStyle(.inline)
.labelsHidden()
} header: {
Text("Widget")
}
}
}
.navigationTitle("Game controller button")
}
Expand Down
3 changes: 2 additions & 1 deletion Moblin/View/Settings/Keyboard/KeyboardSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ struct KeyboardSettingsView: View {
KeyboardKeySettingsView(
key: key,
selection: key.function.toString(),
sceneSelection: key.sceneId
sceneSelection: key.sceneId,
widgetSelection: key.widgetId!
)
} label: {
HStack {
Expand Down

0 comments on commit 8b06516

Please sign in to comment.