goosemusic 1.2.0

A music player with YouTube search, local playback, and OS media controls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
use iced::widget::Id;

use super::{
    operation::{ListGeometry, LIBRARY_LIST_ID, SIDEBAR_LIST_ID},
    BackendResult, Message, MusicPlayer, Task, Track, TrackListKind, TrackPos, ViewData,
    DOUBLE_CLICK_MS,
};
use crate::{
    app::{
        interaction::{DropTarget, Pressed, PressedDrag},
        ui::{QUEUE_LIST_ID, QUEUE_RECENT_LIST_ID, TRACK_LIST_ID},
        ViewKind,
    },
    data::{
        library::{LibraryItem, LibraryKind},
        JsonStore,
    },
};

impl MusicPlayer {
    pub fn handle_left_release(&mut self) -> Task<Message> {
        let cursor = self.drag.cursor_pos;
        let is_cursor_in_search_input =
            self.bounds.search_input.is_some_and(|r| r.contains(cursor));
        if self.show_search_history {
            if !is_cursor_in_search_input {
                self.show_search_history = false;
                self.drag.clear_hovered_search_history();
            }
        } else if is_cursor_in_search_input {
            return self.activate_search_input();
        }

        let Some(pressed) = self.drag.pressed.take().map(|pd| pd.what) else {
            self.drag.stop();
            return Task::none();
        };

        let mut nav_task = Task::none();

        if self.drag.drag_active {
            match pressed {
                Pressed::Track(pos) => self.handle_track_drop(pos),
                Pressed::Card(item) => {
                    if let Some(target) = self
                        .card_drop_target(self.bounds.get_containing(self.drag.cursor_pos).as_ref())
                    {
                        nav_task = self.handle_card_drop(item, target);
                    }
                }
                Pressed::Playlist(_) => self.handle_playlist_drop(),
            }
        } else {
            match pressed {
                Pressed::Track(pos) => {
                    self.toggle_selection(pos);
                }
                Pressed::Card(item) => {
                    let provider = item.provider;
                    nav_task = if item.kind == crate::data::library::LibraryKind::Artist {
                        self.open_artist(Some(&item.id), &item.title, provider)
                    } else {
                        self.handle_browse(&item.into(), provider)
                    }
                }
                // A click without a drag selects the playlist (mirrors how a
                // library card opens on a plain click).
                Pressed::Playlist(i) => {
                    nav_task = self.handle_select_playlist(i);
                }
            }
        }

        self.drag.stop();
        nav_task
    }

    pub fn handle_drag_update(&mut self) -> Task<Message> {
        self.drag.drop_target = None;

        let Some(pressed) = self.drag.pressed.as_ref().map(|pd| &pd.what) else {
            return Task::none();
        };

        let containing = self.bounds.get_containing(self.drag.cursor_pos);

        match pressed {
            Pressed::Card(_) => {
                self.drag.drop_target = self.card_drop_target(containing.as_ref());
            }
            Pressed::Track(_) => {
                self.drag.drop_target = self.resolve_track_drop(containing.as_ref());
            }
            Pressed::Playlist(from) => {
                self.drag.drop_target = self.resolve_playlist_drop(*from, containing.as_ref());
            }
        }

        let Some((target_id, geo)) = containing else {
            return Task::none();
        };

        let count = if target_id == QUEUE_LIST_ID {
            self.track_count(TrackListKind::Queue)
        } else if target_id == TRACK_LIST_ID {
            self.track_count(TrackListKind::Active)
        } else {
            return Task::none();
        };
        let content_height = count as f32 * crate::theme::ROW_HEIGHT;
        self.handle_drag_autoscroll(geo.bounds, geo.translation_y, content_height, target_id)
    }

