Refactor browser & userAgent's capabilities

This commit is contained in:
redphx 2024-07-06 15:53:01 +07:00
parent b6746598a3
commit 2b63edb7eb
14 changed files with 48 additions and 25 deletions

View File

@ -283,7 +283,7 @@ function main() {
getPref(PrefKey.AUDIO_ENABLE_VOLUME_CONTROL) && patchAudioContext();
getPref(PrefKey.BLOCK_TRACKING) && patchMeControl();
STATES.userAgentHasTouchSupport && TouchController.updateCustomList();
STATES.userAgent.capabilities.touch && TouchController.updateCustomList();
overridePreloadState();
VibrationManager.initialSetup();

View File

@ -41,7 +41,7 @@ export class GameBar {
this.actions = [
new ScreenshotAction(),
...(STATES.userAgentHasTouchSupport && (getPref(PrefKey.STREAM_TOUCH_CONTROLLER) !== 'off') ? [new TouchControlAction()] : []),
...(STATES.userAgent.capabilities.touch && (getPref(PrefKey.STREAM_TOUCH_CONTROLLER) !== 'off') ? [new TouchControlAction()] : []),
new MicrophoneAction(),
];

View File

@ -683,7 +683,7 @@ let PATCH_ORDERS: PatchArray = [
'remotePlayKeepAlive',
'remotePlayDirectConnectUrl',
'remotePlayDisableAchievementToast',
STATES.userAgentHasTouchSupport && 'patchUpdateInputConfigurationAsync',
STATES.userAgent.capabilities.touch && 'patchUpdateInputConfigurationAsync',
] : []),
...(BX_FLAGS.EnableXcloudLogging ? [
@ -709,7 +709,7 @@ let PLAYING_PATCH_ORDERS: PatchArray = [
// Skip feedback dialog
getPref(PrefKey.STREAM_DISABLE_FEEDBACK_DIALOG) && 'skipFeedbackDialog',
...(STATES.userAgentHasTouchSupport ? [
...(STATES.userAgent.capabilities.touch ? [
getPref(PrefKey.STREAM_TOUCH_CONTROLLER) === 'all' && 'patchShowSensorControls',
getPref(PrefKey.STREAM_TOUCH_CONTROLLER) === 'all' && 'exposeTouchLayoutManager',
(getPref(PrefKey.STREAM_TOUCH_CONTROLLER) === 'off' || getPref(PrefKey.STREAM_TOUCH_CONTROLLER_AUTO_OFF)) && 'disableTakRenderer',

View File

@ -91,7 +91,7 @@ export class StreamBadges {
let batteryLevel = '100%';
let batteryLevelInt = 100;
let isCharging = false;
if ('getBattery' in navigator) {
if (STATES.browser.capabilities.batteryApi) {
try {
const bm = await (navigator as NavigatorBattery).getBattery();
isCharging = bm.charging;
@ -224,7 +224,7 @@ export class StreamBadges {
// Battery
let batteryLevel = '';
if ('getBattery' in navigator) {
if (STATES.browser.capabilities.batteryApi) {
batteryLevel = '100%';
}
@ -338,7 +338,7 @@ export class StreamBadges {
// Get battery level
try {
'getBattery' in navigator && (navigator as NavigatorBattery).getBattery().then(bm => {
STATES.browser.capabilities.batteryApi && (navigator as NavigatorBattery).getBattery().then(bm => {
streamBadges.startBatteryLevel = Math.round(bm.level * 100);
});
} catch(e) {}

View File

@ -103,7 +103,7 @@ export class StreamSettings {
}],
},
STATES.userAgentHasTouchSupport && {
STATES.userAgent.capabilities.touch && {
group: 'touch-controller',
label: t('touch-controller'),
items: [{

View File

@ -35,7 +35,7 @@ function cloneStreamHudButton($orgButton: HTMLElement, label: string, svgIcon: t
}
};
if (STATES.browserHasTouchSupport) {
if (STATES.browser.capabilities.touch) {
$container.addEventListener('transitionstart', onTransitionStart);
$container.addEventListener('transitionend', onTransitionEnd);
}

View File

@ -66,8 +66,8 @@ const SETTINGS_UI = {
},
[t('touch-controller')]: {
note: !STATES.userAgentHasTouchSupport ? '⚠️ ' + t('device-unsupported-touch') : null,
unsupported: !STATES.userAgentHasTouchSupport,
note: !STATES.userAgent.capabilities.touch ? '⚠️ ' + t('device-unsupported-touch') : null,
unsupported: !STATES.userAgent.capabilities.touch,
items: [
PrefKey.STREAM_TOUCH_CONTROLLER,
PrefKey.STREAM_TOUCH_CONTROLLER_AUTO_OFF,

View File

@ -26,8 +26,10 @@ export function localRedirect(path: string) {
$anchor.click();
}
export function setupStreamUi() {
StreamSettings.getInstance();
onChangeVideoPlayerType();
}
(window as any).localRedirect = localRedirect;

14
src/types/index.d.ts vendored
View File

@ -28,8 +28,18 @@ type BxStates = {
appContext: any | null;
serverRegions: any;
userAgentHasTouchSupport: boolean;
browserHasTouchSupport: boolean;
browser: {
capabilities: {
touch: boolean;
batteryApi: boolean;
};
};
userAgent: {
capabilities: {
touch: boolean;
};
};
currentStream: Partial<{
titleId: string;

View File

@ -34,7 +34,7 @@ export const BxExposed = {
titleInfo.details.hasMkbSupport = supportedInputTypes.includes(InputType.MKB);
if (STATES.userAgentHasTouchSupport) {
if (STATES.userAgent.capabilities.touch) {
let touchControllerAvailability = getPref(PrefKey.STREAM_TOUCH_CONTROLLER);
// Disable touch control when gamepad found

View File

@ -16,8 +16,19 @@ export const STATES: BxStates = {
isPlaying: false,
appContext: {},
serverRegions: {},
userAgentHasTouchSupport: userAgentHasTouchSupport,
browserHasTouchSupport: browserHasTouchSupport,
browser: {
capabilities: {
touch: browserHasTouchSupport,
batteryApi: 'getBattery' in window.navigator,
},
},
userAgent: {
capabilities: {
touch: userAgentHasTouchSupport,
}
},
currentStream: {},
remotePlay: {},

View File

@ -595,7 +595,7 @@ export function interceptHttpRequests() {
}
// Add list of games with custom layouts to the official list
if (STATES.userAgentHasTouchSupport && url.includes('catalog.gamepass.com/sigls/')) {
if (STATES.userAgent.capabilities.touch && url.includes('catalog.gamepass.com/sigls/')) {
const response = await NATIVE_FETCH(request, init);
const obj = await response.clone().json();

View File

@ -268,7 +268,7 @@ export class Preferences {
all: t('tc-all-games'),
off: t('off'),
},
unsupported: !STATES.userAgentHasTouchSupport,
unsupported: !STATES.userAgent.capabilities.touch,
ready: (setting: PreferenceSetting) => {
if (setting.unsupported) {
setting.default = 'default';
@ -278,7 +278,7 @@ export class Preferences {
[PrefKey.STREAM_TOUCH_CONTROLLER_AUTO_OFF]: {
label: t('tc-auto-off'),
default: false,
unsupported: !STATES.userAgentHasTouchSupport,
unsupported: !STATES.userAgent.capabilities.touch,
},
[PrefKey.STREAM_TOUCH_CONTROLLER_DEFAULT_OPACITY]: {
type: SettingElementType.NUMBER_STEPPER,
@ -292,7 +292,7 @@ export class Preferences {
ticks: 10,
hideSlider: true,
},
unsupported: !STATES.userAgentHasTouchSupport,
unsupported: !STATES.userAgent.capabilities.touch,
},
[PrefKey.STREAM_TOUCH_CONTROLLER_STYLE_STANDARD]: {
label: t('tc-standard-layout-style'),
@ -302,7 +302,7 @@ export class Preferences {
white: t('tc-all-white'),
muted: t('tc-muted-colors'),
},
unsupported: !STATES.userAgentHasTouchSupport,
unsupported: !STATES.userAgent.capabilities.touch,
},
[PrefKey.STREAM_TOUCH_CONTROLLER_STYLE_CUSTOM]: {
label: t('tc-custom-layout-style'),
@ -311,7 +311,7 @@ export class Preferences {
default: t('default'),
muted: t('tc-muted-colors'),
},
unsupported: !STATES.userAgentHasTouchSupport,
unsupported: !STATES.userAgent.capabilities.touch,
},
[PrefKey.STREAM_SIMPLIFY_MENU]: {
@ -554,7 +554,7 @@ export class Preferences {
[PrefKey.UI_HOME_CONTEXT_MENU_DISABLED]: {
label: t('disable-home-context-menu'),
default: STATES.browserHasTouchSupport,
default: STATES.browser.capabilities.touch,
},
[PrefKey.BLOCK_SOCIAL_FEATURES]: {

View File

@ -24,7 +24,7 @@ export function overridePreloadState() {
}
// Add list of games with custom layouts to the official list
if (STATES.userAgentHasTouchSupport) {
if (STATES.userAgent.capabilities.touch) {
try {
const sigls = state.xcloud.sigls;
if (GamePassCloudGallery.TOUCH in sigls) {