Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more emphasis to current song #283

Merged
merged 17 commits into from
Oct 19, 2023
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
19 changes: 19 additions & 0 deletions src/renderer/components/virtual-table/cells/row-index-cell.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { ICellRendererParams } from '@ag-grid-community/core';
import { Text } from '/@/renderer/components/text';
import { CellContainer } from '/@/renderer/components/virtual-table/cells/generic-cell';

export const RowIndexCell = ({ value }: ICellRendererParams) => {
return (
<CellContainer $position="right">
<Text
$secondary
align="right"
className="current-song-child current-song-index"
overflow="hidden"
size="md"
>
{value}
</Text>
</CellContainer>
);
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { RowClassRules, RowNode } from '@ag-grid-community/core';
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
import { MutableRefObject, useEffect, useMemo } from 'react';
import { MutableRefObject, useEffect, useMemo, useRef } from 'react';
import { Song } from '/@/renderer/api/types';
import { useAppFocus } from '/@/renderer/hooks';
import { useCurrentSong, usePlayerStore } from '/@/renderer/store';

interface UseCurrentSongRowStylesProps {
Expand All @@ -10,17 +11,43 @@ interface UseCurrentSongRowStylesProps {

export const useCurrentSongRowStyles = ({ tableRef }: UseCurrentSongRowStylesProps) => {
const currentSong = useCurrentSong();
const isFocused = useAppFocus();
const isFocusedRef = useRef<boolean>(isFocused);

useEffect(() => {
// Redraw rows if the app focus changes
if (isFocusedRef.current !== isFocused) {
isFocusedRef.current = isFocused;
if (tableRef?.current) {
const { api, columnApi } = tableRef?.current || {};
if (api == null || columnApi == null) {
return;
}

const currentNode = currentSong?.id ? api.getRowNode(currentSong.id) : undefined;

const rowNodes = [currentNode].filter((e) => e !== undefined) as RowNode<any>[];

if (rowNodes) {
api.redrawRows({ rowNodes });
}
}
}
}, [currentSong?.id, isFocused, tableRef]);

const rowClassRules = useMemo<RowClassRules<Song> | undefined>(() => {
return {
'current-song': (params) => {
return params?.data?.id === currentSong?.id;
return (
params?.data?.id === currentSong?.id &&
params?.data?.albumId === currentSong?.albumId
);
},
};
}, [currentSong?.id]);
}, [currentSong?.albumId, currentSong?.id]);

// Redraw song rows when current song changes
useEffect(() => {
// Redraw song rows when current song changes
const unsubSongChange = usePlayerStore.subscribe(
(state) => state.current.song,
(song, previousSong) => {
Expand All @@ -46,8 +73,35 @@ export const useCurrentSongRowStyles = ({ tableRef }: UseCurrentSongRowStylesPro
{ equalityFn: (a, b) => a?.id === b?.id },
);

// Redraw song rows when the status changes
const unsubStatusChange = usePlayerStore.subscribe(
(state) => state.current.song,
(song, previousSong) => {
if (tableRef?.current) {
const { api, columnApi } = tableRef?.current || {};
if (api == null || columnApi == null) {
return;
}

const currentNode = song?.id ? api.getRowNode(song.id) : undefined;

const previousNode = previousSong?.id
? api.getRowNode(previousSong?.id)
: undefined;

const rowNodes = [currentNode, previousNode].filter(
(e) => e !== undefined,
) as RowNode<any>[];

api.redrawRows({ rowNodes });
}
},
{ equalityFn: (a, b) => a?.id === b?.id },
);

return () => {
unsubSongChange();
unsubStatusChange();
};
}, [tableRef]);

Expand Down
56 changes: 51 additions & 5 deletions src/renderer/components/virtual-table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import formatDuration from 'format-duration';
import { AnimatePresence } from 'framer-motion';
import isElectron from 'is-electron';
import { generatePath } from 'react-router';
import styled from 'styled-components';
import { AlbumArtistCell } from '/@/renderer/components/virtual-table/cells/album-artist-cell';
Expand All @@ -29,14 +30,19 @@ import { GenreCell } from '/@/renderer/components/virtual-table/cells/genre-cell
import { GenericTableHeader } from '/@/renderer/components/virtual-table/headers/generic-table-header';
import { AppRoute } from '/@/renderer/router/routes';
import { PersistedTableColumn } from '/@/renderer/store/settings.store';
import { TableColumn, TablePagination as TablePaginationType } from '/@/renderer/types';
import {
PlayerStatus,
TableColumn,
TablePagination as TablePaginationType,
} from '/@/renderer/types';
import { FavoriteCell } from '/@/renderer/components/virtual-table/cells/favorite-cell';
import { RatingCell } from '/@/renderer/components/virtual-table/cells/rating-cell';
import { TablePagination } from '/@/renderer/components/virtual-table/table-pagination';
import { ActionsCell } from '/@/renderer/components/virtual-table/cells/actions-cell';
import { TitleCell } from '/@/renderer/components/virtual-table/cells/title-cell';
import { useFixedTableHeader } from '/@/renderer/components/virtual-table/hooks/use-fixed-table-header';
import { NoteCell } from '/@/renderer/components/virtual-table/cells/note-cell';
import { RowIndexCell } from '/@/renderer/components/virtual-table/cells/row-index-cell';

export * from './table-config-dropdown';
export * from './table-pagination';
Expand Down Expand Up @@ -260,7 +266,16 @@ const tableColumns: { [key: string]: ColDef } = {
width: 80,
},
rowIndex: {
cellRenderer: (params: ICellRendererParams) => GenericCell(params, { position: 'right' }),
cellClass: 'row-index',
cellClassRules: {
focused: (params) => {
return isElectron() && params.context?.isFocused;
},
playing: (params) => {
return params.context?.status === PlayerStatus.PLAYING;
},
},
cellRenderer: RowIndexCell,
colId: TableColumn.ROW_INDEX,
headerComponent: (params: IHeaderParams) =>
GenericTableHeader(params, { position: 'right', preset: 'rowIndex' }),
Expand Down Expand Up @@ -311,7 +326,29 @@ const tableColumns: { [key: string]: ColDef } = {
width: 250,
},
trackNumber: {
cellRenderer: (params: ICellRendererParams) => GenericCell(params, { position: 'center' }),
cellClass: 'track-number',
cellRenderer: (params: ICellRendererParams) => GenericCell(params, { position: 'right' }),
colId: TableColumn.TRACK_NUMBER,
field: 'trackNumber',
headerComponent: (params: IHeaderParams) =>
GenericTableHeader(params, { position: 'center' }),
headerName: 'Track',
suppressSizeToFit: true,
valueGetter: (params: ValueGetterParams) =>
params.data ? params.data.trackNumber : undefined,
width: 80,
},
trackNumberDetail: {
cellClass: 'row-index',
cellClassRules: {
focused: (params) => {
return isElectron() && params.context?.isFocused;
},
playing: (params) => {
return params.context?.status === PlayerStatus.PLAYING;
},
},
cellRenderer: RowIndexCell,
colId: TableColumn.TRACK_NUMBER,
field: 'trackNumber',
headerComponent: (params: IHeaderParams) =>
Expand Down Expand Up @@ -354,10 +391,19 @@ export const getColumnDef = (column: TableColumn) => {
return tableColumns[column as keyof typeof tableColumns];
};

export const getColumnDefs = (columns: PersistedTableColumn[], useWidth?: boolean) => {
export const getColumnDefs = (
columns: PersistedTableColumn[],
useWidth?: boolean,
type?: 'albumDetail',
) => {
const columnDefs: ColDef[] = [];
for (const column of columns) {
const presetColumn = tableColumns[column.column as keyof typeof tableColumns];
let presetColumn = tableColumns[column.column as keyof typeof tableColumns];

if (type === 'albumDetail' && column.column === TableColumn.TRACK_NUMBER) {
presetColumn = tableColumns['trackNumberDetail' as keyof typeof tableColumns];
}

if (presetColumn) {
columnDefs.push({
...presetColumn,
Expand Down
13 changes: 10 additions & 3 deletions src/renderer/features/albums/components/album-detail-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import {
import { usePlayQueueAdd } from '/@/renderer/features/player';
import { PlayButton, useCreateFavorite, useDeleteFavorite } from '/@/renderer/features/shared';
import { LibraryBackgroundOverlay } from '/@/renderer/features/shared/components/library-background-overlay';
import { useContainerQuery } from '/@/renderer/hooks';
import { useAppFocus, useContainerQuery } from '/@/renderer/hooks';
import { AppRoute } from '/@/renderer/router/routes';
import { useCurrentServer } from '/@/renderer/store';
import { useCurrentServer, useCurrentStatus } from '/@/renderer/store';
import {
usePlayButtonBehavior,
useSettingsStoreActions,
Expand Down Expand Up @@ -70,8 +70,13 @@ export const AlbumDetailContent = ({ tableRef, background }: AlbumDetailContentP
const handlePlayQueueAdd = usePlayQueueAdd();
const tableConfig = useTableSettings('albumDetail');
const { setTable } = useSettingsStoreActions();
const status = useCurrentStatus();
const isFocused = useAppFocus();

const columnDefs = useMemo(() => getColumnDefs(tableConfig.columns), [tableConfig.columns]);
const columnDefs = useMemo(
() => getColumnDefs(tableConfig.columns, false, 'albumDetail'),
[tableConfig.columns],
);

const getRowHeight = useCallback(
(params: RowHeightParams) => {
Expand Down Expand Up @@ -396,7 +401,9 @@ export const AlbumDetailContent = ({ tableRef, background }: AlbumDetailContentP
autoFitColumns={tableConfig.autoFit}
columnDefs={columnDefs}
context={{
isFocused,
onCellContextMenu,
status,
}}
enableCellChangeFlash={false}
fullWidthCellRenderer={FullWidthDiscCell}
Expand Down
8 changes: 7 additions & 1 deletion src/renderer/features/now-playing/components/play-queue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import '@ag-grid-community/styles/ag-theme-alpine.css';
import {
useAppStoreActions,
useCurrentSong,
useCurrentStatus,
useDefaultQueue,
usePlayerControls,
usePreviousSong,
Expand All @@ -34,6 +35,7 @@ import { LibraryItem, QueueSong } from '/@/renderer/api/types';
import { useHandleTableContextMenu } from '/@/renderer/features/context-menu';
import { QUEUE_CONTEXT_MENU_ITEMS } from '/@/renderer/features/context-menu/context-menu-items';
import { VirtualGridAutoSizerContainer } from '/@/renderer/components/virtual-grid';
import { useAppFocus } from '/@/renderer/hooks';

const mpvPlayer = isElectron() ? window.electron.mpvPlayer : null;
const remote = isElectron() ? window.electron.remote : null;
Expand All @@ -49,13 +51,15 @@ export const PlayQueue = forwardRef(({ type }: QueueProps, ref: Ref<any>) => {
const { reorderQueue, setCurrentTrack } = useQueueControls();
const currentSong = useCurrentSong();
const previousSong = usePreviousSong();
const status = useCurrentStatus();
const { setSettings } = useSettingsStoreActions();
const { setAppStore } = useAppStoreActions();
const tableConfig = useTableSettings(type);
const [gridApi, setGridApi] = useState<AgGridReactType | undefined>();
const playerType = usePlayerType();
const { play } = usePlayerControls();
const volume = useVolume();
const isFocused = useAppFocus();

useEffect(() => {
if (tableRef.current) {
Expand Down Expand Up @@ -204,7 +208,7 @@ export const PlayQueue = forwardRef(({ type }: QueueProps, ref: Ref<any>) => {
}
}
}
}, [currentSong, previousSong, tableConfig.followCurrentSong]);
}, [currentSong, previousSong, tableConfig.followCurrentSong, status, isFocused]);

const onCellContextMenu = useHandleTableContextMenu(LibraryItem.SONG, QUEUE_CONTEXT_MENU_ITEMS);

Expand All @@ -219,7 +223,9 @@ export const PlayQueue = forwardRef(({ type }: QueueProps, ref: Ref<any>) => {
autoFitColumns={tableConfig.autoFit}
columnDefs={columnDefs}
context={{
isFocused,
onCellContextMenu,
status,
}}
deselectOnClickOutside={type === 'fullScreen'}
getRowId={(data) => data.data.uniqueId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
SortOrder,
} from '/@/renderer/api/types';
import { VirtualGridAutoSizerContainer } from '/@/renderer/components/virtual-grid';
import { getColumnDefs, TablePagination, VirtualTable } from '/@/renderer/components/virtual-table';
import { TablePagination, VirtualTable, getColumnDefs } from '/@/renderer/components/virtual-table';
import { useCurrentSongRowStyles } from '/@/renderer/components/virtual-table/hooks/use-current-song-row-styles';
import { useHandleTableContextMenu } from '/@/renderer/features/context-menu';
import {
Expand All @@ -34,13 +34,15 @@ import { usePlaylistDetail } from '/@/renderer/features/playlists/queries/playli
import { usePlaylistSongList } from '/@/renderer/features/playlists/queries/playlist-song-list-query';
import {
useCurrentServer,
useCurrentStatus,
usePlaylistDetailStore,
usePlaylistDetailTablePagination,
useSetPlaylistDetailTable,
useSetPlaylistDetailTablePagination,
} from '/@/renderer/store';
import { usePlayButtonBehavior } from '/@/renderer/store/settings.store';
import { ListDisplayType } from '/@/renderer/types';
import { useAppFocus } from '/@/renderer/hooks';

interface PlaylistDetailContentProps {
tableRef: MutableRefObject<AgGridReactType | null>;
Expand All @@ -49,6 +51,8 @@ interface PlaylistDetailContentProps {
export const PlaylistDetailSongListContent = ({ tableRef }: PlaylistDetailContentProps) => {
const { playlistId } = useParams() as { playlistId: string };
const queryClient = useQueryClient();
const status = useCurrentStatus();
const isFocused = useAppFocus();
const server = useCurrentServer();
const page = usePlaylistDetailStore();
const filters: Partial<PlaylistSongListQuery> = useMemo(() => {
Expand Down Expand Up @@ -236,6 +240,11 @@ export const PlaylistDetailSongListContent = ({ tableRef }: PlaylistDetailConten
alwaysShowHorizontalScroll
autoFitColumns={page.table.autoFit}
columnDefs={columnDefs}
context={{
isFocused,
onCellContextMenu: handleContextMenu,
status,
}}
getRowId={(data) => data.data.uniqueId}
infiniteInitialRowCount={checkPlaylistList.data?.totalRecordCount || 100}
pagination={isPaginationEnabled}
Expand Down
10 changes: 9 additions & 1 deletion src/renderer/features/songs/components/song-list-table-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { useCurrentSongRowStyles } from '/@/renderer/components/virtual-table/ho
import { useVirtualTable } from '/@/renderer/components/virtual-table/hooks/use-virtual-table';
import { useListContext } from '/@/renderer/context/list-context';
import { SONG_CONTEXT_MENU_ITEMS } from '/@/renderer/features/context-menu/context-menu-items';
import { useCurrentServer, usePlayButtonBehavior } from '/@/renderer/store';
import { useAppFocus } from '/@/renderer/hooks';
import { useCurrentServer, useCurrentStatus, usePlayButtonBehavior } from '/@/renderer/store';

interface SongListTableViewProps {
itemCount?: number;
Expand All @@ -18,6 +19,8 @@ interface SongListTableViewProps {
export const SongListTableView = ({ tableRef, itemCount }: SongListTableViewProps) => {
const server = useCurrentServer();
const { pageKey, id, handlePlay, customFilters } = useListContext();
const isFocused = useAppFocus();
const status = useCurrentStatus();

const { rowClassRules } = useCurrentSongRowStyles({ tableRef });

Expand Down Expand Up @@ -46,6 +49,11 @@ export const SongListTableView = ({ tableRef, itemCount }: SongListTableViewProp
key={`table-${tableProps.rowHeight}-${server?.id}`}
ref={tableRef}
{...tableProps}
context={{
...tableProps.context,
isFocused,
status,
}}
rowClassRules={rowClassRules}
onRowDoubleClicked={handleRowDoubleClick}
/>
Expand Down
1 change: 1 addition & 0 deletions src/renderer/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './use-should-pad-titlebar';
export * from './use-container-query';
export * from './use-fast-average-color';
export * from './use-hide-scrollbar';
export * from './use-app-focus';
Loading