Maelstrom: Fixed the on-screen keyboard staying up after a dialog is hidden

From 313fc32c1019839ffa4af0083142b9daa1b77200 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 5 Apr 2026 20:46:18 -0700
Subject: [PATCH] Fixed the on-screen keyboard staying up after a dialog is
 hidden

---
 screenlib/UIBaseElement.h      | 11 +++++++++++
 screenlib/UIElementEditbox.cpp | 10 ++++++++++
 screenlib/UIElementEditbox.h   |  2 ++
 3 files changed, 23 insertions(+)

diff --git a/screenlib/UIBaseElement.h b/screenlib/UIBaseElement.h
index e0f3bca9..3650d858 100644
--- a/screenlib/UIBaseElement.h
+++ b/screenlib/UIBaseElement.h
@@ -174,16 +174,27 @@ class UIBaseElement : public UIArea
 		if (m_parent) {
 			m_parent->OnChildShown(this);
 		}
+		OnVisibilityChanged(true);
 	}
 	virtual void Hide() {
 		m_shown = false;
 		if (m_parent) {
 			m_parent->OnChildHidden(this);
 		}
+		OnVisibilityChanged(false);
 	}
 	bool IsShown() const {
 		return m_shown;
 	}
+	virtual void OnVisibilityChanged(bool visible) {
+		for (unsigned int i = 0; i < m_elements.length(); ++i) {
+			UIBaseElement *element = m_elements[i];
+			if (!element->IsShown()) {
+				continue;
+			}
+			element->OnVisibilityChanged(visible);
+		}
+	}
 	virtual void OnRectChanged() {
 		UIArea::OnRectChanged();
 
diff --git a/screenlib/UIElementEditbox.cpp b/screenlib/UIElementEditbox.cpp
index 71a5f887..d175711b 100644
--- a/screenlib/UIElementEditbox.cpp
+++ b/screenlib/UIElementEditbox.cpp
@@ -158,6 +158,16 @@ UIElementEditbox::SetFocus(bool focus)
 	}
 }
 
+void
+UIElementEditbox::OnVisibilityChanged(bool visible)
+{
+	if (!visible) {
+		SetFocus(false);
+	}
+
+	UIElementButton::OnVisibilityChanged(visible);
+}
+
 void
 UIElementEditbox::SetHighlight(bool highlight)
 {
diff --git a/screenlib/UIElementEditbox.h b/screenlib/UIElementEditbox.h
index ade5e6b5..0736e85a 100644
--- a/screenlib/UIElementEditbox.h
+++ b/screenlib/UIElementEditbox.h
@@ -54,6 +54,8 @@ DECLARE_TYPESAFE_CLASS(UIElement)
 		return m_focus;
 	}
 
+	virtual void OnVisibilityChanged(bool visible) override;
+
 	void SetTextMax(int maxLen);
 
 	virtual void SetText(const char *text) override;