Skip to content

Commit

Permalink
Text widget changes (squash)
Browse files Browse the repository at this point in the history
Use separate bool for layout_dirty vs box just opened, as these two states are not really mutually exclusive. Fixes a bug where the box was not considered just opened since the "open" state was overwritten by the "switch" state.

Use center vertical alignment for a just opened box. This is admittedly a subjective matter. However, it looked a bit weird to me how it would scroll past the first few elements when you select, say, just the second or third element. With center, both the first few, and the last items, are shown when selecting something near the beginning or the end respectively. This also feels more symmetrical when the selection box ends up being placed above the select element.
  • Loading branch information
mikke89 committed Oct 30, 2024
1 parent bb8ab0e commit 13e210c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
26 changes: 15 additions & 11 deletions Source/Core/Elements/WidgetDropDown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ WidgetDropDown::WidgetDropDown(ElementFormControl* element)

lock_selection = false;
selection_dirty = false;
box_layout_dirty = DropDownBoxLayoutType::None;
box_layout_dirty = false;
box_opened_since_last_format = false;
value_rml_dirty = false;
value_layout_dirty = false;
box_visible = false;
Expand Down Expand Up @@ -151,7 +152,7 @@ void WidgetDropDown::OnUpdate()

void WidgetDropDown::OnRender()
{
if (box_visible && box_layout_dirty != DropDownBoxLayoutType::None)
if (box_visible && box_layout_dirty)
{
// Layout the selection box.
// The following procedure should ensure that the selection box is never (partly) outside of the context's window.
Expand Down Expand Up @@ -235,13 +236,15 @@ void WidgetDropDown::OnRender()
// Scroll selected element into view, if we have one
if (selection != -1)
{
Rml::ScrollIntoViewOptions scrollOptions {
box_layout_dirty == DropDownBoxLayoutType::Open ? Rml::ScrollAlignment::Start : Rml::ScrollAlignment::Nearest
ScrollIntoViewOptions scroll_options = {
box_opened_since_last_format ? ScrollAlignment::Center : ScrollAlignment::Nearest,
ScrollAlignment::Nearest,
ScrollBehavior::Instant,
};
GetOption(selection)->ScrollIntoView(scrollOptions);
GetOption(selection)->ScrollIntoView(scroll_options);
}

box_layout_dirty = DropDownBoxLayoutType::None;
box_opened_since_last_format = false;
box_layout_dirty = false;
}

if (value_layout_dirty)
Expand Down Expand Up @@ -278,7 +281,7 @@ void WidgetDropDown::OnLayout()
value_element->SetOffset(parent_element->GetBox().GetPosition(BoxArea::Content), parent_element);
value_element->SetBox(Box(size));

box_layout_dirty = DropDownBoxLayoutType::Switch;
box_layout_dirty = true;
value_layout_dirty = true;
}

Expand Down Expand Up @@ -460,7 +463,7 @@ void WidgetDropDown::OnChildAdd(Element* element)
SetSelection(element, true);

selection_dirty = true;
box_layout_dirty = DropDownBoxLayoutType::Switch;
box_layout_dirty = true;
}

void WidgetDropDown::OnChildRemove(Element* element)
Expand All @@ -474,7 +477,7 @@ void WidgetDropDown::OnChildRemove(Element* element)
SetSelection(nullptr);

selection_dirty = true;
box_layout_dirty = DropDownBoxLayoutType::Switch;
box_layout_dirty = true;
}

void WidgetDropDown::AttachScrollEvent()
Expand Down Expand Up @@ -629,7 +632,8 @@ void WidgetDropDown::ShowSelectBox()
selection_element->SetPseudoClass("checked", true);
value_element->SetPseudoClass("checked", true);
button_element->SetPseudoClass("checked", true);
box_layout_dirty = DropDownBoxLayoutType::Open;
box_layout_dirty = true;
box_opened_since_last_format = true;
AttachScrollEvent();

box_visible = true;
Expand Down
12 changes: 3 additions & 9 deletions Source/Core/Elements/WidgetDropDown.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ class ElementFormControl;
@author Lloyd Weehuizen
*/

enum class DropDownBoxLayoutType {
None,
Open, // we just opened the drop-down menu
Switch // we're switching the selected value
};

class WidgetDropDown : public EventListener {
public:
WidgetDropDown(ElementFormControl* element);
Expand Down Expand Up @@ -105,7 +99,7 @@ class WidgetDropDown : public EventListener {

/// Processes the incoming event.
void ProcessEvent(Event& event) override;

/// Shows the selection box.
void ShowSelectBox();
/// Hides the selection box.
Expand All @@ -114,7 +108,6 @@ class WidgetDropDown : public EventListener {
bool IsSelectBoxVisible();

private:

void AttachScrollEvent();
void DetachScrollEvent();

Expand All @@ -130,7 +123,8 @@ class WidgetDropDown : public EventListener {
bool selection_dirty;
bool value_rml_dirty;
bool value_layout_dirty;
DropDownBoxLayoutType box_layout_dirty;
bool box_layout_dirty;
bool box_opened_since_last_format;
bool box_visible;
};

Expand Down

0 comments on commit 13e210c

Please sign in to comment.