Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions src/Forma/Buttons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/Forma/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ public Control PseudoStateSource
/// <summary>Whether focus may currently land here; content-dependent stops can decline while they have nothing to focus.</summary>
public bool AcceptsFocus => FocusMode != FocusMode.None && CanTakeFocus;
internal virtual bool CanTakeFocus => true;
/// <summary>
/// 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 (<c>tabindex="-1"</c>): staying out of the Tab order does not make a button decorative.
/// </summary>
internal virtual bool ConsumesPointerPress => FocusMode != FocusMode.None;
/// <summary>Moves between stops inside this control on Tab; returns false when focus should leave it.</summary>
internal virtual bool MoveFocusWithin(bool backwards) => false;
internal virtual void EnterFocus(bool backwards) { }
Expand Down
2 changes: 2 additions & 0 deletions src/Forma/LineEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 8 additions & 3 deletions src/Forma/UIContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions tests/Forma.Tests/ListBoxRowPointerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Loading