-
Notifications
You must be signed in to change notification settings - Fork 847
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
ClipboardMonitor causes background lag when enabling clipboard buttons #2421
Comments
It is possible there is a better implementation of Clipboard Monitor. I will try to take a look myself and tweak some stuff to see if it is any better. Right now it re-instantiates the ClipboardService every second, which could be an issue. class ClipboardMonitor {
bool _canPaste = false;
bool get canPaste => _canPaste;
Timer? _timer;
void monitorClipboard(bool add, void Function() listener) {
if (kIsWeb) return;
if (add) {
_timer = Timer.periodic(
const Duration(seconds: 1), (timer) => _update(listener));
} else {
_timer?.cancel();
}
}
Future<void> _update(void Function() listener) async {
final clipboardService = ClipboardServiceProvider.instance;
if (await clipboardService.hasClipboardContent) {
_canPaste = true;
listener();
}
}
} |
Did you encounter the issue with older versions in case you used them?
To clarify, the I will disable those buttons as a breaking change in v11 since they were already not enabled and introduced as unexpected change in a minor release. Related lines. I'm thinking about removing them since supporting them is not a priority, and it's better to support less well-done and maintained features. |
I used the previous versions but cannot confirm with certainty that it was also present. I'll be looking into Clipboard monitor and try to fix them on myself and will open a PR if I have success. Thanks for clarifying that Clipboard service is actually being used even when the buttons are not visible, I was not aware of this and it actually reduces the scope for my investigation :) Also, Merry Christmas 🎄 |
The PR #1843 could be related, disabling the clipboard toolbar buttons should fix the issue.
It might be worth trying quill_super_clipboard since it will replace the
It's being used when copying an image, pasting an image (when What is the use-case for clipboard actions in the toolbar, or why is it needed?
You're welcome. I'm familiar with those parts (except #1843), so let me know if you have questions. |
Just a very convenient way to use some actions (especially paste). I have no problem implementing them on my own, but I'll give it a try to fix buttons / clipboard monitor, so those actions are available for everyone using the plugin from toolbar as well.
After a second thought, I do use them for pasting images. I'll give it a proper test and come back to it later on |
Change introduced (7abe6d2). |
I have concluded my review and can give some more details:
I would suggest to allow the users to determine "canPaste" status on their own and take control of the button's state. Then the users can also implement their own error handling for "onPaste". Let me know if you would agree and then I can prepare a PullRequest with changes. |
One possible solution is to listen to the system clipboard changes, which involves platform-specific code and might not be available on some platforms or has some restrictions.
I was considering replacing it with platform code in
How would they solve it? It's not possible without platform code AFAIK, and I think users prefer something that's already implemented without much configuration. For now, we should notify users about this issue before they enable those buttons. |
Exactly. I was trying to see if there are available solutions but it feels like an overkill
This is already implemented in flutter with the simple in-built import from "services". import 'package:flutter/services.dart';
...
await Clipboard.getData(Clipboard.kTextPlain); And this is exactly the same how it is used in flutter quill => clipboard_service /// If the Clipboard is not empty or has something to paste
Future<bool> get hasClipboardContent async {
final clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
return clipboardData != null;
} For me it also feels like that they can implement their own "canPaste" (aka isEnabled) if they really need it to avoid periodic checks.
|
You can paste this code to dartpad to verify that it is available. import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
void getClipboardContent() async {
final ClipboardData? clipboardData =
await Clipboard.getData(Clipboard.kTextPlain);
final String? copiedText = clipboardData?.text;
print("Clipboard content: ${copiedText}");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: OutlinedButton(
onPressed: getClipboardContent,
child: Text("Print clipboard content"),
),
),
),
);
}
} |
BTW, the current solution needs improvement. The
The issue is how we can listen to clipboard state changes, and the current solution achieves that by running platform code every 1 second from the Dart side, which is not efficient.
Assuming they have greater control, I will check to see if it's possible using platform code. |
I think it comes to the discussion if the "canPaste" flag is even needed for the in-built "paste" button. For my use case I would just completely remove the canPaste flag and make it always active, and handle the exceptions/empty clipboard when I actually interact with the button |
Is there an existing issue for this?
Flutter Quill version
flutter_quill: ^11.0.0-dev.17 & flutter_quill_extensions: ^11.0.0-dev.7
Steps to reproduce
QuillSimpleToolbar
and passQuillSimpleToolbarConfig
withshowClipboardCut
,showClipboardCopy
, orshowClipboardPaste
set to trueSource code for reference:
QuillToolbarClipboardButton
was added to the Widget tree, it starts affecting the app performance & memory on the background, even if you are not actively copying or pasting with clipboard at the moment.Expected results
Expected Clipboard to not be as heavy for the application and to not cause performance issues.
Actual results
ClipboardService causes background lag when used.
The text was updated successfully, but these errors were encountered: