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.

fix(ui): search only committed text while composing

baiyuze75a2b4f360be
diff --git a/ui/lib/picker/picker_view.dart b/ui/lib/picker/picker_view.dart
index b4bd332..7cc27af 100644
--- a/ui/lib/picker/picker_view.dart
+++ b/ui/lib/picker/picker_view.dart
@@ -8,7 +8,7 @@ import '../i18n.dart';
import '../icons.dart';
import '../records.dart';
import '../theme.dart';
-import '../widgets/common.dart' show humanBytes, relativeTime;
+import '../widgets/common.dart' show committedText, humanBytes, relativeTime;
import '../widgets/record_hover.dart';
import 'picker_state.dart';
@@ -77,6 +77,7 @@ class _PickerViewState extends State<PickerView> {
super.initState();
s.shown.addListener(_onShown);
s.addListener(_followSelection);
+ _search.addListener(_onSearch);
}
@override
@@ -89,6 +90,12 @@ class _PickerViewState extends State<PickerView> {
super.dispose();
}
+ /// 用输入法打拼音时不拿拼音去搜,选字上屏后再搜(见 [committedText])
+ void _onSearch() {
+ final q = committedText(_search.value);
+ if (q != s.query) s.setQuery(q);
+ }
+
void _onShown() {
_search.clear();
if (_scroll.hasClients) _scroll.jumpTo(0);
@@ -107,7 +114,7 @@ class _PickerViewState extends State<PickerView> {
}
}
- bool get _composing => _search.value.composing.isValid;
+ bool get _composing => committedText(_search.value) != _search.text;
/// 修饰键:Mac 上是 ⌘,其他平台是 Ctrl(序号用 Alt)
bool get _cmd => _mac ? HardwareKeyboard.instance.isMetaPressed : HardwareKeyboard.instance.isControlPressed;
@@ -124,7 +131,6 @@ class _PickerViewState extends State<PickerView> {
if (k == LogicalKeyboardKey.escape) {
if (_search.text.isNotEmpty) {
_search.clear();
- s.setQuery('');
} else {
s.close();
}
@@ -227,7 +233,6 @@ class _PickerViewState extends State<PickerView> {
controller: _search,
focusNode: _focus,
autofocus: true,
- onChanged: s.setQuery,
style: context.text.titleMedium?.copyWith(fontWeight: FontWeight.w400),
decoration: InputDecoration(
hintText: l.pickerSearch,
diff --git a/ui/lib/widgets/common.dart b/ui/lib/widgets/common.dart
index d5b11c5..cd3c4fc 100644
--- a/ui/lib/widgets/common.dart
+++ b/ui/lib/widgets/common.dart
@@ -223,10 +223,18 @@ class Segmented<T> extends StatelessWidget {
}
}
+/// 输入框里已经确定的文字:输入法正在组字时,组字的部分(比如拼音 hui)不算,选字上屏后才算。
+String committedText(TextEditingValue value) {
+ final composing = value.composing;
+ if (!composing.isValid || composing.isCollapsed) return value.text;
+ return composing.textBefore(value.text) + composing.textAfter(value.text);
+}
+
/// 搜索框,对应 macOS 的 NSSearchField:浅灰底、左边放大镜,输入后右边出现清除按钮。
/// 按 Esc 时有字就清空,没字就交出焦点。不给 [width] 时占满父级给的宽度。
/// 高度定为 28,与旁边的 [Segmented] 一样高;不跟着字体变,否则换个系统字体就高低不齐。
-class SearchField extends StatelessWidget {
+/// [onChanged] 收到的是 [committedText]:用输入法打拼音时不拿拼音去搜。
+class SearchField extends StatefulWidget {
const SearchField({
super.key,
required this.controller,
@@ -244,26 +252,59 @@ class SearchField extends StatelessWidget {
final FocusNode? focusNode;
final double? width;
- void _clear() {
- controller.clear();
- onChanged('');
+ @override
+ State<SearchField> createState() => _SearchFieldState();
+}
+
+class _SearchFieldState extends State<SearchField> {
+ late String _reported = committedText(widget.controller.value);
+
+ @override
+ void initState() {
+ super.initState();
+ widget.controller.addListener(_report);
+ }
+
+ @override
+ void didUpdateWidget(SearchField old) {
+ super.didUpdateWidget(old);
+ if (old.controller != widget.controller) {
+ old.controller.removeListener(_report);
+ widget.controller.addListener(_report);
+ }
+ }
+
+ @override
+ void dispose() {
+ widget.controller.removeListener(_report);
+ super.dispose();
+ }
+
+ /// 组字结束时文字可能没变(输入法直接上屏英文),所以监听整个输入框,而不是 TextField.onChanged
+ void _report() {
+ final text = committedText(widget.controller.value);
+ if (text == _reported) return;
+ _reported = text;
+ widget.onChanged(text);
}
@override
Widget build(BuildContext context) {
+ final controller = widget.controller;
+ final focusNode = widget.focusNode;
final p = context.palette;
final round = OutlineInputBorder(
borderRadius: BorderRadius.circular(Radii.sm),
borderSide: BorderSide.none,
);
return SizedBox(
- width: width,
+ width: widget.width,
height: 28,
child: CallbackShortcuts(
bindings: {
const SingleActivator(LogicalKeyboardKey.escape): () {
if (controller.text.isNotEmpty) {
- _clear();
+ controller.clear();
} else {
focusNode?.unfocus();
}
@@ -274,13 +315,12 @@ class SearchField extends StatelessWidget {
builder: (context, _) => TextField(
controller: controller,
focusNode: focusNode,
- onChanged: onChanged,
style: context.text.bodyMedium,
expands: true,
maxLines: null,
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
- hintText: hint,
+ hintText: widget.hint,
fillColor: p.fill,
contentPadding: EdgeInsets.zero,
border: round,
@@ -291,8 +331,8 @@ class SearchField extends StatelessWidget {
suffixIcon: controller.text.isEmpty
? null
: IconButton(
- onPressed: _clear,
- tooltip: clearTooltip,
+ onPressed: controller.clear,
+ tooltip: widget.clearTooltip,
icon: Icon(AppIcons.clearField, size: 14, color: p.textFaint),
padding: EdgeInsets.zero,
constraints: const BoxConstraints(minWidth: 28, minHeight: 0),
diff --git a/ui/test/history_search_test.dart b/ui/test/history_search_test.dart
index 01776aa..b4c8365 100644
--- a/ui/test/history_search_test.dart
+++ b/ui/test/history_search_test.dart
@@ -121,6 +121,26 @@ void main() {
expect(field.controller!.text, isEmpty);
});
+ testWidgets('用输入法打拼音时不拿拼音去搜,上屏后才搜', (tester) async {
+ final state = _seed((q) => ListHistoryResponse(records: [_note], searched: true));
+ await tester.pumpWidget(PasteFerryApp(state: state));
+ final controller = tester.widget<TextField>(find.byType(TextField)).controller!;
+
+ controller.value = const TextEditingValue(
+ text: 'wu',
+ selection: TextSelection.collapsed(offset: 2),
+ composing: TextRange(start: 0, end: 2),
+ );
+ await tester.pump(const Duration(milliseconds: 300));
+ expect(state.queries, isEmpty);
+ expect(state.searching, isFalse);
+
+ // 有的输入法直接把拼音当英文上屏:文字没变,只是组字结束了
+ controller.value = const TextEditingValue(text: 'wu', selection: TextSelection.collapsed(offset: 2));
+ await tester.pump(const Duration(milliseconds: 300));
+ expect(state.queries, ['wu']);
+ });
+
test('标出所有匹配处,不分大小写', () {
const mark = TextStyle(backgroundColor: Color(0xFF000000));
final spans = highlightMatches('Copy, copy, COPY', 'copy', mark);
diff --git a/ui/test/picker_test.dart b/ui/test/picker_test.dart
index dae5dcb..0ab44d2 100644
--- a/ui/test/picker_test.dart
+++ b/ui/test/picker_test.dart
@@ -250,6 +250,15 @@ void main() {
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump();
expect(s.applied, isEmpty);
+ // 拼音不拿去搜,列表不变;选字上屏后才搜
+ expect(s.query, '');
+ expect(find.textContaining('docker compose'), findsOneWidget);
+ field.controller!.value = const TextEditingValue(text: '会', selection: TextSelection.collapsed(offset: 1));
+ await tester.pump();
+ expect(s.query, '会');
+ expect(find.textContaining('会议'), findsOneWidget);
+ expect(find.textContaining('docker compose'), findsNothing);
+ await tester.pump(const Duration(milliseconds: 200));
});
testWidgets('每次呼出都从「全部」、第一条、空搜索开始', (tester) async {