Skip to content

Commit

Permalink
Delete word by word after 3 seconds have passed since pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
vedasjad committed Dec 16, 2023
1 parent 273e2ff commit 99b92dd
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
41 changes: 37 additions & 4 deletions lib/src/default_emoji_picker_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,48 @@ class _DefaultEmojiPickerViewState extends State<DefaultEmojiPickerView>
closeSkinToneOverlay();
}

/// Start the callback for long-pressing the backspace button.
void _startOnBackspacePressedCallback() {
const callbackInterval = Duration(milliseconds: 75);
// Initial callback interval for short presses
var callbackInterval = const Duration(milliseconds: 75);
var millisecondsSincePressed = 0;

// Callback function executed on each timer tick
void _callback(Timer timer) {
// Accumulate elapsed time since the last tick
millisecondsSincePressed += callbackInterval.inMilliseconds;

// If the long-press duration exceeds 3 seconds
if (millisecondsSincePressed > 3000 &&
callbackInterval == const Duration(milliseconds: 75)) {
// Switch to a longer callback interval for word-by-word deletion
callbackInterval = const Duration(milliseconds: 300);

// Cancel the existing timer and start a new one with the updated interval
_onBackspacePressedCallbackTimer?.cancel();
_onBackspacePressedCallbackTimer =
Timer.periodic(callbackInterval, _callback);

// Reset the elapsed time for the new interval
millisecondsSincePressed = 0;
}

// Trigger the appropriate callback based on the interval
if (callbackInterval == const Duration(milliseconds: 75)) {
widget.state.onBackspacePressed!(); // Short-press callback
} else {
widget.state.onBackspaceLongPressed(); // Long-press callback
}
}

// Start the initial timer with the short-press interval
_onBackspacePressedCallbackTimer =
Timer.periodic(callbackInterval, (timer) {
widget.state.onBackspacePressed!();
});
Timer.periodic(callbackInterval, _callback);
}

/// Stop the callback for long-pressing the backspace button.
void _stopOnBackspacePressedCallback() {
// Cancel the active timer
_onBackspacePressedCallbackTimer?.cancel();
}
}
52 changes: 52 additions & 0 deletions lib/src/emoji_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ typedef void OnSkinToneDialogRequested(
/// Callback function for backspace button
typedef void OnBackspacePressed();

/// Callback function for backspace button when long pressed
typedef void OnBackspaceLongPressed();

/// Callback function for custom view
typedef EmojiViewBuilder = Widget Function(Config config, EmojiViewState state);

Expand Down Expand Up @@ -220,6 +223,54 @@ class EmojiPickerState extends State<EmojiPicker> {
widget.onBackspacePressed?.call();
}

OnBackspaceLongPressed _onBackspaceLongPressed() {
return () {
if (widget.textEditingController != null) {
final controller = widget.textEditingController!;

final text = controller.value.text;
var cursorPosition = controller.selection.base.offset;

// If cursor is not set, then place it at the end of the textfield
if (cursorPosition < 0) {
controller.selection = TextSelection(
baseOffset: controller.text.length,
extentOffset: controller.text.length,
);
cursorPosition = controller.selection.base.offset;
}

if (cursorPosition >= 0) {
final selection = controller.value.selection;
final newTextBeforeCursor = _deleteWordByWord(
selection.textBefore(text).toString(),
);
controller
..text = newTextBeforeCursor + selection.textAfter(text)
..selection = TextSelection.fromPosition(
TextPosition(offset: newTextBeforeCursor.length),
);
}
}
};
}

String _deleteWordByWord(String text) {
// Trim trailing spaces
text = text.trimRight();

// Find the last space to determine the start of the last word
final lastSpaceIndex = text.lastIndexOf(' ');

// If there is a space, remove the last word and spaces before it
if (lastSpaceIndex != -1) {
return text.substring(0, lastSpaceIndex).trimRight();
}

// If there is no space, remove the entire text
return '';
}

// Add recent emoji handling to tap listener
OnEmojiSelected _getOnEmojiListener() {
return (category, emoji) {
Expand Down Expand Up @@ -290,6 +341,7 @@ class EmojiPickerState extends State<EmojiPicker> {
_categoryEmoji,
_getOnEmojiListener(),
widget.onBackspacePressed == null ? null : _onBackspacePressed,
_onBackspaceLongPressed(),
);
if (mounted) {
setState(() {
Expand Down
4 changes: 4 additions & 0 deletions lib/src/emoji_view_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class EmojiViewState {
this.categoryEmoji,
this.onEmojiSelected,
this.onBackspacePressed,
this.onBackspaceLongPressed,
);

/// List of all category including their emoji
Expand All @@ -17,4 +18,7 @@ class EmojiViewState {

/// Callback when pressed on backspace
final OnBackspacePressed? onBackspacePressed;

/// Callback when long pressed on backspace
final OnBackspaceLongPressed onBackspaceLongPressed;
}

0 comments on commit 99b92dd

Please sign in to comment.