ui: fold the composer to one line until it has focus

dsh-Go opens the input into two rows only while focused: the text above, the
model / mode / keys below, folding back when focus leaves. ComposerBox keeps
the field across the two layouts with a GlobalKey and animates the swap.

Model and thinking level are now one control (ModelPill shows "model · effort"
and the sheet offers the level after choosing a model); Build/Plan is a
separate ModeChip. The draft page uses the same box, with its project chip
above it. 0.3.15 (315).
This commit is contained in:
blumia committed 2026-11-21 17:47:04 +08:00
1 parent 7f2f454539
commit ea3b1f0f5e
8 files changed
+553 -430

No files matched your search

+187
View File
@@ -0,0 +1,187 @@
// The input capsule. Focus opens it into two rows — the text above, the
// model / mode / keys below — and losing focus folds it back to one line, the
// way dsh-Go's composer behaves. The field keeps its state across the two
// layouts through a GlobalKey.
import 'package:flutter/material.dart';
class ComposerBox extends StatefulWidget {
final TextEditingController controller;
final FocusNode focusNode;
final String hint;
final int maxLines;
final ValueChanged<String>? onChanged;
final ValueChanged<String>? onSubmitted;
final TextInputAction textInputAction;
/// The attach key; shown on the text's line when folded and on the second
/// row when open.
final Widget leading;
/// Model (and thinking level) pill, second row.
final Widget? model;
/// The mode (Build / Plan), second row and separate from [model].
final Widget? mode;
/// The context ring, right of the keys.
final Widget? accessory;
/// Send or stop.
final Widget trailing;
const ComposerBox({
super.key,
required this.controller,
required this.focusNode,
required this.hint,
required this.leading,
required this.trailing,
this.model,
this.mode,
this.accessory,
this.maxLines = 5,
this.onChanged,
this.onSubmitted,
this.textInputAction = TextInputAction.newline,
});
@override
State<ComposerBox> createState() => _ComposerBoxState();
}
class _ComposerBoxState extends State<ComposerBox> {
// The editor moves between the one-line row and the two-line column; a
// GlobalKey carries its state (and its input connection) across the swap.
final GlobalKey _fieldKey = GlobalKey();
bool _open = false;
@override
void initState() {
super.initState();
widget.focusNode.addListener(_onFocus);
_open = widget.focusNode.hasFocus;
}
@override
void dispose() {
widget.focusNode.removeListener(_onFocus);
super.dispose();
}
void _onFocus() {
final open = widget.focusNode.hasFocus;
if (open == _open) return;
setState(() => _open = open);
}
Widget _field() {
return TextField(
key: _fieldKey,
controller: widget.controller,
focusNode: widget.focusNode,
minLines: 1,
maxLines: widget.maxLines,
keyboardType: TextInputType.multiline,
textInputAction: widget.textInputAction,
onChanged: widget.onChanged,
onSubmitted: widget.onSubmitted,
style: const TextStyle(fontSize: 15, height: 1.45),
decoration: InputDecoration(
hintText: widget.hint,
hintMaxLines: 1,
isDense: true,
border: InputBorder.none,
contentPadding: const EdgeInsets.symmetric(vertical: 6),
),
);
}
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
final accessory = widget.accessory;
return AnimatedContainer(
duration: const Duration(milliseconds: 200),
curve: Curves.easeOutCubic,
padding: const EdgeInsets.all(6),
decoration: BoxDecoration(
color: scheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(_open ? 20 : 26),
),
child: AnimatedSize(
duration: const Duration(milliseconds: 200),
curve: Curves.easeOutCubic,
alignment: Alignment.bottomCenter,
child: _open
? Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(6, 0, 6, 2),
child: _field(),
),
_secondRow(),
],
)
: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
widget.leading,
const SizedBox(width: 4),
Expanded(child: _field()),
const SizedBox(width: 4),
if (accessory != null) ...[
accessory,
const SizedBox(width: 2),
],
widget.trailing,
],
),
),
);
}
Widget _secondRow() {
final accessory = widget.accessory;
// The row fades and rises a little as the composer opens.
return TweenAnimationBuilder<double>(
tween: Tween(begin: 0, end: 1),
duration: const Duration(milliseconds: 220),
curve: Curves.easeOutCubic,
builder: (context, t, child) => Opacity(
opacity: t,
child: Transform.translate(
offset: Offset(0, (1 - t) * 4),
child: child,
),
),
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 2, 0, 0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
widget.leading,
const SizedBox(width: 6),
if (widget.model != null)
Flexible(
fit: FlexFit.loose,
child: Align(
alignment: Alignment.centerLeft,
child: widget.model!,
),
),
const SizedBox(width: 6),
if (widget.mode != null) widget.mode!,
const Spacer(),
if (accessory != null) ...[
accessory,
const SizedBox(width: 4),
],
widget.trailing,
],
),
),
);
}
}
+91 -73
View File
@@ -1,6 +1,5 @@
// The composer's model row, shared by the chat and the new-session draft:
// the model/agent chip, the thinking level when the model has one, and any
// leading chip the screen adds (the draft's project).
// The composer's two controls: the model pill (model and thinking level in
// one group) and, separately, the mode (Build / Plan).
import 'package:flutter/material.dart';
import '../api/models.dart';
@@ -8,113 +7,132 @@ import '../model_prefs.dart';
import '../state.dart';
import 'model_picker.dart';
class ModelControlsBar extends StatelessWidget {
/// `deepseek-flash · 高` — model and effort together, as dsh-Go shows them.
class ModelPill extends StatelessWidget {
final AppState app;
final ModelRef? model;
final String agent;
final ValueChanged<ModelRef> onModel;
final ValueChanged<String> onAgent;
/// Shown first, e.g. the project chip on the draft.
final Widget? leading;
const ModelControlsBar({
const ModelPill({
super.key,
required this.app,
required this.model,
required this.agent,
required this.onModel,
required this.onAgent,
this.leading,
});
ModelInfo? get _info => app.modelInfo(model);
@override
Widget build(BuildContext context) {
final info = _info;
final modelLabel = model == null
? '模型'
: (agent.isEmpty ? app.modelName(model!) : '${app.modelName(model!)} · $agent');
final hasEffort = info != null && info.hasEffort;
// The server may name the default level "default"; the picker only knows
// the variants the catalog lists, so anything else counts as no effort.
final info = app.modelInfo(model);
// The server may name the default level "default"; only a level the
// catalog lists counts as a choice.
final effort = model?.variant != null &&
info != null &&
info.variants.contains(model!.variant)
? model!.variant
: null;
return Wrap(
spacing: 6,
runSpacing: 4,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
if (leading != null) leading!,
_chip(
context,
icon: Icons.smart_toy_outlined,
label: modelLabel,
onTap: () => showModelPickerSheet(
context,
app: app,
kind: 'model',
model: model,
agent: agent,
onModel: onModel,
onAgent: onAgent,
),
),
if (hasEffort)
_chip(
context,
icon: Icons.psychology_outlined,
label: '思考·${effortLabel(effort)}',
active: effort != null,
onTap: () => showEffortSheet(
context,
variants: info.variants,
value: effort,
modelName: info.name,
onPick: (variant) => onModel(
ModelRef(model!.id, model!.providerId, variant)),
final name = model == null ? '模型' : app.modelName(model!);
final label = effort == null ? name : '$name · ${effortLabel(effort)}';
return _Pill(
icon: Icons.smart_toy_outlined,
label: label,
onTap: () => showModelPickerSheet(
context,
app: app,
model: model,
onModel: onModel,
),
);
}
}
/// The mode is its own control: Build or Plan, not part of the model.
class ModeChip extends StatelessWidget {
final AppState app;
final String agent;
final ValueChanged<String> onAgent;
const ModeChip({
super.key,
required this.app,
required this.agent,
required this.onAgent,
});
static const _modes = ['build', 'plan'];
@override
Widget build(BuildContext context) {
final current = agent.isEmpty ? _modes.first : agent;
return PopupMenuButton<String>(
tooltip: '模式',
initialValue: current,
onSelected: onAgent,
itemBuilder: (_) {
final known = [
for (final a in app.agentList)
if (a.mode == 'primary' && !a.hidden) a,
];
final ids = {
..._modes,
for (final a in known) a.id,
};
return [
for (final id in ids)
PopupMenuItem(
value: id,
child: Text(_label(id),
style: TextStyle(
fontWeight:
id == current ? FontWeight.w700 : FontWeight.w400)),
),
),
],
];
},
child: _Pill(
icon: Icons.tune,
label: _label(current),
onTap: null,
),
);
}
Widget _chip(BuildContext context,
{required IconData icon,
required String label,
required VoidCallback onTap,
bool active = false}) {
static String _label(String id) =>
id.isEmpty ? 'Build' : id[0].toUpperCase() + id.substring(1);
}
class _Pill extends StatelessWidget {
final IconData icon;
final String label;
final VoidCallback? onTap;
const _Pill({required this.icon, required this.label, this.onTap});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final scheme = theme.colorScheme;
return InkWell(
borderRadius: BorderRadius.circular(16),
onTap: onTap,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 9, vertical: 5),
decoration: BoxDecoration(
color: active
? scheme.primary.withValues(alpha: 0.14)
: scheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(16),
constraints: const BoxConstraints(minHeight: 30),
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
decoration: ShapeDecoration(
color: scheme.onSurface.withValues(alpha: 0.07),
shape: const StadiumBorder(),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(icon, size: 14, color: active ? scheme.primary : theme.hintColor),
Icon(icon, size: 14, color: theme.hintColor),
const SizedBox(width: 4),
ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 210),
Flexible(
child: Text(
label,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12,
color: active ? scheme.primary : theme.hintColor,
fontSize: 12.5,
color: scheme.onSurface,
),
),
),
+87 -129
View File
@@ -1,6 +1,6 @@
// The model / agent picker, shared by the chat composer and the new-session
// draft: favourites, recents, models grouped by provider with their context
// size, and the thinking level of the chosen model.
// The model picker: favourites, recents, models grouped by provider with their
// context size, and — when a model has them — its thinking levels, chosen in
// the same sheet because model and effort are one selection.
import 'package:flutter/material.dart';
import '../api/models.dart';
@@ -10,11 +10,8 @@ import '../state.dart';
Future<void> showModelPickerSheet(
BuildContext context, {
required AppState app,
required String kind,
required ModelRef? model,
required String agent,
required ValueChanged<ModelRef> onModel,
required ValueChanged<String> onAgent,
}) async {
app.refreshModels();
await ModelPrefs.instance.ensureLoaded();
@@ -28,61 +25,9 @@ Future<void> showModelPickerSheet(
maxChildSize: 0.95,
builder: (context, controller) => _PickerSheet(
app: app,
kind: kind,
model: model,
agent: agent,
scrollController: controller,
onModel: onModel,
onAgent: onAgent,
),
),
);
}
/// The thinking levels of one model, as a small sheet.
Future<void> showEffortSheet(
BuildContext context, {
required List<String> variants,
required String? value,
required String modelName,
required ValueChanged<String?> onPick,
}) {
return showModalBottomSheet<void>(
context: context,
builder: (context) => SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(16, 14, 16, 2),
child: Text('思考强度',
style:
const TextStyle(fontSize: 17, fontWeight: FontWeight.w600)),
),
Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
child: Text(modelName,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 12, color: Theme.of(context).hintColor)),
),
for (final option in <String?>[null, ...variants])
ListTile(
dense: true,
title: Text(effortLabel(option)),
trailing: (value ?? '') == (option ?? '')
? Icon(Icons.check,
size: 18, color: Theme.of(context).colorScheme.primary)
: null,
onTap: () {
Navigator.pop(context);
onPick(option);
},
),
const SizedBox(height: 8),
],
),
),
);
@@ -90,21 +35,15 @@ Future<void> showEffortSheet(
class _PickerSheet extends StatefulWidget {
final AppState app;
final String kind;
final ModelRef? model;
final String agent;
final ScrollController scrollController;
final ValueChanged<ModelRef> onModel;
final ValueChanged<String> onAgent;
const _PickerSheet({
required this.app,
required this.kind,
required this.model,
required this.agent,
required this.scrollController,
required this.onModel,
required this.onAgent,
});
@override
@@ -112,10 +51,14 @@ class _PickerSheet extends StatefulWidget {
}
class _PickerSheetState extends State<_PickerSheet> {
late String _kind = widget.kind;
String _filter = '';
final Set<String> _collapsed = {};
/// The model whose thinking level the bottom panel is offering; null hides
/// the panel.
ModelInfo? _effortModel;
String? _effortVariant;
ModelPrefs get _prefs => ModelPrefs.instance;
@override
@@ -124,26 +67,10 @@ class _PickerSheetState extends State<_PickerSheet> {
listenable: widget.app,
builder: (context, _) => Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(12, 12, 12, 4),
child: Text(_kind == 'agent' ? '选择智能体' : '选择模型',
style: const TextStyle(
fontSize: 18, fontWeight: FontWeight.bold)),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
child: SegmentedButton<String>(
segments: const [
ButtonSegment(value: 'model', label: Text('模型')),
ButtonSegment(value: 'agent', label: Text('智能体')),
],
selected: {_kind},
showSelectedIcon: false,
onSelectionChanged: (s) {
setState(() => _kind = s.first);
widget.app.refreshModels();
},
),
const Padding(
padding: EdgeInsets.fromLTRB(12, 12, 12, 4),
child: Text('选择模型',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
),
Padding(
padding: const EdgeInsets.all(12),
@@ -155,48 +82,15 @@ class _PickerSheetState extends State<_PickerSheet> {
isDense: true),
),
),
Expanded(child: _kind == 'agent' ? _agents() : _models()),
Expanded(child: _models()),
if (_effortModel != null) _effortPanel(_effortModel!),
],
),
);
}
// -------------------------------------------------------------- agents
Widget _agents() {
final filter = _filter.toLowerCase();
final agents = [
for (final a in widget.app.agentList)
if (a.mode == 'primary' && !a.hidden)
if (filter.isEmpty || a.name.toLowerCase().contains(filter)) a,
];
if (agents.isEmpty) return const Center(child: Text('没有结果'));
return ListView.builder(
controller: widget.scrollController,
itemCount: agents.length,
itemBuilder: (context, i) {
final agent = agents[i];
return ListTile(
dense: true,
title: Text(agent.name),
trailing: widget.agent == agent.id
? Icon(Icons.check,
size: 18, color: Theme.of(context).colorScheme.primary)
: null,
onTap: () {
Navigator.pop(context);
widget.onAgent(agent.id);
},
);
},
);
}
// -------------------------------------------------------------- models
Widget _models() {
final app = widget.app;
final all = app.modelList;
final all = widget.app.modelList;
if (all.isEmpty) {
return const Center(child: Text('正在载入模型…'));
}
@@ -218,11 +112,10 @@ class _PickerSheetState extends State<_PickerSheet> {
for (final key in _prefs.favorites)
if (byKey[key] != null) byKey[key]!,
]..sort((a, b) => a.name.compareTo(b.name));
final recentKeys = [
final recents = [
for (final key in _prefs.recents)
if (byKey[key] != null && !_prefs.favorites.contains(key)) key,
if (byKey[key] != null && !_prefs.favorites.contains(key)) byKey[key]!,
];
final recents = [for (final key in recentKeys) byKey[key]!];
final providers = <String, List<ModelInfo>>{};
for (final model in matching) {
@@ -258,10 +151,7 @@ class _PickerSheetState extends State<_PickerSheet> {
}
}
}
return ListView(
controller: widget.scrollController,
children: children,
);
return ListView(controller: widget.scrollController, children: children);
}
Widget _sectionHeader(String label, String key, {int? count}) {
@@ -360,7 +250,75 @@ class _PickerSheetState extends State<_PickerSheet> {
: null;
await _prefs.rememberModel(key);
if (!mounted) return;
Navigator.pop(context);
// Model and effort are one choice: apply the model with its remembered
// level, then let the level be changed without leaving the sheet.
widget.onModel(ModelRef(model.id, model.providerId, variant));
if (model.hasEffort) {
setState(() {
_effortModel = model;
_effortVariant = variant;
});
} else {
Navigator.pop(context);
}
}
Future<void> _pickEffort(ModelInfo model, String? variant) async {
final key = ModelPrefs.keyFor(model.providerId, model.id);
await _prefs.rememberEffort(key, variant);
widget.onModel(ModelRef(model.id, model.providerId, variant));
if (!mounted) return;
Navigator.pop(context);
}
Widget _effortPanel(ModelInfo model) {
final theme = Theme.of(context);
final options = <String?>[null, ...model.variants];
return Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(
color: theme.dividerColor.withValues(alpha: 0.6))),
),
padding: const EdgeInsets.fromLTRB(16, 10, 16, 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
const Text('思考强度',
style: TextStyle(
fontSize: 13, fontWeight: FontWeight.w700)),
const SizedBox(width: 6),
Expanded(
child: Text(model.name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style:
TextStyle(fontSize: 12, color: theme.hintColor)),
),
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('完成'),
),
],
),
Wrap(
spacing: 6,
runSpacing: 6,
children: [
for (final option in options)
ChoiceChip(
label: Text(effortLabel(option),
style: const TextStyle(fontSize: 12.5)),
selected: (_effortVariant ?? '') == (option ?? ''),
onSelected: (_) => _pickEffort(model, option),
),
],
),
],
),
);
}
}