Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/picker-badges-placement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'lyra': patch
---

The speaker picker shows each person with the same badge as the timeline and transcript, and stays fully on screen in a short window.
43 changes: 34 additions & 9 deletions apps/lyra/src/renderer/src/components/Transcript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '../../../shared/sessionDoc'
import { expandToEdits, visibleText, type Span, type TranscriptEdit } from '../../../shared/transcriptOverlay'
import type { NewSpeaker } from '../hooks/usePages'
import { useAnchoredPosition } from '../hooks/useAnchoredPosition'
import { useDialogFocus } from '../hooks/useDialogFocus'
import { rovingIndex } from '../lib/focus'
import { clockLabel as clock } from '../lib/format'
Expand Down Expand Up @@ -362,8 +363,7 @@ function SpeakerPicker({
? `Who is ${speaker.name}?`
: speaker.name
const sample = line ?? own[0]
const top = Math.min(picker.anchor.bottom + 8, window.innerHeight - 420)
const left = Math.min(picker.anchor.left, window.innerWidth - 356)
const position = useAnchoredPosition(dialog, picker.anchor)

const active = options[highlight]
const count = justLine ? 1 : own.length
Expand Down Expand Up @@ -398,11 +398,11 @@ function SpeakerPicker({
<div className="picker-layer" onMouseDown={onClose}>
<ScrollArea
ref={dialog}
className={cn('picker fixed z-[100] flex max-h-[calc(100vh-16px)] flex-col overflow-y-auto rounded-lg border-(length:--border-width) border-border bg-popover p-2.5 text-body-sm text-popover-foreground shadow-md', 'w-[320px]')}
className={cn('picker fixed z-[100] flex max-h-[calc(100vh-16px)] flex-col overflow-y-auto rounded-lg border-(length:--border-width) border-border bg-popover p-2.5 text-body-sm text-popover-foreground shadow-md', 'w-[320px] max-w-[calc(100vw-16px)]')}
role="dialog"
aria-modal="true"
aria-label={title}
style={{ top: Math.max(8, top), left: Math.max(8, left) }}
style={position}
onMouseDown={(event) => {
event.stopPropagation()
}}
Expand Down Expand Up @@ -516,7 +516,7 @@ function SpeakerPicker({
>
{option.kind === 'person' ? (
<>
<SpeakerBadge name={option.person.name} slot={speakerSlot(people, option.person.id)} />
<PersonBadge person={option.person} speakers={allSpeakers} people={people} />
<span className="min-w-0 flex-1 truncate">
{option.person.name}
{option.person.self ? ' (you)' : ''}
Expand All @@ -525,7 +525,11 @@ function SpeakerPicker({
</>
) : option.kind === 'speaker' ? (
<>
<SpeakerBadge name={option.speaker.name} slot={speakerSlot(allSpeakers, option.speaker.id)} />
<SpeakerBadge
name={option.speaker.name}
slot={speakerSlot(allSpeakers, option.speaker.id)}
certainty={stateOf(option.speaker)}
/>
<span className="min-w-0 flex-1 truncate">{option.speaker.name}</span>
<span className="text-label text-text-3">{justLine ? 'Move here' : 'Merge'}</span>
</>
Expand Down Expand Up @@ -583,6 +587,28 @@ function SpeakerPicker({
)
}

/**
* A saved voice's badge in the picker: someone named in this session wears their speaker's
* badge, as on the timeline and the transcript; anyone else (a voice only suggested here
* included) their badge from Saved voices.
*/
function PersonBadge({
person,
speakers,
people
}: {
person: Person
speakers: readonly SpeakerEntry[]
people: readonly Person[]
}) {
const here = speakers.find((entry) => entry.personId === person.id && stateOf(entry) === 'sure')
return here ? (
<SpeakerBadge name={here.name} slot={speakerSlot(speakers, here.id)} />
) : (
<SpeakerBadge name={person.name} slot={speakerSlot(people, person.id)} />
)
}

/** How well a saved voice matches, in plain words rather than a percentage. */
function MatchStrength({ score }: { score: number | null }) {
if (score === null) return null
Expand Down Expand Up @@ -624,8 +650,7 @@ function SplitDialog({
const valid = picked.size > 0 && picked.size < own.length
const dialog = useRef<HTMLElement>(null)
useDialogFocus(dialog, true)
const top = Math.min(anchor.bottom + 8, window.innerHeight - 440)
const left = Math.min(anchor.left, window.innerWidth - 536)
const position = useAnchoredPosition(dialog, anchor)

const least = own[0]?.id
return (
Expand All @@ -636,7 +661,7 @@ function SplitDialog({
role="dialog"
aria-modal="true"
aria-label={`Split ${speaker.name}`}
style={{ top: Math.max(8, top), left: Math.max(8, left) }}
style={position}
onMouseDown={(event) => {
event.stopPropagation()
}}
Expand Down
35 changes: 35 additions & 0 deletions apps/lyra/src/renderer/src/hooks/useAnchoredPosition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useLayoutEffect, useState, type RefObject } from 'react'
import { placeNear } from '../lib/placement'

/**
* Positions a fixed popover in `ref` next to `anchor`, from its measured size rather than a
* guess: placed before paint, and again whenever its content (a filtered list, a hint) or the
* window changes size, so no part of it ends up off screen.
*/
export function useAnchoredPosition(
ref: RefObject<HTMLElement | null>,
anchor: DOMRect
): { top: number; left: number } {
const [position, setPosition] = useState({ top: anchor.bottom + 8, left: anchor.left })
useLayoutEffect(() => {
const element = ref.current
if (!element) return
const place = (): void => {
const next = placeNear(
anchor,
{ width: element.offsetWidth, height: element.offsetHeight },
{ width: window.innerWidth, height: window.innerHeight }
)
setPosition((current) => (current.top === next.top && current.left === next.left ? current : next))
}
place()
const observer = new ResizeObserver(place)
observer.observe(element)
window.addEventListener('resize', place)
return () => {
observer.disconnect()
window.removeEventListener('resize', place)
}
}, [ref, anchor])
return position
}
37 changes: 37 additions & 0 deletions apps/lyra/src/renderer/src/lib/placement.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, it } from 'vitest'
import { placeNear } from './placement'

const view = { width: 360, height: 700 }

describe('placeNear', () => {
it('goes below the anchor when it fits', () => {
expect(placeNear({ top: 100, bottom: 120, left: 20 }, { width: 320, height: 300 }, view)).toEqual({
top: 128,
left: 20
})
})

it('goes above the anchor when there is no room below', () => {
expect(placeNear({ top: 600, bottom: 620, left: 20 }, { width: 320, height: 300 }, view)).toEqual({
top: 292,
left: 20
})
})

it('sits at the bottom of the window when neither side has room', () => {
// 520 tall under an anchor at 162–178: below ends at 706, above starts at -366.
expect(placeNear({ top: 162, bottom: 178, left: 20 }, { width: 320, height: 520 }, view)).toEqual({
top: 172,
left: 20
})
})

it('never starts above the top margin', () => {
expect(placeNear({ top: 300, bottom: 320, left: 20 }, { width: 320, height: 684 }, view).top).toBe(8)
})

it('keeps the right edge inside the window', () => {
expect(placeNear({ top: 100, bottom: 120, left: 200 }, { width: 320, height: 100 }, view).left).toBe(32)
expect(placeNear({ top: 100, bottom: 120, left: 0 }, { width: 520, height: 100 }, view).left).toBe(8)
})
})
23 changes: 23 additions & 0 deletions apps/lyra/src/renderer/src/lib/placement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** Space kept between a popover and its anchor, and between a popover and the window's edge. */
const GAP = 8
const MARGIN = 8

type Box = { width: number; height: number }

/**
* Where a popover goes next to the thing it's about: below it, else above it, else as low as the
* window allows. Always fully inside the window, so every row and button in it can be reached
* (the popover's own max height is the window less the margins, so the last case always fits).
*/
export function placeNear(
anchor: { top: number; bottom: number; left: number },
size: Box,
view: Box
): { top: number; left: number } {
const left = Math.max(MARGIN, Math.min(anchor.left, view.width - MARGIN - size.width))
const below = anchor.bottom + GAP
if (below + size.height <= view.height - MARGIN) return { top: below, left }
const above = anchor.top - GAP - size.height
if (above >= MARGIN) return { top: above, left }
return { top: Math.max(MARGIN, view.height - MARGIN - size.height), left }
}
Loading