|
| 1 | +import { css, html, LitElement, nothing } from 'lit'; |
| 2 | +import { property } from 'lit/decorators.js'; |
| 3 | +import { |
| 4 | + mdiAccessPoint, |
| 5 | + mdiClose, |
| 6 | + mdiPlay, |
| 7 | + mdiPlayBoxMultiple, |
| 8 | + mdiPlaylistPlus, |
| 9 | + mdiSkipNext, |
| 10 | + mdiSkipNextCircle, |
| 11 | +} from '@mdi/js'; |
| 12 | +import { customEvent } from '../utils/utils'; |
| 13 | + |
| 14 | +export type PlayMenuAction = { |
| 15 | + enqueue: 'replace' | 'play' | 'next' | 'add' | 'replace_next'; |
| 16 | + radioMode?: boolean; |
| 17 | +}; |
| 18 | + |
| 19 | +const PLAY_MENU_ACTIONS: PlayMenuAction[] = [ |
| 20 | + { enqueue: 'replace' }, |
| 21 | + { enqueue: 'play', radioMode: true }, |
| 22 | + { enqueue: 'play' }, |
| 23 | + { enqueue: 'next' }, |
| 24 | + { enqueue: 'add' }, |
| 25 | + { enqueue: 'replace_next' }, |
| 26 | +]; |
| 27 | + |
| 28 | +export class PlayMenu extends LitElement { |
| 29 | + @property({ type: Boolean }) disabled = false; |
| 30 | + @property({ type: Boolean }) hasSelection = false; |
| 31 | + /** When true, renders as an already-open dropdown panel instead of a trigger button */ |
| 32 | + @property({ type: Boolean }) inline = false; |
| 33 | + |
| 34 | + render() { |
| 35 | + if (!this.hasSelection) { |
| 36 | + return nothing; |
| 37 | + } |
| 38 | + if (this.inline) { |
| 39 | + return this.renderInlineMenu(); |
| 40 | + } |
| 41 | + return this.renderButtonMenu(); |
| 42 | + } |
| 43 | + |
| 44 | + private renderButtonMenu() { |
| 45 | + return html` |
| 46 | + <ha-button-menu fixed corner="BOTTOM_END" @action=${this.handleAction}> |
| 47 | + <ha-icon-button |
| 48 | + slot="trigger" |
| 49 | + .path=${mdiPlay} |
| 50 | + title="Play options" |
| 51 | + ?disabled=${this.disabled} |
| 52 | + ></ha-icon-button> |
| 53 | + ${this.renderMenuItems()} |
| 54 | + </ha-button-menu> |
| 55 | + `; |
| 56 | + } |
| 57 | + |
| 58 | + private renderInlineMenu() { |
| 59 | + return html` |
| 60 | + <div class="inline-menu" @click=${(e: Event) => e.stopPropagation()}> |
| 61 | + <ha-icon-button class="close-btn" .path=${mdiClose} @click=${this.closeMenu} title="Close"></ha-icon-button> |
| 62 | + ${PLAY_MENU_ACTIONS.map( |
| 63 | + (_action, index) => html` |
| 64 | + <div class="inline-menu-item" @click=${() => this.selectAction(index)}> |
| 65 | + <ha-svg-icon .path=${this.getActionIcon(index)}></ha-svg-icon> |
| 66 | + <span>${this.getActionLabel(index)}</span> |
| 67 | + </div> |
| 68 | + `, |
| 69 | + )} |
| 70 | + </div> |
| 71 | + `; |
| 72 | + } |
| 73 | + |
| 74 | + private renderMenuItems() { |
| 75 | + return html` |
| 76 | + <ha-list-item graphic="icon"> |
| 77 | + Play Now (clear queue) |
| 78 | + <ha-svg-icon slot="graphic" .path=${mdiPlayBoxMultiple}></ha-svg-icon> |
| 79 | + </ha-list-item> |
| 80 | + <ha-list-item graphic="icon"> |
| 81 | + Start Radio |
| 82 | + <ha-svg-icon slot="graphic" .path=${mdiAccessPoint}></ha-svg-icon> |
| 83 | + </ha-list-item> |
| 84 | + <ha-list-item graphic="icon"> |
| 85 | + Play Now |
| 86 | + <ha-svg-icon slot="graphic" .path=${mdiPlay}></ha-svg-icon> |
| 87 | + </ha-list-item> |
| 88 | + <ha-list-item graphic="icon"> |
| 89 | + Play Next |
| 90 | + <ha-svg-icon slot="graphic" .path=${mdiSkipNext}></ha-svg-icon> |
| 91 | + </ha-list-item> |
| 92 | + <ha-list-item graphic="icon"> |
| 93 | + Add to Queue |
| 94 | + <ha-svg-icon slot="graphic" .path=${mdiPlaylistPlus}></ha-svg-icon> |
| 95 | + </ha-list-item> |
| 96 | + <ha-list-item graphic="icon"> |
| 97 | + Play Next (clear queue) |
| 98 | + <ha-svg-icon slot="graphic" .path=${mdiSkipNextCircle}></ha-svg-icon> |
| 99 | + </ha-list-item> |
| 100 | + `; |
| 101 | + } |
| 102 | + |
| 103 | + private getActionIcon(index: number): string { |
| 104 | + return [mdiPlayBoxMultiple, mdiAccessPoint, mdiPlay, mdiSkipNext, mdiPlaylistPlus, mdiSkipNextCircle][index]; |
| 105 | + } |
| 106 | + |
| 107 | + private getActionLabel(index: number): string { |
| 108 | + return [ |
| 109 | + 'Play Now (clear queue)', |
| 110 | + 'Start Radio', |
| 111 | + 'Play Now', |
| 112 | + 'Play Next', |
| 113 | + 'Add to Queue', |
| 114 | + 'Play Next (clear queue)', |
| 115 | + ][index]; |
| 116 | + } |
| 117 | + |
| 118 | + private handleAction(e: CustomEvent) { |
| 119 | + const action = PLAY_MENU_ACTIONS[e.detail.index]; |
| 120 | + if (action) { |
| 121 | + this.dispatchEvent(customEvent('play-menu-action', action)); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private selectAction(index: number) { |
| 126 | + const action = PLAY_MENU_ACTIONS[index]; |
| 127 | + if (action) { |
| 128 | + this.dispatchEvent(customEvent('play-menu-action', action)); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private closeMenu() { |
| 133 | + this.dispatchEvent(customEvent('play-menu-close')); |
| 134 | + } |
| 135 | + |
| 136 | + static styles = css` |
| 137 | + :host { |
| 138 | + display: contents; |
| 139 | + } |
| 140 | + .inline-menu { |
| 141 | + position: relative; |
| 142 | + background: var(--card-background-color, var(--primary-background-color)); |
| 143 | + border: 1px solid var(--divider-color, rgba(255, 255, 255, 0.12)); |
| 144 | + border-radius: 8px; |
| 145 | + box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3); |
| 146 | + min-width: 200px; |
| 147 | + padding: 17px 17px; |
| 148 | + z-index: 10; |
| 149 | + } |
| 150 | + .close-btn { |
| 151 | + position: absolute; |
| 152 | + top: 2px; |
| 153 | + right: 2px; |
| 154 | + --mdc-icon-button-size: 28px; |
| 155 | + --mdc-icon-size: 18px; |
| 156 | + color: var(--secondary-text-color); |
| 157 | + } |
| 158 | + .inline-menu-item { |
| 159 | + display: flex; |
| 160 | + align-items: center; |
| 161 | + gap: 12px; |
| 162 | + padding: 10px 16px; |
| 163 | + cursor: pointer; |
| 164 | + color: var(--primary-text-color); |
| 165 | + font-size: 0.9rem; |
| 166 | + } |
| 167 | + .inline-menu-item:hover { |
| 168 | + background: var(--secondary-background-color); |
| 169 | + } |
| 170 | + .inline-menu-item ha-svg-icon { |
| 171 | + --mdc-icon-size: 20px; |
| 172 | + flex-shrink: 0; |
| 173 | + } |
| 174 | + `; |
| 175 | +} |
| 176 | + |
| 177 | +customElements.define('sonos-play-menu', PlayMenu); |
0 commit comments