Skip to content

PasteFerry (隔空贴): clipboard and file sync across macOS, Windows and Linux. Direct-first, end-to-end encrypted. Optional official access: no server to host. Independent self-hosting remains available.

12.4 KB
Raw Blame
import 'package:flutter/material.dart';

import '../app_state.dart';
import '../gen/pasteferry/v1/daemon.pb.dart';
import '../i18n.dart';
import '../icons.dart';
import '../main.dart' show AppScope;
import '../theme.dart';
import '../widgets/common.dart';
import '../widgets/send_dialog.dart';
import '../pixel_theme.dart';
import 'host.dart';
import 'file_picker.dart';
import 'ui.dart';

/// 手机上的隔空传送(design/android-send-files.md):挑一台设备发文件,看收发进度,
/// 收到的文件保存到「下载」或分享出去。
///
/// 桌面那页是给大屏和拖拽设计的,手机上重做:设备一行一个「选择文件」,
/// 下面按时间倒序列出发出的和收到的;状态文案与桌面共用,排版按触屏设计。
class TransfersTab extends StatefulWidget {
  const TransfersTab({super.key, this.onPair});

  /// 还没有可以发的设备时,「去配对」切到设备页
  final VoidCallback? onPair;

  @override
  State<TransfersTab> createState() => _TransfersTabState();
}

class _TransfersTabState extends State<TransfersTab> {
  bool _picking = false;

  Future<void> _send(AppState state, Device device) async {
    if (_picking) return;
    final l = context.l10n;
    setState(() => _picking = true);
    try {
      final picked = await pickFilesForSend(context);
      if (picked == null || !mounted) return;
      await sendFilesTo(context, device, [
        for (final file in picked) file.path,
      ]);
    } catch (e) {
      if (mounted) {
        showToast(
          context,
          l.mSendFailed(AppState.describeError(e)),
          error: true,
        );
      }
    } finally {
      if (mounted) setState(() => _picking = false);
    }
  }

  Future<void> _save(Transfer t) async {
    final l = context.l10n;
    try {
      await Host.save(t.paths.where((p) => p.isNotEmpty).toList());
      if (mounted) showToast(context, l.mSaved);
    } catch (e) {
      if (mounted) showToast(context, l.mSaveMediaFailed, error: true);
    }
  }

  Future<void> _share(Transfer t) async {
    final l = context.l10n;
    try {
      await Host.share(t.paths.where((p) => p.isNotEmpty).toList());
    } catch (e) {
      if (mounted) showToast(context, l.mShareMediaFailed, error: true);
    }
  }

