Skip to content

Commit

Permalink
cleanly handle disable and enable. possibly fixing crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
sakithb committed Nov 19, 2023
1 parent 3cc16f7 commit c58fa7c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 55 deletions.
7 changes: 7 additions & 0 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ export default class MediaControlsExtension extends Extension {
mcExtension.disable();
mcExtension = null;
}

reload() {
log(_("[MediaControls] Reloading"));
mcExtension.disable();
mcExtension = new MediaControls();
mcExtension.enable(this);
}
}
72 changes: 36 additions & 36 deletions player.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const Player = GObject.registerClass(
this._scrollSourceId = null;
this._doubleClick = false;
this._clicked = false;
this._extension = parent;
this._mcExtension = parent;
}

async _initDbus() {
Expand Down Expand Up @@ -85,13 +85,13 @@ export const Player = GObject.registerClass(
this.dummyLabelTitle.clutter_text.ellipsize = Pango.EllipsizeMode.NONE;

this.labelSeperatorStart = new St.Label({
text: this._extension.sepChars[0],
text: this._mcExtension.sepChars[0],
style: "padding: 0px 3px 0px 0px; margin: 0px;",
y_align: Clutter.ActorAlign.CENTER,
});

this.labelSeperatorEnd = new St.Label({
text: this._extension.sepChars[1],
text: this._mcExtension.sepChars[1],
style: "padding: 0px 0px 0px 3px; margin: 0px;",
y_align: Clutter.ActorAlign.CENTER,
});
Expand Down Expand Up @@ -222,11 +222,11 @@ export const Player = GObject.registerClass(
this.buttonMenu.set_child(PopupMenu.arrowIcon(St.Side.BOTTOM));

this.buttonMenu.connect("button-release-event", () => {
this._extension.menu.toggle();
this._mcExtension.menu.toggle();
});

this.buttonMenu.connect("touch-event", () => {
this._extension.menu.toggle();
this._mcExtension.menu.toggle();
});

this.dummyContainer = new St.BoxLayout();
Expand Down Expand Up @@ -283,9 +283,9 @@ export const Player = GObject.registerClass(
}

_seekBack() {
const offset = this._extension.seekInterval * 1_000_000;
const offset = this._mcExtension.seekInterval * 1_000_000;

if (this._extension.preferNativeSeek) {
if (this._mcExtension.preferNativeSeek) {
this._playerProxy.SeekRemote(-offset);
} else {
const position = this._getDbusProperty("Position");
Expand All @@ -299,9 +299,9 @@ export const Player = GObject.registerClass(
}

_seekForward() {
const offset = this._extension.seekInterval * 1_000_000;
const offset = this._mcExtension.seekInterval * 1_000_000;

if (this._extension.preferNativeSeek) {
if (this._mcExtension.preferNativeSeek) {
this._playerProxy.SeekRemote(offset);
} else {
const position = this._getDbusProperty("Position");
Expand Down Expand Up @@ -346,10 +346,10 @@ export const Player = GObject.registerClass(
if (changed.Metadata) {
this._metadata = parseMetadata(this._getDbusProperty("Metadata"));
if (this._metadata.isInactive) {
this._extension.hidePlayer(this.busName);
this._mcExtension.hidePlayer(this.busName);
} else {
if (this.hidden) {
this._extension.unhidePlayer(this.busName);
this._mcExtension.unhidePlayer(this.busName);
}

this.updateWidgets();
Expand All @@ -359,10 +359,10 @@ export const Player = GObject.registerClass(
this._metadata = parseMetadata(this._getDbusProperty("Metadata"));

if (this._metadata.isInactive) {
this._extension.hidePlayer(this.busName);
this._mcExtension.hidePlayer(this.busName);
} else {
if (this.hidden) {
this._extension.unhidePlayer(this.busName);
this._mcExtension.unhidePlayer(this.busName);
}

this.updateWidgets();
Expand All @@ -372,8 +372,8 @@ export const Player = GObject.registerClass(

if (changed.PlaybackStatus) {
this._status = changed.PlaybackStatus;
if (this.isPlaying && !this._extension.isFixedPlayer && !this._active) {
this._extension.updatePlayer(this.busName);
if (this.isPlaying && !this._mcExtension.isFixedPlayer && !this._active) {
this._mcExtension.updatePlayer(this.busName);
}

this._updateStatusIcons();
Expand Down Expand Up @@ -407,9 +407,9 @@ export const Player = GObject.registerClass(
GLib.Source.remove(this._scrollSourceId);
}

if (this._extension.scrolltracklabel) {
if (this._mcExtension.scrolltracklabel) {
this.labelTitle.set_style("text-align: center;");
this.labelTitle.width = this._extension.maxWidgetWidth;
this.labelTitle.width = this._mcExtension.maxWidgetWidth;
} else {
this.labelTitle.set_style(`text-align: center; ${this.maxWidthStyle}`);
this.labelTitle.width = -1;
Expand All @@ -421,12 +421,12 @@ export const Player = GObject.registerClass(

this.dummyLabelTitle.set_text(label);
const lastPosX = this.dummyLabelTitle.clutter_text.position_to_coords(labelLength)[1];
if (lastPosX <= this._extension.maxWidgetWidth) {
if (lastPosX <= this._mcExtension.maxWidgetWidth) {
this.labelTitle.width = lastPosX;
return;
}

const maxWidgetWidth = this._extension.maxWidgetWidth;
const maxWidgetWidth = this._mcExtension.maxWidgetWidth;
const duplicatedLabel = `${label} ${label}`;
let offset = 0;

Expand Down Expand Up @@ -478,8 +478,8 @@ export const Player = GObject.registerClass(
this._infoIcon.set_gicon(this.trackIcon);
this.infoTitleLabel.set_text(this.title);
this.infoArtistLabel.set_text(this.artist);
wrappingText(!this._extension.cliptextsmenu, this.infoTitleLabel);
wrappingText(!this._extension.cliptextsmenu, this.infoArtistLabel);
wrappingText(!this._mcExtension.cliptextsmenu, this.infoTitleLabel);
wrappingText(!this._mcExtension.cliptextsmenu, this.infoArtistLabel);
this._updateInfoIcon();
}
}
Expand Down Expand Up @@ -545,19 +545,19 @@ export const Player = GObject.registerClass(
this.infoArtistLabel.set_style(this.maxWidthStyle);
this.infoTitleLabel.set_style(`font-size: large; ${this.maxWidthStyle}`);

wrappingText(!this._extension.cliptextsmenu, this.infoTitleLabel);
wrappingText(!this._extension.cliptextsmenu, this.infoArtistLabel);
wrappingText(!this._mcExtension.cliptextsmenu, this.infoTitleLabel);
wrappingText(!this._mcExtension.cliptextsmenu, this.infoArtistLabel);

if (this._extension.maxWidgetWidth !== 0) {
this._infoIcon.set_icon_size(this._extension.maxWidgetWidth);
if (this._mcExtension.maxWidgetWidth !== 0) {
this._infoIcon.set_icon_size(this._mcExtension.maxWidgetWidth);
} else {
this._updateInfoIcon();
}
}
}

updateIconEffects() {
if (this._extension.coloredPlayerIcon) {
if (this._mcExtension.coloredPlayerIcon) {
this.iconPlayer.clear_effects();
this.iconPlayer.set_style("margin: 0px; padding: 0px; -st-icon-style: requested;");
this.iconPlayer.set_fallback_icon_name("audio-x-generic");
Expand Down Expand Up @@ -870,11 +870,11 @@ export const Player = GObject.registerClass(
}

async _saveImage() {
if (this._extension.cacheImages) {
if (this._mcExtension.cacheImages) {
try {
if (urlRegexp.test(this.image)) {
const destination = GLib.build_filenamev([
this._extension.dataDir,
this._mcExtension.dataDir,
"media-controls",
"cache",
GLib.base64_encode(this.image),
Expand Down Expand Up @@ -905,7 +905,7 @@ export const Player = GObject.registerClass(
_getImage() {
try {
const destination = GLib.build_filenamev([
this._extension.dataDir,
this._mcExtension.dataDir,
"media-controls",
"cache",
GLib.base64_encode(this.image),
Expand All @@ -923,7 +923,7 @@ export const Player = GObject.registerClass(
}

_mouseAction(index) {
switch (this._extension.mouseActions[index]) {
switch (this._mcExtension.mouseActions[index]) {
case "toggle_play":
this._playerProxy.PlayPauseRemote();
break;
Expand All @@ -947,10 +947,10 @@ export const Player = GObject.registerClass(
break;
case "toggle_menu":
this.menu.close(BoxPointer.PopupAnimation.FULL);
this._extension.menu.toggle();
this._mcExtension.menu.toggle();
break;
case "toggle_info":
this._extension.menu.close(BoxPointer.PopupAnimation.FULL);
this._mcExtension.menu.close(BoxPointer.PopupAnimation.FULL);
this.menu.toggle();
break;
case "toggle_loop":
Expand All @@ -975,7 +975,7 @@ export const Player = GObject.registerClass(
if (!this._clicked) {
this._timeoutSourceId = GLib.timeout_add(
GLib.PRIORITY_HIGH,
this._extension.clutterSettings.double_click_time,
this._mcExtension.clutterSettings.double_click_time,
() => {
if (!this._doubleClick) {
if (button === 1) {
Expand Down Expand Up @@ -1033,7 +1033,7 @@ export const Player = GObject.registerClass(
this._scrollSourceId = null;
}

this._extension = null;
this._mcExtension = null;
this._playerProxy = null;
this._otherProxy = null;
this._doubleClick = null;
Expand Down Expand Up @@ -1107,7 +1107,7 @@ export const Player = GObject.registerClass(
}

get maxWidthStyle() {
let maxWidth = this._extension.maxWidgetWidth;
let maxWidth = this._mcExtension.maxWidgetWidth;

if (maxWidth !== 0) {
maxWidth = `max-width: ${maxWidth}px;`;
Expand Down Expand Up @@ -1151,7 +1151,7 @@ export const Player = GObject.registerClass(
none: null,
};

const trackLabelSetting = this._extension.trackLabel;
const trackLabelSetting = this._mcExtension.trackLabel;

const startLabel = labelEls[trackLabelSetting[0]] || "";
const endLabel = labelEls[trackLabelSetting[2]] || "";
Expand Down
16 changes: 11 additions & 5 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export const msToHHMMSS = (ms) => {
return `${hours}:${minutes}:${seconds}`;
};

const isValidTitle = (title) => {
return title && title.trim() !== "";
};

const isValidArtist = (artist) => {
return artist && Array.isArray(artist) && artist.length > 0 && artist.some((a) => a.trim() !== "");
};

export const parseMetadata = (_metadata) => {
if (!_metadata) {
return _metadata;
Expand All @@ -51,7 +59,7 @@ export const parseMetadata = (_metadata) => {
metadata[metadataKeys[key]] = val instanceof GLib.Variant ? val.recursiveUnpack() : val;
}

metadata.isInactive = metadata.title === "" && metadata.artist === "" && metadata.length === 0;
metadata.isInactive = !isValidTitle(metadata.title) && !isValidArtist(metadata.artist) && metadata.length === 0;

let title = metadata.title || metadata.url || metadata.id;

Expand Down Expand Up @@ -99,7 +107,7 @@ export const getRequest = (url) => {
});
};

function wrappingText(wrapping, widget) {
export const wrappingText = (wrapping, widget) => {
if (wrapping) {
widget.clutter_text.single_line_mode = false;
widget.clutter_text.activatable = false;
Expand All @@ -111,6 +119,4 @@ function wrappingText(wrapping, widget) {
widget.clutter_text.line_wrap = false;
}
return true;
}

export { wrappingText };
};
38 changes: 24 additions & 14 deletions widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,17 @@ export const MediaControls = GObject.registerClass(
});

this._onExtensionPositionChanged = this._settings.connect("changed::extension-position", () => {
this.removeWidgets();
this.extensionPosition = this._settings.get_string("extension-position");
this.addWidgets();
// this.removeWidgets();
// this.extensionPosition = this._settings.get_string("extension-position");
// this.addWidgets();
this._extension.reload();
});

this._onExtensionIndexChanged = this._settings.connect("changed::extension-index", () => {
this.extensionIndex = this._settings.get_int("extension-index");
this.removeWidgets();
this.addWidgets();
// this.extensionIndex = this._settings.get_int("extension-index");
// this.removeWidgets();
// this.addWidgets();
this._extension.reload();
});

this._onColoredPlayerIconChanged = this._settings.connect("changed::colored-player-icon", () => {
Expand Down Expand Up @@ -203,7 +205,9 @@ export const MediaControls = GObject.registerClass(
}

enable(Me) {
this._settings = Me.getSettings();
this._extension = Me;

this._settings = this._extension.getSettings();
this.connectSignals();

this.maxWidgetWidth = this._settings.get_int("max-widget-width");
Expand Down Expand Up @@ -305,6 +309,8 @@ export const MediaControls = GObject.registerClass(
*/

this._connectDbus();

Main.panel.addToStatusArea("media_controls_extension", this, this.extensionIndex, this.extensionPosition);
}

disable() {
Expand Down Expand Up @@ -333,12 +339,13 @@ export const MediaControls = GObject.registerClass(
}

addWidgets() {
if (Main.panel.statusArea["media_controls_extension"]) {
console.warn("[Media Controls] Instance not destroyed properly, attempting to fix...");
Main.panel.statusArea["media_controls_extension"].destroy();
}
// if (Main.panel.statusArea["media_controls_extension"]) {
// console.warn("[Media Controls] Instance not destroyed properly, attempting to fix...");
// Main.panel.statusArea["media_controls_extension"].destroy();
// delete Main.panel.statusArea["media_controls_extension"];
// }

Main.panel.addToStatusArea("media_controls_extension", this, this.extensionIndex, this.extensionPosition);
this.container.show();

Main.wm.addKeybinding(
"mediacontrols-toggle-trackinfomenu",
Expand Down Expand Up @@ -431,6 +438,8 @@ export const MediaControls = GObject.registerClass(
} else {
this.remove_all_children();
}

this.container.hide();
}

async _connectDbus() {
Expand Down Expand Up @@ -596,8 +605,9 @@ export const MediaControls = GObject.registerClass(

if (this.player && this.player.busName === busName && this.fixedPlayer) {
this.fixedPlayer = false;
this.updatePlayer();
} else if (this.player && this.player.busName !== busName && this.fixedPlayer) {
}

if (this.player && this.player.busName !== busName && this.fixedPlayer) {
this.updatePlayer(this.player);
} else {
this.updatePlayer();
Expand Down

0 comments on commit c58fa7c

Please sign in to comment.