    /// Resolve a playlist-row reorder: the press must be over the sidebar
    /// playlist list, and the resolved insertion index must differ from the
    /// row's current position (a drop onto itself is a no-op).
    fn resolve_playlist_drop(
        &self,
        from: usize,
        containing: Option<&(Id, &ListGeometry)>,
    ) -> Option<DropTarget> {
        let (id, geo) = containing?;
        if *id != SIDEBAR_LIST_ID {
            return None;
        }
        let count = self.playlists.playlists.len();
        if from >= count {
            return None;
        }
        let to = self.sidebar_insertion_index(geo, count);
        // Dragging a row onto its own slot (or the gap just below it) is a
        // no-op.
        if to == from || to == from + 1 {
            return None;
        }
        Some(DropTarget::PlaylistReorder { from, to })
    }

    fn resolve_track_drop(&self, containing: Option<&(Id, &ListGeometry)>) -> Option<DropTarget> {
        let (id, geo) = containing?;
        let list = if *id == QUEUE_LIST_ID {
            TrackListKind::Queue
        } else if *id == TRACK_LIST_ID {
            TrackListKind::Active
        } else if *id == QUEUE_RECENT_LIST_ID {
            TrackListKind::Recent
        } else if *id == SIDEBAR_LIST_ID {
            let count = self.playlists.playlists.len();
            let cursor_y = self.drag.cursor_pos.y + geo.translation_y;
            let idx = nearest_row_index(&geo.rows, cursor_y).filter(|&idx| idx < count)?;
            return Some(DropTarget::PlaylistAdd(idx));
        } else {
            return None;
        };
        // Only local playlists and queue can be dropped onto
        if list == TrackListKind::Active && !matches!(&self.view_data().kind, ViewKind::Playlist(_))
        {
            return None;
        }
        let drop_idx = self.compute_drop_idx(list);
        Some(DropTarget::Track(TrackPos::new(drop_idx, list)))
    }

    fn compute_drop_idx(&self, list: TrackListKind) -> usize {
        let geo = match list {
            TrackListKind::Queue | TrackListKind::Recent => &self.bounds.queue,
            TrackListKind::Active => &self.bounds.track,
        };
        let Some(geo) = geo else {
            return list.first_index();
        };
        let first = list.first_index();
        let count = self.track_count(list);
        // Cursor position in the scrollable's content space (y = 0 at the top
        // of the first row).
        let cursor_y = (self.drag.cursor_pos.y - geo.bounds.y) + geo.translation_y;
        drop_index_from_cursor(cursor_y, first, count)
    }

    fn handle_drag_autoscroll(
        &self,
        bounds: iced::Rectangle,
        current_scroll: f32,
        content_height: f32,
        scrollable_id: Id,
    ) -> Task<Message> {
        let cursor = self.drag.cursor_pos;
        let y_offset = cursor.y - bounds.y;
        let list_height = bounds.height;

        let max_scroll = (content_height - list_height).max(0.0);

        if max_scroll <= 0.0 {
            return Task::none();
        }

        let edge_zone = crate::theme::DRAG_AUTO_SCROLL_ZONE;
        let scroll_speed = crate::theme::DRAG_AUTO_SCROLL_SPEED;

        let scroll_amount = if y_offset < edge_zone {
            -scroll_speed
        } else if y_offset > list_height - edge_zone {
            scroll_speed
        } else {
            0.0
        };

        if scroll_amount == 0.0 {
            return Task::none();
        }

        let new_scroll = (current_scroll + scroll_amount).clamp(0.0, max_scroll);
        if (new_scroll - current_scroll).abs() < 0.1 {
            return Task::none();
        }

        iced::widget::operation::scroll_by::<Message>(
            scrollable_id,
            iced::widget::operation::AbsoluteOffset {
                x: 0.0,
                y: scroll_amount,
            },
        )
    }

