Skip to content

Commit

Permalink
Bug 1596317 - Implement SetPrefersReducedMotionOverrideForTest backen…
Browse files Browse the repository at this point in the history
…d for GTK. r=emilio

On GTK changing gtk-enable-animation in a process doesn't affect in different
processes for some reasons.  So we take the same approach as what we did for
OSX[1] that is when SetPrefersReducedMotionOverrideForTest is called we set the
given value as a cache in the parent process and send a notification to system
as if the value changed thus the notification kicks PBroser.ThemeChanged to
update the cache in the content process, thus we can use the cache value on
querying the corresponding value in the content process.

[1] https://hg.mozilla.org/mozilla-central/rev/67a5acf7363d

Differential Revision: https://phabricator.services.mozilla.com/D57260
  • Loading branch information
hiikezoe committed Dec 19, 2019
1 parent c38009e commit d40c8b7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dom/interfaces/base/nsIDOMWindowUtils.idl
Original file line number Diff line number Diff line change
Expand Up @@ -1976,7 +1976,7 @@ interface nsIDOMWindowUtils : nsISupports {
* Simulate the system setting corresponding to 'prefers-reduced-motion'
* media queries feature is changed to 'on' or 'off'.
*
* Currently this function is available only on MacOSX.
* This function doesn't work on Windows.
*/
void setPrefersReducedMotionOverrideForTest(in boolean aValue);
/**
Expand Down
2 changes: 1 addition & 1 deletion layout/style/test/mochitest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ skip-if = verify
[test_mq_any_hover_and_any_pointer.html]
[test_mq_hover_and_pointer.html]
[test_mq_prefers_reduced_motion_dynamic.html]
run-if = (os == 'mac' || toolkit == 'android')
run-if = !headless && (os == 'mac' || toolkit == 'android' || toolkit == 'gtk')
[test_moz_device_pixel_ratio.html]
[test_moz_prefixed_cursor.html]
[test_namespace_rule.html]
Expand Down
36 changes: 26 additions & 10 deletions widget/gtk/nsLookAndFeel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,20 @@ nsresult nsLookAndFeel::InitCellHighlightColors() {
void nsLookAndFeel::NativeInit() { EnsureInit(); }

void nsLookAndFeel::RefreshImpl() {
if (mShouldRetainCacheForTest) {
return;
}

nsXPLookAndFeel::RefreshImpl();
moz_gtk_refresh();

mDefaultFontCached = false;
mButtonFontCached = false;
mFieldFontCached = false;
mMenuFontCached = false;
if (XRE_IsParentProcess()) {
mPrefersReducedMotionCached = false;
}

mInitialized = false;
}
Expand All @@ -278,11 +285,15 @@ nsTArray<LookAndFeelInt> nsLookAndFeel::GetIntCacheImpl() {
nsTArray<LookAndFeelInt> lookAndFeelIntCache =
nsXPLookAndFeel::GetIntCacheImpl();

LookAndFeelInt lafInt;
lafInt.id = eIntID_SystemUsesDarkTheme;
lafInt.value = GetInt(eIntID_SystemUsesDarkTheme);
LookAndFeelInt lafInt{.id = eIntID_SystemUsesDarkTheme,
.value = GetInt(eIntID_SystemUsesDarkTheme)};
lookAndFeelIntCache.AppendElement(lafInt);

LookAndFeelInt prefersReducedMotion{
.id = eIntID_PrefersReducedMotion,
.value = GetInt(eIntID_PrefersReducedMotion)};
lookAndFeelIntCache.AppendElement(prefersReducedMotion);

return lookAndFeelIntCache;
}

Expand All @@ -293,6 +304,10 @@ void nsLookAndFeel::SetIntCacheImpl(
case eIntID_SystemUsesDarkTheme:
mSystemUsesDarkTheme = entry.value;
break;
case eIntID_PrefersReducedMotion:
mPrefersReducedMotion = entry.value;
mPrefersReducedMotionCached = true;
break;
}
}
}
Expand Down Expand Up @@ -725,13 +740,14 @@ nsresult nsLookAndFeel::GetIntImpl(IntID aID, int32_t& aResult) {
aResult = mCSDReversedPlacement;
break;
case eIntID_PrefersReducedMotion: {
GtkSettings* settings;
gboolean enableAnimations;

settings = gtk_settings_get_default();
g_object_get(settings, "gtk-enable-animations", &enableAnimations,
nullptr);
aResult = enableAnimations ? 0 : 1;
if (!mPrefersReducedMotionCached && XRE_IsParentProcess()) {
gboolean enableAnimations;
GtkSettings* settings = gtk_settings_get_default();
g_object_get(settings, "gtk-enable-animations", &enableAnimations,
nullptr);
mPrefersReducedMotion = enableAnimations ? 0 : 1;
}
aResult = mPrefersReducedMotion;
break;
}
case eIntID_SystemUsesDarkTheme: {
Expand Down
2 changes: 2 additions & 0 deletions widget/gtk/nsLookAndFeel.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class nsLookAndFeel final : public nsXPLookAndFeel {
char16_t mInvisibleCharacter = 0;
float mCaretRatio = 0.0f;
int32_t mCaretBlinkTime = 0;
int32_t mPrefersReducedMotion = -1;
bool mMenuSupportsDrag = false;
bool mCSDAvailable = false;
bool mCSDHideTitlebarByDefault = false;
Expand All @@ -101,6 +102,7 @@ class nsLookAndFeel final : public nsXPLookAndFeel {
bool mCSDReversedPlacement = false;
bool mSystemUsesDarkTheme = false;
bool mInitialized = false;
bool mPrefersReducedMotionCached = false;

void EnsureInit();

Expand Down
25 changes: 25 additions & 0 deletions widget/gtk/nsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "SystemTimeConverter.h"
#include "nsViewManager.h"
#include "nsMenuPopupFrame.h"
#include "nsXPLookAndFeel.h"

#include "nsGtkKeyUtils.h"
#include "nsGtkCursors.h"
Expand Down Expand Up @@ -7693,6 +7694,30 @@ void nsWindow::LockAspectRatio(bool aShouldLock) {
ApplySizeConstraints();
}

nsresult nsWindow::SetPrefersReducedMotionOverrideForTest(bool aValue) {
LookAndFeel::SetShouldRetainCacheForTest(true);

LookAndFeelInt prefersReducedMotion{
.id = LookAndFeel::eIntID_PrefersReducedMotion, .value = aValue ? 1 : 0};

AutoTArray<LookAndFeelInt, 1> lookAndFeelCache;
lookAndFeelCache.AppendElement(prefersReducedMotion);

LookAndFeel::SetIntCache(lookAndFeelCache);

// Notify as if the corresponding setting changed.
g_object_notify(G_OBJECT(gtk_settings_get_default()),
"gtk-enable-animations");

return NS_OK;
}

nsresult nsWindow::ResetPrefersReducedMotionOverrideForTest() {
LookAndFeel::SetShouldRetainCacheForTest(false);

return NS_OK;
}

#ifdef MOZ_WAYLAND
void nsWindow::SetEGLNativeWindowSize(
const LayoutDeviceIntSize& aEGLWindowSize) {
Expand Down
3 changes: 3 additions & 0 deletions widget/gtk/nsWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ class nsWindow final : public nsBaseWidget {
nsresult SetSystemFont(const nsCString& aFontName) override;
nsresult GetSystemFont(nsCString& aFontName) override;

nsresult SetPrefersReducedMotionOverrideForTest(bool aValue) final;
nsresult ResetPrefersReducedMotionOverrideForTest() final;

typedef enum {
CSD_SUPPORT_SYSTEM, // CSD including shadows
CSD_SUPPORT_CLIENT, // CSD without shadows
Expand Down

0 comments on commit d40c8b7

Please sign in to comment.