Files
pipelines/app/lib/features/chat/conversation/chat_sidebar_controls.dart
T

100 lines
3.2 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../ui/sessions/chat_adaptive.dart';
import '../../../ui/adaptive/segmented_bar.dart';
enum ChatSidebarMode { expanded, compact, hidden }
/// Native list-layout selector, also available in the chat actions menu.
class ChatSidebarButton extends StatelessWidget
implements SegmentedOverflowable {
const ChatSidebarButton({
super.key,
required this.mode,
required this.onChanged,
});
final ChatSidebarMode mode;
final ValueChanged<ChatSidebarMode> onChanged;
@override
SegmentedOverflowEntry get overflowEntry => SegmentedOverflowEntry(
label: 'Chat list layout',
icon: Icons.view_sidebar_outlined,
choices: [
for (final value in ChatSidebarMode.values)
SegmentedMenuChoice(
label: _label(value),
selected: value == mode,
onSelected: () => onChanged(value),
),
],
);
Future<void> _choose(BuildContext context) async {
final ChatSidebarMode? choice;
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
choice = await showCupertinoModalPopup<ChatSidebarMode>(
context: context,
builder: (context) => CupertinoActionSheet(
title: const Text('Chat list'),
actions: [
for (final value in ChatSidebarMode.values)
CupertinoActionSheetAction(
isDefaultAction: mode == value,
onPressed: () => Navigator.pop(context, value),
child: Text(_label(value)),
),
],
cancelButton: CupertinoActionSheetAction(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
),
);
} else {
choice = await showDialog<ChatSidebarMode>(
context: context,
builder: (context) => SimpleDialog(
title: const Text('Chat list'),
children: [
for (final value in ChatSidebarMode.values)
SimpleDialogOption(
onPressed: () => Navigator.pop(context, value),
child: Row(
children: [
Expanded(child: Text(_label(value))),
if (value == mode) const Icon(Icons.check),
],
),
),
],
),
);
}
if (choice != null && context.mounted) onChanged(choice);
}
static String _label(ChatSidebarMode value) => switch (value) {
ChatSidebarMode.expanded => 'Expanded chat list',
ChatSidebarMode.compact => 'Compact chat list',
ChatSidebarMode.hidden => 'Hide chat list',
};
@override
Widget build(BuildContext context) =>
chatControlsStyleOf(context) == ChatControlsStyle.cupertino
? Semantics(
label: 'Chat list layout',
button: true,
child: CupertinoButton(
padding: EdgeInsets.zero,
onPressed: () => _choose(context),
child: const Icon(CupertinoIcons.sidebar_left),
),
)
: IconButton(
tooltip: 'Chat list layout',
onPressed: () => _choose(context),
icon: const Icon(Icons.view_sidebar_outlined),
);
}