Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
Fix initial value of percentage label
Browse files Browse the repository at this point in the history
When changing settings the volume control has already been initialized
and no volume-changed signal is sent after precentage label registers to
it.

Also adds a small space between value and %, like GNOME does for the
power percentage.
  • Loading branch information
aleho committed Apr 3, 2020
1 parent 87806d6 commit c973f02
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
29 changes: 21 additions & 8 deletions [email protected]/lib/volume/mixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ var Mixer = class
this._streamEvents.connect('volume-changed', callback);
}

/**
* Returns the current volume in percent.
*
* @returns {?number}
*/
getVolume() {
if (!this._defaultSink) {
return null;
}

if (this._defaultSink.is_muted) {
return 0;
}

return Math.round(this._defaultSink.volume / this._control.get_vol_max_norm() * 100);
}

/**
* Cleanup.
*/
Expand Down Expand Up @@ -112,13 +129,11 @@ var Mixer = class
* @private
*/
_onVolumeUpdate() {
if (!this._defaultSink || this._defaultSink.is_muted) {
return;
}

const percent = this._defaultSink.volume / this._control.get_vol_max_norm() * 100;
const percent = this.getVolume();

this._streamEvents.emit('volume-changed', Math.round(percent));
if (percent !== null) {
this._streamEvents.emit('volume-changed', percent);
}
}

/**
Expand Down Expand Up @@ -175,8 +190,6 @@ var Mixer = class
}




/**
* Hotkey handler to switch between profiles.
* @private
Expand Down
19 changes: 18 additions & 1 deletion [email protected]/lib/widget/percentageLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,24 @@ var PercentageLabel = GObject.registerClass(class Indicator extends St.Label {
this.add_style_class_name('percentage-label');

mixer.connectVolumeChanges((event, volume) => {
this.clutter_text.set_markup(`<span size="smaller">${volume}%</span>`);
this._setText(volume);
});

// set initial value, if available
this._setText(mixer.getVolume());
}

/**
* @param {?number} percent
* @private
*/
_setText(percent) {
if (percent === null) {
this.text = '';

} else {
const formatted = _('%d\u2009%%').format(percent);
this.clutter_text.set_markup(`<span size="smaller">${formatted}</span>`);
}
}
});

0 comments on commit c973f02

Please sign in to comment.