diff --git a/src/Forma/Buttons.cs b/src/Forma/Buttons.cs index 3b890f98..65cf24f1 100644 --- a/src/Forma/Buttons.cs +++ b/src/Forma/Buttons.cs @@ -41,6 +41,9 @@ public BaseButton() FocusMode = FocusMode.All; Padding = new Thickness(8, 4, 8, 4); } + // A button keeps a pointer press even when the author took it out of the Tab order: inside a list row it must + // not fall through to the row's selection or activation. + internal override bool ConsumesPointerPress => true; public string Text { get => _text; diff --git a/src/Forma/Control.cs b/src/Forma/Control.cs index 5be2172f..f5cad6c6 100644 --- a/src/Forma/Control.cs +++ b/src/Forma/Control.cs @@ -301,6 +301,12 @@ public Control PseudoStateSource /// Whether focus may currently land here; content-dependent stops can decline while they have nothing to focus. public bool AcceptsFocus => FocusMode != FocusMode.None && CanTakeFocus; internal virtual bool CanTakeFocus => true; + /// + /// Whether a pointer press on this control is the control's own interaction, so a list row it sits in must not also + /// treat the press as row content and select or activate the row. Widgets answer true even when they are not + /// focusable (tabindex="-1"): staying out of the Tab order does not make a button decorative. + /// + internal virtual bool ConsumesPointerPress => FocusMode != FocusMode.None; /// Moves between stops inside this control on Tab; returns false when focus should leave it. internal virtual bool MoveFocusWithin(bool backwards) => false; internal virtual void EnterFocus(bool backwards) { } diff --git a/src/Forma/LineEdit.cs b/src/Forma/LineEdit.cs index 079a87ff..4f37f36b 100644 --- a/src/Forma/LineEdit.cs +++ b/src/Forma/LineEdit.cs @@ -61,6 +61,8 @@ public LineEdit() _controlCharacterMenu = new PopupMenu { Visible = false }; _controlCharacterMenu.IdPressed += (_, id) => MenuOption((LineEditMenuOption)id); } + // A text field keeps a pointer press even when the author took it out of the Tab order (see Control.ConsumesPointerPress). + internal override bool ConsumesPointerPress => true; public string Text { get => _text; diff --git a/src/Forma/UIContext.cs b/src/Forma/UIContext.cs index a741a1cb..cbe008f7 100644 --- a/src/Forma/UIContext.cs +++ b/src/Forma/UIContext.cs @@ -877,14 +877,19 @@ private static void DispatchPointerPressed(Control target, Point point) // Row content (a Label, Border, container) defaults to MouseFilter.Stop, so a press on it never bubbles to the // ListBox that owns the row. When a non-interactive control stops the event inside a ListBoxItem, the list - // still receives it; a focusable control (a Button) keeps the click for itself. + // still receives it; a widget (a Button, a LineEdit, anything focusable) keeps the press for itself, and so does + // a press anywhere inside such a widget - a button's own border or label must not read as row content either. private static ListBox GetRowOwnerForStoppedPointer(Control stoppedAt) { - if (stoppedAt is ListBox || (stoppedAt.FocusMode != FocusMode.None && stoppedAt is not ListBoxItem)) return null; + if (stoppedAt is ListBox) return null; for (var control = stoppedAt; control != null; control = control.VisualParent) { if (control is ListBox) return null; - if (control is not ListBoxItem) continue; + if (control is not ListBoxItem) + { + if (control.ConsumesPointerPress) return null; + continue; + } for (var parent = control.VisualParent; parent != null; parent = parent.VisualParent) if (parent is ListBox list) return list; return null; diff --git a/tests/Forma.Tests/ListBoxRowPointerTest.cs b/tests/Forma.Tests/ListBoxRowPointerTest.cs index 6e22f67a..b74686af 100644 --- a/tests/Forma.Tests/ListBoxRowPointerTest.cs +++ b/tests/Forma.Tests/ListBoxRowPointerTest.cs @@ -84,7 +84,50 @@ public void ButtonInsideARowStillGetsItsOwnClick() return Row(button); }); using var context = input.Context; + var activated = 0; + list.ItemActivated += (_, _) => activated++; input.Click(HeadlessInput.CenterOf(list.GetRealizedContainer(0))); Assert.That(clicked, Is.EqualTo(1)); + Assert.That(activated, Is.EqualTo(0)); + } + + [Test] + public void ButtonOutsideTheTabOrderKeepsItsClickAndDoesNotActivateTheRow() + { + // tabindex="-1" turns off FocusMode but not the button: its press must not fall through to the row. + var clicked = 0; + var (input, list) = Build(() => + { + var button = new Button { Text = "x", CustomMinimumSize = new Vector2(280, 30), FocusMode = FocusMode.None }; + button.Pressed += (_, _) => clicked++; + return Row(button); + }, singleClick: true); + using var context = input.Context; + var activated = 0; + list.ItemActivated += (_, _) => activated++; + input.Click(HeadlessInput.CenterOf(list.GetRealizedContainer(1))); + Assert.That(clicked, Is.EqualTo(1)); + Assert.That(activated, Is.EqualTo(0)); + } + + [Test] + public void PressInsideAButtonOfARowIsNotRowContent() + { + // What the hit test finds can be the button's own border or label, not the button itself. + var clicked = 0; + var (input, list) = Build(() => + { + var button = new Button { Text = "x", CustomMinimumSize = new Vector2(280, 30), FocusMode = FocusMode.None }; + button.Pressed += (_, _) => clicked++; + var box = new HBoxContainer { CustomMinimumSize = new Vector2(280, 30) }; + box.AddChild(button); + return box; + }, singleClick: true); + using var context = input.Context; + var activated = 0; + list.ItemActivated += (_, _) => activated++; + input.Click(HeadlessInput.CenterOf(list.GetRealizedContainer(2))); + Assert.That(clicked, Is.EqualTo(1)); + Assert.That(activated, Is.EqualTo(0)); } }