Skip to content

Commit 032ee9b

Browse files
committed
fix: Stop player when hiding the window
1 parent 2f73225 commit 032ee9b

File tree

4 files changed

+35
-39
lines changed

4 files changed

+35
-39
lines changed

app/lib/main.dart

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -116,40 +116,9 @@ class PikaTorrent extends StatelessWidget {
116116
}
117117
}
118118

119-
class PikaTorrentApp extends StatefulWidget {
119+
class PikaTorrentApp extends StatelessWidget {
120120
const PikaTorrentApp({super.key});
121121

122-
@override
123-
State<PikaTorrentApp> createState() => _PikaTorrentAppState();
124-
}
125-
126-
class _PikaTorrentAppState extends State<PikaTorrentApp> with WindowListener {
127-
@override
128-
void initState() {
129-
super.initState();
130-
windowManager.addListener(this);
131-
_init();
132-
}
133-
134-
@override
135-
void dispose() {
136-
windowManager.removeListener(this);
137-
super.dispose();
138-
}
139-
140-
void _init() async {
141-
if (isDesktop()) {
142-
// Add this line to override the default close handler
143-
await windowManager.setPreventClose(true);
144-
setState(() {});
145-
}
146-
}
147-
148-
@override
149-
void onWindowClose() async {
150-
windowManager.hide();
151-
}
152-
153122
// App root
154123
@override
155124
Widget build(BuildContext context) {

app/lib/navigation/app_shell_route.dart

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import 'package:pikatorrent/navigation/navigation.dart';
1212
import 'package:pikatorrent/platforms/desktop/tray.dart';
1313
import 'package:pikatorrent/utils/app_links.dart';
1414
import 'package:pikatorrent/utils/connectivity.dart';
15+
import 'package:pikatorrent/utils/device.dart';
1516
import 'package:pikatorrent/utils/update.dart';
1617
import 'package:provider/provider.dart';
18+
import 'package:window_manager/window_manager.dart';
1719

1820
class AppShellRoute extends StatefulWidget {
1921
final Widget child;
@@ -24,7 +26,7 @@ class AppShellRoute extends StatefulWidget {
2426
State<AppShellRoute> createState() => _AppShellRouteState();
2527
}
2628

27-
class _AppShellRouteState extends State<AppShellRoute> {
29+
class _AppShellRouteState extends State<AppShellRoute> with WindowListener {
2830
late AppLinks _appLinks;
2931
bool isTermsOfUseDialogDisplayed = false;
3032
bool hasShownUpdateDialog = false;
@@ -36,14 +38,35 @@ class _AppShellRouteState extends State<AppShellRoute> {
3638
startConnectivityCheck(context);
3739
initTray(context);
3840
_initAppLinks();
41+
windowManager.addListener(this);
42+
_initWindowManager();
3943
}
4044

4145
@override
4246
void dispose() {
4347
stopConnectivityCheck();
48+
windowManager.removeListener(this);
4449
super.dispose();
4550
}
4651

52+
@override
53+
void onWindowClose() async {
54+
// Pop video player if it's the current route
55+
if (ModalRoute.of(context)?.settings.name == 'player') {
56+
Navigator.pop(context);
57+
}
58+
59+
windowManager.hide();
60+
}
61+
62+
_initWindowManager() async {
63+
if (isDesktop()) {
64+
// Add this line to override the default close handler
65+
await windowManager.setPreventClose(true);
66+
setState(() {});
67+
}
68+
}
69+
4770
_initAppLinks() {
4871
_appLinks = AppLinks();
4972
_appLinks.uriLinkStream.listen((uri) async {

app/lib/screens/torrents/sheets/torrent_details/tabs/files.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ class FilesTab extends StatelessWidget {
4040
_handlePlayClick(BuildContext context, File file) {
4141
String filePath = path.join(location, file.name);
4242

43-
Navigator.of(context, rootNavigator: true)
44-
.push(MaterialPageRoute(builder: (BuildContext context) {
45-
return TorrentPlayer(filePath: filePath, torrent: torrent, file: file);
46-
}));
43+
Navigator.of(context, rootNavigator: true).push(MaterialPageRoute(
44+
settings: const RouteSettings(name: 'player'),
45+
builder: (BuildContext context) {
46+
return TorrentPlayer(
47+
filePath: filePath, torrent: torrent, file: file);
48+
}));
4749
}
4850

4951
@override

app/lib/widgets/torrent_player/torrent_player.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class TorrentPlayerState extends State<TorrentPlayer> {
6666
@override
6767
void dispose() {
6868
widget.torrent.stopStreaming();
69+
player.stop();
6970
player.dispose();
7071
server.stop();
7172
subsServer.stop();
@@ -164,7 +165,6 @@ class TorrentPlayerState extends State<TorrentPlayer> {
164165
return IconButton(
165166
icon: const Icon(Icons.arrow_back, color: Colors.white),
166167
onPressed: () {
167-
player.stop();
168168
Navigator.pop(context);
169169
},
170170
);
@@ -203,7 +203,9 @@ class TorrentPlayerState extends State<TorrentPlayer> {
203203
seekBarThumbColor: Colors.blue,
204204
seekBarPositionColor: Colors.blue,
205205
padding: const EdgeInsets.only(bottom: 64),
206-
topButtonBar: [_buildBackButton()],
206+
topButtonBar: [
207+
_buildBackButton()
208+
],
207209
bottomButtonBar: [
208210
const MaterialPositionIndicator(),
209211
const Spacer(),

0 commit comments

Comments
 (0)