Sfoglia il codice sorgente

Allow documents to set themselves as focusable from modal documents, use in debugger to solve #642

Michael Ragazzon 1 anno fa
parent
commit
263328ab5d

+ 6 - 0
Include/RmlUi/Core/ElementDocument.h

@@ -148,6 +148,11 @@ protected:
 	/// Called during update if the element size has been changed.
 	void OnResize() override;
 
+	/// Returns whether the document can receive focus during click when another document is modal.
+	bool IsFocusableFromModal() const;
+	/// Sets whether the document can receive focus when another document is modal.
+	void SetFocusableFromModal(bool focusable);
+
 private:
 	/// Find the next element to focus, starting at the current element
 	Element* FindNextTabElement(Element* current_element, bool forward);
@@ -183,6 +188,7 @@ private:
 	Context* context;
 
 	bool modal;
+	bool focusable_from_modal;
 
 	bool layout_dirty;
 	bool position_dirty;

+ 20 - 14
Source/Core/Context.cpp

@@ -989,8 +989,8 @@ bool Context::OnFocusChange(Element* new_focus, bool focus_visible)
 	ElementDocument* old_document = old_focus ? old_focus->GetOwnerDocument() : nullptr;
 	ElementDocument* new_document = new_focus->GetOwnerDocument();
 
-	// If the current focus is modal and the new focus is not modal, deny the request
-	if (old_document && old_document->IsModal() && (!new_document || !new_document->GetOwnerDocument()->IsModal()))
+	// If the current focus is modal and the new focus is cannot receive focus from modal, deny the request.
+	if (old_document && old_document->IsModal() && (!new_document || !(new_document->IsModal() || new_document->IsFocusableFromModal())))
 		return false;
 
 	// Build the old chains
@@ -1162,17 +1162,15 @@ Element* Context::GetElementAtPoint(Vector2f point, const Element* ignore_elemen
 		element = root.get();
 	}
 
-	// Check if any documents have modal focus; if so, only check down than document.
-	if (element == root.get())
+	bool is_modal = false;
+	ElementDocument* focus_document = nullptr;
+
+	// If we have modal focus, only check down documents that can receive focus from modals.
+	if (element == root.get() && focus)
 	{
-		if (focus)
-		{
-			ElementDocument* focus_document = focus->GetOwnerDocument();
-			if (focus_document && focus_document->IsModal())
-			{
-				element = focus_document;
-			}
-		}
+		focus_document = focus->GetOwnerDocument();
+		if (focus_document && focus_document->IsModal())
+			is_modal = true;
 	}
 
 	// Check any elements within our stacking context. We want to return the lowest-down element
