Skip to content

Commit

Permalink
#23 #35, Easy acces to change multimedia source
Browse files Browse the repository at this point in the history
posible fix for #35
  • Loading branch information
jsmitar committed Nov 7, 2017
1 parent 0ec5bf7 commit 2bdb678
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 12 deletions.
107 changes: 107 additions & 0 deletions plasmoid/contents/ui/CoverArt.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/
import QtQuick 2.4
import QtQuick.Layouts 1.2
import QtQuick.Controls 1.4
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as PlasmaComponents
import QtGraphicalEffects 1.0

Item {
Expand Down Expand Up @@ -98,12 +100,117 @@ Item {
to: 1.0
duration: units.longDuration * 1.5
}

}

MouseArea {
id: toggleWindow
anchors.fill: parent
acceptedButtons: Qt.LeftButton
onClicked: action_raise()
hoverEnabled: true
}

PlasmaComponents.ToolButton {
id: mediaSelector
anchors {
left: cover.left
top: cover.top
leftMargin: units.smallSpacing
topMargin: units.smallSpacing
}

opacity: (toggleWindow.containsMouse || hovered) ? 1 : 0
visible: repeater.count >= 2

Behavior on opacity {
NumberAnimation { duration: units.largeDuration }
}

iconName: mpris2.icon(mpris2.currentSource)
onClicked: menu.open()

PlasmaComponents.ContextMenu {
id: menu
visualParent: mediaSelector

function caption(source) {
var text = 'None'

if (source === "@multiplex") {
text = i18n("Select automatically")
} else if (source && mpris2.currentSource === source) {
text = mpris2.capitalize(source)
} else {
var e = mpris2.recentSources.find(function (e) {
return e.source === source
});

if (!e)
text = source[0].toUpperCase() + source.substr(1)
else
text = e.identity
}

return text
}

function changeSource(source) {
if (mpris2.currentSource !== source) {
if (mpris2.sourceActive)
mpris2.disconnectSource(mpris2.currentSource)

mpris2.connectSource(source)
}
}


PlasmaComponents.MenuItem {
id: menuItemAuto
readonly property string source: '@multiplex'
text: menu.caption(source)
onClicked: {
sourceChanged()
menu.changeSource(source)
}
checkable: true
Binding {
target: menuItemAuto
property: 'checked'
value: mpris2.currentSource === menuItemAuto.source
}
}

//!BEGIN: Media players menu items
readonly property Item _items: Item {
Repeater {
id: repeater
readonly property var sources: mpris2.sources.filter(function(e){ return e !== '@multiplex' })

model: sources.length
onItemAdded: menu.addMenuItem(item)
onItemRemoved: menu.removeMenuItem(item)

delegate: PlasmaComponents.MenuItem {
id: menuItem
readonly property string source: repeater.sources[index]
text: menu.caption(source)
onClicked: {
sourceChanged()
menu.changeSource(source)
}
checkable: true

Binding {
target: menuItem
property: 'checked'
value: mpris2.currentSource === menuItem.source
}
}
}
}
//!END: Media players menu items
}

}
}
36 changes: 24 additions & 12 deletions plasmoid/contents/ui/Mpris2.qml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ PlasmaCore.DataSource {

readonly property var metadata: currentSource ? data[currentSource].Metadata : undefined

readonly property string identity: currentSource ? capitalize(source) : ''
readonly property string identity: currentSource ? capitalize(currentSource) : ''

readonly property string playbackStatus: currentSource
&& sourceActive ? data[currentSource].PlaybackStatus : 'Stopped'
Expand Down Expand Up @@ -150,10 +150,11 @@ PlasmaCore.DataSource {
onSourcesChanged: {
if (connectedSources.length === 0)
nextSource()

console.log("sources availables:", sources)
}

onSourceAdded: {

// debug( 'Source added', source )
// debug( 'sources', sources )
if (source !== '@multiplex' && connectedSources.length === 0) {
Expand Down Expand Up @@ -247,29 +248,40 @@ PlasmaCore.DataSource {
}

function capitalize(source) {
var i = data[source]['Identity']
return i[0].toUpperCase() + i.substr(1)
if (source !== '') {
var i = (data[source] || { Identity: source }).Identity
return i[0].toUpperCase() + i.substr(1)
}

return source
}

function icon(source) {
var icon = source
var iconName = source

if (icon.match('vlc'))
icon = 'vlc'
if (iconName.match('vlc'))
iconName = 'vlc'

switch (icon) {
switch (iconName) {
case 'spotify':
icon = 'spotify-client'
iconName = 'spotify-client'
break
case 'clementine':
icon = 'application-x-clementine'
iconName = 'application-x-clementine'
break
case 'yarock':
icon = 'application-x-yarock'
iconName = 'application-x-yarock'
break
case '@multiplex':
if (sourceActive && currentSource == '@multiplex')
iconName = icon(data[source]['Source Name'])
else
iconName = 'emblem-music-symbolic'

break
}

return icon
return iconName
}

function addRecentSource(source) {
Expand Down

0 comments on commit 2bdb678

Please sign in to comment.