mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2026-02-21 07:34:24 +01:00
#64 new: process page
This commit is contained in:
139
lib/view/page/process.dart
Normal file
139
lib/view/page/process.dart
Normal file
@@ -0,0 +1,139 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
import 'package:toolbox/core/extension/stringx.dart';
|
||||
import 'package:toolbox/core/extension/uint8list.dart';
|
||||
import 'package:toolbox/core/utils/ui.dart';
|
||||
import 'package:toolbox/data/model/server/proc.dart';
|
||||
import 'package:toolbox/data/model/server/server_private_info.dart';
|
||||
import 'package:toolbox/data/res/ui.dart';
|
||||
import 'package:toolbox/view/widget/round_rect_card.dart';
|
||||
import 'package:toolbox/view/widget/two_line_text.dart';
|
||||
|
||||
import '../../data/provider/server.dart';
|
||||
import '../../locator.dart';
|
||||
|
||||
class ProcessPage extends StatefulWidget {
|
||||
final ServerPrivateInfo spi;
|
||||
const ProcessPage({super.key, required this.spi});
|
||||
|
||||
@override
|
||||
_ProcessPageState createState() => _ProcessPageState();
|
||||
}
|
||||
|
||||
class _ProcessPageState extends State<ProcessPage> {
|
||||
late S _s;
|
||||
late Timer _timer;
|
||||
|
||||
PsResult _result = PsResult(procs: []);
|
||||
int? _lastFocusId;
|
||||
ProcSortMode _procSortMode = ProcSortMode.cpu;
|
||||
|
||||
final _serverProvider = locator<ServerProvider>();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final client = _serverProvider.servers[widget.spi.id]?.client;
|
||||
if (client == null) {
|
||||
showSnackBar(context, Text(_s.noClient));
|
||||
return;
|
||||
}
|
||||
_timer = Timer.periodic(const Duration(seconds: 3), (_) async {
|
||||
if (mounted) {
|
||||
final result = await client.run('ps -aux'.withLangExport).string;
|
||||
if (result.isEmpty) {
|
||||
showSnackBar(context, Text(_s.noResult));
|
||||
return;
|
||||
}
|
||||
_result = PsResult.parse(result, sort: _procSortMode);
|
||||
setState(() {});
|
||||
} else {
|
||||
_timer.cancel();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
_timer.cancel();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_s = S.of(context)!;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final actions = <Widget>[
|
||||
PopupMenuButton<ProcSortMode>(
|
||||
onSelected: (value) {
|
||||
setState(() {
|
||||
_procSortMode = value;
|
||||
});
|
||||
},
|
||||
icon: const Icon(Icons.sort),
|
||||
initialValue: _procSortMode,
|
||||
itemBuilder: (_) => ProcSortMode.values
|
||||
.map(
|
||||
(e) => PopupMenuItem(value: e, child: Text(e.name)),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
];
|
||||
if (_result.error != null) {
|
||||
actions.add(IconButton(
|
||||
icon: const Icon(Icons.error),
|
||||
onPressed: () => showRoundDialog(
|
||||
context: context,
|
||||
child: Text(_result.error!),
|
||||
),
|
||||
));
|
||||
}
|
||||
Widget child;
|
||||
if (_result.procs.isEmpty) {
|
||||
child = centerLoading;
|
||||
} else {
|
||||
child = ListView.builder(
|
||||
itemCount: _result.procs.length,
|
||||
itemBuilder: (ctx, idx) {
|
||||
final proc = _result.procs[idx];
|
||||
return _buildListItem(proc);
|
||||
},
|
||||
);
|
||||
}
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
title: TwoLineText(up: widget.spi.name, down: _s.process),
|
||||
actions: actions,
|
||||
),
|
||||
body: child,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildListItem(Proc proc) {
|
||||
return RoundRectCard(ListTile(
|
||||
leading: SizedBox(
|
||||
width: 57,
|
||||
child: TwoLineText(up: proc.pid.toString(), down: 'pid'),
|
||||
),
|
||||
title: Text(proc.binary),
|
||||
subtitle: Text(proc.command, style: grey),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TwoLineText(up: proc.cpu.toStringAsFixed(1), down: 'cpu'),
|
||||
width13,
|
||||
TwoLineText(up: proc.mem.toStringAsFixed(1), down: 'mem'),
|
||||
],
|
||||
),
|
||||
onTap: () => _lastFocusId = proc.pid,
|
||||
autofocus: _lastFocusId == proc.pid,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import 'package:provider/provider.dart';
|
||||
import 'package:toolbox/core/extension/navigator.dart';
|
||||
import 'package:toolbox/core/extension/order.dart';
|
||||
import 'package:toolbox/core/utils/misc.dart';
|
||||
import 'package:toolbox/data/res/server_cmd.dart';
|
||||
import 'package:toolbox/view/page/process.dart';
|
||||
|
||||
import '../../../core/route.dart';
|
||||
import '../../../core/utils/ui.dart';
|
||||
@@ -338,13 +338,14 @@ class _ServerPageState extends State<ServerPage>
|
||||
AppRoute(DockerManagePage(spi), 'Docker manage').go(context);
|
||||
break;
|
||||
case ServerTabMenuType.process:
|
||||
AppRoute(
|
||||
SSHPage(
|
||||
spi: spi,
|
||||
initCmd: 'sh $shellPath -${shellFuncProcess.flag}',
|
||||
),
|
||||
'ssh page (process)',
|
||||
).go(context);
|
||||
// AppRoute(
|
||||
// SSHPage(
|
||||
// spi: spi,
|
||||
// initCmd: 'sh $shellPath -${shellFuncProcess.flag}',
|
||||
// ),
|
||||
// 'ssh page (process)',
|
||||
// ).go(context);
|
||||
AppRoute(ProcessPage(spi: spi), 'process page').go(context);
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
@@ -10,14 +10,18 @@ class TwoLineText extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
up,
|
||||
style: textSize15,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
Text(
|
||||
down,
|
||||
style: textSize11,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
)
|
||||
],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user