#10 Modernize code
Merged 5 years ago by fmuellner. Opened 5 years ago by fmuellner.

cleanup: Use destructuring for imports
Florian Müllner • 5 years ago  
cleanup: Remove old compatibility code
Florian Müllner • 5 years ago  
cleanup: Use Extension object
Florian Müllner • 5 years ago  
cleanup: Use ES6 classes
Florian Müllner • 5 years ago  
cleanup: Use method syntax
Florian Müllner • 5 years ago  
cleanup: Stop using Lang.bind()
Florian Müllner • 5 years ago  
file modified
+75 -79
@@ -14,10 +14,7 @@ 

   * You should have received a copy of the GNU General Public License

   * along with this program; if not, see <http://www.gnu.org/licenses/>.

   */

- const Clutter = imports.gi.Clutter;

- const Gio = imports.gi.Gio;

- const Lang = imports.lang;

- const St = imports.gi.St;

+ const { Clutter, Gio, St } = imports.gi;

  

  const Background = imports.ui.background;

  const Layout = imports.ui.layout;
@@ -28,10 +25,8 @@ 

  const Me = ExtensionUtils.getCurrentExtension();

  const Convenience = Me.imports.convenience;

  

- const BackgroundLogo = new Lang.Class({

-     Name: 'BackgroundLogo',

- 

-     _init: function(bgManager) {

+ class BackgroundLogo {

+     constructor(bgManager) {

          this._bgManager = bgManager;

  

          this._logoFile = null;
@@ -39,29 +34,28 @@ 

          this._settings = Convenience.getSettings();

  

          this._settings.connect('changed::logo-file',

-                                Lang.bind(this, this._updateLogo));

+                                this._updateLogo.bind(this));

          this._settings.connect('changed::logo-size',

-                                Lang.bind(this, this._updateScale));

+                                this._updateScale.bind(this));

          this._settings.connect('changed::logo-position',

-                                Lang.bind(this, this._updatePosition));

+                                this._updatePosition.bind(this));

          this._settings.connect('changed::logo-border',

-                                Lang.bind(this, this._updateBorder));

+                                this._updateBorder.bind(this));

          this._settings.connect('changed::logo-always-visible',

-                                Lang.bind(this, this._updateVisibility));

+                                this._updateVisibility.bind(this));

  

          this._textureCache = St.TextureCache.get_default();

-         this._textureCache.connect('texture-file-changed', Lang.bind(this,

-             function(cache, file) {

-                 if (!this._logoFile || !this._logoFile.equal(file))

-                     return;

-                 this._updateLogoTexture();

-             }));

+         this._textureCache.connect('texture-file-changed', (cache, file) => {

+             if (!this._logoFile || !this._logoFile.equal(file))

+                 return;

+             this._updateLogoTexture();

+         });

  

          this.actor = new St.Widget({ layout_manager: new Clutter.BinLayout(),

                                       opacity: 0 });

          bgManager._container.add_actor(this.actor);

  

-         this.actor.connect('destroy', Lang.bind(this, this._onDestroy));

+         this.actor.connect('destroy', this._onDestroy.bind(this));

  

          let monitorIndex = bgManager._monitorIndex;

          let constraint = new Layout.MonitorConstraint({ index: monitorIndex,
@@ -80,14 +74,14 @@ 

  

          this._bgDestroyedId =

              bgManager.backgroundActor.connect('destroy',

-                                               Lang.bind(this, this._backgroundDestroyed));

+                                               this._backgroundDestroyed.bind(this));

  

          this._bgChangedId =

-             bgManager.connect('changed', Lang.bind(this, this._updateVisibility));

+             bgManager.connect('changed', this._updateVisibility.bind(this));

          this._updateVisibility();

-     },

+     }

  

-     _updateLogo: function() {

+     _updateLogo() {

          let filename = this._settings.get_string('logo-file');

          let file = Gio.File.new_for_commandline_arg(filename);

          if (this._logoFile && this._logoFile.equal(file))
@@ -96,24 +90,20 @@ 

          this._logoFile = file;

  

          this._updateLogoTexture();

-     },

+     }

  

