blur.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Bing Wallpaper GNOME extension
  2. // Copyright (C) 2017-2023 Michael Carroll
  3. // This extension is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Lesser General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. // See the GNU General Public License, version 3 or later for details.
  8. // Based on GNOME shell extension NASA APOD by Elia Argentieri https://github.com/Elinvention/gnome-shell-extension-nasa-apod
  9. // This code based on https://github.com/PRATAP-KUMAR/Control_Blur_Effect_On_Lock_Screen
  10. // and https://github.com/sunwxg/gnome-shell-extension-unlockDialogBackground
  11. import St from 'gi://St';
  12. import * as UnlockDialog from 'resource:///org/gnome/shell/ui/unlockDialog.js';
  13. import * as Config from 'resource:///org/gnome/shell/misc/config.js';
  14. var _updateBackgroundEffects = UnlockDialog.UnlockDialog.prototype._updateBackgroundEffects;
  15. var _showClock = UnlockDialog.UnlockDialog.prototype._showClock;
  16. var _showPrompt = UnlockDialog.UnlockDialog.prototype._showPrompt;
  17. var shellVersionMajor = parseInt(Config.PACKAGE_VERSION.split('.')[0]);
  18. var shellVersionMinor = parseInt(Config.PACKAGE_VERSION.split('.')[1]);
  19. var shellVersionPoint = parseInt(Config.PACKAGE_VERSION.split('.')[2]);
  20. var blurField = shellVersionMajor >= 46 ? "radius" : "sigma";
  21. // default BWP mild blur
  22. var BWP_BLUR_SIGMA = 2;
  23. var BWP_BLUR_BRIGHTNESS = 55;
  24. // GNOME defaults
  25. var BLUR_BRIGHTNESS = 0.55;
  26. var BLUR_SIGMA = 60;
  27. var debug = false;
  28. var promptActive = false; // default GNOME method of testing this relies on state of a transisiton
  29. // so we are being explicit here (do not want any races, thanks)
  30. function log(msg) {
  31. if (debug) // set 'debug' above to false to keep the noise down in journal
  32. console.log("BingWallpaper extension/Blur: " + msg);
  33. }
  34. // we patch UnlockDialog._updateBackgroundEffects()
  35. export function _updateBackgroundEffects_BWP(monitorIndex) {
  36. // GNOME shell 3.36.4 and above
  37. log("_updateBackgroundEffects_BWP() called for shell >= 3.36.4");
  38. const themeContext = St.ThemeContext.get_for_stage(global.stage);
  39. for (const widget of this._backgroundGroup.get_children()) {
  40. // set blur effects, we have two modes in lockscreen: login prompt or clock
  41. // blur on when clock is visible is adjustable
  42. const effect = widget.get_effect('blur');
  43. if (promptActive) {
  44. log('default blur active');
  45. if (effect) {
  46. effect.set({ // GNOME defaults when login prompt is visible
  47. brightness: BLUR_BRIGHTNESS,
  48. [blurField]: BLUR_SIGMA * themeContext.scale_factor,
  49. });
  50. }
  51. }
  52. else {
  53. log('adjustable blur active');
  54. if (effect) {
  55. effect.set({ // adjustable blur when clock is visible
  56. brightness: BWP_BLUR_BRIGHTNESS * 0.01, // we use 0-100 rather than 0-1, so divide by 100
  57. [blurField]: BWP_BLUR_SIGMA * themeContext.scale_factor,
  58. });
  59. }
  60. }
  61. }
  62. }
  63. // we patch both UnlockDialog._showClock() and UnlockDialog._showPrompt() to let us
  64. // adjustable blur in a Windows-like way (this ensures login prompt is readable)
  65. export function _showClock_BWP() {
  66. promptActive = false;
  67. this._showClock_GNOME(); // pass to default GNOME function
  68. this._updateBackgroundEffects();
  69. }
  70. export function _showPrompt_BWP() {
  71. promptActive = true;
  72. this._showPrompt_GNOME(); // pass to default GNOME function
  73. this._updateBackgroundEffects();
  74. }
  75. export function _clampValue(value) {
  76. // valid values are 0 to 100
  77. if (value > 100)
  78. value = 100;
  79. if (value < 0 )
  80. value = 0;
  81. return value;
  82. }
  83. export default class Blur {
  84. constructor() {
  85. this.enabled = false;
  86. log('Bing Wallpaper adjustable blur is '+(supportedVersion()?'available':'not available'));
  87. }
  88. set_blur_strength(value) {
  89. BWP_BLUR_SIGMA = _clampValue(value);
  90. log("lockscreen blur strength set to "+BWP_BLUR_SIGMA);
  91. }
  92. set_blur_brightness(value) {
  93. BWP_BLUR_BRIGHTNESS = _clampValue(value);
  94. log("lockscreen brightness set to " + BWP_BLUR_BRIGHTNESS);
  95. }
  96. _switch(enabled) {
  97. if (enabled && !this.enabled) {
  98. this._enable();
  99. }
  100. else {
  101. this._disable();
  102. }
  103. }
  104. _enable() {
  105. if (supportedVersion()) {
  106. log("Blur._enable() called on GNOME "+Config.PACKAGE_VERSION);
  107. UnlockDialog.UnlockDialog.prototype._updateBackgroundEffects = _updateBackgroundEffects_BWP;
  108. // we override _showClock and _showPrompt to patch in updates to blur effect before calling the GNOME functions
  109. UnlockDialog.UnlockDialog.prototype._showClock = _showClock_BWP;
  110. UnlockDialog.UnlockDialog.prototype._showPrompt = _showPrompt_BWP;
  111. // this are the original functions which we call into from our versions above
  112. UnlockDialog.UnlockDialog.prototype._showClock_GNOME = _showClock;
  113. UnlockDialog.UnlockDialog.prototype._showPrompt_GNOME = _showPrompt;
  114. }
  115. this.enabled = true;
  116. }
  117. _disable() {
  118. if (!this.enabled)
  119. return;
  120. log("_lockscreen_blur_disable() called");
  121. if (supportedVersion()) {
  122. // restore default functions
  123. UnlockDialog.UnlockDialog.prototype._updateBackgroundEffects = _updateBackgroundEffects;
  124. UnlockDialog.UnlockDialog.prototype._showClock = _showClock;
  125. UnlockDialog.UnlockDialog.prototype._showPrompt = _showPrompt;
  126. // clean up unused functions we created
  127. UnlockDialog.UnlockDialog.prototype._showClock_GNOME = null;
  128. delete UnlockDialog.UnlockDialog.prototype._showClock_GNOME;
  129. UnlockDialog.UnlockDialog.prototype._showPrompt_GNOME = null;
  130. delete UnlockDialog.UnlockDialog.prototype._showPrompt_GNOME;
  131. }
  132. this.enabled = false;
  133. }
  134. };
  135. function supportedVersion() { // when current lockscren blur implementation was first shipped (we ignore earlier weird version)
  136. if (shellVersionMajor >= 40 ||
  137. (shellVersionMajor == 3 && shellVersionMinor == 36 && shellVersionPoint >= 4)) {
  138. return true;
  139. }
  140. return false;
  141. }