    /// Screen-space rectangle of the single drop indicator, derived from
    /// captured row geometry rather than an injected row (so the list layout
    /// is never perturbed). `None` when there is no drop target drawing an
    /// insertion line (`PlaylistAdd` highlights a row instead) or when the
    /// boundary falls outside the visible viewport.
    pub fn drop_indicator_rect(&self) -> Option<iced::Rectangle> {
        // Resolve to the targeted geometry and the 0-based insertion index
        // within its captured rows.
        let (geo, rel) = match self.drag.drop_target {
            Some(DropTarget::Playlist(i)) => (self.bounds.sidebar.as_ref()?, i),
            Some(DropTarget::Library(i)) => (self.bounds.library.as_ref()?, i),
            Some(DropTarget::PlaylistReorder { to, .. }) => (self.bounds.sidebar.as_ref()?, to),
            Some(DropTarget::Track(pos)) => {
                let geo = match pos.list {
                    TrackListKind::Queue => self.bounds.queue.as_ref()?,
                    TrackListKind::Active => self.bounds.track.as_ref()?,
                    TrackListKind::Recent => return None,
                };
                (geo, pos.index.saturating_sub(pos.list.first_index()))
            }
            // A track dropped on the playlist list highlights the target row
            // (handled in the sidebar view) rather than drawing an insertion
            // line, so it produces no indicator rect.
            Some(DropTarget::PlaylistAdd(_)) | None => return None,
        };

        // The queue/active lists are virtualized, so `geo.rows` is intentionally
        // empty for them; their boundary comes from the uniform `ROW_HEIGHT`.
        // Sidebar/library lists are not virtualized, so their measured `rows`
        // must be present for the drop math below.
        let is_track = matches!(self.drag.drop_target, Some(DropTarget::Track(_)));
        let rows = &geo.rows;
        if !is_track && rows.is_empty() {
            return None;
        }
        let boundary_y = if is_track {
            geo.bounds.y - geo.translation_y + rel as f32 * crate::theme::ROW_HEIGHT
        } else if rel == 0 {
            rows[0].y - geo.translation_y
        } else if rel <= rows.len() {
            let k = rel - 1;
            rows[k].y + rows[k].height - geo.translation_y
        } else {
            let last = rows.last().unwrap();
            last.y + last.height - geo.translation_y
        };
        if boundary_y < geo.bounds.y || boundary_y > geo.bounds.y + geo.bounds.height {
            return None;
        }
        Some(iced::Rectangle {
            x: geo.bounds.x,
            y: boundary_y - crate::theme::DROP_LINE_HEIGHT / 2.0,
            width: geo.bounds.width,
            height: crate::theme::DROP_LINE_HEIGHT,
        })
    }

    pub fn handle_track_drop(&mut self, pos: TrackPos) {
        let source = pos.list;
        let indices = self.dragged_indices(source).to_vec();

        // Dropped on the playlist sidebar: add to that playlist (prepend). The
        // target was resolved during the drag into `drop_target` and is shown
        // by the row highlight, so there is no separate insertion bar.
        if let Some(DropTarget::PlaylistAdd(playlist_idx)) = self.drag.drop_target {
            let tracks: Vec<Track> = indices
                .iter()
                .rev()
                .filter_map(|&i| self.get_track_at(TrackPos::new(i, source)))
                .collect();
            let count = self
                .playlists
                .insert_tracks_at(playlist_idx, tracks.iter(), 0);
            let name = self.playlists.playlists[playlist_idx].name.clone();
            let msg = (self.strings.added_to)(count, &name);
            self.notify(msg);
            return;
        }

        let Some(DropTarget::Track(drop)) = self.drag.drop_target else {
            return;
        };
        let drop_idx = drop.index;

        // Determine if this is a cross-list copy or a same-list reorder.
        match drop.list {
            target if target == source => {
                self.handle_same_list_reorder(drop_idx, &indices, source);
            }
            TrackListKind::Queue => self.copy_to_queue(source, &indices, drop_idx),
            TrackListKind::Active => self.copy_from_queue(source, &indices, drop_idx),
            TrackListKind::Recent => {}
        }
    }