-     _updateLogoTexture: function() {

+     _updateLogoTexture() {

          if (this._icon)

              this._icon.destroy();

  

          let scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor;

-         if (this._textureCache.load_file_async) { // > 3.14

-             this._icon = this._textureCache.load_file_async(this._logoFile, -1, -1, scaleFactor);

-         } else { // <= 3.14

-             this._icon = this._textureCache.load_uri_async(this._logoFile.get_uri(), -1, -1, scaleFactor);

-         }

+         this._icon = this._textureCache.load_file_async(this._logoFile, -1, -1, scaleFactor);

          this._icon.connect('allocation-changed',

-                            Lang.bind(this, this._updateScale));

+                            this._updateScale.bind(this));

          this._bin.add_actor(this._icon);

-     },

+     }

  

-     _updateScale: function() {

+     _updateScale() {

          if (this._icon.width == 0)

              return;

  
@@ -129,9 +119,9 @@ 

              return;

          }

          this._icon.set_size(width, height);

-     },

+     }

  

-     _updatePosition: function() {

+     _updatePosition() {

          let xAlign, yAlign;

          switch (this._settings.get_string('logo-position')) {

              case 'center':
@@ -153,14 +143,14 @@ 

          }

          this._bin.x_align = xAlign;

          this._bin.y_align = yAlign;

-     },

+     }

  

-     _updateBorder: function() {

+     _updateBorder() {

          let border = this._settings.get_uint('logo-border');

          this.actor.style = 'padding: %dpx;'.format(border);

-     },

+     }

  

-     _updateVisibility: function() {

+     _updateVisibility() {

          let background = this._bgManager.backgroundActor.background._delegate;

          let defaultUri = background._settings.get_default_value('picture-uri');

          let file = Gio.File.new_for_commandline_arg(defaultUri.deep_unpack());
@@ -180,20 +170,20 @@ 

                             time: Background.FADE_ANIMATION_TIME,

                             transition: 'easeOutQuad'

                           });

-     },

+     }

  

-     _backgroundDestroyed: function() {

+     _backgroundDestroyed() {

          this._bgDestroyedId = 0;

  

          if (this._bgManager._backgroundSource) // background swapped

              this._bgDestroyedId =

                  this._bgManager.backgroundActor.connect('destroy',

-                                                         Lang.bind(this, this._backgroundDestroyed));

+                                                         this._backgroundDestroyed.bind(this));

          else // bgManager destroyed

              this.actor.destroy();

-     },

+     }

  

-     _onDestroy: function() {

+     _onDestroy() {

          this._settings.run_dispose();

          this._settings = null;

  
@@ -209,48 +199,54 @@ 

  

          this._logoFile = null;

      }

- });

- 

+ }

  

- let monitorsChangedId = 0;

- let startupPreparedId = 0;

- let logos = [];

  

- function forEachBackgroundManager(func) {

-     Main.overview._bgManagers.forEach(func);

-     Main.layoutManager._bgManagers.forEach(func);

- }

