From b3d3a37e5ea20d9d79ed8ed233c92a1a579ac687 Mon Sep 17 00:00:00 2001
From: Jonathan Slenders <jonathan@slenders.be>
Date: Thu, 27 Feb 2025 20:50:39 +0000
Subject: [PATCH] When toggling between sessions, ensure that applications in
 the new session adapt to the client's theme.

---
 server-client.c |  1 +
 session.c       | 13 +++++++++++++
 tmux.h          |  1 +
 tty-keys.c      | 18 ++----------------
 4 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/server-client.c b/server-client.c
index a783a311..1705e0c7 100644
--- a/server-client.c
+++ b/server-client.c
@@ -401,6 +401,7 @@ server_client_set_session(struct client *c, struct session *s)
 		recalculate_sizes();
 		window_update_focus(s->curw->window);
 		session_update_activity(s, NULL);
+		session_theme_changed(s);
 		gettimeofday(&s->last_attached_time, NULL);
 		s->curw->flags &= ~WINLINK_ALERTFLAGS;
 		s->curw->window->latest = c;
diff --git a/session.c b/session.c
index e4aca34d..e6a26d27 100644
--- a/session.c
+++ b/session.c
@@ -751,3 +751,16 @@ session_renumber_windows(struct session *s)
 	RB_FOREACH_SAFE(wl, winlinks, &old_wins, wl1)
 		winlink_remove(&old_wins, wl);
 }
+
+/* Set the PANE_THEMECHANGED flag for every pane in this session. */
+void
+session_theme_changed(struct session *s)
+{
+	struct window_pane	*wp;
+	struct winlink		*wl;
+
+	RB_FOREACH(wl, winlinks, &s->windows) {
+		TAILQ_FOREACH(wp, &wl->window->panes, entry)
+			wp->flags |= PANE_THEMECHANGED;
+	}
+}
diff --git a/tmux.h b/tmux.h
index 852ac642..f405fa09 100644
--- a/tmux.h
+++ b/tmux.h
@@ -3464,6 +3464,7 @@ void		 session_group_synchronize_from(struct session *);
 u_int		 session_group_count(struct session_group *);
 u_int		 session_group_attached_count(struct session_group *);
 void		 session_renumber_windows(struct session *);
+void		 session_theme_changed(struct session *);
 
 /* utf8.c */
 enum utf8_state	 utf8_towc (const struct utf8_data *, wchar_t *);
diff --git a/tty-keys.c b/tty-keys.c
index 15baa1ca..8ce08dd5 100644
--- a/tty-keys.c
+++ b/tty-keys.c
@@ -59,7 +59,6 @@ static int	tty_keys_device_attributes2(struct tty *, const char *, size_t,
 		    size_t *);
 static int	tty_keys_extended_device_attributes(struct tty *, const char *,
 		    size_t, size_t *);
-static void	tty_keys_theme_changed(struct tty *);
 
 /* A key tree entry. */
 struct tty_key {
@@ -796,12 +795,12 @@ tty_keys_next(struct tty *tty)
 	switch (tty_keys_colours(tty, buf, len, &size, &tty->fg, &tty->bg)) {
 	case 0:		/* yes */
 		key = KEYC_UNKNOWN;
-		tty_keys_theme_changed(tty);
+		session_theme_changed(tty->client->session);
 		goto complete_key;
 	case -1:	/* no, or not valid */
 		break;
 	case 1:		/* partial */
-		tty_keys_theme_changed(tty);
+		session_theme_changed(tty->client->session);
 		goto partial_key;
 	}
 
@@ -1686,16 +1685,3 @@ tty_keys_colours(struct tty *tty, const char *buf, size_t len, size_t *size,
 
 	return (0);
 }
-
-/* Set the PANE_THEMECHANGED flag for every pane for client's session. */
-static void
-tty_keys_theme_changed(struct tty *tty)
-{
-	struct window_pane	*wp;
-	struct winlink		*wl;
-
-	RB_FOREACH(wl, winlinks, &tty->client->session->windows) {
-		TAILQ_FOREACH(wp, &wl->window->panes, entry)
-			wp->flags |= PANE_THEMECHANGED;
-	}
-}