mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-18 06:13:42 +00:00
feat: new ui (#1455)
This commit is contained in:
742 files changed
+59951
-5934
No files matched your search
@@ -0,0 +1,236 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/material.dart' show Icons, Scrollbar, ScrollbarOrientation;
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../../../../../api/model/task_stats.dart';
|
||||
import '../../../../../core/utils/byte_size_formatter.dart';
|
||||
import '../../../../../shared/theme/app_palette.dart';
|
||||
import '../../../../../l10n/l10n.dart';
|
||||
|
||||
enum PeerTableProtocol { bt, ed2k }
|
||||
|
||||
enum _PeerSortColumn { address, client, transport, downloadSpeed, uploadSpeed, pieces, completion, relevance, source }
|
||||
|
||||
class PeerTable extends StatefulWidget {
|
||||
const PeerTable({super.key, required this.protocol, required this.peers});
|
||||
|
||||
final PeerTableProtocol protocol;
|
||||
final List<TaskPeerStats> peers;
|
||||
|
||||
@override
|
||||
State<PeerTable> createState() => _PeerTableState();
|
||||
}
|
||||
|
||||
class _PeerTableState extends State<PeerTable> {
|
||||
static const _headerHeight = 40.0;
|
||||
static const _rowHeight = 38.0;
|
||||
|
||||
double get _tableWidth => widget.protocol == PeerTableProtocol.bt ? 1070 : 870;
|
||||
|
||||
final ScrollController _horizontalController = ScrollController();
|
||||
final ScrollController _verticalController = ScrollController();
|
||||
_PeerSortColumn _sortColumn = _PeerSortColumn.downloadSpeed;
|
||||
bool _ascending = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_horizontalController.dispose();
|
||||
_verticalController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final palette = AppPalette.of(context);
|
||||
final peers = [...widget.peers]..sort(_comparePeers);
|
||||
return SizedBox(
|
||||
height: 300,
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final contentWidth = math.max(_tableWidth, constraints.maxWidth);
|
||||
return Scrollbar(
|
||||
controller: _horizontalController,
|
||||
thumbVisibility: true,
|
||||
trackVisibility: true,
|
||||
interactive: true,
|
||||
thickness: 7,
|
||||
scrollbarOrientation: ScrollbarOrientation.bottom,
|
||||
child: SingleChildScrollView(
|
||||
controller: _horizontalController,
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: SizedBox(
|
||||
width: contentWidth,
|
||||
height: constraints.maxHeight,
|
||||
child: Column(
|
||||
children: [
|
||||
Container(
|
||||
height: _headerHeight,
|
||||
decoration: BoxDecoration(
|
||||
color: palette.sideBg,
|
||||
border: Border(bottom: BorderSide(color: palette.border)),
|
||||
),
|
||||
child: Row(children: _headers(context)),
|
||||
),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
controller: _verticalController,
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
itemCount: peers.length,
|
||||
itemExtent: _rowHeight,
|
||||
itemBuilder: (context, index) => _PeerRow(protocol: widget.protocol, peer: peers[index]),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<Widget> _headers(BuildContext context) => [
|
||||
_header(context.l10n.address, _PeerSortColumn.address, 180),
|
||||
_header(context.l10n.client, _PeerSortColumn.client, 150),
|
||||
_header(context.l10n.protocol, _PeerSortColumn.transport, 80),
|
||||
_header(context.l10n.speed, _PeerSortColumn.downloadSpeed, 120),
|
||||
_header(context.l10n.uploadSpeed, _PeerSortColumn.uploadSpeed, 120),
|
||||
_header(context.l10n.pieces, _PeerSortColumn.pieces, 100),
|
||||
if (widget.protocol == PeerTableProtocol.bt) ...[
|
||||
_header(context.l10n.progress, _PeerSortColumn.completion, 100),
|
||||
_header(context.l10n.fileRelevance, _PeerSortColumn.relevance, 100),
|
||||
],
|
||||
_header(context.l10n.source, _PeerSortColumn.source, 120),
|
||||
];
|
||||
|
||||
Widget _header(String label, _PeerSortColumn column, double width) {
|
||||
final palette = AppPalette.of(context);
|
||||
final active = _sortColumn == column;
|
||||
return GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onTap: () {
|
||||
setState(() {
|
||||
if (_sortColumn == column) {
|
||||
_ascending = !_ascending;
|
||||
} else {
|
||||
_sortColumn = column;
|
||||
_ascending = column != _PeerSortColumn.downloadSpeed && column != _PeerSortColumn.uploadSpeed;
|
||||
}
|
||||
});
|
||||
},
|
||||
child: SizedBox(
|
||||
width: width,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
label,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
color: active ? palette.textPrimary : palette.textSecondary,
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (active) ...[
|
||||
const SizedBox(width: 3),
|
||||
Icon(_ascending ? Icons.arrow_upward : Icons.arrow_downward, size: 12, color: palette.textMuted),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
int _comparePeers(TaskPeerStats a, TaskPeerStats b) {
|
||||
final result = switch (_sortColumn) {
|
||||
_PeerSortColumn.address => a.address.compareTo(b.address),
|
||||
_PeerSortColumn.client => a.client.compareTo(b.client),
|
||||
_PeerSortColumn.transport => a.transport.compareTo(b.transport),
|
||||
_PeerSortColumn.downloadSpeed => a.downloadSpeed.compareTo(b.downloadSpeed),
|
||||
_PeerSortColumn.uploadSpeed => a.uploadSpeed.compareTo(b.uploadSpeed),
|
||||
_PeerSortColumn.pieces => a.pieceCount.compareTo(b.pieceCount),
|
||||
_PeerSortColumn.completion => (a.completion ?? -1).compareTo(b.completion ?? -1),
|
||||
_PeerSortColumn.relevance => (a.relevance ?? -1).compareTo(b.relevance ?? -1),
|
||||
_PeerSortColumn.source => a.source.compareTo(b.source),
|
||||
};
|
||||
return _ascending ? result : -result;
|
||||
}
|
||||
}
|
||||
|
||||
class _PeerRow extends StatelessWidget {
|
||||
const _PeerRow({required this.protocol, required this.peer});
|
||||
|
||||
final PeerTableProtocol protocol;
|
||||
final TaskPeerStats peer;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final palette = AppPalette.of(context);
|
||||
return DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
border: Border(bottom: BorderSide(color: palette.border)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
_cell(context, peer.address, 180),
|
||||
_cell(context, peer.client, 150),
|
||||
_cell(context, _formatProtocol(protocol, peer.transport), 80),
|
||||
_cell(context, _formatRate(peer.downloadSpeed), 120),
|
||||
_cell(context, _formatRate(peer.uploadSpeed), 120),
|
||||
_cell(context, peer.pieceCount.toString(), 100),
|
||||
if (protocol == PeerTableProtocol.bt) ...[
|
||||
_cell(context, _formatPercentage(peer.completion), 100),
|
||||
_cell(context, _formatPercentage(peer.relevance), 100),
|
||||
],
|
||||
_cell(context, peer.source, 120),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _cell(BuildContext context, String value, double width) {
|
||||
final palette = AppPalette.of(context);
|
||||
return SizedBox(
|
||||
width: width,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10),
|
||||
child: Text(
|
||||
value,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(color: palette.textSecondary, fontSize: 11),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _formatRate(int bytesPerSecond) => bytesPerSecond <= 0 ? '—' : '${_formatBytes(bytesPerSecond)}/s';
|
||||
|
||||
String _formatProtocol(PeerTableProtocol protocol, String transport) {
|
||||
if (protocol == PeerTableProtocol.ed2k) return 'ED2K';
|
||||
return switch (transport.toLowerCase()) {
|
||||
'utp' => 'uTP',
|
||||
'webrtc' => 'WebRTC',
|
||||
_ => 'BT',
|
||||
};
|
||||
}
|
||||
|
||||
String _formatPercentage(double? ratio) {
|
||||
if (ratio == null) return '—';
|
||||
final percentage = ratio.clamp(0.0, 1.0) * 100;
|
||||
final fractionDigits = percentage == percentage.roundToDouble() ? 0 : 1;
|
||||
return '${percentage.toStringAsFixed(fractionDigits)}%';
|
||||
}
|
||||
|
||||
String _formatBytes(int bytes) {
|
||||
return ByteSizeFormatter.format(bytes);
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../../../../../api/model/task_stats.dart';
|
||||
import '../../../../../core/utils/byte_size_formatter.dart';
|
||||
import '../../../../../shared/theme/app_palette.dart';
|
||||
import '../../../../../l10n/l10n.dart';
|
||||
|
||||
class PieceMap extends StatefulWidget {
|
||||
const PieceMap({super.key, required this.pieceMap, this.totalBytes});
|
||||
|
||||
final TaskPieceMap pieceMap;
|
||||
final int? totalBytes;
|
||||
|
||||
@override
|
||||
State<PieceMap> createState() => _PieceMapState();
|
||||
}
|
||||
|
||||
class _PieceMapState extends State<PieceMap> {
|
||||
static const _gap = 3.0;
|
||||
static const _targetCellSize = 11.0;
|
||||
// The default desktop drawer fits 28 columns, so 252 cells fill 9 rows.
|
||||
static const _maxPaintedCells = 252;
|
||||
|
||||
int? _focusedCell;
|
||||
PointerDeviceKind? _lastPointerKind;
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant PieceMap oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (_focusedCell != null && _focusedCell! >= math.min(widget.pieceMap.pieceCount, _maxPaintedCells)) {
|
||||
_focusedCell = null;
|
||||
}
|
||||
}
|
||||
|
||||
List<TaskPieceState> _displayPieces(int displayCount) {
|
||||
final pieceMap = widget.pieceMap;
|
||||
if (pieceMap.pieceCount <= displayCount) {
|
||||
return List.generate(pieceMap.pieceCount, pieceMap.stateAt, growable: false);
|
||||
}
|
||||
return List.generate(displayCount, (cell) {
|
||||
final start = cell * pieceMap.pieceCount ~/ displayCount;
|
||||
final end = math.max(start + 1, (cell + 1) * pieceMap.pieceCount ~/ displayCount);
|
||||
var allCompleted = true;
|
||||
for (var index = start; index < math.min(end, pieceMap.pieceCount); index++) {
|
||||
final state = pieceMap.stateAt(index);
|
||||
if (state != TaskPieceState.completed) allCompleted = false;
|
||||
}
|
||||
if (allCompleted) return TaskPieceState.completed;
|
||||
return TaskPieceState.pending;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final palette = AppPalette.of(context);
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
if (widget.pieceMap.isEmpty) return const SizedBox.shrink();
|
||||
final width = constraints.maxWidth;
|
||||
final columns = math.max(1, ((width + _gap) / (_targetCellSize + _gap)).floor());
|
||||
final displayCount = math.min(widget.pieceMap.pieceCount, _maxPaintedCells);
|
||||
final pieces = _displayPieces(displayCount);
|
||||
final focusedCell = _focusedCell != null && _focusedCell! < pieces.length ? _focusedCell : null;
|
||||
final cellSize = (width - _gap * (columns - 1)) / columns;
|
||||
final rows = (pieces.length / columns).ceil();
|
||||
final mapHeight = rows * cellSize + math.max(0, rows - 1) * _gap;
|
||||
|
||||
int? cellAt(Offset offset) {
|
||||
final column = (offset.dx / (cellSize + _gap)).floor();
|
||||
final row = (offset.dy / (cellSize + _gap)).floor();
|
||||
if (column < 0 || column >= columns || row < 0 || row >= rows) return null;
|
||||
final localX = offset.dx - column * (cellSize + _gap);
|
||||
final localY = offset.dy - row * (cellSize + _gap);
|
||||
if (localX > cellSize || localY > cellSize) return null;
|
||||
final index = row * columns + column;
|
||||
return index < pieces.length ? index : null;
|
||||
}
|
||||
|
||||
void updateFocus(PointerEvent event, {required bool pressed}) {
|
||||
final index = cellAt(event.localPosition);
|
||||
if (pressed || event.kind == PointerDeviceKind.mouse || event.kind == PointerDeviceKind.trackpad) {
|
||||
setState(() {
|
||||
_focusedCell = index;
|
||||
_lastPointerKind = event.kind;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
MouseRegion(
|
||||
onExit: (_) {
|
||||
if (_lastPointerKind == PointerDeviceKind.mouse || _lastPointerKind == PointerDeviceKind.trackpad) {
|
||||
setState(() => _focusedCell = null);
|
||||
}
|
||||
},
|
||||
child: Listener(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onPointerHover: (event) => updateFocus(event, pressed: false),
|
||||
onPointerDown: (event) => updateFocus(event, pressed: true),
|
||||
child: CustomPaint(
|
||||
size: Size(width, mapHeight),
|
||||
painter: _PieceMapPainter(
|
||||
pieces: pieces,
|
||||
columns: columns,
|
||||
cellSize: cellSize,
|
||||
gap: _gap,
|
||||
palette: palette,
|
||||
focusedCell: focusedCell,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 24,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 6),
|
||||
child: Text(
|
||||
focusedCell == null ? '' : _rangeLabel(focusedCell, pieces.length),
|
||||
style: TextStyle(color: palette.textMuted, fontSize: 11),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
String _rangeLabel(int displayIndex, int displayCount) {
|
||||
final sourceCount = widget.pieceMap.pieceCount;
|
||||
final startPiece = displayIndex * sourceCount ~/ displayCount;
|
||||
final endPiece = math.max(startPiece + 1, (displayIndex + 1) * sourceCount ~/ displayCount) - 1;
|
||||
final pieceLabel = startPiece == endPiece
|
||||
? context.l10n.piece(startPiece + 1)
|
||||
: context.l10n.pieceRange(startPiece + 1, endPiece + 1);
|
||||
final bytesPerPiece = widget.pieceMap.pieceSize;
|
||||
if (bytesPerPiece <= 0) return pieceLabel;
|
||||
final startByte = startPiece * bytesPerPiece;
|
||||
final rawEnd = (endPiece + 1) * bytesPerPiece - 1;
|
||||
final endByte = widget.totalBytes == null ? rawEnd : math.min(rawEnd, widget.totalBytes! - 1);
|
||||
return '$pieceLabel · ${_formatBytes(startByte)}–${_formatBytes(math.max(startByte, endByte))}';
|
||||
}
|
||||
}
|
||||
|
||||
class _PieceMapPainter extends CustomPainter {
|
||||
const _PieceMapPainter({
|
||||
required this.pieces,
|
||||
required this.columns,
|
||||
required this.cellSize,
|
||||
required this.gap,
|
||||
required this.palette,
|
||||
required this.focusedCell,
|
||||
});
|
||||
|
||||
final List<TaskPieceState> pieces;
|
||||
final int columns;
|
||||
final double cellSize;
|
||||
final double gap;
|
||||
final AppPalette palette;
|
||||
final int? focusedCell;
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final paint = Paint();
|
||||
for (var index = 0; index < pieces.length; index++) {
|
||||
final column = index % columns;
|
||||
final row = index ~/ columns;
|
||||
final cellRect = Rect.fromLTWH(column * (cellSize + gap), row * (cellSize + gap), cellSize, cellSize);
|
||||
final rect = RRect.fromRectAndRadius(cellRect, const Radius.circular(2));
|
||||
final state = pieces[index];
|
||||
paint.color = switch (state) {
|
||||
TaskPieceState.pending => palette.surfaceSoft,
|
||||
TaskPieceState.completed => palette.success,
|
||||
};
|
||||
canvas.drawRRect(rect, paint);
|
||||
if (focusedCell == index) {
|
||||
paint
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1.5
|
||||
..color = palette.textPrimary;
|
||||
canvas.drawRRect(rect, paint);
|
||||
paint.style = PaintingStyle.fill;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant _PieceMapPainter oldDelegate) {
|
||||
return oldDelegate.pieces != pieces ||
|
||||
oldDelegate.palette != palette ||
|
||||
oldDelegate.focusedCell != focusedCell ||
|
||||
oldDelegate.columns != columns ||
|
||||
oldDelegate.cellSize != cellSize;
|
||||
}
|
||||
}
|
||||
|
||||
String _formatBytes(int bytes) {
|
||||
return ByteSizeFormatter.format(bytes);
|
||||
}
|
||||
+528
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user