diff --git a/OpenParsec/ParsecView.swift b/OpenParsec/ParsecView.swift index f45d533..99b4b67 100644 --- a/OpenParsec/ParsecView.swift +++ b/OpenParsec/ParsecView.swift @@ -176,6 +176,14 @@ class ParsecSession: ObservableObject { } } +struct MenuRowsHeightKey: PreferenceKey { + static let defaultValue: CGFloat = 0 + + static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { + value = max(value, nextValue()) + } +} + struct ParsecView: View { var controller: ContentView? @@ -189,15 +197,24 @@ struct ParsecView: View { @AppStorage("keyboardPanHintShown") var keyboardPanHintShown: Bool = false @State var hint: String? @State var showMenu: Bool = false + @State var menuRowsHeight: CGFloat = 0 + @ScaledMetric var menuWidth: CGFloat = 230 @AppStorage("menuSwipeHintShown") var menuSwipeHintShown: Bool = false @State var showKeyboard: Bool = false @State var zoomEnabled: Bool = false + @Environment(\.verticalSizeClass) var verticalSizeClass @State var muted: Bool = false @State var preferH265: Bool = true @State var isReconnecting: Bool = false @State var connectionLost: Bool = false + @State var showSettings: Bool = false + @State var decoderAtOpen: DecoderPref = .h264 + @State var fpsAtOpen: Int = 0 + @State var pipAtOpen: Bool = false + @State var resolutionAtOpen: ParsecResolution = .client + @State var bitrateAtOpen: Int = 0 @State var resolutions: [ParsecResolution] @State var bitrates: [Int] @@ -286,159 +303,63 @@ struct ParsecView: View { .zIndex(3) } - VStack { - if !hideOverlay { - HStack { - Button(action: { - if showMenu { - showMenu = false - } else { - showMenu = true - ParsecView.getHostUserData() + HStack(alignment: .top, spacing: 0) { + VStack { + if !hideOverlay { + HStack { + Button(action: { + if showMenu { + showMenu = false + } else { + showMenu = true + ParsecView.getHostUserData() + } + }) { + Image("IconTransparent") + .resizable() + .aspectRatio(contentMode: .fit) + .frame(width: 48, height: 48) + .background(Rectangle().fill(Color("BackgroundPrompt").opacity(showMenu ? 0.75 : 1))) + .cornerRadius(8) + .opacity(showMenu ? 1 : 0.25) } - }) { - Image("IconTransparent") - .resizable() - .aspectRatio(contentMode: .fit) - .frame(width: 48, height: 48) - .background(Rectangle().fill(Color("BackgroundPrompt").opacity(showMenu ? 0.75 : 1))) - .cornerRadius(8) - .opacity(showMenu ? 1 : 0.25) - } - .padding() - .edgesIgnoringSafeArea(.all) - Spacer() - } - } - if !hideKeyboardButton { - HStack { - Button(action: toggleKeyboard) { - Image(systemName: "keyboard") - .resizable() - .aspectRatio(contentMode: .fit) - .frame(width: 32, height: 32) - .foregroundColor(Color("Foreground")) - .padding(8) - .background(Rectangle().fill(Color("BackgroundPrompt").opacity(showKeyboard ? 0.75 : 0.5))) - .cornerRadius(8) + .padding() + .edgesIgnoringSafeArea(.all) + Spacer() } - .padding(.leading) - .edgesIgnoringSafeArea(.all) - Spacer() } - } - if showMenu { - HStack { - VStack(spacing: 3) { - Button(action: disableOverlay) { - Text("Hide Buttons") - .padding(8) - .frame(maxWidth: .infinity) - .multilineTextAlignment(.center) - } - Button(action: toggleMute) { - Text("Sound: \(muted ? "OFF" : "ON")") - .padding(8) - .frame(maxWidth: .infinity) - .multilineTextAlignment(.center) - } - Menu { - ForEach(resolutions, id: \.self) { resolution in - Button(action: { - changeResolution(res: resolution) - }) { - if resolution.width == dataModel.resolutionX && resolution.height == dataModel.resolutionY { - Label(resolution.desc, systemImage: "checkmark") - } else { - Text(resolution.desc) - } - } - } - } label: { - Text("Resolution") - .padding(8) - .frame(maxWidth: .infinity) - .multilineTextAlignment(.center) - } - Menu { - Button(action: { - changeBitRate(bitrate: 0) - }) { - if settingsStore.bitrate == 0 { - Label("Host Default", systemImage: "checkmark") - } else { - Text("Host Default") - } - } - ForEach(bitrates, id: \.self) { bitrate in - Button(action: { - changeBitRate(bitrate: bitrate) - }) { - if bitrate == settingsStore.bitrate { - Label("\(bitrate) Mbps", systemImage: "checkmark") - } else { - Text("\(bitrate) Mbps") - } - } - } - } label: { - Text("Bitrate") + if !hideKeyboardButton { + HStack { + Button(action: toggleKeyboard) { + Image(systemName: "keyboard") + .resizable() + .aspectRatio(contentMode: .fit) + .frame(width: 32, height: 32) + .foregroundColor(Color("Foreground")) .padding(8) - .frame(maxWidth: .infinity) - .multilineTextAlignment(.center) - } - if DataManager.model.displayConfigs.count > 1 { - Menu { - Button("Auto") { - changeDisplay(displayId: "none") - } - ForEach(DataManager.model.displayConfigs, id: \.self) { config in - Button("\(config.name) \(config.adapterName)") { - changeDisplay(displayId: config.id) - } - } - } label: { - Text("Switch Display") - .padding(8) - .frame(maxWidth: .infinity) - .multilineTextAlignment(.center) - } - } - - Button(action: toggleConstantFps) { - Text("Constant Frame Rate: \(dataModel.constantFps ? "ON" : "OFF")") - .padding(8) - .frame(maxWidth: .infinity) - .multilineTextAlignment(.center) - } - Button(action: toggleZoom) { - Text("Pinch to Zoom: \(zoomEnabled ? "ON" : "OFF")") - .padding(8) - .frame(maxWidth: .infinity) - .multilineTextAlignment(.center) - } - Rectangle() - .fill(Color("Foreground")) - .opacity(0.25) - .frame(height: 1) - Button(action: { disconnect()}) { - Text("Disconnect") - .foregroundColor(.red) - .padding(8) - .frame(maxWidth: .infinity) - .multilineTextAlignment(.center) + .background(Rectangle().fill(Color("BackgroundPrompt").opacity(showKeyboard ? 0.75 : 0.5))) + .cornerRadius(8) } + .padding(.leading) + .edgesIgnoringSafeArea(.all) + Spacer() + } + } + if showMenu && verticalSizeClass != .compact { + HStack { + menuPanel + Spacer() } - .background(Rectangle().fill(Color("BackgroundPrompt").opacity(0.75))) - .foregroundColor(Color("Foreground")) - .frame(maxWidth: 230) - .cornerRadius(8) - .padding(.horizontal) - // .edgesIgnoringSafeArea(.all) - Spacer() + .layoutPriority(1) } + Spacer() + } + .fixedSize(horizontal: showMenu && verticalSizeClass == .compact, vertical: false) + if showMenu && verticalSizeClass == .compact { + menuPanel + .padding(.top) + Spacer() } - Spacer() } .zIndex(2) } @@ -451,14 +372,13 @@ struct ParsecView: View { return } showDCAlert = false - isReconnecting = true - ParsecBackgroundManager.shared.reconnectAttempts = 0 - ParsecBackgroundManager.shared.reconnectStartTime = Date() - parsecViewController.resetKeyState() - CParsec.reconnect(peerId) + reconnectTo(peerId) }), secondaryButton: .cancel(Text("Close"), action: {disconnect()})) } + .onChange(of: showSettings) { open in + if !open { applyStreamSettings() } + } .onChange(of: isReconnecting) { reconnecting in if !reconnecting { CParsec.setMuted(muted) @@ -475,9 +395,140 @@ struct ParsecView: View { disconnect(isBackgroundDisconnect: true) } .edgesIgnoringSafeArea(.all) + .overlay(SettingsView(visible: $showSettings)) } + var menuPanel: some View { + VStack(spacing: 3) { + ScrollView { + VStack(spacing: 3) { + Button(action: disableOverlay) { + Text("Hide Buttons") + .padding(8) + .frame(maxWidth: .infinity) + .multilineTextAlignment(.center) + } + Button(action: toggleMute) { + Text("Sound: \(muted ? "OFF" : "ON")") + .padding(8) + .frame(maxWidth: .infinity) + .multilineTextAlignment(.center) + } + Menu { + ForEach(resolutions, id: \.self) { resolution in + Button(action: { + changeResolution(res: resolution) + }) { + if resolution.width == dataModel.resolutionX && resolution.height == dataModel.resolutionY { + Label(resolution.desc, systemImage: "checkmark") + } else { + Text(resolution.desc) + } + } + } + } label: { + Text("Resolution") + .padding(8) + .frame(maxWidth: .infinity) + .multilineTextAlignment(.center) + } + Menu { + Button(action: { + changeBitRate(bitrate: 0) + }) { + if settingsStore.bitrate == 0 { + Label("Host Default", systemImage: "checkmark") + } else { + Text("Host Default") + } + } + ForEach(bitrates, id: \.self) { bitrate in + Button(action: { + changeBitRate(bitrate: bitrate) + }) { + if bitrate == settingsStore.bitrate { + Label("\(bitrate) Mbps", systemImage: "checkmark") + } else { + Text("\(bitrate) Mbps") + } + } + } + } label: { + Text("Bitrate") + .padding(8) + .frame(maxWidth: .infinity) + .multilineTextAlignment(.center) + } + if DataManager.model.displayConfigs.count > 1 { + Menu { + Button("Auto") { + changeDisplay(displayId: "none") + } + ForEach(DataManager.model.displayConfigs, id: \.self) { config in + Button("\(config.name) \(config.adapterName)") { + changeDisplay(displayId: config.id) + } + } + } label: { + Text("Switch Display") + .padding(8) + .frame(maxWidth: .infinity) + .multilineTextAlignment(.center) + } + } + + Button(action: toggleConstantFps) { + Text("Constant Frame Rate: \(dataModel.constantFps ? "ON" : "OFF")") + .padding(8) + .frame(maxWidth: .infinity) + .multilineTextAlignment(.center) + } + Button(action: toggleZoom) { + Text("Pinch to Zoom: \(zoomEnabled ? "ON" : "OFF")") + .padding(8) + .frame(maxWidth: .infinity) + .multilineTextAlignment(.center) + } + Button(action: toggleTouchMode) { + Text("Touch Mode: \(settingsStore.cursorMode == .direct ? "Direct" : "Touchpad")") + .padding(8) + .frame(maxWidth: .infinity) + .multilineTextAlignment(.center) + } + Button(action: openSettings) { + Text("Settings") + .padding(8) + .frame(maxWidth: .infinity) + .multilineTextAlignment(.center) + } + } + .background(GeometryReader { proxy in + Color.clear.preference(key: MenuRowsHeightKey.self, value: proxy.size.height) + }) + } + .frame(maxHeight: menuRowsHeight) + .onPreferenceChange(MenuRowsHeightKey.self) { menuRowsHeight = $0 } + Rectangle() + .fill(Color("Foreground")) + .opacity(0.25) + .frame(height: 1) + Button(action: { disconnect()}) { + Text("Disconnect") + .foregroundColor(.red) + .padding(8) + .frame(maxWidth: .infinity) + .multilineTextAlignment(.center) + } + } + .background(Rectangle().fill(Color("BackgroundPrompt").opacity(0.75))) + .foregroundColor(Color("Foreground")) + .frame(maxWidth: menuWidth) + .cornerRadius(8) + .padding(.horizontal) + // .edgesIgnoringSafeArea(.all) + } + func post() { ParsecBackgroundManager.shared.onShouldDisconnect = { NotificationCenter.default.post(name: NSNotification.Name("ParsecBackgroundDisconnect"), object: nil) @@ -562,6 +613,62 @@ struct ParsecView: View { parsecViewController.setKeyboardVisible(showKeyboard) } + func openSettings() { + decoderAtOpen = SettingsHandler.decoder + fpsAtOpen = SettingsHandler.preferredFramesPerSecond + pipAtOpen = SettingsHandler.enablePiP + resolutionAtOpen = SettingsHandler.resolution + bitrateAtOpen = SettingsHandler.bitrate + showMenu = false + if showKeyboard { toggleKeyboard() } + withAnimation(.easeInOut(duration: 0.24)) { showSettings = true } + } + + func applyStreamSettings() { + zoomEnabled = SettingsHandler.zoomEnabled + parsecViewController.setZoomEnabled(zoomEnabled) + parsecViewController.updatePanHandle() + parsecViewController.positionCursorOverlay() + hideOverlay = SettingsHandler.noOverlay + hideKeyboardButton = !SettingsHandler.showKeyboardButton + parsecViewController.updateMenuEdgeSwipe(menuHidden: hideOverlay) + offerHint() + + var hostConfigChanged = false + if SettingsHandler.resolution != resolutionAtOpen { + DataManager.model.resolutionX = SettingsHandler.resolution.width + DataManager.model.resolutionY = SettingsHandler.resolution.height + hostConfigChanged = true + } + if SettingsHandler.constantFps != DataManager.model.constantFps { + DataManager.model.constantFps = SettingsHandler.constantFps + hostConfigChanged = true + } + if hostConfigChanged { CParsec.updateHostVideoConfig() } + if SettingsHandler.bitrate != bitrateAtOpen { applyBitrate() } + if SettingsHandler.preferredFramesPerSecond != fpsAtOpen { parsecViewController.applyFrameRate() } + if SettingsHandler.enablePiP != pipAtOpen { + ParsecBackgroundManager.shared.reconnectPending = true + disconnect() + } else if SettingsHandler.decoder != decoderAtOpen { + reconnectToApply() + } + } + + func reconnectToApply() { + guard let peerId = ParsecBackgroundManager.shared.lastPeerId else { return } + appNote("[reconnect] to apply settings") + reconnectTo(peerId) + } + + func reconnectTo(_ peerId: String) { + isReconnecting = true + ParsecBackgroundManager.shared.reconnectAttempts = 0 + ParsecBackgroundManager.shared.reconnectStartTime = Date() + parsecViewController.resetKeyState() + CParsec.reconnect(peerId) + } + func disableOverlay() { hideOverlay = true hideKeyboardButton = true @@ -686,6 +793,13 @@ struct ParsecView: View { } } + func toggleTouchMode() { + DispatchQueue.main.async { + SettingsHandler.cursorMode = SettingsHandler.cursorMode == .direct ? .touchpad : .direct + parsecViewController.updatePanHandle() + } + } + func changeDisplay(displayId: String) { DispatchQueue.main.async { DataManager.model.output = displayId diff --git a/OpenParsec/ParsecViewController.swift b/OpenParsec/ParsecViewController.swift index 8e92791..3115f95 100644 --- a/OpenParsec/ParsecViewController.swift +++ b/OpenParsec/ParsecViewController.swift @@ -52,7 +52,7 @@ class ParsecViewController: UIViewController, UIScrollViewDelegate, ParsecTouchI let staleMoveSlop: CGFloat = 24.0 // the moving finger must travel this far before a ghost is swept var twoFingerDidMove = false // true once a 2-finger gesture became a pinch/scroll (suppresses right-click) - var mouseSensitivity: Float = Float(SettingsHandler.mouseSensitivity) + var mouseSensitivity: Float { Float(SettingsHandler.mouseSensitivity) } // Two-finger scroll-wheel accumulation, kept separate from cursor movement. var lastScrollTranslation: CGPoint = .zero var accumulatedScrollY: Float = 0.0 @@ -200,6 +200,12 @@ class ParsecViewController: UIViewController, UIScrollViewDelegate, ParsecTouchI } } + func applyFrameRate() { + let fps = SettingsHandler.preferredFramesPerSecond == 0 ? UIScreen.main.maximumFramesPerSecond : SettingsHandler.preferredFramesPerSecond + (glkView as? ParsecMetalViewController)?.metalView.preferredFramesPerSecond = fps + (glkView as? ParsecGLKViewController)?.glkViewController.preferredFramesPerSecond = fps + } + override func viewDidLoad() { // ScrollView Setup scrollView = UIScrollView(frame: view.bounds) diff --git a/OpenParsec/SettingsView.swift b/OpenParsec/SettingsView.swift index efc0d84..97e55b6 100644 --- a/OpenParsec/SettingsView.swift +++ b/OpenParsec/SettingsView.swift @@ -52,130 +52,132 @@ struct SettingsView: View { .zIndex(1) ScrollView { - CatTitle("Touch & Mouse") - CatList { - CatItem("Touch Mode") { - MultiPicker(selection: $settings.cursorMode, options: - [ - Choice("Touchpad", CursorMode.touchpad), - Choice("Direct", CursorMode.direct) - ]) - } - CatItem("Right Click") { - MultiPicker(selection: $settings.rightClickPosition, options: - [ - Choice("First Finger", RightClickPosition.firstFinger), - Choice("Middle", RightClickPosition.middle), - Choice("Second Finger", RightClickPosition.secondFinger) - ]) - } - CatSlider("Pointer Speed", value: $settings.mouseSensitivity, in: 0.1...4, step: 0.1) - CatSlider("Cursor Size", value: $settings.cursorScale, in: 0.1...4, step: 0.1) - CatSlider("Scroll Speed", value: $settings.scrollSensitivity, in: 0.5...5, step: 0.5) - CatItem("Reverse Scrolling") { - Toggle("", isOn: $settings.reverseScrollDirection) - .frame(width: 80) - } - CatItem("Pinch to Zoom") { - Toggle("", isOn: $settings.zoomEnabled) - .frame(width: 80) - } - } - CatTitle("Keyboard") - CatList { - CatItem("Show Keyboard Button") { - Toggle("", isOn: $settings.showKeyboardButton) - .frame(width: 80) - } - CatItem("Option Key as Command") { - Toggle("", isOn: $settings.optionAsCommand) - .frame(width: 80) - } - } - CatTitle("Video") - CatList { - CatItem("Renderer") { - SegmentPicker(selection: $settings.renderer, options: [ - Choice("OpenGL", RendererType.opengl), - Choice("Metal", RendererType.metal) - ]) - .frame(width: 165) - } - CatItem("Resolution") { - MultiPicker(selection: $settings.resolution, options: resolutionChoices) - } - CatItem("Bitrate") { - MultiPicker(selection: $settings.bitrate, options: [Choice("Host Default", 0)] + ParsecResolution.bitrates.map { Choice("\($0) Mbps", $0) }) - } - CatItem("Frame Rate") { - MultiPicker(selection: $settings.preferredFramesPerSecond, options: - [ - Choice("Auto (Device Max)", 0), - Choice("120 FPS", 120), - Choice("60 FPS", 60), - Choice("30 FPS", 30) - ]) - } - CatItem("Constant Frame Rate") { - Toggle("", isOn: $settings.constantFps) - .frame(width: 80) - } - CatItem("Codec") { - MultiPicker(selection: $settings.decoder, options: - [ - Choice("H.264", DecoderPref.h264), - Choice("Prefer H.265", DecoderPref.h265) - ]) - } - } - CatTitle("During a Stream") - CatList { - CatItem("Show Stream Stats") { - Toggle("", isOn: $settings.alwaysShowStatus) - .frame(width: 80) - } - CatItem("Show Menu Button") { - Toggle("", isOn: Binding(get: { !settings.noOverlay }, set: { settings.noOverlay = !$0 })) - .frame(width: 80) - } - CatItem("Menu Edge Swipe") { - MultiPicker(selection: $settings.menuSwipeEdge, options: - [ - Choice("Off", MenuSwipeEdge.off), - Choice("Left Edge", MenuSwipeEdge.left), - Choice("Right Edge", MenuSwipeEdge.right) - ]) - } - CatItem(UIDevice.current.userInterfaceIdiom == .pad ? "Hide iPad Status Bar" : "Hide iPhone Status Bar") { - Toggle("", isOn: $settings.hideStatusBar) - .frame(width: 80) + VStack { + CatTitle("Touch & Mouse") + CatList { + CatItem("Touch Mode") { + MultiPicker(selection: $settings.cursorMode, options: + [ + Choice("Touchpad", CursorMode.touchpad), + Choice("Direct", CursorMode.direct) + ]) + } + CatItem("Right Click") { + MultiPicker(selection: $settings.rightClickPosition, options: + [ + Choice("First Finger", RightClickPosition.firstFinger), + Choice("Middle", RightClickPosition.middle), + Choice("Second Finger", RightClickPosition.secondFinger) + ]) + } + CatSlider("Pointer Speed", value: $settings.mouseSensitivity, in: 0.1...4, step: 0.1) + CatSlider("Cursor Size", value: $settings.cursorScale, in: 0.1...4, step: 0.1) + CatSlider("Scroll Speed", value: $settings.scrollSensitivity, in: 0.5...5, step: 0.5) + CatItem("Reverse Scrolling") { + Toggle("", isOn: $settings.reverseScrollDirection) + .frame(width: 80) + } + CatItem("Pinch to Zoom") { + Toggle("", isOn: $settings.zoomEnabled) + .frame(width: 80) + } } - CatItem("Picture in Picture") { - Toggle("", isOn: $settings.enablePiP) - .frame(width: 80) + CatTitle("Keyboard") + CatList { + CatItem("Show Keyboard Button") { + Toggle("", isOn: $settings.showKeyboardButton) + .frame(width: 80) + } + CatItem("Option Key as Command") { + Toggle("", isOn: $settings.optionAsCommand) + .frame(width: 80) + } } - CatItem("Start Muted") { - Toggle("", isOn: $settings.startMuted) - .frame(width: 80) + CatTitle("Video") + CatList { + CatItem("Renderer") { + SegmentPicker(selection: $settings.renderer, options: [ + Choice("OpenGL", RendererType.opengl), + Choice("Metal", RendererType.metal) + ]) + .frame(width: 165) + } + CatItem("Resolution") { + MultiPicker(selection: $settings.resolution, options: resolutionChoices) + } + CatItem("Bitrate") { + MultiPicker(selection: $settings.bitrate, options: [Choice("Host Default", 0)] + ParsecResolution.bitrates.map { Choice("\($0) Mbps", $0) }) + } + CatItem("Frame Rate") { + MultiPicker(selection: $settings.preferredFramesPerSecond, options: + [ + Choice("Auto (Device Max)", 0), + Choice("120 FPS", 120), + Choice("60 FPS", 60), + Choice("30 FPS", 30) + ]) + } + CatItem("Constant Frame Rate") { + Toggle("", isOn: $settings.constantFps) + .frame(width: 80) + } + CatItem("Codec") { + MultiPicker(selection: $settings.decoder, options: + [ + Choice("H.264", DecoderPref.h264), + Choice("Prefer H.265", DecoderPref.h265) + ]) + } } - } - CatTitle("Developer") - CatList { - CatItem("Auto Reconnect") { - Toggle("", isOn: $settings.autoReconnect) - .frame(width: 80) + CatTitle("During a Stream") + CatList { + CatItem("Show Stream Stats") { + Toggle("", isOn: $settings.alwaysShowStatus) + .frame(width: 80) + } + CatItem("Show Menu Button") { + Toggle("", isOn: Binding(get: { !settings.noOverlay }, set: { settings.noOverlay = !$0 })) + .frame(width: 80) + } + CatItem("Menu Edge Swipe") { + MultiPicker(selection: $settings.menuSwipeEdge, options: + [ + Choice("Off", MenuSwipeEdge.off), + Choice("Left Edge", MenuSwipeEdge.left), + Choice("Right Edge", MenuSwipeEdge.right) + ]) + } + CatItem(UIDevice.current.userInterfaceIdiom == .pad ? "Hide iPad Status Bar" : "Hide iPhone Status Bar") { + Toggle("", isOn: $settings.hideStatusBar) + .frame(width: 80) + } + CatItem("Picture in Picture") { + Toggle("", isOn: $settings.enablePiP) + .frame(width: 80) + } + CatItem("Start Muted") { + Toggle("", isOn: $settings.startMuted) + .frame(width: 80) + } } - if settings.autoReconnect { - CatItem("Show Reconnect Errors") { - Toggle("", isOn: $settings.errorPrompts) + CatTitle("Developer") + CatList { + CatItem("Auto Reconnect") { + Toggle("", isOn: $settings.autoReconnect) .frame(width: 80) } + if settings.autoReconnect { + CatItem("Show Reconnect Errors") { + Toggle("", isOn: $settings.errorPrompts) + .frame(width: 80) + } + } } + Text(getVersionInfo()) + .multilineTextAlignment(.center) + .opacity(0.5) + .padding() } - Text(getVersionInfo()) - .multilineTextAlignment(.center) - .opacity(0.5) - .padding() } .foregroundColor(Color("Foreground")) }