From aba3e1e0e1ba3feccd59b32cf560d4a147a2a096 Mon Sep 17 00:00:00 2001 From: James Benton Date: Mon, 27 Jan 2025 13:53:41 +0000 Subject: [PATCH] Add ElementDocument::Focus method, separated out from ::Show --- Include/RmlUi/Core/ElementDocument.h | 2 ++ Source/Core/ElementDocument.cpp | 50 +++++++++++++--------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Include/RmlUi/Core/ElementDocument.h b/Include/RmlUi/Core/ElementDocument.h index 66a7a306a..1f8685ea3 100644 --- a/Include/RmlUi/Core/ElementDocument.h +++ b/Include/RmlUi/Core/ElementDocument.h @@ -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); diff --git a/Source/Core/ElementDocument.cpp b/Source/Core/ElementDocument.cpp index 59e2818ca..b6264629f 100644 --- a/Source/Core/ElementDocument.cpp +++ b/Source/Core/ElementDocument.cpp @@ -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: @@ -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(); } @@ -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()