    pub fn dragged_indices(&self, list: TrackListKind) -> &[usize] {
        match &self.drag.dragged {
            Some((drag_list, indices)) if *drag_list == list => indices,
            _ => &[],
        }
    }

    pub fn is_dragging_track(&self, pos: TrackPos) -> bool {
        self.dragged_indices(pos.list).contains(&pos.index)
    }

    fn copy_to_queue(&mut self, source: TrackListKind, indices: &[usize], drop_idx: usize) {
        let clamped = drop_idx.min(self.queue.tracks.len());
        let tracks: Vec<Track> = indices
            .iter()
            .filter_map(|&i| self.get_track_at(TrackPos::new(i, source)))
            .collect();
        let inserted = tracks.len();
        for (j, track) in tracks.into_iter().enumerate() {
            self.queue.tracks.insert(clamped + j, track);
        }
        self.save_session();
        let msg = (self.strings.added_to)(inserted, self.strings.queue);
        self.notify(msg);
    }

    /// Insert tracks from `source` into the current playlist at the given
    /// drop index. `indices` are positions in the source list.
    fn copy_from_queue(&mut self, source: TrackListKind, indices: &[usize], drop_idx: usize) {
        let active = match &self.view_data().kind {
            ViewKind::Playlist(p) => Some(p.index),
            _ => None,
        };
        let Some(sp) = active else {
            if !self.view_data().is_search_like() {
                self.notify(self.strings.select_playlist_drop);
            }
            return;
        };
        if sp >= self.playlists.playlists.len() {
            return;
        }

        let clamped = drop_idx.min(self.playlists.playlists[sp].tracks.len());
        let tracks: Vec<Track> = indices
            .iter()
            .filter_map(|&i| self.get_track_at(TrackPos::new(i, source)))
            .collect();
        let inserted = self.playlists.insert_tracks_at(sp, tracks.iter(), clamped);
        self.save_session();
        let name = self.playlists.playlists[sp].name.clone();
        if inserted > 0 {
            let msg = (self.strings.added_to)(inserted, &name);
            self.notify(msg);
        }
    }

    /// Handle reordering within the same list. The selection is always
    /// remapped to reflect the new positions of all selected tracks — both
    /// the moved ones and any that merely shifted.
    fn handle_same_list_reorder(
        &mut self,
        drop_idx: usize,
        indices: &[usize],
        source: TrackListKind,
    ) {
        if drop_idx > self.track_count(source) {
            return;
        }

        match source {
            TrackListKind::Queue => {
                let selection = self.queue_selected_indices.clone();
                self.queue_selected_indices =
                    self.handle_reorder_queue(drop_idx, indices, &selection);
                self.save_session();
            }
            TrackListKind::Active => {
                let selection = self.view_data_mut().selection.clone();
                self.view_data_mut().selection =
                    self.handle_reorder_tracks_selected(drop_idx, indices, &selection);
            }
            TrackListKind::Recent => {}
        }
    }

    fn card_drop_target(&self, containing: Option<&(Id, &ListGeometry)>) -> Option<DropTarget> {
        if let Some((id, geo)) = containing {
            if *id == SIDEBAR_LIST_ID {
                let count = self.playlists.playlists.len();
                return Some(DropTarget::Playlist(
                    self.sidebar_insertion_index(geo, count),
                ));
            }
            if *id == LIBRARY_LIST_ID {
                let count = self.library.items.len();
                return Some(DropTarget::Library(
                    self.sidebar_insertion_index(geo, count),
                ));
            }
        }
        None
    }

    /// The index within a sidebar list where a drop would insert, given the
    /// cursor's position relative to the list's measured row bounds.
    fn sidebar_insertion_index(&self, geo: &ListGeometry, count: usize) -> usize {
        let cursor_y = self.drag.cursor_pos.y + geo.translation_y;
        for (i, row) in geo.rows.iter().enumerate() {
            if cursor_y < row.y + row.height / 2.0 {
                return i;
            }
        }
        geo.rows.len().min(count)
    }

