diff --git a/src/components/TrackList.svelte b/src/components/TrackList.svelte index 012c887..a6ec5ea 100644 --- a/src/components/TrackList.svelte +++ b/src/components/TrackList.svelte @@ -88,9 +88,9 @@ } } - let selection = new SvelteSelection(tracks_page.itemIds, { - scroll_to_item(i) { - tracklist_actions.scroll_to_index?.(i) + const selection = new SvelteSelection(tracks_page.itemIds, { + scroll_to({ index }) { + tracklist_actions.scroll_to_index?.(index) }, async on_contextmenu() { const action = await ipc_renderer.invoke('show_tracks_menu', { diff --git a/src/lib/selection-new.ts b/src/lib/selection-new.ts index f1faecb..c8da706 100644 --- a/src/lib/selection-new.ts +++ b/src/lib/selection-new.ts @@ -2,7 +2,7 @@ import { writable, type Writable } from 'svelte/store' import { check_mouse_shortcut, check_shortcut } from './helpers' type SelectionOptions = { - scroll_to_item: (index: number) => void + scroll_to: (target: { item: T; index: number }) => void on_contextmenu: (items: Set) => void } @@ -19,12 +19,12 @@ class Selection { /** Whether the user is current mouseup is a click or a selection update */ possible_row_click = false - scroll_to_item: SelectionOptions['scroll_to_item'] + scroll_to: SelectionOptions['scroll_to'] on_contextmenu: SelectionOptions['on_contextmenu'] constructor(all_items: T[], options: SelectionOptions) { this.all = all_items - this.scroll_to_item = options.scroll_to_item + this.scroll_to = options.scroll_to this.on_contextmenu = options.on_contextmenu } @@ -150,7 +150,9 @@ class Selection { /** Replace selection with the previous index, like perssing `ArrowUp` in a list. */ go_backward() { - if (this.items.size === 0) { + if (this.all.length === 0) { + return + } else if (this.items.size === 0) { this.add_index(this.all.length - 1) } else if (this.last_added !== null) { const prev_index = this.last_added.index - 1 @@ -161,7 +163,9 @@ class Selection { /** Replace selection with the previous index, like perssing `ArrowDown` in a list. */ go_forward() { - if (this.items.size === 0) { + if (this.all.length === 0) { + return + } else if (this.items.size === 0) { this.add_index(0) } else if (this.last_added !== null) { const next_index = this.last_added.index + 1 @@ -274,34 +278,34 @@ class Selection { } } else if (check_shortcut(e, 'ArrowUp')) { this.go_backward() - this.scroll_to_item(this.last_added?.index || 0) + if (this.last_added) this.scroll_to({ ...this.last_added }) } else if (check_shortcut(e, 'ArrowUp', { shift: true })) { this.shift_select_backward() - this.scroll_to_item(this.last_added?.index || 0) + if (this.last_added) this.scroll_to({ ...this.last_added }) } else if (check_shortcut(e, 'ArrowUp', { alt: true })) { this.clear() if (this.all.length > 1) { this.add_index(0) - this.scroll_to_item(0) + if (this.last_added) this.scroll_to({ ...this.last_added }) } } else if (check_shortcut(e, 'ArrowUp', { shift: true, alt: true })) { this.shift_select_to(0) - this.scroll_to_item(this.last_added?.index || 0) + if (this.last_added) this.scroll_to({ ...this.last_added }) } else if (check_shortcut(e, 'ArrowDown')) { this.go_forward() - this.scroll_to_item(this.last_added?.index || 0) + if (this.last_added) this.scroll_to({ ...this.last_added }) } else if (check_shortcut(e, 'ArrowDown', { shift: true })) { this.shift_select_forward() - this.scroll_to_item(this.last_added?.index || 0) + if (this.last_added) this.scroll_to({ ...this.last_added }) } else if (check_shortcut(e, 'ArrowDown', { alt: true })) { this.clear() if (this.all.length > 1) { this.add_index(this.all.length - 1) - this.scroll_to_item(this.last_added?.index || 0) + if (this.last_added) this.scroll_to({ ...this.last_added }) } } else if (check_shortcut(e, 'ArrowDown', { shift: true, alt: true })) { this.shift_select_to(this.all.length - 1) - this.scroll_to_item(this.last_added?.index || 0) + if (this.last_added) this.scroll_to({ ...this.last_added }) } else { return }