fix: do not recreate the toolbar when the selection not changed (#1086)

This commit is contained in:
Morn 2025-04-01 09:22:48 +08:00 • committed by GitHub
commit d5c9f64ebe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 0 deletions

View file

@ -75,6 +75,7 @@ class _FloatingToolbarState extends State<FloatingToolbar>
late Brightness brightness = Theme.of(context).brightness; late Brightness brightness = Theme.of(context).brightness;
bool hasMetricsChanged = false; bool hasMetricsChanged = false;
Selection? lastSelection;
@override @override
void initState() { void initState() {
@ -82,6 +83,7 @@ class _FloatingToolbarState extends State<FloatingToolbar>
WidgetsBinding.instance.addObserver(this); WidgetsBinding.instance.addObserver(this);
editorState.selectionNotifier.addListener(_onSelectionChanged); editorState.selectionNotifier.addListener(_onSelectionChanged);
lastSelection = editorState.selection;
widget.editorScrollController.offsetNotifier.addListener( widget.editorScrollController.offsetNotifier.addListener(
_onScrollPositionChanged, _onScrollPositionChanged,
); );
@ -133,6 +135,8 @@ class _FloatingToolbarState extends State<FloatingToolbar>
void _onSelectionChanged() { void _onSelectionChanged() {
final selection = editorState.selection; final selection = editorState.selection;
final selectionType = editorState.selectionType; final selectionType = editorState.selectionType;
if (lastSelection == selection) return;
lastSelection = selection;
if (selection == null || if (selection == null ||
selection.isCollapsed || selection.isCollapsed ||

View file

@ -54,6 +54,32 @@ class FloatingToolbarWidget extends StatefulWidget {
} }
class _FloatingToolbarWidgetState extends State<FloatingToolbarWidget> { class _FloatingToolbarWidgetState extends State<FloatingToolbarWidget> {
EditorState get editorState => widget.editorState;
PropertyValueNotifier<Selection?> get selectionNotifier =>
editorState.selectionNotifier;
@override
void initState() {
super.initState();
selectionNotifier.addListener(_onSelectionChanged);
}
@override
void didUpdateWidget(FloatingToolbarWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.editorState != oldWidget.editorState) {
oldWidget.editorState.selectionNotifier
.removeListener(_onSelectionChanged);
selectionNotifier.addListener(_onSelectionChanged);
}
}
@override
void dispose() {
selectionNotifier.removeListener(_onSelectionChanged);
super.dispose();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var activeItems = _computeActiveItems(); var activeItems = _computeActiveItems();
@ -97,6 +123,10 @@ class _FloatingToolbarWidgetState extends State<FloatingToolbarWidget> {
); );
} }
void _onSelectionChanged() {
if (mounted) setState(() {});
}
Iterable<ToolbarItem> _computeActiveItems() { Iterable<ToolbarItem> _computeActiveItems() {
final activeItems = widget.items final activeItems = widget.items
.where((e) => e.isActive?.call(widget.editorState) ?? false) .where((e) => e.isActive?.call(widget.editorState) ?? false)