Skip to content

Commit

Permalink
appIndicator: Simplify loading of custom image files
Browse files Browse the repository at this point in the history
We don't need to create a child actor, we can just reuse the same logic we
now use for pixmaps.
  • Loading branch information
3v1n0 committed Mar 9, 2023
1 parent dce478e commit c28d554
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 53 deletions.
64 changes: 15 additions & 49 deletions appIndicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -926,11 +926,7 @@ if (imports.system.version >= 17501) {
} catch (e) {}
}

var IconActor = GObject.registerClass({
Signals: {
'requires-custom-image': {},
},
},
var IconActor = GObject.registerClass(
class AppIndicatorsIconActor extends St.Icon {

_init(indicator, iconSize) {
Expand Down Expand Up @@ -1206,7 +1202,7 @@ class AppIndicatorsIconActor extends St.Icon {
if (width >= height * 1.5) {
/* Hello indicator-multiload! */
await this._loadCustomImage(Gio.File.new_for_path(path),
width, height, cancellable);
width, height, iconSize, iconScaling, cancellable);
return null;
} else if (StTextureCacheSkippingGIcon) {
/* We'll wrap the icon so that it won't be cached forever by the shell */
Expand All @@ -1227,27 +1223,23 @@ class AppIndicatorsIconActor extends St.Icon {
}
}

async _loadCustomImage(file, width, height, cancellable) {
if (!(this instanceof CustomImageIconActor)) {
this.emit('requires-custom-image');
throw new GLib.Error(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED,
'Loading cancelled, need specific class');
}

const { scaleFactor } = St.ThemeContext.get_for_stage(global.stage);
async _loadCustomImage(file, width, height, iconSize, iconScaling, cancellable) {
const textureCache = St.TextureCache.get_default();
const resourceScale = this._getResourceScale();

const customImage = textureCache.load_file_async(file, -1,
height, scaleFactor, resourceScale);
height, 1, iconScaling);

customImage.set({
xAlign: Clutter.ActorAlign.CENTER,
yAlign: Clutter.ActorAlign.CENTER,
});
const setCustomImageActor = imageActor => {
const { scaleFactor } = St.ThemeContext.get_for_stage(global.stage);
const { content } = imageActor;
imageActor.content = null;
imageActor.destroy();

this._setImageContent(content,
width * scaleFactor, height * scaleFactor);
};

if (customImage.content) {
this._setCustomImage(customImage, width, height);
setCustomImageActor(customImage);
return;
}

Expand All @@ -1261,28 +1253,15 @@ class AppIndicatorsIconActor extends St.Icon {
try {
await Promise.race(racingPromises);
if (!waitPromise.resolved())
this._setCustomImage(customImage, width, height);
setCustomImageActor(customImage);
} catch (e) {
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
throw e;
} finally {
racingPromises.forEach(p => p.cancel());

if (this._customImage !== customImage)
customImage.destroy();
}
}

_setCustomImage(imageActor, width, height) {
if (this._customImage)
this._customImage.destroy();

this._customImage = imageActor;
this.add_child(this._customImage);
this.width = width;
this.height = height;
}

_getIconInfo(name, themePath, size, scale) {
if (name && name[0] === '/') {
// HACK: icon is a path name. This is not specified by the api but at least inidcator-sensors uses it.
Expand Down Expand Up @@ -1590,16 +1569,3 @@ class AppIndicatorsIconActor extends St.Icon {
});
}
});

var CustomImageIconActor = GObject.registerClass(
class CustomImageIconActor extends IconActor {
vfunc_paint(paintContext) {
if (this._customImage) {
this.paint_background(paintContext);
this._customImage.paint(paintContext);
return;
}

super.vfunc_paint(paintContext);
}
});
4 changes: 0 additions & 4 deletions indicatorStatusIcon.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,6 @@ class AppIndicatorsIndicatorStatusIcon extends BaseStatusIcon {
this._updateStatus();
this._updateLabel();
});
Util.connectSmart(this.icon, 'requires-custom-image', this, () => {
this._setIconActor(new AppIndicator.CustomImageIconActor(
indicator, Panel.PANEL_ICON_SIZE));
});
Util.connectSmart(this._indicator, 'accessible-name', this, () =>
this.set_accessible_name(this._indicator.accessibleName));
Util.connectSmart(this._indicator, 'destroy', this, () => this.destroy());
Expand Down

0 comments on commit c28d554

Please sign in to comment.