Skip to content

Commit

Permalink
fix: keyboard shortcuts changing route but not update sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Sep 26, 2023
1 parent 6dced5e commit 2d93441
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 23 deletions.
11 changes: 11 additions & 0 deletions lib/collections/assets.gen.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/collections/intents.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_desktop_tools/flutter_desktop_tools.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:spotube/components/player/player_controls.dart';
Expand All @@ -8,7 +9,6 @@ import 'package:spotube/models/logger.dart';
import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart';
import 'package:spotube/services/audio_player/audio_player.dart';
import 'package:spotube/utils/platform.dart';
import 'package:window_manager/window_manager.dart';

class PlayPauseIntent extends Intent {
final WidgetRef ref;
Expand Down Expand Up @@ -115,7 +115,7 @@ class CloseAppAction extends Action<CloseAppIntent> {
@override
invoke(intent) {
if (kIsDesktop) {
windowManager.close();
DesktopTools.window.close();
} else {
SystemNavigator.pop();
}
Expand Down
13 changes: 11 additions & 2 deletions lib/components/root/sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,17 @@ class Sidebar extends HookConsumerWidget {
Color.lerp(bg, Colors.black, 0.45)!,
);

final sidebarTileList =
useMemoized(() => getSidebarTileList(context.l10n), [context.l10n]);
final sidebarTileList = useMemoized(
() => getSidebarTileList(context.l10n),
[context.l10n],
);

useEffect(() {
if (controller.selectedIndex != selectedIndex) {
controller.selectIndex(selectedIndex);
}
return null;
}, [selectedIndex]);

useEffect(() {
controller.addListener(() {
Expand Down
23 changes: 11 additions & 12 deletions lib/components/shared/page_window_title_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotube/provider/user_preferences_provider.dart';
import 'package:spotube/utils/platform.dart';
import 'package:titlebar_buttons/titlebar_buttons.dart';
import 'package:window_manager/window_manager.dart';
import 'dart:math';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'dart:io' show Platform;
Expand All @@ -18,7 +17,7 @@ final closeNotification = DesktopTools.createNotification(
LocalNotificationAction(text: 'Close The App'),
],
)?..onClickAction = (value) {
windowManager.close();
DesktopTools.window.close();
};

class PageWindowTitleBar extends StatefulHookConsumerWidget
Expand Down Expand Up @@ -114,16 +113,16 @@ class WindowTitleBarButtons extends HookConsumerWidget {

Future<void> onClose() async {
if (preferences.closeBehavior == CloseBehavior.close) {
await windowManager.close();
await DesktopTools.window.close();
} else {
await windowManager.hide();
await DesktopTools.window.hide();
await closeNotification?.show();
}
}

useEffect(() {
if (kIsDesktop) {
windowManager.isMaximized().then((value) {
DesktopTools.window.isMaximized().then((value) {
isMaximized.value = value;
});
}
Expand Down Expand Up @@ -160,22 +159,22 @@ class WindowTitleBarButtons extends HookConsumerWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MinimizeWindowButton(
onPressed: windowManager.minimize,
onPressed: DesktopTools.window.minimize,
colors: colors,
),
if (isMaximized.value != true)
MaximizeWindowButton(
colors: colors,
onPressed: () {
windowManager.maximize();
DesktopTools.window.maximize();
isMaximized.value = true;
},
)
else
RestoreWindowButton(
colors: colors,
onPressed: () {
windowManager.unmaximize();
DesktopTools.window.unmaximize();
isMaximized.value = false;
},
),
Expand All @@ -195,16 +194,16 @@ class WindowTitleBarButtons extends HookConsumerWidget {
children: [
DecoratedMinimizeButton(
type: type,
onPressed: windowManager.minimize,
onPressed: DesktopTools.window.minimize,
),
DecoratedMaximizeButton(
type: type,
onPressed: () async {
if (await windowManager.isMaximized()) {
await windowManager.unmaximize();
if (await DesktopTools.window.isMaximized()) {
await DesktopTools.window.unmaximize();
isMaximized.value = false;
} else {
await windowManager.maximize();
await DesktopTools.window.maximize();
isMaximized.value = true;
}
},
Expand Down
10 changes: 10 additions & 0 deletions lib/extensions/duration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ extension DurationToHumanReadableString on Duration {
abbreviated: abbreviated,
);
}

extension ParseDuration on Duration {
static Duration fromString(String duration) {
final parts = duration.split(':').reversed.toList();
final seconds = int.parse(parts[0]);
final minutes = parts.length > 1 ? int.parse(parts[1]) : 0;
final hours = parts.length > 2 ? int.parse(parts[2]) : 0;
return Duration(hours: hours, minutes: minutes, seconds: seconds);
}
}
12 changes: 5 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ class SpotubeState extends ConsumerState<Spotube> {
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
routeInformationParser: router.routeInformationParser,
routerDelegate: router.routerDelegate,
routeInformationProvider: router.routeInformationProvider,
routerConfig: router,
debugShowCheckedModeBanner: false,
title: 'Spotube',
builder: (context, child) {
Expand All @@ -229,22 +227,22 @@ class SpotubeState extends ConsumerState<Spotube> {
LogicalKeySet(LogicalKeyboardKey.comma, LogicalKeyboardKey.control):
NavigationIntent(router, "/settings"),
LogicalKeySet(
LogicalKeyboardKey.keyB,
LogicalKeyboardKey.digit1,
LogicalKeyboardKey.control,
LogicalKeyboardKey.shift,
): HomeTabIntent(ref, tab: HomeTabs.browse),
LogicalKeySet(
LogicalKeyboardKey.keyS,
LogicalKeyboardKey.digit2,
LogicalKeyboardKey.control,
LogicalKeyboardKey.shift,
): HomeTabIntent(ref, tab: HomeTabs.search),
LogicalKeySet(
LogicalKeyboardKey.keyL,
LogicalKeyboardKey.digit3,
LogicalKeyboardKey.control,
LogicalKeyboardKey.shift,
): HomeTabIntent(ref, tab: HomeTabs.library),
LogicalKeySet(
LogicalKeyboardKey.keyY,
LogicalKeyboardKey.digit4,
LogicalKeyboardKey.control,
LogicalKeyboardKey.shift,
): HomeTabIntent(ref, tab: HomeTabs.lyrics),
Expand Down
16 changes: 16 additions & 0 deletions lib/pages/root/root_app.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
Expand Down Expand Up @@ -39,6 +40,21 @@ class RootApp extends HookConsumerWidget {
final downloader = ref.watch(downloadManagerProvider);
final scaffoldMessenger = ScaffoldMessenger.of(context);
final theme = Theme.of(context);
final location = GoRouterState.of(context).matchedLocation;

useEffect(() {
final newIndex = rootPaths.entries.firstWhereOrNull((e) {
if (e.value == "/" || location == "/") {
return location == e.value;
}
return location.startsWith(e.value);
})?.key;
if (newIndex != null) {
index.value = newIndex;
}

return null;
}, [location]);

useEffect(() {
WidgetsBinding.instance.addPostFrameCallback((_) async {
Expand Down

0 comments on commit 2d93441

Please sign in to comment.