class SoundRow(Gtk.ListBoxRow): __gtype_name__ = 'SoundRow' """ SoundRow Widget to show, play and manage a sound """ box = Gtk.Template.Child() title = Gtk.Template.Child() volume = Gtk.Template.Child() def __init__(self, sound, model): super().__init__() self.first_load = True # SoundObject self.sound = sound # Gio.ListStore self.model = model # Connect mainplayer preset-changed signal self.sound.mainplayer.connect('preset-changed', self._on_preset_changed) # Connect mainplayer reset-volumes signal self.sound.mainplayer.connect('reset-volumes', self._on_reset_volumes) # Create a new SoundPlayer self.player = SoundPlayer(self.sound) # Set title self.title.set_label(self.sound.title) # Connect scale with volume function self.volume.connect('value-changed', self._on_volume_changed) # Load saved volume self.volume.set_value(self.sound.saved_volume) icon = Gtk.Image.new_from_icon_name(self.sound.icon_name) icon.add_css_class('sound-icon') icon.set_pixel_size(64) self.box.prepend(icon) if self.sound.custom: # Add a remove button remove = Gtk.Button(valign=Gtk.Align.CENTER) remove.connect('clicked', self.remove) self.box.append(remove) # Add destructive-action CSS class remove.get_style_context().add_class('image-button') remove.set_icon_name('edit-delete-symbolic') def remove(self, _button): # Remove audio from list self.model.remove(self.get_index()) # Remove player self.player.remove() # Remove audio from settings self.sound.remove() def toggle_mute(self): # Toggle player mute state if self.sound.saved_volume > 0: self.sound.saved_mute = not self.sound.saved_mute if self.sound.saved_mute: self.player.set_virtual_volume(0) elif self.sound.saved_volume > 0: self.player.set_virtual_volume(self.sound.saved_volume) else: self.volume.set_value(0.5) self._update_active_state() def _on_volume_changed(self, scale): # Round volume value volume = round(scale.get_value(), 2) # Save volume on settings self.sound.saved_volume = volume if self.first_load and self.sound.saved_mute: self.player.set_virtual_volume(0) self.first_load = False else: self.player.set_virtual_volume(volume) self.sound.saved_mute = False self._update_active_state() def _update_active_state(self): if self.sound.saved_mute or self.sound.saved_volume == 0: self._show_active_indicator(False) else: self._show_active_indicator(True) def _show_active_indicator(self, show): if not show: self.get_style_context().remove_class('playing') elif not self.get_style_context().has_class('playing'): self.get_style_context().add_class('playing') def _on_preset_changed(self, _player): self.first_load = True self.volume.set_value(self.sound.saved_volume) def _on_reset_volumes(self, _player): self.volume.set_value(0.0)
class SoundRow(Gtk.ListBoxRow): __gtype_name__ = 'SoundRow' """ SoundRow Widget to show, play and manage a sound """ custom = GObject.Property(type=bool, default=False) icon = Gtk.Template.Child() title = Gtk.Template.Child() volume = Gtk.Template.Child() remove_btn = Gtk.Template.Child() def __init__(self, sound, model): super().__init__() self.custom = sound.custom self.first_load = True # SoundObject self.sound = sound # Gio.ListStore self.model = model # Create a new SoundPlayer self.player = SoundPlayer(self.sound) # Set title self.title.set_label(self.sound.title) # Connect scale with volume function self.volume.connect('value-changed', self._on_volume_changed) # Load saved volume self.volume.set_value(self.sound.saved_volume) # Set icon self.icon.props.icon_name = self.sound.icon_name # Connect remove self.remove_btn.connect('clicked', self.remove) # Connect mainplayer preset-changed signal self.sound.mainplayer.connect('preset-changed', self._on_preset_changed) # Connect mainplayer reset-volumes signal self.sound.mainplayer.connect('reset-volumes', self._on_reset_volumes) def remove(self, _button): if self.sound.custom: # Remove audio from list self.model.remove(self.get_index()) # Remove player self.player.remove() # Remove audio from settings self.sound.remove() def toggle_mute(self): # Toggle player mute state if self.sound.saved_volume > 0: self.sound.saved_mute = not self.sound.saved_mute if self.sound.saved_mute: self.player.set_virtual_volume(0) elif self.sound.saved_volume > 0: self.player.set_virtual_volume(self.sound.saved_volume) else: self.volume.set_value(0.5) self._update_active_state() def _on_volume_changed(self, scale): # Round volume value volume = round(scale.get_value(), 2) # Save volume on settings self.sound.saved_volume = volume if self.first_load and self.sound.saved_mute: self.player.set_virtual_volume(0) self.first_load = False else: self.player.set_virtual_volume(volume) self.sound.saved_mute = False self._update_active_state() def _update_active_state(self): if self.sound.saved_mute or self.sound.saved_volume == 0: self._show_active_indicator(False) else: self._show_active_indicator(True) def _show_active_indicator(self, show): if not show: self.icon.remove_css_class('accent') elif not self.icon.has_css_class('accent'): self.icon.add_css_class('accent') def _on_preset_changed(self, _player): self.first_load = True self.volume.set_value(self.sound.saved_volume) def _on_reset_volumes(self, _player): self.volume.set_value(0.0)
class SoundRow(Gtk.ListBoxRow): __gtype_name__ = 'SoundRow' """ SoundRow Widget to show, play and manage a sound """ box = Gtk.Template.Child() title = Gtk.Template.Child() playing = Gtk.Template.Child() volume = Gtk.Template.Child() def __init__(self, sound, model, settings, **kwargs): super().__init__(**kwargs) # SoundObject self.sound = sound # Gio.ListStore self.model = model # Settings self.settings = settings # Get playing state playing = self.sound.mainplayer.get_property('playing') self.volume.set_sensitive(playing) # Connect playing state signal self.sound.mainplayer.connect('notify::playing', self._on_playing_changed) # Create a new SoundPlayer self.player = SoundPlayer(self.sound) # Set title self.title.set_label(self.sound.title) # Connnect scale with volume function self.volume.connect('value-changed', self.change_vol) # Load saved volume saved_vol = self.settings.get_sound_volume(self.sound.name) if saved_vol and saved_vol > 0: self.volume.set_value(saved_vol) if self.sound.custom: # Add a remove button remove = Gtk.Button(valign=Gtk.Align.CENTER) remove.connect('clicked', self.remove) self.box.pack_end(remove, False, True, 0) # Add destructive-action CSS class Gtk.StyleContext.add_class(remove.get_style_context(), 'image-button') # Create button icon remove_icon = Gtk.Image.new_from_icon_name('edit-delete-symbolic', Gtk.IconSize.MENU) remove.add(remove_icon) # Compact widget self.box.props.margin_top = 0 self.box.props.margin_bottom = 0 else: # Set icon for the sound icon = Gtk.Image.new_from_icon_name(self.sound.icon_name, Gtk.IconSize.DIALOG) Gtk.StyleContext.add_class(icon.get_style_context(), 'sound-icon') icon.set_pixel_size(64) self.box.pack_start(icon, False, True, 0) self.box.child_set_property(icon, 'position', 0) def change_vol(self, scale): # Round volume value volume = round(scale.get_value(), 2) # Set player volume self.player.set_virtual_volume(volume) # Save volume on settings self.settings.set_sound_volume(self.sound.name, volume) # Toggle playing indicator if volume == 0: self.playing.set_reveal_child(False) Gtk.StyleContext.remove_class(self.get_style_context(), 'playing') elif not self.playing.get_reveal_child(): self.playing.set_reveal_child(True) Gtk.StyleContext.add_class(self.get_style_context(), 'playing') def remove(self, widget): # Remove audio from list self.model.remove(self.get_index()) # Remove player self.player.remove() # Remove audio from settings self.settings.remove_custom_audio(self.sound.name) def _on_playing_changed(self, player, playing): playing = self.sound.mainplayer.get_property('playing') self.volume.set_sensitive(playing)