Merge pull request #180 from GoogleChrome/fix/hide-casting-button-when-no-cast-target

Only return the casting button when there are devices to cast to.
This commit is contained in:
Derek Herman 2022-01-31 23:22:24 -08:00 коммит произвёл GitHub
Родитель 9a352a1479 3cc1ef6bc8
Коммит cf09a7e664
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 26 добавлений и 6 удалений

Просмотреть файл

@ -46,7 +46,7 @@ import ErrorPage from './js/pages/Error';
* Settings
*/
import { loadSetting } from './js/utils/settings';
import { SETTING_KEY_TOGGLE_OFFLINE, SETTING_KEY_DARK_MODE } from './js/constants';
import { SETTING_KEY_TOGGLE_OFFLINE, SETTING_KEY_DARK_MODE, CAST_BUTTON_HIDDEN_CLASSNAME } from './js/constants';
/**
* Custom Elements definition.
@ -170,16 +170,31 @@ window.kinoInitGoogleCast = (function kinoInitGoogleCastIIFE() {
return () => {
if (!castButtonPromise) {
castButtonPromise = new Promise((resolve) => {
const castButton = document.createElement('button');
const castCustomElement = document.createElement('google-cast-launcher');
castButton.setAttribute('aria-label', 'Cast this video');
castButton.appendChild(castCustomElement);
const applyCastState = () => {
const castState = window.cast.framework.CastContext.getInstance().getCastState();
castButton.classList.toggle(
CAST_BUTTON_HIDDEN_CLASSNAME,
castState === 'NO_DEVICES_AVAILABLE',
);
};
const initCastApi = () => {
window.cast.framework.CastContext.getInstance().setOptions({
receiverApplicationId: window.chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID,
});
const castButton = document.createElement('button');
const castCustomElement = document.createElement('google-cast-launcher');
castButton.setAttribute('aria-label', 'Cast this video');
castButton.appendChild(castCustomElement);
window.cast.framework.CastContext.getInstance().addEventListener(
window.cast.framework.CastContextEventType.CAST_STATE_CHANGED,
applyCastState,
);
applyCastState();
resolve(castButton);
};

Просмотреть файл

@ -137,3 +137,4 @@ export const PIP_CLASSNAME = 'picture-in-picture';
export const CAST_CLASSNAME = 'cast';
export const CAST_HAS_TARGET_NAME = 'cast-has-target';
export const CAST_TARGET_NAME = 'cast-target-name';
export const CAST_BUTTON_HIDDEN_CLASSNAME = 'hidden';

Просмотреть файл

@ -38,6 +38,10 @@
border: none;
}
:host .floating-buttons > button.hidden {
display: none;
}
button google-cast-launcher {
height: 24px;
width: auto;