diff --git a/extension.js b/extension.js index 0ff85d7..a5882d9 100644 --- a/extension.js +++ b/extension.js @@ -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); + } } diff --git a/player.js b/player.js index 7a93cc6..a270226 100644 --- a/player.js +++ b/player.js @@ -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() { @@ -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, }); @@ -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(); @@ -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"); @@ -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"); @@ -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(); @@ -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(); @@ -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(); @@ -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; @@ -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; @@ -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(); } } @@ -545,11 +545,11 @@ 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(); } @@ -557,7 +557,7 @@ export const Player = GObject.registerClass( } 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"); @@ -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), @@ -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), @@ -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; @@ -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": @@ -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) { @@ -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; @@ -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;`; @@ -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]] || ""; diff --git a/utils.js b/utils.js index 49dd08b..3cecc3b 100644 --- a/utils.js +++ b/utils.js @@ -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; @@ -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; @@ -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; @@ -111,6 +119,4 @@ function wrappingText(wrapping, widget) { widget.clutter_text.line_wrap = false; } return true; -} - -export { wrappingText }; +}; diff --git a/widget.js b/widget.js index 39113fb..cefd45f 100644 --- a/widget.js +++ b/widget.js @@ -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", () => { @@ -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"); @@ -305,6 +309,8 @@ export const MediaControls = GObject.registerClass( */ this._connectDbus(); + + Main.panel.addToStatusArea("media_controls_extension", this, this.extensionIndex, this.extensionPosition); } disable() { @@ -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", @@ -431,6 +438,8 @@ export const MediaControls = GObject.registerClass( } else { this.remove_all_children(); } + + this.container.hide(); } async _connectDbus() { @@ -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();