mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
feat: add list file search listener
This commit is contained in:
75
flutter/lib/desktop/widgets/list_search_action_listener.dart
Normal file
75
flutter/lib/desktop/widgets/list_search_action_listener.dart
Normal file
@@ -0,0 +1,75 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ListSearchActionListener extends StatelessWidget {
|
||||
final FocusNode node;
|
||||
final TimeoutStringBuffer buffer;
|
||||
final Widget child;
|
||||
final Function(String) onNext;
|
||||
final Function(String) onSearch;
|
||||
|
||||
const ListSearchActionListener(
|
||||
{super.key,
|
||||
required this.node,
|
||||
required this.buffer,
|
||||
required this.child,
|
||||
required this.onNext,
|
||||
required this.onSearch});
|
||||
|
||||
@mustCallSuper
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return KeyboardListener(
|
||||
autofocus: true,
|
||||
onKeyEvent: (kv) {
|
||||
final ch = kv.character;
|
||||
if (ch == null) {
|
||||
return;
|
||||
}
|
||||
final action = buffer.input(ch);
|
||||
switch (action) {
|
||||
case ListSearchAction.search:
|
||||
onSearch(buffer.buffer);
|
||||
break;
|
||||
case ListSearchAction.next:
|
||||
onNext(buffer.buffer);
|
||||
break;
|
||||
}
|
||||
},
|
||||
focusNode: node,
|
||||
child: child);
|
||||
}
|
||||
}
|
||||
|
||||
enum ListSearchAction { search, next }
|
||||
|
||||
class TimeoutStringBuffer {
|
||||
var _buffer = "";
|
||||
late DateTime _duration;
|
||||
|
||||
static int timeoutMilliSec = 1500;
|
||||
|
||||
String get buffer => _buffer;
|
||||
|
||||
TimeoutStringBuffer() {
|
||||
_duration = DateTime.now();
|
||||
}
|
||||
|
||||
ListSearchAction input(String ch) {
|
||||
final curr = DateTime.now();
|
||||
try {
|
||||
if (curr.difference(_duration).inMilliseconds > timeoutMilliSec) {
|
||||
_buffer = ch;
|
||||
return ListSearchAction.search;
|
||||
} else {
|
||||
if (ch == _buffer) {
|
||||
return ListSearchAction.next;
|
||||
} else {
|
||||
_buffer += ch;
|
||||
return ListSearchAction.search;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
_duration = curr;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user