Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

appIndicator Use Clutter to load RGBA Pixmap textures natively #413

Merged
merged 3 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 61 additions & 68 deletions appIndicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ else
PromiseUtils._promisify(Gtk.IconInfo.prototype, 'load_symbolic_async', 'load_symbolic_finish');

const MAX_UPDATE_FREQUENCY = 100; // In ms
const FALLBACK_ICON_NAME = 'image-loading-symbolic';
const PIXMAPS_FORMAT = imports.gi.Cogl.PixelFormat.ARGB_8888;

// eslint-disable-next-line no-unused-vars
const SNICategory = Object.freeze({
Expand Down Expand Up @@ -644,6 +646,12 @@ var AppIndicator = class AppIndicatorsAppIndicator {
};
}

get hasOverlayIcon() {
const { name, pixmap } = this.overlayIcon;

return name || (pixmap && pixmap.n_children());
}

get hasNameOwner() {
if (this._nameWatcher && !this._nameWatcher.nameOnBus)
return false;
Expand Down Expand Up @@ -918,18 +926,14 @@ 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) {
super._init({
reactive: true,
style_class: 'system-status-icon',
fallback_icon_name: 'image-loading-symbolic',
fallbackIconName: FALLBACK_ICON_NAME,
});

this.name = this.constructor.name;
Expand Down Expand Up @@ -1198,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 @@ -1219,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: imports.gi.Clutter.ActorAlign.CENTER,
yAlign: imports.gi.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 @@ -1253,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 All @@ -1299,7 +1286,7 @@ class AppIndicatorsIconActor extends St.Icon {
iconTheme.append_search_path(themePath);

if (!Meta.is_wayland_compositor() && !St.IconTheme) {
const defaultScreen = imports.gi.Gdk.Screen.get_default();
const defaultScreen = Gdk.Screen.get_default();
if (defaultScreen)
iconTheme.set_screen(defaultScreen);
}
Expand All @@ -1326,26 +1313,42 @@ class AppIndicatorsIconActor extends St.Icon {
return { iconInfo: null, path: null };
}

async _argbToRgba(src, cancellable) {
await new PromiseUtils.IdlePromise(GLib.PRIORITY_LOW, cancellable);

return PixmapsUtils.argbToRgba(src);
_setImageContent(content, width, height) {
this.set({
content,
width,
height,
contentGravity: Clutter.ContentGravity.RESIZE_ASPECT,
fallbackIconName: null,
});
}

async _createIconFromPixmap(iconType, iconSize, iconScaling, pixmapsVariant) {
iconSize *= iconScaling;

async _createIconFromPixmap(iconType, iconSize, iconScaling, scaleFactor, pixmapsVariant) {
const { pixmapVariant, width, height, rowStride } =
PixmapsUtils.getBestPixmap(pixmapsVariant, iconSize);
PixmapsUtils.getBestPixmap(pixmapsVariant, iconSize * iconScaling);

const id = `__PIXMAP_ICON_${width}x${height}`;

const imageContent = new St.ImageContent({
preferredWidth: width,
preferredHeight: height,
});

imageContent.set_bytes(pixmapVariant.get_data_as_bytes(), PIXMAPS_FORMAT,
width, height, rowStride);

if (iconType !== SNIconType.OVERLAY && !this._indicator.hasOverlayIcon) {
const scaledSize = iconSize * scaleFactor;
this._setImageContent(imageContent, scaledSize, scaledSize);
return null;
}

const cancellable = this._getIconLoadingCancellable(iconType, id);
try {
return GdkPixbuf.Pixbuf.new_from_bytes(
await this._argbToRgba(pixmapVariant.deep_unpack(), cancellable),
GdkPixbuf.Colorspace.RGB, true,
8, width, height, rowStride);
// FIXME: async API results in a gray icon for some reason
const [inputStream] = imageContent.load(iconSize, cancellable);
return await GdkPixbuf.Pixbuf.new_from_stream_at_scale_async(
inputStream, -1, iconSize * iconScaling, true, cancellable);
} catch (e) {
// the image data was probably bogus. We don't really know why, but it _does_ happen.
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
Expand Down Expand Up @@ -1431,11 +1434,12 @@ class AppIndicatorsIconActor extends St.Icon {
e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.PENDING))
return null;


if (iconType === SNIconType.OVERLAY)
if (iconType === SNIconType.OVERLAY) {
logError(e, `${this.debugId} unable to update icon emblem`);
else
} else {
this.fallbackIconName = FALLBACK_ICON_NAME;
logError(e, `${this.debugId} unable to update icon`);
}
}

try {
Expand Down Expand Up @@ -1470,8 +1474,10 @@ class AppIndicatorsIconActor extends St.Icon {
return gicon;
}

if (pixmap && pixmap.n_children())
return this._createIconFromPixmap(iconType, iconSize, iconScaling, pixmap);
if (pixmap && pixmap.n_children()) {
return this._createIconFromPixmap(iconType,
iconSize, iconScaling, scaleFactor, pixmap);
}

return null;
}
Expand Down Expand Up @@ -1563,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