    /// Reorder an existing playlist row within the sidebar list. The active
    /// `Playlist` view selection is remapped so the same playlist stays
    /// selected after its index changes.
    pub fn handle_playlist_drop(&mut self) {
        let Some(DropTarget::PlaylistReorder { from, to }) = self.drag.drop_target else {
            return;
        };
        if from >= self.playlists.playlists.len() || to > self.playlists.playlists.len() {
            return;
        }
        crate::util::reorder_tracks(&mut self.playlists.playlists, to, &[from], &[]);
        self.playlists.save();

        // Keep the active Playlist view pointed at the same playlist. After
        // `reorder_tracks`, the moved row lands at `to - removed_before` where
        // `removed_before = from < to`. Other selections shift down by one if
        // the moved row passed above them, then shift again if the moved row
        // is inserted at or before their new position.
        let removed_before = usize::from(from < to);
        let landed = to - removed_before;
        if let ViewKind::Playlist(entry) = &mut self.view_data_mut().kind {
            if entry.index == from {
                entry.index = landed;
            } else {
                let mut new_sp = entry.index - usize::from(from < entry.index);
                if landed <= new_sp {
                    new_sp += 1;
                }
                entry.index = new_sp;
            }
        }
        self.notify(self.strings.reordered_playlist);
    }

    fn handle_card_drop(&mut self, item: LibraryItem, target: DropTarget) -> Task<Message> {
        match target {
            DropTarget::Playlist(idx) => self.create_playlist_from_card(&item, idx),
            DropTarget::Library(idx) => {
                if self.library.contains(item.kind, &item.id) {
                    if let Some(from) = self
                        .library
                        .items
                        .iter()
                        .position(|it| it.kind == item.kind && it.id == item.id)
                    {
                        // Adjust for the removed row so the indicator position
                        // maps to the post-removal list.
                        let to = if idx > from { idx - 1 } else { idx };
                        self.library.move_item(from, to);
                    }
                } else {
                    let title = item.title.clone();
                    self.library.insert(item, idx);
                    let msg = (self.strings.saved_title)(&title);
                    self.notify(msg);
                }
                Task::none()
            }
            _ => Task::none(),
        }
    }

    /// Create a local playlist from a dragged card at `insert_at`: make a new
    /// uniquely-named playlist, then fetch its contents in the background and
    /// fill it once they arrive.
    fn create_playlist_from_card(&mut self, item: &LibraryItem, insert_at: usize) -> Task<Message> {
        let idx = self.playlists.create_at(&item.title, insert_at);
        let name = self.playlists.playlists[idx].name.clone();

        let kind_str = match item.kind {
            LibraryKind::Artist => "artist",
            LibraryKind::Album => "album",
            LibraryKind::Playlist => "playlist",
        };
        let id = item.id.clone();
        let name_for_thread = name.clone();
        let tx = self.result_tx.clone();
        let provider = match &self.view_data().kind {
            ViewKind::Search(s) => s.provider,
            _ => crate::providers::ProviderId::YouTube,
        };
        self.notify(format!("Creating playlist \"{name}\"..."));
        Self::spawn_backend_thread(
            move || crate::providers::browse(provider, &id, kind_str),
            move |(tracks, _)| BackendResult::CardPlaylistReady(idx, name_for_thread, tracks),
            tx,
        );

        // Jump to the freshly created (initially empty) playlist view; tracks
        // stream in as the browse result arrives. Select it directly (rather
        // than `handle_select_playlist`) so it activates even when the drop
        // index coincides with the currently selected playlist.
        let task = self.push_new_view(ViewData::new_playlist(idx, name));
        self.save_session();
        task
    }

