Bus, Ui, main: the system's appearance, dark or light and its accent

The desktop portal's org.freedesktop.appearance settings on the session
bus say whether the system prefers dark (color-scheme 1) and which
accent colour it uses, and a flatpak may read them. Bus:Appearance
subscribes to SettingChanged and reads the namespace in one ReadAll
call, which every portal version has, so a start waits for one answer at
most, 2 s; a portal that gave no colour scheme by then — still starting,
or not on the bus yet — is asked again when its name has an owner,
without waiting, and its answer comes as a change. The canvas gained a
dark palette and takes the accent for the avatars, badges, buttons and
outgoing bubbles, with the text on it dark or light by the accent's
brightness. The app applies the theme to the window at once and keeps it
for the next one; --theme dark|light or IMSD_MESSAGES_THEME (the same
two values: another is refused on the command line and ignored in the
environment) overrides the portal's colour scheme, for benches and
desktops without a portal.
This commit is contained in:
Jorijn van der Graaf 2026-10-18 03:08:03 +02:00
commit 9332094333
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
7 changed files with 222 additions and 7 deletions

View file

@ -55,6 +55,7 @@ struct Options {
bool openNew = false; // the bench opens the new-conversation screen
std::optional<std::string> benchSheet; // "conversation" or "message": the bench's long press
std::string country;
std::optional<std::string> theme; // "dark" or "light": instead of the portal's answer
std::string error;
};
@ -99,6 +100,7 @@ Options Parse(std::span<const char* const> args) {
else if (a == "--new") o.openNew = true;
else if (a == "--bench-sheet" && i + 1 < args.size()) o.benchSheet = args[++i];
else if (a == "--country" && i + 1 < args.size()) o.country = args[++i];
else if (a == "--theme" && i + 1 < args.size()) o.theme = args[++i];
else if (IsSmsUri(a)) o.uri = std::string(a);
else o.error = std::format("unknown argument: {}", a);
}
@ -114,6 +116,7 @@ Options Parse(std::span<const char* const> args) {
// and CountryCodeOf gives it back from a number that begins with it): a
// completed key is made of it as it is, and read by that rule.
if (!o.country.empty() && (o.country.front() == '0' || CountryCodeOf(std::format("+{}0000000", o.country)) != o.country)) o.error = "--country: not a country code (one to three digits, without its +)";
if (o.theme && *o.theme != "dark" && *o.theme != "light") o.error = "--theme: dark or light";
if (o.hideEvery && *o.hideEvery * 1000 < 1) o.error = "--hide-every: a millisecond at least";
if (o.benchSheet && *o.benchSheet != "conversation" && *o.benchSheet != "message") o.error = "--bench-sheet: conversation or message";
if (o.send && o.quit) o.error = "--send and --quit do not go together";
@ -281,7 +284,7 @@ int main(int argc, char** argv) {
return 0;
}
if (o.help) {
std::println("imsd-messages [--daemon|--headless] [--send NUMBER TEXT] [--quit] [--session] [--data-dir DIR] [--country CC] [--screenshot FILE.png] [--screenshot-after SECONDS] [--quit-after SECONDS] [--hide-after SECONDS] [--hide-every SECONDS] [--prefill TEXT] [--send-draft] [--new] [--bench-sheet conversation|message] [sms:NUMBER] [--version]");
std::println("imsd-messages [--daemon|--headless] [--send NUMBER TEXT] [--quit] [--session] [--data-dir DIR] [--country CC] [--theme dark|light] [--screenshot FILE.png] [--screenshot-after SECONDS] [--quit-after SECONDS] [--hide-after SECONDS] [--hide-every SECONDS] [--prefill TEXT] [--send-draft] [--new] [--bench-sheet conversation|message] [sms:NUMBER] [--version]");
return 0;
}
@ -395,6 +398,21 @@ int main(int argc, char** argv) {
if (o.screenshot) app->exitCode = 1; // an asked-for screenshot is a failure until the PNG is written: a run that ends first says so
app->closeQuits = session == nullptr;
app->appId = std::string(Bus::appId);
// The theme: --theme or IMSD_MESSAGES_THEME wins, else the portal's
// appearance settings, else light.
Bus::AppearanceWatch appearance(session);
std::optional<std::string> themeOverride = o.theme;
if (!themeOverride) {
// The flag's two values and no other: anything else leaves the theme to the system.
if (const char* env = std::getenv("IMSD_MESSAGES_THEME"); env && (std::string_view(env) == "dark" || std::string_view(env) == "light")) themeOverride = env;
}
const auto applyTheme = [&app, themeOverride](const Bus::Appearance& a) {
const bool dark = themeOverride ? *themeOverride == "dark" : a.dark;
app->SetTheme(dark, a.accent);
};
applyTheme(appearance.Current());
appearance.onChanged = applyTheme;
Log(std::format("theme: {} ({}){}", themeOverride ? (*themeOverride == "dark" ? "dark" : "light") : appearance.Current().dark ? "dark" : "light", themeOverride ? "override" : appearance.Current().known ? "the system" : "no colour scheme from a portal, the default", appearance.Current().accent ? " with the system's accent" : ""));
app->sync = &sync;
app->onQuit = [loop] { g_main_loop_quit(loop); };
app->onConversationOpened = [&notifier](const std::string& conversation) { notifier.Close(conversation); };