fix: slash menu sometimes incorrectly show on left side (#1082)

This commit is contained in:
Morn 2025-03-27 13:43:03 +08:00 • committed by GitHub
commit 5ad9d771a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -27,6 +27,8 @@ class SelectionMenu extends SelectionMenuService {
this.style = SelectionMenuStyle.light, this.style = SelectionMenuStyle.light,
this.itemCountFilter = 0, this.itemCountFilter = 0,
this.singleColumn = false, this.singleColumn = false,
this.menuHeight = 300,
this.menuWidth = 300,
}); });
final BuildContext context; final BuildContext context;
@ -35,6 +37,8 @@ class SelectionMenu extends SelectionMenuService {
final bool deleteSlashByDefault; final bool deleteSlashByDefault;
final bool deleteKeywordsByDefault; final bool deleteKeywordsByDefault;
final bool singleColumn; final bool singleColumn;
final double menuHeight;
final double menuWidth;
@override @override
final SelectionMenuStyle style; final SelectionMenuStyle style;
@ -207,7 +211,6 @@ class SelectionMenu extends SelectionMenuService {
// Workaround: We can customize the padding through the [EditorStyle], // Workaround: We can customize the padding through the [EditorStyle],
// but the coordinates of overlay are not properly converted currently. // but the coordinates of overlay are not properly converted currently.
// Just subtract the padding here as a result. // Just subtract the padding here as a result.
const menuHeight = 300.0;
const menuOffset = Offset(0, 10); const menuOffset = Offset(0, 10);
final editorOffset = final editorOffset =
editorState.renderBox?.localToGlobal(Offset.zero) ?? Offset.zero; editorState.renderBox?.localToGlobal(Offset.zero) ?? Offset.zero;
@ -235,8 +238,14 @@ class SelectionMenu extends SelectionMenuService {
); );
} }
// show on right
if (_offset.dx + menuWidth < editorOffset.dx + editorWidth) {
_offset = Offset(
_offset.dx,
_offset.dy,
);
} else if (offset.dx - editorOffset.dx > menuWidth) {
// show on left // show on left
if (_offset.dx - editorOffset.dx > editorWidth / 2) {
_alignment = _alignment == Alignment.topLeft _alignment = _alignment == Alignment.topLeft
? Alignment.topRight ? Alignment.topRight
: Alignment.bottomRight; : Alignment.bottomRight;