Skip to content

Commit

Permalink
Bug 1819034 - Reset LookAndFeel only if color scheme is really change…
Browse files Browse the repository at this point in the history
…d r=stransky

We get sometimes two color-scheme changes on GNOME + Wayland at least,
which do unnecessary work. Avoiding that extra work is rather
straight-forward.

This is a slightly simpler approach than D171065.

Differential Revision: https://phabricator.services.mozilla.com/D171081
  • Loading branch information
emilio committed Feb 28, 2023
1 parent d372a5e commit 82406cc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
32 changes: 16 additions & 16 deletions widget/gtk/nsLookAndFeel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,6 @@ static nsDependentCString GVariantGetString(GVariant* aVariant) {
return nsDependentCString(v, len);
}

// Observed settings for portal.
static constexpr struct {
nsLiteralCString mNamespace;
nsLiteralCString mKey;
} kObservedSettings[] = {
{"org.freedesktop.appearance"_ns, "color-scheme"_ns},
};

static void settings_changed_signal_cb(GDBusProxy* proxy, gchar* sender_name,
gchar* signal_name, GVariant* parameters,
gpointer user_data) {
Expand All @@ -132,13 +124,13 @@ static void settings_changed_signal_cb(GDBusProxy* proxy, gchar* sender_name,
return;
}

auto* lnf = static_cast<nsLookAndFeel*>(user_data);

auto nsStr = GVariantGetString(ns);
auto keyStr = GVariantGetString(key);
for (const auto& setting : kObservedSettings) {
if (setting.mNamespace.Equals(nsStr) && setting.mKey.Equals(keyStr)) {
OnSettingsChange();
return;
}
if (nsStr.Equals("org.freedesktop.appearance"_ns) &&
keyStr.Equals("color-scheme"_ns)) {
lnf->OnColorSchemeSettingChanged();
}
}

Expand Down Expand Up @@ -187,7 +179,7 @@ nsLookAndFeel::nsLookAndFeel() {
"org.freedesktop.portal.Settings", nullptr, getter_Transfers(error)));
if (mDBusSettingsProxy) {
g_signal_connect(mDBusSettingsProxy, "g-signal",
G_CALLBACK(settings_changed_signal_cb), nullptr);
G_CALLBACK(settings_changed_signal_cb), this);
} else {
LOGLNF("Can't create DBus proxy for settings: %s\n", error->message);
}
Expand All @@ -197,8 +189,7 @@ nsLookAndFeel::nsLookAndFeel() {
nsLookAndFeel::~nsLookAndFeel() {
if (mDBusSettingsProxy) {
g_signal_handlers_disconnect_by_func(
mDBusSettingsProxy, FuncToGpointer(settings_changed_signal_cb),
nullptr);
mDBusSettingsProxy, FuncToGpointer(settings_changed_signal_cb), this);
mDBusSettingsProxy = nullptr;
}
g_signal_handlers_disconnect_by_func(
Expand Down Expand Up @@ -1436,6 +1427,15 @@ void nsLookAndFeel::Initialize() {
RecordTelemetry();
}

void nsLookAndFeel::OnColorSchemeSettingChanged() {
if (NS_WARN_IF(mColorSchemePreference == ComputeColorSchemeSetting())) {
// We sometimes get duplicate color-scheme changes from dbus, avoid doing
// extra work if not needed.
return;
}
OnSettingsChange();
}

void nsLookAndFeel::InitializeGlobalSettings() {
GtkSettings* settings = gtk_settings_get_default();

Expand Down
1 change: 1 addition & 0 deletions widget/gtk/nsLookAndFeel.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class nsLookAndFeel final : public nsXPLookAndFeel {

static const nscolor kBlack = NS_RGB(0, 0, 0);
static const nscolor kWhite = NS_RGB(255, 255, 255);
void OnColorSchemeSettingChanged();

protected:
static bool WidgetUsesImage(WidgetNodeType aNodeType);
Expand Down

0 comments on commit 82406cc

Please sign in to comment.