import 'package:flutter/material.dart'; import 'package:flutter/semantics.dart'; import 'package:flutter/services.dart'; import '../../../ui/adaptive/app_menu_anchor.dart'; import '../domain/chat_send_later.dart'; import 'chat_schedule_time.dart'; const String chatSendLaterLabel = 'Send later…'; const String chatSendLaterPickLabel = 'Pick a time…'; /// The send button's secondary action: right-click, long-press, the context /// menu key or Shift+F10 on it (or the screen reader's custom action) opens /// "Send later…" with quick times and a time picker. A chosen time is handed /// to [onSchedule]; the message takes the ordinary send path with `send_at`. /// /// On touch the long-press opens this menu instead of the button's tooltip, /// so the tooltip's account and usage line ([info]) heads the menu. class ChatSendLaterMenu extends StatefulWidget { const ChatSendLaterMenu({ super.key, required this.enabled, required this.onSchedule, required this.builder, this.info, this.usageReset, this.now = DateTime.now, }); /// Whether the draft is a message that can be scheduled. final bool enabled; final String? info; final DateTime? Function(DateTime now)? usageReset; final void Function(DateTime at) onSchedule; /// Builds the send button, given the callback that opens the menu. final Widget Function(BuildContext context, VoidCallback open) builder; final DateTime Function() now; @override State createState() => _ChatSendLaterMenuState(); } class _ChatSendLaterMenuState extends State { final _menu = MenuController(); void _open([Offset? position]) { if (_menu.isOpen) _menu.close(); setState(() {}); _menu.open(position: position); } Future _pickTime() async { final now = widget.now(); final initial = TimeOfDay.fromDateTime(now.add(const Duration(hours: 1))); final picked = await showTimePicker( context: context, initialTime: TimeOfDay(hour: initial.hour, minute: 0), helpText: 'Send at', // The same 24-hour clock as "Scheduled · sends at 21:03". builder: (context, child) => MediaQuery( data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true), child: child!, ), ); if (!mounted || picked == null || !widget.enabled) return; widget.onSchedule( chatSendLaterAt(widget.now(), picked.hour, picked.minute), ); } String _label(BuildContext context, ChatSendLaterChoice choice) { final time = chatScheduleTime(context, choice.at, now: widget.now); return switch (choice.kind) { ChatSendLaterKind.halfHour => 'In 30 minutes · $time', ChatSendLaterKind.hour => 'In 1 hour · $time', ChatSendLaterKind.tomorrowMorning => 'Tomorrow at 9:00', ChatSendLaterKind.usageReset => 'At usage reset · $time', }; } @override Widget build(BuildContext context) { final now = widget.now(); final choices = chatSendLaterChoices( now, usageReset: widget.usageReset?.call(now), ); final theme = Theme.of(context); final info = widget.info; return AppMenuAnchor( controller: _menu, builder: (context, controller, child) => child!, menuChildren: [ if (info != null && info.isNotEmpty) Padding( padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), child: Text( info, style: theme.textTheme.bodySmall?.copyWith( color: theme.colorScheme.onSurfaceVariant, ), ), ), Padding( key: const Key('chat-send-later-title'), padding: const EdgeInsets.fromLTRB(12, 8, 12, 4), child: Row( mainAxisSize: MainAxisSize.min, spacing: 12, children: [ Icon( Icons.schedule_send, color: theme.colorScheme.onSurfaceVariant, ), Text(chatSendLaterLabel, style: theme.textTheme.titleSmall), ], ), ), for (final choice in choices) MenuItemButton( key: Key('chat-send-later-${choice.kind.name}'), onPressed: widget.enabled ? () => widget.onSchedule(choice.at) : null, child: Text(_label(context, choice)), ), MenuItemButton( key: const Key('chat-send-later-pick'), leadingIcon: const Icon(Icons.access_time), onPressed: widget.enabled ? _pickTime : null, child: const Text(chatSendLaterPickLabel), ), ], child: CallbackShortcuts( bindings: { const SingleActivator(LogicalKeyboardKey.contextMenu): _open, const SingleActivator(LogicalKeyboardKey.f10, shift: true): _open, }, child: GestureDetector( onSecondaryTapUp: (details) => _open(details.localPosition), child: Semantics( customSemanticsActions: { const CustomSemanticsAction(label: chatSendLaterLabel): _open, }, child: widget.builder(context, _open), ), ), ), ); } }