Skip to content

Commit

Permalink
Add ElementDocument::Focus method, separated out from ::Show
Browse files Browse the repository at this point in the history
  • Loading branch information
exjam committed Feb 3, 2025
1 parent c3432b3 commit aba3e1e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Include/RmlUi/Core/ElementDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class RMLUICORE_API ElementDocument : public Element {
/// @note The destruction of the document is deferred until the next call to Context::Update().
void Close();

void Focus(FocusFlag focus_flag = FocusFlag::Auto);

/// Creates the named element.
/// @param[in] name The tag name of the element.
ElementPtr CreateElement(const String& name);
Expand Down
50 changes: 24 additions & 26 deletions Source/Core/ElementDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,26 @@ void ElementDocument::Show(ModalFlag modal_flag, FocusFlag focus_flag)
case ModalFlag::Keep: break;
}

// Set to visible and switch focus if necessary.
SetProperty(PropertyId::Visibility, Property(Style::Visibility::Visible));

// Update the document now, otherwise the focusing methods below do not think we are visible. This is also important
// to ensure correct layout for any event handlers, such as for focused input fields to submit the proper caret
// position.
UpdateDocument();

Focus(focus_flag);

DispatchEvent(EventId::Show, Dictionary());
}

void ElementDocument::Focus(FocusFlag focus_flag)
{
bool focus = false;
bool autofocus = false;
bool focus_previous = false;

switch (focus_flag)
{
switch (focus_flag) {
case FocusFlag::None: break;
case FocusFlag::Document: focus = true; break;
case FocusFlag::Keep:
Expand All @@ -401,39 +415,25 @@ void ElementDocument::Show(ModalFlag modal_flag, FocusFlag focus_flag)
break;
}

// Set to visible and switch focus if necessary.
SetProperty(PropertyId::Visibility, Property(Style::Visibility::Visible));

// Update the document now, otherwise the focusing methods below do not think we are visible. This is also important
// to ensure correct layout for any event handlers, such as for focused input fields to submit the proper caret
// position.
UpdateDocument();
if (focus) {
Element *focus_element = this;

if (focus)
{
Element* focus_element = this;
if (autofocus) {
Element *first_element = nullptr;
Element *element = FindNextTabElement(this, true);

if (autofocus)
{
Element* first_element = nullptr;
Element* element = FindNextTabElement(this, true);

while (element && element != first_element)
{
while (element && element != first_element) {
if (!first_element)
first_element = element;

if (element->HasAttribute("autofocus"))
{
if (element->HasAttribute("autofocus")) {
focus_element = element;
break;
}

element = FindNextTabElement(element, true);
}
}
else if (focus_previous)
{
} else if (focus_previous) {
focus_element = GetFocusLeafNode();
}

Expand All @@ -442,8 +442,6 @@ void ElementDocument::Show(ModalFlag modal_flag, FocusFlag focus_flag)
if (focused && focus_element != this)
focus_element->ScrollIntoView(false);
}

DispatchEvent(EventId::Show, Dictionary());
}

void ElementDocument::Hide()
Expand Down

0 comments on commit aba3e1e

Please sign in to comment.