+ class Extension {

+     constructor() {

+         this._monitorsChangedId = 0;

+         this._startupPreparedId = 0;

+         this._logos = [];

+     }

  

- function addLogo() {

-     destroyLogo();

-     forEachBackgroundManager(function(bgManager) {

-         logos.push(new BackgroundLogo(bgManager));

-     });

- }

+     _forEachBackgroundManager(func) {

+         Main.overview._bgManagers.forEach(func);

+         Main.layoutManager._bgManagers.forEach(func);

+     }

  

- function destroyLogo() {

-     logos.forEach(function(l) { l.actor.destroy(); });

-     logos = [];

- }

+     _addLogo() {

+         this._destroyLogo();

+         this._forEachBackgroundManager(bgManager => {

+             this._logos.push(new BackgroundLogo(bgManager));

+         });

+     }

  

- function init() {

- }

+     _destroyLogo() {

+         this._logos.forEach(l => { l.actor.destroy(); });

+         this._logos = [];

+     }

  

- function enable() {

+     enable() {

+         this._monitorsChangedId =

+             Main.layoutManager.connect('monitors-changed', this._addLogo.bind(this));

+         this._startupPreparedId =

+             Main.layoutManager.connect('startup-prepared', this._addLogo.bind(this));

+         this._addLogo();

+     }

  

-     monitorsChangedId = Main.layoutManager.connect('monitors-changed', addLogo);

-     startupPreparedId = Main.layoutManager.connect('startup-prepared', addLogo);

-     addLogo();

- }

+     disable() {

+         if (this._monitorsChangedId)

+             Main.layoutManager.disconnect(this._monitorsChangedId);

+         this._monitorsChangedId = 0;

  

- function disable() {

-     if (monitorsChangedId)

-         Main.layoutManager.disconnect(monitorsChangedId);

-     monitorsChangedId = 0;

+         if (this._startupPreparedId)

+             Main.layoutManager.disconnect(this._startupPreparedId);

+         this._startupPreparedId = 0;

  

-     if (startupPreparedId)

-         Main.layoutManager.disconnect(startupPreparedId);

-     startupPreparedId = 0;

+         this._destroyLogo();

+     }

+ }

  

-     destroyLogo();

+ function init() {

+     return new Extension();

  }

file modified
+35 -44
@@ -1,10 +1,4 @@ 

- const Gdk = imports.gi.Gdk;

- const GdkPixbuf = imports.gi.GdkPixbuf;

- const Gio = imports.gi.Gio;

- const GnomeDesktop = imports.gi.GnomeDesktop;

- const Gtk = imports.gi.Gtk;

- 

- const Lang = imports.lang;

+ const { Gdk, GdkPixbuf, Gio, GnomeDesktop, GObject, Gtk } = imports.gi;

  

  const ExtensionUtils = imports.misc.extensionUtils;

  const Me = ExtensionUtils.getCurrentExtension();
@@ -14,30 +8,29 @@ 

  

  const PREVIEW_WIDTH = 400;

  

- const BackgroundLogoPrefsWidget = new Lang.Class({

-     Name: 'BackgroundLogoPrefsWidget',

-     Extends: Gtk.Grid,

- 

-     _init: function() {

-         this.parent({ halign: Gtk.Align.CENTER,

-                       margin: 24,

-                       column_spacing: 12,

-                       row_spacing: 6 });

+ let BackgroundLogoPrefsWidget = GObject.registerClass(

+ class BackgroundLogoPrefsWidget extends Gtk.Grid {

+     _init() {

+         super._init({

+             halign: Gtk.Align.CENTER,

+             margin: 24,

+             column_spacing: 12,

+             row_spacing: 6

+         });

  

-         this.connect('screen-changed', Lang.bind(this, this._onScreenChanged));

+         this.connect('screen-changed', this._onScreenChanged.bind(this));

  

          this._settings = Convenience.getSettings();

-         this._settings.connect('changed', Lang.bind(this,

-             function(settings, key) {

-                 if (key == 'logo-file' ||

-                     key == 'logo-size')

-                     this._logo = null;

-                 this._preview.queue_draw();

-             }));

+         this._settings.connect('changed', (settings, key) => {

+             if (key == 'logo-file' ||

+                 key == 'logo-size')

+                 this._logo = null;

+             this._preview.queue_draw();

+         });

  

          this._preview = new Gtk.DrawingArea({ halign: Gtk.Align.CENTER,

                                                margin_bottom: 18 });

-         this._preview.connect('draw', Lang.bind(this, this._drawPreview));

+         this._preview.connect('draw', this._drawPreview.bind(this));

          this.attach(this._preview, 0, 0, 2, 1);

  

          let filter = new Gtk.FileFilter();
@@ -46,11 +39,9 @@ 

          let fileChooser = new Gtk.FileChooserButton({ title: "Select an Image",

                                                        filter: filter });

          fileChooser.set_filename(this._settings.get_string('logo-file'));

-         fileChooser.connect('file-set', Lang.bind(this,

-             function() {

-                 this._settings.set_string('logo-file',

-                                           fileChooser.get_filename());

-             }));

+         fileChooser.connect('file-set',  () => {

+             this._settings.set_string('logo-file', fileChooser.get_filename());

+         });

          this._addRow(1, "Logo image", fileChooser);

  

          let comboBox = new Gtk.ComboBoxText();
@@ -79,9 +70,9 @@ 

          this._settings.bind('logo-always-visible', checkWidget, 'active',

                              Gio.SettingsBindFlags.DEFAULT);

          this.attach(checkWidget, 1, 6, 1, 1);

-     },

+     }

  

-     _addRow: function(row, label, widget) {

+     _addRow(row, label, widget) {

          let margin = 48;

  

          widget.margin_end = margin;
@@ -94,9 +85,9 @@ 

          this.attach(new Gtk.Label({ label: label, xalign: 1.0,

                                      margin_start: margin }), 0, row, 1, 1);

          this.attach(widget, 1, row, 1, 1);

-     },

+     }

  

-     _createAdjustment: function(key, step) {

+     _createAdjustment(key, step) {

          let schemaKey = this._settings.settings_schema.get_key(key);

          let [type, variant] = schemaKey.get_range().deep_unpack();

          if (type != 'range')
@@ -107,9 +98,9 @@ 

                                         page_increment: 10 * step });

          this._settings.bind(key, adj, 'value', Gio.SettingsBindFlags.DEFAULT);

          return adj;

-     },

+     }

  

-     _drawPreview: function(preview, cr) {

+     _drawPreview(preview, cr) {

          let width = preview.get_allocated_width();

          let height = preview.get_allocated_height();

  
@@ -124,9 +115,9 @@ 

          let [x, y] = this._getLogoPosition(width, height);

          Gdk.cairo_set_source_pixbuf(cr, this._logo, x, y);

          cr.paintWithAlpha(this._settings.get_uint('logo-opacity') / 255.0);

-     },

+     }

  

-     _createBackgroundThumbnail: function(width, height) {

+     _createBackgroundThumbnail(width, height) {

          let settings = new Gio.Settings({ schema_id: BACKGROUND_SCHEMA });

          let uri = settings.get_default_value('picture-uri').deep_unpack();

          let file = Gio.File.new_for_commandline_arg(uri);
@@ -142,9 +133,9 @@ 

          let pixbuf = GdkPixbuf.Pixbuf.new_from_file(file.get_path());

          this._background = pixbuf.scale_simple(width, height,

                                                 GdkPixbuf.InterpType.BILINEAR);

-     },

+     }

  

-     _createLogoThumbnail: function(width, height) {

+     _createLogoThumbnail(width, height) {

          let filename = this._settings.get_string('logo-file');

          let file = Gio.File.new_for_commandline_arg(filename);

          let pixbuf = GdkPixbuf.Pixbuf.new_from_file(file.get_path());
@@ -153,9 +144,9 @@ 

          this._logo = pixbuf.scale_simple(size * width,

                                           size * width / ratio,

                                           GdkPixbuf.InterpType.BILINEAR);

-     },

+     }

  

-     _getLogoPosition: function(width, height) {

+     _getLogoPosition(width, height) {

          let scaledBorder = this._settings.get_uint('logo-border') * this._scale;

          let x, y;

          switch (this._settings.get_string('logo-position')) {
@@ -177,9 +168,9 @@ 

                  break;

          }

          return [x, y];

-     },

+     }

  

-     _onScreenChanged: function() {

+     _onScreenChanged() {

          let screen = this.get_screen();

          if (!screen)

              return;

Pull-Request has been merged by fmuellner

5 years ago
Metadata