diff --git a/TelegramDownloader/ClientApp/apps/web/src/i18n/es/shell.ts b/TelegramDownloader/ClientApp/apps/web/src/i18n/es/shell.ts index 9bbd6e5..16d9470 100644 --- a/TelegramDownloader/ClientApp/apps/web/src/i18n/es/shell.ts +++ b/TelegramDownloader/ClientApp/apps/web/src/i18n/es/shell.ts @@ -11,6 +11,8 @@ const shell: Record = { 'Not in a folder': 'Sin carpeta', 'Filter channels': 'Filtrar canales', 'Expand the channel list': 'Ampliar la lista de canales', + 'Resize the channel list': 'Ajustar el tamaño de la lista de canales', + 'Drag to resize the channel list; double click to reset': 'Arrastra para ajustar la lista de canales; doble clic para restablecer', 'Show the menu': 'Mostrar el menú', 'Show all channels': 'Mostrar todos los canales', 'Only favourites': 'Solo favoritos', diff --git a/TelegramDownloader/ClientApp/apps/web/src/i18n/gl/shell.ts b/TelegramDownloader/ClientApp/apps/web/src/i18n/gl/shell.ts index c90135e..409cdc5 100644 --- a/TelegramDownloader/ClientApp/apps/web/src/i18n/gl/shell.ts +++ b/TelegramDownloader/ClientApp/apps/web/src/i18n/gl/shell.ts @@ -11,6 +11,8 @@ const shell: Record = { 'Not in a folder': 'Sen cartafol', 'Filter channels': 'Filtrar canles', 'Expand the channel list': 'Ampliar a lista de canles', + 'Resize the channel list': 'Axustar o tamaño da lista de canles', + 'Drag to resize the channel list; double click to reset': 'Arrastra para axustar a lista de canles; dobre clic para restablecer', 'Show the menu': 'Amosar o menú', 'Show all channels': 'Amosar todas as canles', 'Only favourites': 'Só favoritas', diff --git a/TelegramDownloader/ClientApp/apps/web/src/shell/Sidebar.test.tsx b/TelegramDownloader/ClientApp/apps/web/src/shell/Sidebar.test.tsx index 82638b0..99bb89e 100644 --- a/TelegramDownloader/ClientApp/apps/web/src/shell/Sidebar.test.tsx +++ b/TelegramDownloader/ClientApp/apps/web/src/shell/Sidebar.test.tsx @@ -35,7 +35,7 @@ function renderSidebar(inDrawer: boolean) { beforeEach(() => { localStorage.removeItem('tfm.web.ui'); - useUi.setState({ drawerCollapsedSections: [], drawerChannelsExpanded: false }); + useUi.setState({ drawerCollapsedSections: [], drawerChannelsExpanded: false, sidebarNavHeight: null }); }); describe(' in the mobile drawer', () => { @@ -76,3 +76,61 @@ describe(' on desktop', () => { expect(screen.queryByRole('button', { name: 'Expand the channel list' })).toBeNull(); }); }); + +/** Gives the navigation a layout (jsdom has none): its content, top and the sidebar bottom. */ +function layout(contentHeight: number, top = 56, sidebarBottom = 900) { + const nav = screen.getByRole('navigation', { name: 'Main navigation' }); + Object.defineProperty(nav, 'scrollHeight', { configurable: true, value: contentHeight }); + nav.getBoundingClientRect = () => { + const height = useUi.getState().sidebarNavHeight ?? contentHeight; + return { top, bottom: top + height, height, left: 0, right: 0, width: 0, x: 0, y: top, toJSON: () => ({}) }; + }; + const container = nav.parentElement!; + container.getBoundingClientRect = () => + ({ top: 0, bottom: sidebarBottom, height: sidebarBottom, left: 0, right: 0, width: 0, x: 0, y: 0, toJSON: () => ({}) }); + return nav; +} + +describe(' navigation divider', () => { + it('is only on the desktop sidebar', () => { + renderSidebar(true); + expect(screen.queryByRole('separator', { name: 'Resize the channel list' })).toBeNull(); + }); + + it('shrinks the navigation with the keyboard and remembers it', () => { + renderSidebar(false); + const nav = layout(500); + const divider = screen.getByRole('separator', { name: 'Resize the channel list' }); + fireEvent.keyDown(divider, { key: 'ArrowUp' }); + expect(useUi.getState().sidebarNavHeight).toBe(476); + expect(nav).toHaveStyle({ height: '476px' }); + expect(localStorage.getItem('tfm.web.ui')).toContain('"sidebarNavHeight":476'); + }); + + it('follows the pointer and never hides the channels nor the whole navigation', () => { + renderSidebar(false); + layout(500, 56, 600); + const divider = screen.getByRole('separator', { name: 'Resize the channel list' }); + fireEvent.pointerDown(divider, { button: 0, clientY: 556, pointerId: 1 }); + fireEvent.pointerMove(divider, { clientY: 300, pointerId: 1 }); + expect(useUi.getState().sidebarNavHeight).toBe(244); + fireEvent.pointerMove(divider, { clientY: -500, pointerId: 1 }); + expect(useUi.getState().sidebarNavHeight).toBe(48); + // The channel list keeps 160 px: 600 - 56 - 160 + fireEvent.pointerMove(divider, { clientY: 2000, pointerId: 1 }); + expect(useUi.getState().sidebarNavHeight).toBe(384); + fireEvent.pointerUp(divider, { pointerId: 1 }); + }); + + it('goes back to the full navigation on double click or End', () => { + useUi.setState({ sidebarNavHeight: 200 }); + renderSidebar(false); + layout(500); + const divider = screen.getByRole('separator', { name: 'Resize the channel list' }); + fireEvent.doubleClick(divider); + expect(useUi.getState().sidebarNavHeight).toBeNull(); + useUi.setState({ sidebarNavHeight: 200 }); + fireEvent.keyDown(divider, { key: 'End' }); + expect(useUi.getState().sidebarNavHeight).toBeNull(); + }); +}); diff --git a/TelegramDownloader/ClientApp/apps/web/src/shell/Sidebar.tsx b/TelegramDownloader/ClientApp/apps/web/src/shell/Sidebar.tsx index ece0f29..4295ab5 100644 --- a/TelegramDownloader/ClientApp/apps/web/src/shell/Sidebar.tsx +++ b/TelegramDownloader/ClientApp/apps/web/src/shell/Sidebar.tsx @@ -1,5 +1,6 @@ import { cn, IconButton, Tooltip } from '@tfm/ui'; import { ChevronDown, PanelLeftClose, PanelLeftOpen } from 'lucide-react'; +import { useRef, type KeyboardEvent, type PointerEvent, type RefObject } from 'react'; import { NavLink } from 'react-router'; import { t } from '@/i18n'; import { useUi } from '@/stores/ui'; @@ -35,6 +36,86 @@ function NavEntry({ item, collapsed, onNavigate }: { item: NavItem; collapsed: b ); } +/** Smallest height of the navigation and of the channel list when dragging the divider. */ +const MIN_NAV_HEIGHT = 48; +const MIN_CHANNELS_HEIGHT = 160; +const KEY_STEP = 24; + +/** + * Divider between the navigation and the channel list of the desktop sidebar: + * drag it (or use the arrow keys) to give the channels more room. The height + * is remembered; double click or End goes back to the full navigation. + */ +function NavResizer({ navRef, containerRef }: { navRef: RefObject; containerRef: RefObject }) { + const navHeight = useUi((s) => s.sidebarNavHeight); + const setNavHeight = useUi((s) => s.setSidebarNavHeight); + const drag = useRef<{ startY: number; startHeight: number } | null>(null); + + // Tallest useful height: the whole navigation, leaving room for the channels + const maxHeight = () => { + const nav = navRef.current; + const container = containerRef.current; + if (!nav || !container) return Number.POSITIVE_INFINITY; + const room = container.getBoundingClientRect().bottom - nav.getBoundingClientRect().top - MIN_CHANNELS_HEIGHT; + return Math.max(MIN_NAV_HEIGHT, Math.min(nav.scrollHeight, room)); + }; + const apply = (height: number) => { + const nav = navRef.current; + const clamped = Math.min(Math.max(height, MIN_NAV_HEIGHT), maxHeight()); + // Dragged back to the full navigation: follow its natural height again + setNavHeight(nav && clamped >= nav.scrollHeight ? null : clamped); + }; + const current = () => navRef.current?.getBoundingClientRect().height ?? navHeight ?? 0; + + const onPointerDown = (e: PointerEvent) => { + if (e.button !== 0) return; + e.preventDefault(); + e.currentTarget.setPointerCapture?.(e.pointerId); + drag.current = { startY: e.clientY, startHeight: current() }; + }; + const onPointerMove = (e: PointerEvent) => { + if (!drag.current) return; + apply(drag.current.startHeight + e.clientY - drag.current.startY); + }; + const onPointerUp = (e: PointerEvent) => { + drag.current = null; + if (e.currentTarget.hasPointerCapture?.(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId); + }; + const onKeyDown = (e: KeyboardEvent) => { + const moves: Record void> = { + ArrowUp: () => apply(current() - KEY_STEP), + ArrowDown: () => apply(current() + KEY_STEP), + Home: () => apply(MIN_NAV_HEIGHT), + End: () => setNavHeight(null) + }; + const move = moves[e.key]; + if (!move) return; + e.preventDefault(); + move(); + }; + + return ( +
setNavHeight(null)} + onKeyDown={onKeyDown} + className="group mx-3 flex h-2.5 shrink-0 cursor-row-resize touch-none items-center outline-none" + > +
+
+ ); +} + export interface SidebarProps { /** Icon rail (desktop collapsed). */ collapsed?: boolean; @@ -52,9 +133,14 @@ export function Sidebar({ collapsed = false, inDrawer = false, onNavigate }: Sid const sections = navSections(); // In the drawer the channel list can take the whole height (the pages stay one tap away) const channelsOnly = inDrawer && drawerChannelsExpanded; + // Desktop: the user can drag the divider to give the channel list more room + const resizable = !inDrawer && !collapsed; + const navHeight = useUi((s) => s.sidebarNavHeight); + const containerRef = useRef(null); + const navRef = useRef(null); return ( -
+
{!collapsed && ( @@ -78,7 +164,11 @@ export function Sidebar({ collapsed = false, inDrawer = false, onNavigate }: Sid {!channelsOnly && ( <> -