Unverified Commit 5a6ae71d authored by Arnesh Roychowdhury's avatar Arnesh Roychowdhury Committed by GitHub

feat(mpris): autohide (#533)

* fixed typo * mpris autohide: initial implementation * mpris autohide: added documentation --------- Co-authored-by: 's avatarErik Reider <35975961+ErikReider@users.noreply.github.com>
parent 4ac4cea5
...@@ -344,6 +344,12 @@ config file to be able to detect config errors ...@@ -344,6 +344,12 @@ config file to be able to detect config errors
type: string ++ type: string ++
description: Audio source/app name. Regex alowed. Hint ++ description: Audio source/app name. Regex alowed. Hint ++
`$ qdbus | grep mpris` to find source names. ++ `$ qdbus | grep mpris` to find source names. ++
autohide: ++
type: bool ++
optional: true ++
default: false ++
description: Whether to hide the widget when the ++
player has no metadata. ++
description: A widget that displays multiple music players. ++ description: A widget that displays multiple music players. ++
*menubar*++ *menubar*++
type: object ++ type: object ++
...@@ -610,7 +616,8 @@ config file to be able to detect config errors ...@@ -610,7 +616,8 @@ config file to be able to detect config errors
"mpris": { "mpris": {
"image-size": 96, "image-size": 96,
"image-radius": 12, "image-radius": 12,
"blacklist": ["playerctld"] "blacklist": ["playerctld"],
"autohide": true
}, },
"menubar": { "menubar": {
"menu#power": { "menu#power": {
......
...@@ -76,7 +76,8 @@ ...@@ -76,7 +76,8 @@
"mpris": { "mpris": {
"image-size": 96, "image-size": 96,
"image-radius": 12, "image-radius": 12,
"blacklist": [] "blacklist": [],
"autohide": false
}, },
"buttons-grid": { "buttons-grid": {
"actions": [ "actions": [
......
...@@ -9,8 +9,8 @@ namespace SwayNotificationCenter.Widgets.Mpris { ...@@ -9,8 +9,8 @@ namespace SwayNotificationCenter.Widgets.Mpris {
public const string INTERFACE_PATH = "/org/mpris/MediaPlayer2"; public const string INTERFACE_PATH = "/org/mpris/MediaPlayer2";
private MprisSource (MprisMediaPlayer _meddia_player, DbusPropChange _props) { private MprisSource (MprisMediaPlayer _media_player, DbusPropChange _props) {
this.media_player = _meddia_player; this.media_player = _media_player;
this.props = _props; this.props = _props;
this.props.properties_changed.connect ( this.props.properties_changed.connect (
(i, c, inv) => properties_changed (i, c, inv)); (i, c, inv) => properties_changed (i, c, inv));
......
...@@ -3,6 +3,7 @@ namespace SwayNotificationCenter.Widgets.Mpris { ...@@ -3,6 +3,7 @@ namespace SwayNotificationCenter.Widgets.Mpris {
int image_size; int image_size;
int image_radius; int image_radius;
bool blur; bool blur;
bool autohide;
string[] blacklist; string[] blacklist;
} }
...@@ -31,6 +32,7 @@ namespace SwayNotificationCenter.Widgets.Mpris { ...@@ -31,6 +32,7 @@ namespace SwayNotificationCenter.Widgets.Mpris {
image_size = 96, image_size = 96,
image_radius = 12, image_radius = 12,
blur = true, blur = true,
autohide = false
}; };
public Mpris (string suffix, SwayncDaemon swaync_daemon, NotiDaemon noti_daemon) { public Mpris (string suffix, SwayncDaemon swaync_daemon, NotiDaemon noti_daemon) {
...@@ -111,6 +113,11 @@ namespace SwayNotificationCenter.Widgets.Mpris { ...@@ -111,6 +113,11 @@ namespace SwayNotificationCenter.Widgets.Mpris {
mpris_config.blacklist[i] = blacklist.get_string_element (i); mpris_config.blacklist[i] = blacklist.get_string_element (i);
} }
} }
// Get autohide
bool autohide_found;
bool? autohide = get_prop<bool> (config, "autohide", out autohide_found);
if (autohide_found) mpris_config.autohide = autohide;
} }
hide (); hide ();
...@@ -168,20 +175,67 @@ namespace SwayNotificationCenter.Widgets.Mpris { ...@@ -168,20 +175,67 @@ namespace SwayNotificationCenter.Widgets.Mpris {
return false; return false;
} }
private bool check_player_metadata_empty (string name) {
MprisPlayer ? player = players.lookup (name);
if (player == null) return true;
HashTable<string, Variant> metadata = player.source.media_player.metadata;
if (metadata == null || metadata.size () == 0) {
debug ("Metadata is empty");
return true;
}
return false;
}
private bool check_carousel_has_player (MprisPlayer player) {
return carousel.get_children ().find (player) != null;
}
private void add_player_to_carousel (string name) {
MprisPlayer ? player = players.lookup (name);
if (player == null || check_carousel_has_player (player)) return;
carousel.prepend (player);
uint children_length = carousel.get_children ().length ();
if (children_length > 1) {
button_prev.show ();
button_next.show ();
// Scroll to the new player
carousel.scroll_to (player);
}
if (!visible) show ();
}
private void add_player (string name, MprisSource source) { private void add_player (string name, MprisSource source) {
MprisPlayer player = new MprisPlayer (source, mpris_config); MprisPlayer player = new MprisPlayer (source, mpris_config);
player.get_style_context ().add_class ("%s-player".printf (css_class_name)); player.get_style_context ().add_class ("%s-player".printf (css_class_name));
carousel.prepend (player);
players.set (name, player); players.set (name, player);
if (!visible) show (); if (mpris_config.autohide) {
player.content_updated.connect (() => {
if (!check_player_metadata_empty (name)) {
add_player_to_carousel (name);
} else {
remove_player_from_carousel (name);
}
});
if (check_player_metadata_empty (name)) return;
}
// Scroll to the new player add_player_to_carousel (name);
carousel.scroll_to (player); }
private void remove_player_from_carousel (string name) {
MprisPlayer ? player = players.lookup (name);
if (player == null || !check_carousel_has_player (player)) return;
carousel.remove (player);
uint children_length = carousel.get_children ().length (); uint children_length = carousel.get_children ().length ();
if (children_length > 1) {
button_prev.show (); if (children_length == 0) hide ();
button_next.show ();
if (children_length <= 1) {
button_prev.hide ();
button_next.hide ();
} }
} }
...@@ -190,18 +244,12 @@ namespace SwayNotificationCenter.Widgets.Mpris { ...@@ -190,18 +244,12 @@ namespace SwayNotificationCenter.Widgets.Mpris {
MprisPlayer ? player; MprisPlayer ? player;
bool result = players.lookup_extended (name, out key, out player); bool result = players.lookup_extended (name, out key, out player);
if (!result || key == null || player == null) return; if (!result || key == null || player == null) return;
remove_player_from_carousel (name);
player.before_destroy (); player.before_destroy ();
player.destroy (); player.destroy ();
players.remove (name); players.remove (name);
uint children_length = carousel.get_children ().length ();
if (children_length == 0) {
hide ();
}
if (children_length <= 1) {
button_prev.hide ();
button_next.hide ();
}
} }
private void change_carousel_position (int delta) { private void change_carousel_position (int delta) {
......
...@@ -43,6 +43,8 @@ namespace SwayNotificationCenter.Widgets.Mpris { ...@@ -43,6 +43,8 @@ namespace SwayNotificationCenter.Widgets.Mpris {
private unowned Config mpris_config; private unowned Config mpris_config;
public signal void content_updated ();
public MprisPlayer (MprisSource source, Config mpris_config) { public MprisPlayer (MprisSource source, Config mpris_config) {
Object (source: source); Object (source: source);
this.mpris_config = mpris_config; this.mpris_config = mpris_config;
...@@ -228,6 +230,10 @@ namespace SwayNotificationCenter.Widgets.Mpris { ...@@ -228,6 +230,10 @@ namespace SwayNotificationCenter.Widgets.Mpris {
// Update the buttons // Update the buttons
update_buttons (metadata); update_buttons (metadata);
// Emit signal
content_updated ();
} }
private void update_buttons (HashTable<string, Variant> metadata) { private void update_buttons (HashTable<string, Variant> metadata) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment