diff --git a/lib/components/Home/Home.dart b/lib/components/Home/Home.dart index efb2ddfe7..e479384fa 100644 --- a/lib/components/Home/Home.dart +++ b/lib/components/Home/Home.dart @@ -37,6 +37,8 @@ List spotifyScopes = [ "playlist-read-collaborative" ]; +final selectedIndexState = StateProvider((ref) => 0); + class Home extends HookConsumerWidget { Home({Key? key}) : super(key: key); final logger = getLogger(Home); @@ -51,8 +53,9 @@ class Home extends HookConsumerWidget { xxl: 256.0, ); final extended = ref.watch(sidebarExtendedStateProvider); - final _selectedIndex = useState(0); - _onSelectedIndexChanged(int index) => _selectedIndex.value = index; + final selectedIndex = ref.watch(selectedIndexState); + onSelectedIndexChanged(int index) => + ref.read(selectedIndexState.notifier).state = index; final downloader = ref.watch(downloaderProvider); final isMounted = useIsMounted(); @@ -115,12 +118,12 @@ class Home extends HookConsumerWidget { return Scaffold( bottomNavigationBar: SpotubeNavigationBar( - selectedIndex: _selectedIndex.value, - onSelectedIndexChanged: _onSelectedIndexChanged, + selectedIndex: selectedIndex, + onSelectedIndexChanged: onSelectedIndexChanged, ), body: Column( children: [ - if (_selectedIndex.value != 3) + if (selectedIndex != 3) kIsMobile ? titleBarContents : WindowTitleBarBox(child: titleBarContents), @@ -128,11 +131,11 @@ class Home extends HookConsumerWidget { child: Row( children: [ Sidebar( - selectedIndex: _selectedIndex.value, - onSelectedIndexChanged: _onSelectedIndexChanged, + selectedIndex: selectedIndex, + onSelectedIndexChanged: onSelectedIndexChanged, ), // contents of the spotify - if (_selectedIndex.value == 0) + if (selectedIndex == 0) Expanded( child: Padding( padding: const EdgeInsets.only( @@ -177,9 +180,9 @@ class Home extends HookConsumerWidget { }), ), ), - if (_selectedIndex.value == 1) const Search(), - if (_selectedIndex.value == 2) const UserLibrary(), - if (_selectedIndex.value == 3) const SyncedLyrics(), + if (selectedIndex == 1) const Search(), + if (selectedIndex == 2) const UserLibrary(), + if (selectedIndex == 3) const SyncedLyrics(), ], ), ), diff --git a/lib/main.dart b/lib/main.dart index 19f2539a5..65ad818bd 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -211,12 +211,39 @@ class _SpotubeState extends ConsumerState with WidgetsBindingObserver { ...WidgetsApp.defaultShortcuts, const SingleActivator(LogicalKeyboardKey.space): PlayPauseIntent(ref), const SingleActivator(LogicalKeyboardKey.comma, control: true): - OpenSettingsIntent(_router), + NavigationIntent(_router, "/settings"), + const SingleActivator( + LogicalKeyboardKey.keyB, + control: true, + shift: true, + ): HomeTabIntent(ref, tab: HomeTabs.browse), + const SingleActivator( + LogicalKeyboardKey.keyS, + control: true, + shift: true, + ): HomeTabIntent(ref, tab: HomeTabs.search), + const SingleActivator( + LogicalKeyboardKey.keyL, + control: true, + shift: true, + ): HomeTabIntent(ref, tab: HomeTabs.library), + const SingleActivator( + LogicalKeyboardKey.keyY, + control: true, + shift: true, + ): HomeTabIntent(ref, tab: HomeTabs.lyrics), + const SingleActivator( + LogicalKeyboardKey.keyW, + control: true, + shift: true, + ): CloseAppIntent(), }, actions: { ...WidgetsApp.defaultActions, PlayPauseIntent: PlayPauseAction(), - OpenSettingsIntent: OpenSettingsAction(), + NavigationIntent: NavigationAction(), + HomeTabIntent: HomeTabAction(), + CloseAppIntent: CloseAppAction(), }, ); } diff --git a/lib/models/Intents.dart b/lib/models/Intents.dart index 3bd7fe7f1..8254a4e46 100644 --- a/lib/models/Intents.dart +++ b/lib/models/Intents.dart @@ -1,10 +1,14 @@ +import 'package:bitsdojo_window/bitsdojo_window.dart'; import 'package:flutter/cupertino.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import 'package:spotify/spotify.dart'; +import 'package:spotube/components/Home/Home.dart'; import 'package:spotube/components/Player/PlayerControls.dart'; import 'package:spotube/models/Logger.dart'; import 'package:spotube/provider/Playback.dart'; +import 'package:spotube/utils/platform.dart'; class PlayPauseIntent extends Intent { final WidgetRef ref; @@ -40,16 +44,51 @@ class PlayPauseAction extends Action { } } -class OpenSettingsIntent extends Intent { +class NavigationIntent extends Intent { final GoRouter router; - const OpenSettingsIntent(this.router); + final String path; + const NavigationIntent(this.router, this.path); } -class OpenSettingsAction extends Action { +class NavigationAction extends Action { @override - invoke(intent) async { - intent.router.push("/settings"); - FocusManager.instance.primaryFocus?.unfocus(); + invoke(intent) { + intent.router.go(intent.path); + return null; + } +} + +enum HomeTabs { + browse, + search, + library, + lyrics, +} + +class HomeTabIntent extends Intent { + final WidgetRef ref; + final HomeTabs tab; + const HomeTabIntent(this.ref, {required this.tab}); +} + +class HomeTabAction extends Action { + @override + invoke(intent) { + final notifier = intent.ref.read(selectedIndexState.notifier); + switch (intent.tab) { + case HomeTabs.browse: + notifier.state = 0; + break; + case HomeTabs.search: + notifier.state = 1; + break; + case HomeTabs.library: + notifier.state = 2; + break; + case HomeTabs.lyrics: + notifier.state = 3; + break; + } return null; } } @@ -83,3 +122,17 @@ class SeekAction extends Action { return null; } } + +class CloseAppIntent extends Intent {} + +class CloseAppAction extends Action { + @override + invoke(intent) { + if (kIsDesktop) { + appWindow.close(); + } else { + SystemNavigator.pop(); + } + return null; + } +}