Fix volume button icon behaviour

This commit is contained in:
Rafostar
2020-09-01 23:43:40 +02:00
parent 647ad3f1ec
commit 9f04b74e05
2 changed files with 21 additions and 36 deletions

View File

@@ -29,7 +29,16 @@ class ClapperControls extends Gtk.HBox
this.positionAdjustment = this.positionScale.get_adjustment(); this.positionAdjustment = this.positionScale.get_adjustment();
this.volumeButton = new Gtk.ScaleButton({ this.volumeButton = new Gtk.ScaleButton({
icons: ['audio-volume-muted-symbolic'], icons: [
'audio-volume-muted-symbolic',
'audio-volume-overamplified-symbolic',
'audio-volume-low-symbolic',
'audio-volume-medium-symbolic',
'audio-volume-high-symbolic',
'audio-volume-overamplified-symbolic',
'audio-volume-overamplified-symbolic',
'audio-volume-overamplified-symbolic',
],
size: Gtk.IconSize.SMALL_TOOLBAR size: Gtk.IconSize.SMALL_TOOLBAR
}); });
this.volumeButtonImage = this.volumeButton.get_child(); this.volumeButtonImage = this.volumeButton.get_child();
@@ -55,14 +64,10 @@ class ClapperControls extends Gtk.HBox
_prepareVolumeButton() _prepareVolumeButton()
{ {
this.volumeAdjustment.set_upper(2); this.volumeAdjustment.set_upper(2.001);
this.volumeAdjustment.set_step_increment(0.05); this.volumeAdjustment.set_step_increment(0.05);
this.volumeAdjustment.set_page_increment(0.05); this.volumeAdjustment.set_page_increment(0.05);
this.volumeButton.connect(
'value-changed', this._onVolumeValueChanged.bind(this)
);
let popup = this.volumeButton.get_popup(); let popup = this.volumeButton.get_popup();
let box = popup.get_child(); let box = popup.get_child();
let boxChildren = box.get_children(); let boxChildren = box.get_children();
@@ -72,29 +77,11 @@ class ClapperControls extends Gtk.HBox
box.remove(child); box.remove(child);
else if(child.constructor === Gtk.Scale) { else if(child.constructor === Gtk.Scale) {
child.height_request = 200; child.height_request = 200;
child.round_digits = 2;
child.add_mark(0, Gtk.PositionType.LEFT, '0%'); child.add_mark(0, Gtk.PositionType.LEFT, '0%');
child.add_mark(1, Gtk.PositionType.LEFT, '100%'); child.add_mark(1, Gtk.PositionType.LEFT, '100%');
child.add_mark(2, Gtk.PositionType.LEFT, '200%'); child.add_mark(2, Gtk.PositionType.LEFT, '200%');
} }
} }
} }
_onVolumeValueChanged(widget, value)
{
if(value <= 0)
return;
let iconName = (value <= 0.33)
? 'audio-volume-low-symbolic'
: (value <= 0.66)
? 'audio-volume-medium-symbolic'
: (value <= 1)
? 'audio-volume-high-symbolic'
: 'audio-volume-overamplified-symbolic';
if(this.volumeButtonImage.icon_name === iconName)
return;
this.volumeButtonImage.icon_name = iconName;
}
}); });

View File

@@ -8,6 +8,7 @@ class ClapperInterface extends Gtk.Grid
{ {
super._init(); super._init();
this.lastVolumeValue = null;
this.lastPositionValue = 0; this.lastPositionValue = 0;
this.controls = new Controls(); this.controls = new Controls();
@@ -19,12 +20,6 @@ class ClapperInterface extends Gtk.Grid
this._player = player; this._player = player;
this._player.widget.expand = true; this._player.widget.expand = true;
this.attach(this._player.widget, 0, 0, 1, 1);
this._connectControlsToPlayer();
}
_connectControlsToPlayer()
{
this._player.connect('state-changed', this._onPlayerStateChanged.bind(this)); this._player.connect('state-changed', this._onPlayerStateChanged.bind(this));
this._player.connect('volume-changed', this._onPlayerVolumeChanged.bind(this)); this._player.connect('volume-changed', this._onPlayerVolumeChanged.bind(this));
this._player.connect('duration-changed', this._onPlayerDurationChanged.bind(this)); this._player.connect('duration-changed', this._onPlayerDurationChanged.bind(this));
@@ -39,6 +34,8 @@ class ClapperInterface extends Gtk.Grid
this.controls.volumeButton.connect( this.controls.volumeButton.connect(
'value-changed', this._onControlsVolumeChanged.bind(this) 'value-changed', this._onControlsVolumeChanged.bind(this)
); );
this.attach(this._player.widget, 0, 0, 1, 1);
} }
_onPlayerStateChanged(player, state) _onPlayerStateChanged(player, state)
@@ -85,14 +82,14 @@ class ClapperInterface extends Gtk.Grid
this.controls.positionScale.set_value(positionSeconds); this.controls.positionScale.set_value(positionSeconds);
} }
_onPlayerVolumeChanged(player) _onPlayerVolumeChanged()
{ {
let volume = player.get_volume(); let volume = Number(this._player.get_volume().toFixed(2));
if(this.controls.volumeButton.value === volume) if(volume === this.lastVolumeValue)
return; return;
this.controls.volumeButton.value = volume; this.controls.volumeButton.set_value(volume);
} }
_onControlsTogglePlayClicked() _onControlsTogglePlayClicked()
@@ -113,9 +110,10 @@ class ClapperInterface extends Gtk.Grid
_onControlsVolumeChanged(widget, value) _onControlsVolumeChanged(widget, value)
{ {
if(this._player.get_volume() === value) if(value === this.lastVolumeValue)
return; return;
this.lastVolumeValue = value;
this._player.set_volume(value); this._player.set_volume(value);
} }
}); });