  @override
  Widget build(BuildContext context) {
    final state = AppScope.of(context);
    final l = context.l10n;

    // 能收文件的设备才列出来:其余的在设备页里管
    final devices = state.peers
        .where((d) => d.online && d.receivesFiles && !d.receiveDisabled)
        .toList(growable: false);
    final rows = [
      ...state.transfers(TransferDirection.TRANSFER_DIRECTION_INCOMING),
      ...state.transfers(TransferDirection.TRANSFER_DIRECTION_OUTGOING),
    ]..sort((a, b) => b.createdAtUnix.compareTo(a.createdAtUnix));

    return SafeArea(
      bottom: false,
      child: ListView(
        physics: const AlwaysScrollableScrollPhysics(),
        padding: const EdgeInsets.only(bottom: Insets.xl),
        children: [
          MobileHeader(title: l.navSend),
          MobileGroup(
            title: l.navDevices,
            footer: devices.isEmpty ? l.mNoOnlineDevices : null,
            children: [
              for (final d in devices)
                Padding(
                  padding: const EdgeInsets.all(Insets.lg),
                  child: Row(
                    children: [
                      Icon(
                        AppIcons.deviceLaptop,
                        size: 24,
                        color: context.palette.textDim,
                      ),
                      const SizedBox(width: Insets.md),
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              d.name,
                              maxLines: 2,
                              overflow: TextOverflow.ellipsis,
                              style: context.text.bodyLarge,
                            ),
                            Text(
                              routeLabel(l, state, d),
                              style: context.text.bodySmall,
                            ),
                          ],
                        ),
                      ),
                      const SizedBox(width: Insets.sm),
                      ConstrainedBox(
                        constraints: const BoxConstraints(maxWidth: 140),
                        child: FilledButton(
                          style: secondaryButtonStyle(context),
                          onPressed: _picking ? null : () => _send(state, d),
                          child: Text(
                            l.sendChooseFiles,
                            maxLines: 2,
                            textAlign: TextAlign.center,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              if (devices.isEmpty && widget.onPair != null)
                MobileTile(title: l.addDevice, onTap: widget.onPair),
            ],
          ),
          if (rows.isNotEmpty)
            MobileGroup(
              title: l.friendHistory,
              children: [
                for (final t in rows)
                  Padding(
                    padding: const EdgeInsets.fromLTRB(
                      Insets.lg,
                      Insets.md,
                      Insets.lg,
                      Insets.md,
                    ),
                    child: MobileTransferCard(
                      transfer: t,
                      actions: _actions(t),
                    ),
                  ),
              ],
            ),
        ],
      ),
    );
  }

  /// 一排按钮:收到的问「接不接受」,收到的完成后可以保存/分享,中断的可以继续,任何一条都能删。
  List<Widget> _actions(Transfer t) {
    final l = context.l10n;
    final state = AppScope.read(context);
    final done = t.status == 'done';
    final resumable =
        t.direction == TransferDirection.TRANSFER_DIRECTION_OUTGOING &&
        (t.status == 'interrupted' || t.status == 'paused');
    final finished = const {
      'done',
      'failed',
      'declined',
      'cancelled',
      'expired',
    }.contains(t.status);
    Widget action(
      IconData icon,
      String label,
      VoidCallback onPressed, {
      bool primary = false,
    }) => IconButton(
      tooltip: label,
      onPressed: onPressed,
      icon: Icon(icon, size: 20),
      style: IconButton.styleFrom(
        backgroundColor: primary ? context.palette.fill : Colors.transparent,
        foregroundColor: context.palette.textDim,
        minimumSize: const Size(40, 40),
        padding: const EdgeInsets.all(8),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(context.shape.md),
        ),
      ),
    );
    final secondary = <({String label, IconData icon, VoidCallback run})>[
      if (done && t.direction == TransferDirection.TRANSFER_DIRECTION_INCOMING)
        (label: l.mShare, icon: AppIcons.share, run: () => _share(t)),
      if (resumable)
        (
          label: l.cancel,
          icon: AppIcons.xmark,
          run: () => state.cancelTransfer(t.id),
        ),
      if (finished || resumable || done)
        (
          label: l.mDelete,
          icon: AppIcons.trash,
          run: () => state.deleteTransfers(ids: [t.id]),
        ),
    ];
    final buttons = <Widget>[
      if (done && t.direction == TransferDirection.TRANSFER_DIRECTION_INCOMING)
        action(AppIcons.download, l.save, () => _save(t), primary: true),
      if (resumable)
        action(
          AppIcons.arrowClockwise,
          l.continueAction,
          () => state.resumeTransfer(t.id),
          primary: true,
        ),
      if (!resumable &&
          const {
            'scanning',
            'queued',
            'waiting',
            'asking',
            'active',
          }.contains(t.status))
        action(
          AppIcons.xmark,
          t.status == 'queued' ? l.sendRemoveQueued : l.cancel,
          () => state.cancelTransfer(t.id),
        ),
      if (secondary.length == 1)
        action(
          secondary.first.icon,
          secondary.first.label,
          secondary.first.run,
        ),
      if (secondary.length > 1)
        PopupMenuButton<int>(
          tooltip: l.moreActions,
          icon: Icon(
            AppIcons.ellipsis,
            size: 20,
            color: context.palette.textDim,
          ),
          padding: const EdgeInsets.all(8),
          constraints: const BoxConstraints(minWidth: 140),
          onSelected: (i) => secondary[i].run(),
          itemBuilder: (_) => [
            for (var i = 0; i < secondary.length; i++)
              PopupMenuItem(
                value: i,
                child: Row(
                  children: [
                    Icon(secondary[i].icon, size: 18),
                    const SizedBox(width: 10),
                    Text(secondary[i].label),
                  ],
                ),
              ),
          ],
        ),
    ];
    return buttons;
  }
}

/// Compact transfer row with metadata on the left and controls on the right.
class MobileTransferCard extends StatelessWidget {
  const MobileTransferCard({
    super.key,
    required this.transfer,
    this.actions = const [],
  });
  final Transfer transfer;
  final List<Widget> actions;

  @override
  Widget build(BuildContext context) {
    final t = transfer;
    final l = context.l10n;
    final p = context.palette;
    final incoming =
        t.direction == TransferDirection.TRANSFER_DIRECTION_INCOMING;
    final active = t.status == 'active';
    final bad = const {'failed', 'declined', 'interrupted'}.contains(t.status);
    final status = t.status == 'done'
        ? l.sendDelivered
        : transferStatusText(l, t);
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          padding: const EdgeInsets.all(8),
          decoration: BoxDecoration(
            color: p.fill,
            borderRadius: BorderRadius.circular(context.shape.md),
          ),
          child: Icon(transferIcon(t), size: 22, color: p.textDim),
        ),
        const SizedBox(width: Insets.md),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                transferTitle(l, t),
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
                style: context.text.bodyLarge?.copyWith(
                  fontWeight: FontWeight.w600,
                  height: 1.3,
                ),
              ),
              const SizedBox(height: 2),
              Text(
                [
                  incoming
                      ? l.transferFrom(t.deviceName)
                      : l.transferTo(t.deviceName),
                  if (t.totalBytes > 0) humanBytes(t.totalBytes.toInt()),
                ].join(' · '),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: context.text.bodySmall,
              ),
              if (active) ...[
                const SizedBox(height: 4),
                ThemedProgressBar(
                  minHeight: 3,
                  borderRadius: BorderRadius.circular(context.shape.scaled(2)),
                  value: t.totalBytes > 0
                      ? (t.doneBytes.toDouble() / t.totalBytes.toDouble())
                            .clamp(0, 1)
                      : null,
                ),
                const SizedBox(height: 2),
                Text(
                  progressLabel(l, t),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: context.text.bodySmall,
                ),
              ] else
                Text(
                  status,
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  style: context.text.bodySmall?.copyWith(
                    color: bad
                        ? p.danger
                        : t.status == 'done'
                        ? p.online
                        : p.textDim,
                  ),
                ),
            ],
          ),
        ),
        if (actions.isNotEmpty) ...[
          const SizedBox(width: 4),
          Row(mainAxisSize: MainAxisSize.min, children: actions),
        ],
      ],
    );
  }
}