Skip to content

Commit

Permalink
Add volume button/slider (resolves #20)
Browse files Browse the repository at this point in the history
  • Loading branch information
yktoo committed Feb 8, 2021
1 parent aaab2a3 commit 2b44b46
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
31 changes: 31 additions & 0 deletions internal/player/main-window.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type MainWindow struct {
RandomButton *gtk.ToggleToolButton
RepeatButton *gtk.ToggleToolButton
ConsumeButton *gtk.ToggleToolButton
VolumeButton *gtk.VolumeButton
VolumeAdjustment *gtk.Adjustment
PlayPositionScale *gtk.Scale
PlayPositionAdjustment *gtk.Adjustment
AlbumArtworkImage *gtk.Image
Expand Down Expand Up @@ -159,6 +161,7 @@ type MainWindow struct {
playerTitleTemplate *template.Template // Compiled template for player's track title
playerCurrentAlbumArtUri string // URI of the current player's album art

volumeUpdating bool // Volume button update (initiated by an MPD event) flag
playPosUpdating bool // Play position manual update flag
optionsUpdating bool // Options update flag
addingStream bool // Whether the property popover is open to add a stream (rather than edit an existing one)
Expand Down Expand Up @@ -227,6 +230,7 @@ func NewMainWindow(application *gtk.Application) (*MainWindow, error) {
"on_StreamsListBox_selectionChange": w.updateStreamsActions,
"on_StreamPropsChanged": w.onStreamPropsChanged,
"on_QueueSavePopoverMenu_validate": w.onQueueSavePopoverValidate,
"on_VolumeButton_valueChanged": w.onVolumeValueChanged,
"on_PlayPositionScale_buttonEvent": w.onPlayPositionButtonEvent,
"on_PlayPositionScale_valueChanged": w.updatePlayerSeekBar,
"on_QueueNowPlayingMenuItem_activate": w.updateQueueNowPlaying,
Expand Down Expand Up @@ -291,6 +295,8 @@ func (w *MainWindow) onConnectorSubsystemChange(subsystem string) {
switch subsystem {
case "database", "update":
util.WhenIdle("updateLibrary()", w.updateLibrary)
case "mixer":
util.WhenIdle("updateVolume()", w.updateVolume)
case "options":
util.WhenIdle("updateOptions()", w.updateOptions)
case "player":
Expand Down Expand Up @@ -676,6 +682,16 @@ func (w *MainWindow) onStreamPropsChanged() {
util.EntryText(w.StreamPropsUriEntry, "") != "")
}

func (w *MainWindow) onVolumeValueChanged() {
if !w.volumeUpdating {
vol := int(w.VolumeAdjustment.GetValue())
log.Debugf("Adjusting volume to %d", vol)
w.connector.IfConnected(func(client *mpd.Client) {
errCheck(client.SetVolume(vol), "SetVolume() failed")
})
}
}

// about shows the application's about dialog
func (w *MainWindow) about() {
dlg, err := gtk.AboutDialogNew()
Expand Down Expand Up @@ -1734,6 +1750,7 @@ func (w *MainWindow) updateAll() {
w.updateLibraryActions()
w.updateOptions()
w.updatePlayer()
w.updateVolume()
}

// updateLibrary updates the current library list contents
Expand Down Expand Up @@ -2518,3 +2535,17 @@ func (w *MainWindow) updateStyle() {
}
}
}

// updateVolume synchronises the volume scale position to the current MPD volume
func (w *MainWindow) updateVolume() {
// Update the volume button's state
connected, _ := w.connector.ConnectStatus()
w.VolumeButton.SetSensitive(connected)

// The update comes from MPD: adjust the volume bar position if there's a connection
if vol := util.AtoiDef(w.connector.Status()["volume"], -1); vol >= 0 && vol <= 100 {
w.volumeUpdating = true
w.VolumeAdjustment.SetValue(float64(vol))
w.volumeUpdating = false
}
}
48 changes: 46 additions & 2 deletions resources/player.glade
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@
</object>
</child>
</object>
<object class="GtkAdjustment" id="VolumeAdjustment">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
</object>
<object class="GtkPopoverMenu" id="AppPopoverMenu">
<property name="can_focus">False</property>
<property name="relative_to">AppMenuButton</property>
Expand Down Expand Up @@ -1298,6 +1303,45 @@
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkVolumeButton" id="VolumeButton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="focus_on_click">False</property>
<property name="receives_default">True</property>
<property name="relief">none</property>
<property name="orientation">vertical</property>
<property name="adjustment">VolumeAdjustment</property>
<property name="icons">audio-volume-muted-symbolic
audio-volume-high-symbolic
audio-volume-low-symbolic
audio-volume-medium-symbolic</property>
<signal name="value-changed" handler="on_VolumeButton_valueChanged" swapped="no"/>
<child internal-child="plus_button">
<object class="GtkButton">
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="relief">none</property>
</object>
</child>
<child internal-child="minus_button">
<object class="GtkButton">
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="relief">none</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkScale" id="PlayPositionScale">
<property name="width_request">100</property>
Expand All @@ -1318,7 +1362,7 @@
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
<property name="position">2</property>
</packing>
</child>
<child>
Expand All @@ -1335,7 +1379,7 @@
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">6</property>
<property name="position">2</property>
<property name="position">3</property>
</packing>
</child>
</object>
Expand Down

0 comments on commit 2b44b46

Please sign in to comment.