Maelstrom: Clicking off of a focused editbox clears focus

From 815bbb23febd65b0db7a3c4388c5dc7768aada31 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 5 Apr 2026 22:06:53 -0700
Subject: [PATCH] Clicking off of a focused editbox clears focus

Clicking on a focused editbox highlights the text again.
---
 screenlib/UIElementEditbox.cpp | 13 +++++++++++++
 screenlib/UIElementEditbox.h   |  3 ++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/screenlib/UIElementEditbox.cpp b/screenlib/UIElementEditbox.cpp
index d175711b..e0724fa2 100644
--- a/screenlib/UIElementEditbox.cpp
+++ b/screenlib/UIElementEditbox.cpp
@@ -72,6 +72,19 @@ UIElementEditbox::HandleEvent(const SDL_Event &event)
 		return UIElementButton::HandleEvent(event);
 	}
 
+	if (event.type == SDL_EVENT_MOUSE_MOTION ||
+	    event.type == SDL_EVENT_MOUSE_BUTTON_DOWN ||
+	    event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
+		if (UIElementButton::HandleEvent(event)) {
+			return true;
+		}
+		if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
+			// Clicked somewhere else, give up focus
+			SetFocus(false);
+			return false;
+		}
+	}
+
 	if (event.type == SDL_EVENT_KEY_UP) {
 		switch (event.key.key) {
 			// This is confusing in dialogs which are canceled by Escape.
diff --git a/screenlib/UIElementEditbox.h b/screenlib/UIElementEditbox.h
index 0736e85a..f956a908 100644
--- a/screenlib/UIElementEditbox.h
+++ b/screenlib/UIElementEditbox.h
@@ -41,7 +41,8 @@ DECLARE_TYPESAFE_CLASS(UIElement)
 	virtual bool HandleEvent(const SDL_Event &event) override;
 
 	virtual void OnClick() override {
-		SetFocus(!HasFocus());
+		SetFocus(false);
+		SetFocus(true);
 	}
 
 	bool IsHighlighted() const {