Don't poll gamepad input when the GUI is not focused/visible

This commit is contained in:
Cameron Gutman 2024-09-26 19:24:29 -05:00
parent 9b3d4c1ad7
commit 2a63ad53d7
5 changed files with 43 additions and 21 deletions

View file

@ -2,7 +2,6 @@ import QtQuick 2.0
import QtQuick.Controls 2.2
import ComputerManager 1.0
import SdlGamepadKeyNavigation 1.0
Item {
function onSearchingComputer() {
@ -39,10 +38,6 @@ Item {
if (!launcher.isExecuted()) {
toolBar.visible = false
// Normally this is enabled by PcView, but we will won't
// load PcView when streaming from the command-line.
SdlGamepadKeyNavigation.enable()
launcher.searchingComputer.connect(onSearchingComputer)
launcher.pairing.connect(onPairing)
launcher.failed.connect(onFailed)

View file

@ -2,7 +2,6 @@ import QtQuick 2.0
import QtQuick.Controls 2.2
import ComputerManager 1.0
import SdlGamepadKeyNavigation 1.0
Item {
function onSearchingComputer() {
@ -38,10 +37,6 @@ Item {
if (!launcher.isExecuted()) {
toolBar.visible = false
// Normally this is enabled by PcView, but we will won't
// load PcView when streaming from the command-line.
SdlGamepadKeyNavigation.enable()
launcher.searchingComputer.connect(onSearchingComputer)
launcher.searchingApp.connect(onSearchingApp)
launcher.sessionCreated.connect(onSessionCreated)

View file

@ -7,7 +7,6 @@ import ComputerModel 1.0
import ComputerManager 1.0
import StreamingPreferences 1.0
import SystemProperties 1.0
import SdlGamepadKeyNavigation 1.0
CenteredGridView {
property ComputerModel computerModel : createModel()
@ -34,11 +33,6 @@ CenteredGridView {
// Setup signals on CM
ComputerManager.computerAddCompleted.connect(addComplete)
// This is a bit of a hack to do this here as opposed to main.qml, but
// we need it enabled before calling getConnectedGamepads() and PcView
// is never destroyed, so it should be okay.
SdlGamepadKeyNavigation.enable()
// Highlight the first item if a gamepad is connected
if (currentIndex == -1 && SdlGamepadKeyNavigation.getConnectedGamepads() > 0) {
currentIndex = 0

View file

@ -76,8 +76,10 @@ Item {
streamSegueErrorDialog.text += "\n\n" + qsTr("This PC's Internet connection is blocking Moonlight. Streaming over the Internet may not work while connected to this network.")
}
// Enable GUI gamepad usage now
if (window.gamepadInputActive) {
// Re-enable GUI gamepad usage now
SdlGamepadKeyNavigation.enable()
}
if (quitAfter) {
if (streamSegueErrorDialog.text) {
@ -119,9 +121,11 @@ Item {
// Show the toolbar again when popped off the stack
toolBar.visible = true
// Enable GUI gamepad usage now
if (window.gamepadInputActive) {
// Re-enable GUI gamepad usage now
SdlGamepadKeyNavigation.enable()
}
}
StackView.onActivated: {
// Hide the toolbar before we start loading

View file

@ -12,6 +12,7 @@ import SdlGamepadKeyNavigation 1.0
ApplicationWindow {
property bool pollingActive: false
property bool gamepadInputActive: false
// Set by SettingsView to force the back operation to pop all
// pages except the initial view. This is required when doing
@ -22,7 +23,7 @@ ApplicationWindow {
width: 1280
height: 600
Component.onCompleted: {
function doEarlyInit() {
// Override the background color to Material 2 colors for Qt 6.5+
// in order to improve contrast between GFE's placeholder box art
// and the background of the app grid.
@ -30,6 +31,11 @@ ApplicationWindow {
Material.background = "#303030"
}
SdlGamepadKeyNavigation.enable()
gamepadInputActive = true
}
Component.onCompleted: {
// Show the window according to the user's preferences
if (SystemProperties.hasDesktopEnvironment) {
if (StreamingPreferences.uiDisplayMode == StreamingPreferences.UI_MAXIMIZED) {
@ -85,6 +91,14 @@ ApplicationWindow {
anchors.fill: parent
focus: true
onEmptyChanged: {
// Hijack this callback to perform our very early init
// that runs before the first StackView item is pushed
if (!empty) {
doEarlyInit();
}
}
onCurrentItemChanged: {
// Ensure focus travels to the next view when going back
if (currentItem) {
@ -147,6 +161,11 @@ ApplicationWindow {
ComputerManager.stopPollingAsync()
pollingActive = false
}
if (gamepadInputActive) {
SdlGamepadKeyNavigation.disable()
gamepadInputActive = false
}
}
else if (active) {
// When we become visible and active again, start polling
@ -157,6 +176,10 @@ ApplicationWindow {
ComputerManager.startPolling()
pollingActive = true
}
if (!gamepadInputActive) {
SdlGamepadKeyNavigation.enable()
gamepadInputActive = true
}
}
}
@ -170,11 +193,22 @@ ApplicationWindow {
ComputerManager.startPolling()
pollingActive = true
}
if (!gamepadInputActive) {
SdlGamepadKeyNavigation.enable()
gamepadInputActive = true
}
}
else {
// Start the inactivity timer to stop polling
// if focus does not return within a few minutes.
inactivityTimer.restart()
// Immediately stop gamepad input since we aren't
// the active window anymore.
if (gamepadInputActive) {
SdlGamepadKeyNavigation.disable()
gamepadInputActive = false
}
}
}