From 0623037e5de6c4c2ea5cb2a3ae2d1528af06648a Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 12 Nov 2026 09:54:45 +0100 Subject: [PATCH] Add large three-button remaining and elapsed timers --- .../media-player-classic/README.md | 27 +++++++++++ .../media-player-classic/companion/HELP.md | 12 +++++ .../media-player-classic/src/definitions.js | 28 ++++++++++- .../media-player-classic/src/feedbacks.js | 30 ++++++++++++ .../media-player-classic/src/state.js | 27 +++++++++++ .../media-player-classic/test/state.test.js | 46 +++++++++++++++++++ 6 files changed, 169 insertions(+), 1 deletion(-) diff --git a/companion-modules/media-player-classic/README.md b/companion-modules/media-player-classic/README.md index eb68c74..681c474 100644 --- a/companion-modules/media-player-classic/README.md +++ b/companion-modules/media-player-classic/README.md @@ -97,6 +97,33 @@ with the player, follows seeks, and does not predict wall-clock completion at nonstandard playback speeds. Playback state is MPC's reported state, not proof that frames or audio are physically reaching an output device. +## Large three-button timers + +Open **Presets → MPC_DEV → Timers — large HH / MM / SS**. Place **Remaining HH +(1/3)**, **Remaining MM (2/3)**, and **Remaining SS (3/3)** in three adjacent slots, +left to right. For example, one hour, two minutes and three seconds displays: + +| HH | MM | SS | +| --- | --- | --- | +| 01 | 02 | 03 | + +There is a matching **Elapsed** set. Inspired by Ontime's separate timer-component +presets, each button shows only large auto-sized digits on a dark background, +with Companion's standard location bar. Buttons are passive; pressing them does +not control playback. The hours field does not wrap at 24 hours. + +Remaining time rounds up once before splitting; elapsed time rounds down. Both +follow the same player snapshots as the existing timers and freeze when paused. +All remaining digits turn amber at 10 seconds left and red at 5 seconds left. +Elapsed digits stay white. Offline/idle displays grey **-- | -- | --**; unknown +duration also makes the remaining timer unavailable. Elapsed time still works +when position is known but duration is not. + +Custom buttons can use `remaining_hh`, `remaining_mm`, `remaining_ss`, +`elapsed_hh`, `elapsed_mm`, and `elapsed_ss`. These are zero-padded display strings; +existing total-second variables are unchanged. Backup equivalents have `backup_` +prefixes. The new presets follow the main player. + ## Playlist — direct playback **Presets → MPC_DEV → Playlist** contains **Play item 1–10**. Each sends MPC-HC's diff --git a/companion-modules/media-player-classic/companion/HELP.md b/companion-modules/media-player-classic/companion/HELP.md index b3b319f..601988a 100644 --- a/companion-modules/media-player-classic/companion/HELP.md +++ b/companion-modules/media-player-classic/companion/HELP.md @@ -30,6 +30,18 @@ while playing, full red while stopped, and amber while paused. Timers stay dark with progress bars; backup sync keeps its original status colors. Re-add copied presets to apply styling updates; linked presets update automatically. +## Large timers + +In **Timers — large HH / MM / SS**, place **Remaining HH (1/3)**, **Remaining MM +(2/3)**, and **Remaining SS (3/3)** next to one another. A matching **Elapsed** set +is available. These passive buttons show large zero-padded digits, following the +main player's position; no playback commands are sent when pressed. + +Remaining digits turn amber at 10 seconds left and red at 5 seconds. Unavailable +values show grey **--**. Remaining rounds up, elapsed rounds down, and hours do +not wrap at 24. Custom display variables are `remaining_hh/mm/ss` and +`elapsed_hh/mm/ss` (each suffix is a separate variable). + ## Playlist — direct playback The **Playlist** folder has **Play item 1–10** presets. These directly select and diff --git a/companion-modules/media-player-classic/src/definitions.js b/companion-modules/media-player-classic/src/definitions.js index c0e5680..bdee22b 100644 --- a/companion-modules/media-player-classic/src/definitions.js +++ b/companion-modules/media-player-classic/src/definitions.js @@ -202,7 +202,28 @@ export function presets(label = 'MPC_DEV') { ] }), ) - return { ...transport, ...playlist, ...playPlaylist, ...catalog } + const timers = Object.fromEntries( + ['remaining', 'elapsed'].flatMap((mode) => + ['hh', 'mm', 'ss'].map((part, index) => [ + `timer_${mode}_${part}`, + { + type: 'simple', + name: `${mode === 'remaining' ? 'Remaining' : 'Elapsed'} ${part.toUpperCase()} (${index + 1}/3)`, + style: { + text: `$(${label}:${mode}_${part})`, + size: 'auto', + color: colors.white, + bgcolor: colors.dark, + show_topbar: true, + alignment: 'center:center', + }, + steps: [{ down: [], up: [] }], + feedbacks: [{ feedbackId: 'split_timer_color', options: { mode } }], + }, + ]), + ), + ) + return { ...transport, ...playlist, ...playPlaylist, ...timers, ...catalog } } export function presetSections(buttons) { @@ -229,6 +250,11 @@ export function presetSections(buttons) { name: 'Playlist', definitions: Object.keys(buttons).filter((id) => id.startsWith('playlist_play_item_')), }, + { + id: 'timers', + name: 'Timers — large HH / MM / SS', + definitions: Object.keys(buttons).filter((id) => id.startsWith('timer_')), + }, { id: 'commands', name: 'All commands', definitions: Object.keys(commands) }, ] } diff --git a/companion-modules/media-player-classic/src/feedbacks.js b/companion-modules/media-player-classic/src/feedbacks.js index 2830a83..6f3acc1 100644 --- a/companion-modules/media-player-classic/src/feedbacks.js +++ b/companion-modules/media-player-classic/src/feedbacks.js @@ -176,6 +176,36 @@ export function feedbacks(getState, getSync = () => null, getLoadStatus = () => affectedProperties: ['imageBuffer'], callback: (feedback) => transportBorder(getState(), feedback.image, feedback.options.control), }, + split_timer_color: { + type: 'advanced', + name: 'Split timer digit color', + options: [ + { + type: 'dropdown', + id: 'mode', + label: 'Timer', + default: 'remaining', + choices: [ + { id: 'remaining', label: 'Remaining' }, + { id: 'elapsed', label: 'Elapsed' }, + ], + }, + ], + affectedProperties: ['color'], + callback: (feedback) => { + const state = getState() + if (!state || state.state === 'idle' || (feedback.options.mode === 'remaining' && state.remaining == null)) + return { color: 0x889099 } + return { + color: + feedback.options.mode === 'remaining' && state.remaining <= 5000 + ? colors.red + : feedback.options.mode === 'remaining' && state.remaining <= 10000 + ? colors.amber + : colors.white, + } + }, + }, progress_bar: { type: 'advanced', name: 'Playback progress bar', diff --git a/companion-modules/media-player-classic/src/state.js b/companion-modules/media-player-classic/src/state.js index 570b26d..c869d8d 100644 --- a/companion-modules/media-player-classic/src/state.js +++ b/companion-modules/media-player-classic/src/state.js @@ -61,6 +61,17 @@ export function clock(milliseconds, roundUp = false) { return hours ? `${hours}:${minutes}:${remainder}` : `${minutes}:${remainder}` } +export function clockParts(milliseconds, roundUp = false) { + if (milliseconds === null || milliseconds === undefined) return { hh: '--', mm: '--', ss: '--' } + // Round once before splitting so all three buttons agree at minute/hour boundaries. + const seconds = Math.max(0, (roundUp ? Math.ceil : Math.floor)(milliseconds / 1000)) + return { + hh: String(Math.floor(seconds / 3600)).padStart(2, '0'), + mm: String(Math.floor(seconds / 60) % 60).padStart(2, '0'), + ss: String(seconds % 60).padStart(2, '0'), + } +} + export const variableDefinitions = Object.fromEntries( Object.entries({ connected: 'Status data is available', @@ -75,6 +86,12 @@ export const variableDefinitions = Object.fromEntries( elapsed: 'Elapsed media time', duration: 'Media duration', remaining: 'Remaining media time', + remaining_hh: 'Remaining hours, padded (split timer)', + remaining_mm: 'Remaining minutes within hour, padded (split timer)', + remaining_ss: 'Remaining seconds within minute, padded (split timer)', + elapsed_hh: 'Elapsed hours, padded (split timer)', + elapsed_mm: 'Elapsed minutes within hour, padded (split timer)', + elapsed_ss: 'Elapsed seconds within minute, padded (split timer)', elapsed_seconds: 'Elapsed media time in seconds', duration_seconds: 'Media duration in seconds', remaining_seconds: 'Remaining media time in seconds', @@ -85,7 +102,17 @@ export const variableDefinitions = Object.fromEntries( ) export function variables(state) { + const available = state && state.state !== 'idle' return { + ...Object.fromEntries( + Object.entries(clockParts(available ? state.remaining : null, true)).map(([part, value]) => [ + `remaining_${part}`, + value, + ]), + ), + ...Object.fromEntries( + Object.entries(clockParts(available ? state.position : null)).map(([part, value]) => [`elapsed_${part}`, value]), + ), connected: !!state, state: state?.state ?? 'offline', state_label: state ? state.state[0].toUpperCase() + state.state.slice(1) : 'Offline', diff --git a/companion-modules/media-player-classic/test/state.test.js b/companion-modules/media-player-classic/test/state.test.js index 3551141..fb6c31d 100644 --- a/companion-modules/media-player-classic/test/state.test.js +++ b/companion-modules/media-player-classic/test/state.test.js @@ -100,3 +100,49 @@ test('transport outlines leave the center clear, thicken when active, and turn g assert.deepEqual(pixel(render({ muted: true }, 'mute'), 0, 0), [186, 41, 54, 255]) assert.deepEqual(transportBorder(null, undefined, 'play'), {}) }) + +test('split timers share rounded time across boundaries, preserve long hours, and clear unavailable values', () => { + const parts = (values, mode) => ['hh', 'mm', 'ss'].map((part) => values[`${mode}_${part}`]) + for (const [milliseconds, expected] of [ + [0, ['00', '00', '00']], + [1, ['00', '00', '01']], + [59999, ['00', '01', '00']], + [3599999, ['01', '00', '00']], + [3600001, ['01', '00', '01']], + [90061000, ['25', '01', '01']], + ]) { + const data = normalizeStatus({ ...status, duration: milliseconds || 1, position: milliseconds ? 0 : 1 }) + assert.deepEqual(parts(variables(data), 'remaining'), expected) + } + assert.deepEqual(parts(variables(normalizeStatus({ ...status, position: 3599999, duration: 4000000 })), 'elapsed'), [ + '00', + '59', + '59', + ]) + assert.deepEqual(parts(variables(null), 'remaining'), ['--', '--', '--']) + assert.deepEqual(parts(variables(normalizeStatus({ ...status, state: -1 })), 'elapsed'), ['--', '--', '--']) + const unknown = variables(normalizeStatus({ ...status, duration: 0 })) + assert.deepEqual(parts(unknown, 'remaining'), ['--', '--', '--']) + assert.deepEqual(parts(unknown, 'elapsed'), ['00', '00', '12']) +}) + +test('split timer presets are passive and countdown urgency follows remaining time', () => { + const buttons = presets('CLOCK') + for (const mode of ['remaining', 'elapsed']) + for (const part of ['hh', 'mm', 'ss']) { + const button = buttons[`timer_${mode}_${part}`] + assert.equal(button.style.text, `$(CLOCK:${mode}_${part})`) + assert.deepEqual(button.steps[0].down, []) + } + for (const [remaining, color] of [ + [10001, 0xffffff], + [10000, 0xe09b20], + [5000, 0xba2936], + [0, 0xba2936], + ]) { + const defs = feedbacks(() => ({ state: 'playing', remaining })) + assert.equal(defs.split_timer_color.callback({ options: { mode: 'remaining' } }).color, color) + assert.equal(defs.split_timer_color.callback({ options: { mode: 'elapsed' } }).color, 0xffffff) + } + assert.equal(feedbacks(() => null).split_timer_color.callback({ options: { mode: 'remaining' } }).color, 0x889099) +})