Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an option for ClipboardPaste button to disable ClipboardMonitor #2427

Merged
merged 27 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
aa35d0a
Removed ClipboardMonitor from ClipboardAction.paste type button
Maksim-Nikolaev Jan 5, 2025
596af77
Removed unused imports & added const keyword
Maksim-Nikolaev Jan 5, 2025
a91df17
Revert "Removed ClipboardMonitor from ClipboardAction.paste type button"
Maksim-Nikolaev Jan 5, 2025
b202b7a
Revert "Removed unused imports & added const keyword"
Maksim-Nikolaev Jan 5, 2025
7600a4d
added enableClipboardPaste to QuillSimpleToolbarConfig
Maksim-Nikolaev Jan 5, 2025
0d4f421
Reverted 5 seconds to 1 second periodic timer
Maksim-Nikolaev Jan 5, 2025
5a729a6
moved enableClipboardPaste to button config
Maksim-Nikolaev Jan 5, 2025
22b744f
Fix monitor enabling & disabling
Maksim-Nikolaev Jan 5, 2025
74df2ce
dartformat
Maksim-Nikolaev Jan 5, 2025
80c779d
remove enableClipboardPaste from toolbar config
Maksim-Nikolaev Jan 5, 2025
f85e444
Adjusted comments & documentation
Maksim-Nikolaev Jan 9, 2025
df4fd18
dart format
Maksim-Nikolaev Jan 9, 2025
3d689c7
docs: improve doc comment of enableClipboardPaste
EchoEllet Jan 10, 2025
e41abf3
docs: minor changes to comments
EchoEllet Jan 10, 2025
db97d4f
revert: remove the addition of const keyword to QuillToolbarClipboard…
EchoEllet Jan 10, 2025
daa20cd
Updated code readability with getter shouldUseClipboardMonitor
Maksim-Nikolaev Jan 10, 2025
f0a4616
Make the _shouldUseClipboardMonitor getter private
Maksim-Nikolaev Jan 10, 2025
7a8a2e5
Update clipboard_button.dart
Maksim-Nikolaev Jan 17, 2025
a48c90f
removed enableClipboardPaste from base_button_options
Maksim-Nikolaev Jan 17, 2025
fd4dd91
chore: revert the trailing comma
EchoEllet Jan 17, 2025
e004647
chore: revert changes to base_button_options.dart
EchoEllet Jan 18, 2025
fb0084e
Update toggle_style_options.dart
Maksim-Nikolaev Jan 18, 2025
a58d4e6
revert: trailing comma in clipboard_button.dart
EchoEllet Jan 18, 2025
e03cef5
revert: formatting to clipboard_button.dart
EchoEllet Jan 18, 2025
a12b4f4
chore: remove a comment
EchoEllet Jan 18, 2025
fd8a359
Update CHANGELOG.md
Maksim-Nikolaev Jan 19, 2025
0cc73be
Added const to QuillToolbarClipboardButton constructor
Maksim-Nikolaev Jan 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `enableClipboardPaste` flag in `QuillToolbarClipboardButton` to determine if the button defaults to `null,` which will use `ClipboardMonitor`, which checks every second if the clipboard has content to paste [#2427](https://github.com/singerdmx/flutter-quill/pull/2427).

## [11.0.0-dev.20] - 2025-01-19

### Changed
Expand Down
67 changes: 57 additions & 10 deletions lib/src/toolbar/buttons/clipboard_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ class ClipboardMonitor {
bool get canPaste => _canPaste;
Timer? _timer;

bool _isCheckingClipboard = false;

void monitorClipboard(bool add, void Function() listener) {
if (kIsWeb) return;

if (add) {
_timer = Timer.periodic(
const Duration(seconds: 1), (timer) => _update(listener));
Expand All @@ -33,17 +36,27 @@ class ClipboardMonitor {
}

Future<void> _update(void Function() listener) async {
if (_isCheckingClipboard) {
return;
}

_isCheckingClipboard = true;

final clipboardService = ClipboardServiceProvider.instance;

if (await clipboardService.hasClipboardContent) {
_canPaste = true;

listener();
}

_isCheckingClipboard = false;
}
}

@experimental
class QuillToolbarClipboardButton extends QuillToolbarToggleStyleBaseButton {
QuillToolbarClipboardButton({
const QuillToolbarClipboardButton({
EchoEllet marked this conversation as resolved.
Show resolved Hide resolved
required super.controller,
required this.clipboardAction,
QuillToolbarClipboardButtonOptions? options,
Expand All @@ -55,21 +68,19 @@ class QuillToolbarClipboardButton extends QuillToolbarToggleStyleBaseButton {
}) : _options = options,
super(options: options ?? const QuillToolbarClipboardButtonOptions());

// TODO: This field will be used by the PR: https://github.com/singerdmx/flutter-quill/pull/2427
// ignore: unused_field
final QuillToolbarClipboardButtonOptions? _options;

final ClipboardAction clipboardAction;

final ClipboardMonitor _monitor = ClipboardMonitor();

@override
State<StatefulWidget> createState() => QuillToolbarClipboardButtonState();
}

class QuillToolbarClipboardButtonState
extends QuillToolbarToggleStyleBaseButtonState<
QuillToolbarClipboardButton> {
final ClipboardMonitor _monitor = ClipboardMonitor();

@override
bool get currentStateValue {
switch (widget.clipboardAction) {
Expand All @@ -78,23 +89,54 @@ class QuillToolbarClipboardButtonState
case ClipboardAction.copy:
return !controller.selection.isCollapsed;
case ClipboardAction.paste:
return !controller.readOnly && (kIsWeb || widget._monitor.canPaste);
return !controller.readOnly &&
(kIsWeb ||
(widget._options?.enableClipboardPaste ?? _monitor.canPaste));
}
}

void _listenClipboardStatus() => didChangeEditingValue();

@override
void didUpdateWidget(QuillToolbarClipboardButton oldWidget) {
super.didUpdateWidget(oldWidget);

// Default didUpdateWidget handler, otherwise simple flag change didn't stop the monitor.
if (oldWidget.controller != controller) {
oldWidget.controller.removeListener(didChangeEditingValue);
removeExtraListener(oldWidget);
controller.addListener(didChangeEditingValue);
addExtraListener();
currentValue = currentStateValue;
}
// The controller didn't change, but enableClipboardPaste did.
else if (widget.clipboardAction == ClipboardAction.paste) {
final isTimerActive = _monitor._timer?.isActive ?? false;

// Enable clipboard monitoring if not active and should be monitored.
if (_shouldUseClipboardMonitor && !isTimerActive) {
_monitor.monitorClipboard(true, _listenClipboardStatus);
}
// Disable clipboard monitoring if active and should not be monitored.
else if (!_shouldUseClipboardMonitor && isTimerActive) {
_monitor.monitorClipboard(false, _listenClipboardStatus);
}

currentValue = currentStateValue;
}
}

@override
void addExtraListener() {
if (widget.clipboardAction == ClipboardAction.paste) {
widget._monitor.monitorClipboard(true, _listenClipboardStatus);
if (_shouldUseClipboardMonitor) {
_monitor.monitorClipboard(true, _listenClipboardStatus);
}
}

@override
void removeExtraListener(covariant QuillToolbarClipboardButton oldWidget) {
if (widget.clipboardAction == ClipboardAction.paste) {
oldWidget._monitor.monitorClipboard(false, _listenClipboardStatus);
if (_shouldUseClipboardMonitor) {
_monitor.monitorClipboard(false, _listenClipboardStatus);
}
}

Expand All @@ -112,6 +154,11 @@ class QuillToolbarClipboardButtonState
ClipboardAction.paste => Icons.paste_outlined,
};

bool get _shouldUseClipboardMonitor {
return widget.clipboardAction == ClipboardAction.paste &&
(widget._options?.enableClipboardPaste == null);
}

void _onPressed() {
switch (widget.clipboardAction) {
case ClipboardAction.cut:
Expand Down
Loading