blur.js 6.2 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. // default BWP mild blur
  21. var BWP_BLUR_SIGMA = 2;
  22. var BWP_BLUR_BRIGHTNESS = 55;
  23. // GNOME defaults
  24. var BLUR_BRIGHTNESS = 0.55;
  25. var BLUR_SIGMA = 60;
  26. var debug = false;
  27. var promptActive = false; // default GNOME method of testing this relies on state of a transisiton
  28. // so we are being explicit here (do not want any races, thanks)
  29. function log(msg) {
  30. if (debug) // set 'debug' above to false to keep the noise down in journal
  31. console.log("BingWallpaper extension/Blur: " + msg);
  32. }
  33. // we patch UnlockDialog._updateBackgroundEffects()
  34. function _updateBackgroundEffects_BWP(monitorIndex) {
  35. // GNOME shell 3.36.4 and above
  36. log("_updateBackgroundEffects_BWP() called for shell >= 3.36.4");
  37. const themeContext = St.ThemeContext.get_for_stage(global.stage);
  38. for (const widget of this._backgroundGroup.get_children()) {
  39. // set blur effects, we have two modes in lockscreen: login prompt or clock
  40. // blur on when clock is visible is adjustable
  41. const effect = widget.get_effect('blur');
  42. if (promptActive) {
  43. log('default blur active');
  44. if (effect) {
  45. effect.set({ // GNOME defaults when login prompt is visible
  46. brightness: BLUR_BRIGHTNESS,
  47. sigma: BLUR_SIGMA * themeContext.scale_factor,
  48. });
  49. }
  50. }
  51. else {
  52. log('adjustable blur active');
  53. if (effect) {
  54. effect.set({ // adjustable blur when clock is visible
  55. brightness: BWP_BLUR_BRIGHTNESS * 0.01, // we use 0-100 rather than 0-1, so divide by 100
  56. sigma: BWP_BLUR_SIGMA * themeContext.scale_factor,
  57. });
  58. }
  59. }
  60. }
  61. }
  62. // we patch both UnlockDialog._showClock() and UnlockDialog._showPrompt() to let us
  63. // adjustable blur in a Windows-like way (this ensures login prompt is readable)
  64. function _showClock_BWP() {
  65. promptActive = false;
  66. this._showClock_GNOME(); // pass to default GNOME function
  67. this._updateBackgroundEffects();
  68. }
  69. function _showPrompt_BWP() {
  70. promptActive = true;
  71. this._showPrompt_GNOME(); // pass to default GNOME function
  72. this._updateBackgroundEffects();
  73. }
  74. export default class Blur {
  75. constructor() {
  76. this.enabled = false;
  77. log('Bing Wallpaper adjustable blur is '+supportedVersion()?'available':'not available');
  78. }
  79. set_blur_strength(value) {
  80. BWP_BLUR_SIGMA = this._clampValue(value);
  81. log("lockscreen blur strength set to "+BWP_BLUR_SIGMA);
  82. }
  83. set_blur_brightness(value) {
  84. BWP_BLUR_BRIGHTNESS = this._clampValue(value);
  85. log("lockscreen brightness set to " + BWP_BLUR_BRIGHTNESS);
  86. }
  87. // valid values are 0 to 100
  88. _clampValue(value) {
  89. if (value > 100)
  90. value = 100;
  91. if (value < 0 )
  92. value = 0;
  93. return value;
  94. }
  95. _switch(enabled) {
  96. if (enabled && !this.enabled) {
  97. this._enable();
  98. }
  99. else {
  100. this._disable();
  101. }
  102. }
  103. _enable() {
  104. if (supportedVersion()) {
  105. log("Blur._enable() called on GNOME "+Config.PACKAGE_VERSION);
  106. UnlockDialog.UnlockDialog.prototype._updateBackgroundEffects = _updateBackgroundEffects_BWP;
  107. // we override _showClock and _showPrompt to patch in updates to blur effect before calling the GNOME functions
  108. UnlockDialog.UnlockDialog.prototype._showClock = _showClock_BWP;
  109. UnlockDialog.UnlockDialog.prototype._showPrompt = _showPrompt_BWP;
  110. // this are the original functions which we call into from our versions above
  111. UnlockDialog.UnlockDialog.prototype._showClock_GNOME = _showClock;
  112. UnlockDialog.UnlockDialog.prototype._showPrompt_GNOME = _showPrompt;
  113. }
  114. this.enabled = true;
  115. }
  116. _disable() {
  117. if (!this.enabled)
  118. return;
  119. log("_lockscreen_blur_disable() called");
  120. if (supportedVersion()) {
  121. // restore default functions
  122. UnlockDialog.UnlockDialog.prototype._updateBackgroundEffects = _updateBackgroundEffects;
  123. UnlockDialog.UnlockDialog.prototype._showClock = _showClock;
  124. UnlockDialog.UnlockDialog.prototype._showPrompt = _showPrompt;
  125. // clean up unused functions we created
  126. UnlockDialog.UnlockDialog.prototype._showClock_GNOME = null;
  127. delete UnlockDialog.UnlockDialog.prototype._showClock_GNOME;
  128. UnlockDialog.UnlockDialog.prototype._showPrompt_GNOME = null;
  129. delete UnlockDialog.UnlockDialog.prototype._showPrompt_GNOME;
  130. }
  131. this.enabled = false;
  132. }
  133. };
  134. function supportedVersion() { // when current lockscren blur implementation was first shipped (we ignore earlier weird version)
  135. if (shellVersionMajor >= 40 ||
  136. (shellVersionMajor == 3 && shellVersionMinor == 36 && shellVersionPoint >= 4)) {
  137. return true;
  138. }
  139. return false;
  140. }