@@ -1184,10 +1182,11 @@ Element* Context::GetElementAtPoint(Vector2f point, const Element* ignore_elemen
 
 		for (int i = (int)element->stacking_context.size() - 1; i >= 0; --i)
 		{
+			Element* stacking_child = element->stacking_context[i];
 			if (ignore_element)
 			{
 				// Check if the element is a descendant of the element we're ignoring.
-				Element* element_hierarchy = element->stacking_context[i];
+				Element* element_hierarchy = stacking_child;
 				while (element_hierarchy)
 				{
 					if (element_hierarchy == ignore_element)
@@ -1200,7 +1199,14 @@ Element* Context::GetElementAtPoint(Vector2f point, const Element* ignore_elemen
 					continue;
 			}
 
-			Element* child_element = GetElementAtPoint(point, ignore_element, element->stacking_context[i]);
+			if (is_modal)
+			{
+				ElementDocument* child_document = stacking_child->GetOwnerDocument();
+				if (!child_document || !(child_document == focus_document || child_document->IsFocusableFromModal()))
+					continue;
+			}
+
+			Element* child_element = GetElementAtPoint(point, ignore_element, stacking_child);
 			if (child_element)
 				return child_element;
 		}

+ 12 - 1
Source/Core/ElementDocument.cpp

@@ -175,8 +175,9 @@ ElementDocument::ElementDocument(const String& tag) : Element(tag)
 	context = nullptr;
 
 	modal = false;
-	layout_dirty = true;
+	focusable_from_modal = false;
 
+	layout_dirty = true;
 	position_dirty = false;
 
 	ForceLocalStackingContext();
@@ -699,6 +700,16 @@ void ElementDocument::OnResize()
 	DirtyPosition();
 }
 
+bool ElementDocument::IsFocusableFromModal() const
+{
+	return focusable_from_modal && IsVisible();
+}
+
+void ElementDocument::SetFocusableFromModal(bool focusable)
+{
+	focusable_from_modal = focusable;
+}
+
 Element* ElementDocument::FindNextTabElement(Element* current_element, bool forward)
 {
 	// This algorithm is quite sneaky, I originally thought a depth first search would work, but it appears not. What is

+ 2 - 0
Source/Debugger/CMakeLists.txt

@@ -8,6 +8,8 @@ add_library(rmlui_debugger
 	DebuggerSystemInterface.h
 	ElementContextHook.cpp
 	ElementContextHook.h
+	ElementDebugDocument.cpp
+	ElementDebugDocument.h
 	ElementInfo.cpp
 	ElementInfo.h
 	ElementLog.cpp

+ 4 - 1
Source/Debugger/DebuggerPlugin.cpp

@@ -35,6 +35,7 @@
 #include "../../Include/RmlUi/Core/Types.h"
 #include "DebuggerSystemInterface.h"
 #include "ElementContextHook.h"
+#include "ElementDebugDocument.h"
 #include "ElementInfo.h"
 #include "ElementLog.h"
 #include "FontSource.h"
@@ -274,7 +275,9 @@ bool DebuggerPlugin::LoadFont()
 
 bool DebuggerPlugin::LoadMenuElement()
 {
-	menu_element = host_context->CreateDocument();
+	debug_document_instancer = MakeUnique<ElementInstancerGeneric<ElementDebugDocument>>();
+	Factory::RegisterElementInstancer("debug-document", debug_document_instancer.get());
+	menu_element = host_context->CreateDocument("debug-document");
 	if (!menu_element)
 		return false;
 

+ 1 - 1
Source/Debugger/DebuggerPlugin.h

@@ -119,7 +119,7 @@ private:
 	Rml::SystemInterface* application_interface;
 	UniquePtr<DebuggerSystemInterface> log_interface;
 
-	UniquePtr<ElementInstancer> hook_element_instancer, info_element_instancer, log_element_instancer;
+	UniquePtr<ElementInstancer> hook_element_instancer, debug_document_instancer, info_element_instancer, log_element_instancer;
 
 	bool render_outlines;
 

+ 1 - 1
Source/Debugger/ElementContextHook.cpp

@@ -32,7 +32,7 @@
 namespace Rml {
 namespace Debugger {
 
-ElementContextHook::ElementContextHook(const String& tag) : ElementDocument(tag)
+ElementContextHook::ElementContextHook(const String& tag) : ElementDebugDocument(tag)
 {
 	debugger = nullptr;
 }

+ 3 - 2
Source/Debugger/ElementContextHook.h

@@ -30,6 +30,7 @@
 #define RMLUI_DEBUGGER_ELEMENTCONTEXTHOOK_H
 
 #include "../../Include/RmlUi/Core/ElementDocument.h"
+#include "ElementDebugDocument.h"
 
 namespace Rml {
 namespace Debugger {
@@ -42,9 +43,9 @@ class DebuggerPlugin;
     @author Peter Curry
  */
 
-class ElementContextHook : public ElementDocument {
+class ElementContextHook : public ElementDebugDocument {
 public:
-	RMLUI_RTTI_DefineWithParent(ElementContextHook, ElementDocument)
+	RMLUI_RTTI_DefineWithParent(ElementContextHook, ElementDebugDocument)
 
 	ElementContextHook(const String& tag);
 	virtual ~ElementContextHook();

+ 40 - 0
Source/Debugger/ElementDebugDocument.cpp

@@ -0,0 +1,40 @@
+/*
+ * This source file is part of RmlUi, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://github.com/mikke89/RmlUi
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ * Copyright (c) 2019-2024 The RmlUi Team, and contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "ElementDebugDocument.h"
+
+namespace Rml {
+namespace Debugger {
+
+ElementDebugDocument::ElementDebugDocument(const String& tag) : ElementDocument(tag)
+{
+	SetFocusableFromModal(true);
+}
+
+} // namespace Debugger
+} // namespace Rml

+ 47 - 0
Source/Debugger/ElementDebugDocument.h

@@ -0,0 +1,47 @@
+/*
+ * This source file is part of RmlUi, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://github.com/mikke89/RmlUi
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ * Copyright (c) 2019-2024 The RmlUi Team, and contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#ifndef RMLUI_DEBUGGER_ELEMENTDEBUGDOCUMENT_H
+#define RMLUI_DEBUGGER_ELEMENTDEBUGDOCUMENT_H
+
+#include "../../Include/RmlUi/Core/ElementDocument.h"
+
+namespace Rml {
+namespace Debugger {
+
+class ElementDebugDocument : public ElementDocument {
+public:
+	RMLUI_RTTI_DefineWithParent(ElementDebugDocument, ElementDocument)
+
+	ElementDebugDocument(const String& tag);
+};
+
+} // namespace Debugger
+} // namespace Rml
+
+#endif

+ 1 - 1
Source/Debugger/ElementInfo.cpp

@@ -47,7 +47,7 @@
 namespace Rml {
 namespace Debugger {
 
-ElementInfo::ElementInfo(const String& tag) : ElementDocument(tag)
+ElementInfo::ElementInfo(const String& tag) : ElementDebugDocument(tag)
 {
 	hover_element = nullptr;
 	source_element = nullptr;

+ 5 - 2
Source/Debugger/ElementInfo.h

@@ -31,6 +31,7 @@
 
 #include "../../Include/RmlUi/Core/ElementDocument.h"
 #include "../../Include/RmlUi/Core/EventListener.h"
+#include "ElementDebugDocument.h"
 
 namespace Rml {
 namespace Debugger {
@@ -42,9 +43,9 @@ typedef Vector<NamedProperty> NamedPropertyList;
     @author Robert Curry
  */
 
-class ElementInfo : public ElementDocument, public EventListener {
+class ElementInfo : public ElementDebugDocument, public EventListener {
 public:
-	RMLUI_RTTI_DefineWithParent(ElementInfo, ElementDocument)
+	RMLUI_RTTI_DefineWithParent(ElementInfo, ElementDebugDocument)
 
 	ElementInfo(const String& tag);
 	~ElementInfo();
@@ -61,6 +62,8 @@ public:
 	void RenderHoverElement();
 	void RenderSourceElement();
 
+	Element* GetSourceElement() const { return source_element; }
+
 protected:
 	void ProcessEvent(Event& event) override;
 	/// Updates the element info if changed

+ 4 - 3
Source/Debugger/ElementLog.cpp

@@ -39,7 +39,7 @@ namespace Debugger {
 
 const int MAX_LOG_MESSAGES = 50;
 
-ElementLog::ElementLog(const String& tag) : ElementDocument(tag)
+ElementLog::ElementLog(const String& tag) : ElementDebugDocument(tag)
 {
 	dirty_logs = false;
 	beacon = nullptr;
@@ -85,7 +85,7 @@ ElementLog::~ElementLog()
 	if (beacon && beacon->GetFirstChild())
 		beacon->GetFirstChild()->RemoveEventListener(EventId::Click, this);
 
-	if (beacon && GetParentNode())
+	if (beacon && beacon->GetParentNode())
 		beacon->GetParentNode()->RemoveChild(beacon);
 
 	if (message_content)
@@ -114,7 +114,8 @@ bool ElementLog::Initialise()
 	AddEventListener(EventId::Click, this);
 
 	// Create the log beacon.
-	beacon = GetContext()->CreateDocument();
+	beacon = GetContext()->CreateDocument("debug-document");
+	RMLUI_ASSERT(rmlui_dynamic_cast<ElementDebugDocument*>(beacon));
 	if (!beacon)
 		return false;
 

+ 3 - 2
Source/Debugger/ElementLog.h

@@ -32,6 +32,7 @@
 #include "../../Include/RmlUi/Core/ElementDocument.h"
 #include "../../Include/RmlUi/Core/EventListener.h"
 #include "../../Include/RmlUi/Core/Types.h"
+#include "ElementDebugDocument.h"
 
 namespace Rml {
 namespace Debugger {
@@ -42,9 +43,9 @@ class DebuggerSystemInterface;
     @author Robert Curry
  */
 
-class ElementLog : public Rml::ElementDocument, public Rml::EventListener {
+class ElementLog : public ElementDebugDocument, public Rml::EventListener {
 public:
-	RMLUI_RTTI_DefineWithParent(ElementLog, Rml::ElementDocument)
+	RMLUI_RTTI_DefineWithParent(ElementLog, ElementDebugDocument)
 
 	ElementLog(const String& tag);
 	~ElementLog();