    /// Arm a press for dragging. A track row also supports double-click (play
    /// + select); a card (artist/album/playlist) opens on a plain click.
    ///
    /// The drag threshold is promoted in `CursorMoved`, so this only records
    /// the press origin.
    pub fn handle_drag_press(&mut self, pressed: Pressed) {
        match pressed {
            Pressed::Track(pos) => {
                let now = std::time::Instant::now();
                // Comparing the full position, not just the index, keeps a click
                // in one list from completing a double-click begun in another.
                let is_double = self.last_click.is_some_and(|(last_pos, last_time)| {
                    last_pos == pos && now.duration_since(last_time).as_millis() < DOUBLE_CLICK_MS
                });
                self.last_click = Some((pos, now));
                if is_double {
                    self.drag.pressed = None;
                    self.handle_play_track(pos);
                    return;
                }
                let sel = self.selection(pos.list);
                let indices = if !sel.is_empty() && sel.contains(&pos.index) {
                    sel.to_vec()
                } else {
                    vec![pos.index]
                };
                self.drag.dragged = Some((pos.list, indices));
                self.drag.pressed = Some(PressedDrag {
                    what: Pressed::Track(pos),
                    origin: self.drag.cursor_pos,
                });
            }
            _ => {
                self.drag.pressed = Some(PressedDrag {
                    what: pressed,
                    origin: self.drag.cursor_pos,
                });
            }
        }
        self.drag.drag_active = false;
    }
}

/// Insertion index for a uniform-height virtualized list, from a cursor
/// position in the scrollable's content space (y = 0 at the top of the first
/// row). Snaps to the nearest row boundary, clamped into `[first, count]` —
/// correct even though only visible rows are laid out.
fn drop_index_from_cursor(cursor_y: f32, first: usize, count: usize) -> usize {
    let row_f = (cursor_y / crate::theme::ROW_HEIGHT).max(0.0);
    let idx = (row_f + 0.5).floor() as usize + first;
    idx.clamp(first, count)
}

/// Index of the row whose center is nearest to `cursor_y`, in the same space
/// as the row rectangles.
fn nearest_row_index(rows: &[iced::Rectangle], cursor_y: f32) -> Option<usize> {
    let mut best: Option<(f32, usize)> = None;
    for (i, row) in rows.iter().enumerate() {
        let d = (cursor_y - (row.y + row.height / 2.0)).abs();
        if best.is_none_or(|(bd, _)| d < bd) {
            best = Some((d, i));
        }
    }
    best.map(|(_, idx)| idx)
}

#[cfg(test)]
mod tests {
    use super::*;

    const ROW: f32 = crate::theme::ROW_HEIGHT;

    #[test]
    fn drop_index_snaps_to_nearest_boundary() {
        assert_eq!(drop_index_from_cursor(0.0, 1, 10), 1);
        assert_eq!(drop_index_from_cursor(ROW * 0.49, 1, 10), 1);
        assert_eq!(drop_index_from_cursor(ROW * 0.51, 1, 10), 2);
        assert_eq!(drop_index_from_cursor(ROW * 3.5 - 0.01, 1, 10), 4);
    }

    #[test]
    fn drop_index_clamps_to_range() {
        assert_eq!(drop_index_from_cursor(-100.0, 1, 5), 1);
        assert_eq!(drop_index_from_cursor(100_000.0, 1, 5), 5);
        assert_eq!(drop_index_from_cursor(ROW * 9.9, 1, 10), 10);
    }

    #[test]
    fn nearest_row_picks_closest_center() {
        let rows: Vec<iced::Rectangle> = (0..3)
            .map(|i| iced::Rectangle {
                x: 0.0,
                y: i as f32 * ROW,
                width: 100.0,
                height: ROW,
            })
            .collect();
        assert_eq!(nearest_row_index(&rows, ROW * 0.4), Some(0));
        assert_eq!(nearest_row_index(&rows, ROW * 1.6), Some(1));
        assert_eq!(nearest_row_index(&rows, ROW * 2.4), Some(2));
        assert_eq!(nearest_row_index(&rows, ROW), Some(0));
        assert_eq!(nearest_row_index(&[], 0.0), None);
    }
}