feat(keyboard-shortcuts): home sidebar tab navigation and close app

This commit is contained in:
Kingkor Roy Tirtho 2022-09-29 22:08:05 +06:00
parent 2734454717
commit 8f258e709a
3 changed files with 102 additions and 19 deletions

View File

@ -37,6 +37,8 @@ List<String> spotifyScopes = [
"playlist-read-collaborative" "playlist-read-collaborative"
]; ];
final selectedIndexState = StateProvider((ref) => 0);
class Home extends HookConsumerWidget { class Home extends HookConsumerWidget {
Home({Key? key}) : super(key: key); Home({Key? key}) : super(key: key);
final logger = getLogger(Home); final logger = getLogger(Home);
@ -51,8 +53,9 @@ class Home extends HookConsumerWidget {
xxl: 256.0, xxl: 256.0,
); );
final extended = ref.watch(sidebarExtendedStateProvider); final extended = ref.watch(sidebarExtendedStateProvider);
final _selectedIndex = useState(0); final selectedIndex = ref.watch(selectedIndexState);
_onSelectedIndexChanged(int index) => _selectedIndex.value = index; onSelectedIndexChanged(int index) =>
ref.read(selectedIndexState.notifier).state = index;
final downloader = ref.watch(downloaderProvider); final downloader = ref.watch(downloaderProvider);
final isMounted = useIsMounted(); final isMounted = useIsMounted();
@ -115,12 +118,12 @@ class Home extends HookConsumerWidget {
return Scaffold( return Scaffold(
bottomNavigationBar: SpotubeNavigationBar( bottomNavigationBar: SpotubeNavigationBar(
selectedIndex: _selectedIndex.value, selectedIndex: selectedIndex,
onSelectedIndexChanged: _onSelectedIndexChanged, onSelectedIndexChanged: onSelectedIndexChanged,
), ),
body: Column( body: Column(
children: [ children: [
if (_selectedIndex.value != 3) if (selectedIndex != 3)
kIsMobile kIsMobile
? titleBarContents ? titleBarContents
: WindowTitleBarBox(child: titleBarContents), : WindowTitleBarBox(child: titleBarContents),
@ -128,11 +131,11 @@ class Home extends HookConsumerWidget {
child: Row( child: Row(
children: [ children: [
Sidebar( Sidebar(
selectedIndex: _selectedIndex.value, selectedIndex: selectedIndex,
onSelectedIndexChanged: _onSelectedIndexChanged, onSelectedIndexChanged: onSelectedIndexChanged,
), ),
// contents of the spotify // contents of the spotify
if (_selectedIndex.value == 0) if (selectedIndex == 0)
Expanded( Expanded(
child: Padding( child: Padding(
padding: const EdgeInsets.only( padding: const EdgeInsets.only(
@ -177,9 +180,9 @@ class Home extends HookConsumerWidget {
}), }),
), ),
), ),
if (_selectedIndex.value == 1) const Search(), if (selectedIndex == 1) const Search(),
if (_selectedIndex.value == 2) const UserLibrary(), if (selectedIndex == 2) const UserLibrary(),
if (_selectedIndex.value == 3) const SyncedLyrics(), if (selectedIndex == 3) const SyncedLyrics(),
], ],
), ),
), ),

View File

@ -211,12 +211,39 @@ class _SpotubeState extends ConsumerState<Spotube> with WidgetsBindingObserver {
...WidgetsApp.defaultShortcuts, ...WidgetsApp.defaultShortcuts,
const SingleActivator(LogicalKeyboardKey.space): PlayPauseIntent(ref), const SingleActivator(LogicalKeyboardKey.space): PlayPauseIntent(ref),
const SingleActivator(LogicalKeyboardKey.comma, control: true): 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: { actions: {
...WidgetsApp.defaultActions, ...WidgetsApp.defaultActions,
PlayPauseIntent: PlayPauseAction(), PlayPauseIntent: PlayPauseAction(),
OpenSettingsIntent: OpenSettingsAction(), NavigationIntent: NavigationAction(),
HomeTabIntent: HomeTabAction(),
CloseAppIntent: CloseAppAction(),
}, },
); );
} }

View File

@ -1,10 +1,14 @@
import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:spotify/spotify.dart'; import 'package:spotify/spotify.dart';
import 'package:spotube/components/Home/Home.dart';
import 'package:spotube/components/Player/PlayerControls.dart'; import 'package:spotube/components/Player/PlayerControls.dart';
import 'package:spotube/models/Logger.dart'; import 'package:spotube/models/Logger.dart';
import 'package:spotube/provider/Playback.dart'; import 'package:spotube/provider/Playback.dart';
import 'package:spotube/utils/platform.dart';
class PlayPauseIntent extends Intent { class PlayPauseIntent extends Intent {
final WidgetRef ref; final WidgetRef ref;
@ -40,16 +44,51 @@ class PlayPauseAction extends Action<PlayPauseIntent> {
} }
} }
class OpenSettingsIntent extends Intent { class NavigationIntent extends Intent {
final GoRouter router; final GoRouter router;
const OpenSettingsIntent(this.router); final String path;
const NavigationIntent(this.router, this.path);
} }
class OpenSettingsAction extends Action<OpenSettingsIntent> { class NavigationAction extends Action<NavigationIntent> {
@override @override
invoke(intent) async { invoke(intent) {
intent.router.push("/settings"); intent.router.go(intent.path);
FocusManager.instance.primaryFocus?.unfocus(); 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<HomeTabIntent> {
@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; return null;
} }
} }
@ -83,3 +122,17 @@ class SeekAction extends Action<SeekIntent> {
return null; return null;
} }
} }
class CloseAppIntent extends Intent {}
class CloseAppAction extends Action<CloseAppIntent> {
@override
invoke(intent) {
if (kIsDesktop) {
appWindow.close();
} else {
SystemNavigator.pop();
}
return null;
}
}