Add mode 2031 support to automatically report dark or light theme. tmux

will guess the theme from the background colour on terminals which do
not themselves support the escape sequence. Written by Jonathan
Slenders, GitHub issue 4353.
This commit is contained in:
nicm
2025-03-04 08:45:04 +00:00
parent 3543d79048
commit eaf70c955b
17 changed files with 380 additions and 107 deletions

View File

@ -60,11 +60,11 @@ static void server_client_set_title(struct client *);
static void server_client_set_path(struct client *);
static void server_client_reset_state(struct client *);
static void server_client_update_latest(struct client *);
static void server_client_dispatch(struct imsg *, void *);
static void server_client_dispatch_command(struct client *, struct imsg *);
static void server_client_dispatch_identify(struct client *, struct imsg *);
static void server_client_dispatch_shell(struct client *);
static void server_client_report_theme(struct client *, enum client_theme);
/* Compare client windows. */
static int
@ -304,6 +304,7 @@ server_client_create(int fd)
c->tty.sx = 80;
c->tty.sy = 24;
c->theme = THEME_UNKNOWN;
status_init(c);
c->flags |= CLIENT_FOCUSED;
@ -405,6 +406,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;
@ -2388,6 +2390,16 @@ server_client_key_callback(struct cmdq_item *item, void *data)
event->key = key;
}
/* Handle theme reporting keys. */
if (key == KEYC_REPORT_LIGHT_THEME) {
server_client_report_theme(c, THEME_LIGHT);
goto out;
}
if (key == KEYC_REPORT_DARK_THEME) {
server_client_report_theme(c, THEME_DARK);
goto out;
}
/* Find affected pane. */
if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0)
cmd_find_from_client(&fs, c, 0);
@ -2678,6 +2690,12 @@ server_client_loop(void)
}
check_window_name(w);
}
/* Send theme updates. */
RB_FOREACH(w, windows, &windows) {
TAILQ_FOREACH(wp, &w->panes, entry)
window_pane_send_theme_update(wp);
}
}
/* Check if window needs to be resized. */
@ -3909,3 +3927,21 @@ out:
if (!parse)
free(msg);
}
static void
server_client_report_theme(struct client *c, enum client_theme theme)
{
if (theme == THEME_LIGHT) {
c->theme = THEME_LIGHT;
notify_client("client-light-theme", c);
} else {
c->theme = THEME_DARK;
notify_client("client-dark-theme", c);
}
/*
* Request foreground and background colour again. Don't forward 2031 to
* panes until a response is received.
*/
tty_puts(&c->tty, "\033]10;?\033\\\033]11;?\033\\");
}