From c416b3463055a0167f2e1412c7442674de5e75d8 Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Thu, 25 Jul 2013 15:56:04 +0200 Subject: service.vala: move menu handling into sound-menu.vala This makes service.vala a lot more readable and makes way for exporting a second - slightly different - menu for the phone profile. --- src/sound-menu.vala | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 src/sound-menu.vala (limited to 'src/sound-menu.vala') diff --git a/src/sound-menu.vala b/src/sound-menu.vala new file mode 100644 index 0000000..acb9adf --- /dev/null +++ b/src/sound-menu.vala @@ -0,0 +1,160 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * Authors: + * Lars Uebernickel + */ + +class SoundMenu: Object +{ + public SoundMenu () { + /* A sound menu always has at least two sections: the volume section (this.volume_section) + * at the start of the menu, and the settings section at the end. Between those two, + * it has a dynamic amount of player sections, one for each registered player. + */ + + this.volume_section = new Menu (); + volume_section.append (_("Mute"), "indicator.mute"); + volume_section.append_item (this.create_slider_menu_item ("indicator.volume", 0.0, 1.0, 0.01, + "audio-volume-low-zero-panel", + "audio-volume-low-high-panel")); + + this.menu = new Menu (); + this.menu.append_section (null, volume_section); + this.menu.append (_("Sound Settings…"), "indicator.settings"); + + var root_item = new MenuItem (null, "indicator.root"); + root_item.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.root"); + root_item.set_submenu (this.menu); + + this.root = new Menu (); + root.append_item (root_item); + } + + public void export (DBusConnection connection, string object_path) throws Error { + connection.export_menu_model (object_path, this.root); + } + + public bool show_mic_volume { + get { + return this.volume_section.get_n_items () == 3; + } + set { + if (value && this.volume_section.get_n_items () < 3) { + var slider = this.create_slider_menu_item ("indicator.mic-volume", 0.0, 1.0, 0.01, + "audio-input-microphone-low-zero-panel", + "audio-input-microphone-high-panel"); + volume_section.append_item (slider); + } + else if (!value && this.volume_section.get_n_items () > 2) { + this.volume_section.remove (2); + } + } + } + + public void add_player (MediaPlayer player) { + /* Add new players to the end of the player sections, just before the settings */ + var player_item = new MenuItem (player.name, "indicator." + player.id); + player_item.set_attribute ("x-canonical-type", "s", "com.canonical.unity.media-player"); + player_item.set_attribute_value ("icon", g_icon_serialize (player.icon)); + + var playback_item = new MenuItem (null, null); + playback_item.set_attribute ("x-canonical-type", "s", "com.canonical.unity.playback-item"); + playback_item.set_attribute ("x-canonical-play-action", "s", "indicator.play." + player.id); + playback_item.set_attribute ("x-canonical-next-action", "s", "indicator.next." + player.id); + playback_item.set_attribute ("x-canonical-previous-action", "s", "indicator.previous." + player.id); + + var section = new Menu (); + section.append_item (player_item); + section.append_item (playback_item); + + player.playlists_changed.connect (this.update_playlists); + player.notify["is-running"].connect ( () => this.update_playlists (player) ); + update_playlists (player); + + this.menu.insert_section (this.menu.get_n_items () -1, null, section); + } + + public void remove_player (MediaPlayer player) { + int index = this.find_player_section (player); + if (index >= 0) + this.menu.remove (index); + } + + Menu root; + Menu menu; + Menu volume_section; + + /* returns the position in this.menu of the section that's associated with @player */ + int find_player_section (MediaPlayer player) { + string action_name = @"indicator.$(player.id)"; + int n = this.menu.get_n_items () -1; + for (int i = 1; i < n; i++) { + var section = this.menu.get_item_link (i, Menu.LINK_SECTION); + string action; + section.get_item_attribute (0, "action", "s", out action); + if (action == action_name) + return i; + } + + return -1; + } + + void update_playlists (MediaPlayer player) { + int index = find_player_section (player); + if (index < 0) + return; + + var player_section = this.menu.get_item_link (index, Menu.LINK_SECTION) as Menu; + + /* if a section has three items, the playlists menu is in it */ + if (player_section.get_n_items () == 3) + player_section.remove (2); + + if (!player.is_running) + return; + + var count = player.get_n_playlists (); + if (count == 0) + return; + + var playlists_section = new Menu (); + for (int i = 0; i < count; i++) { + var playlist_id = player.get_playlist_id (i); + playlists_section.append (player.get_playlist_name (i), + @"indicator.play-playlist.$(player.id)::$playlist_id"); + + } + + var submenu = new Menu (); + submenu.append_section (null, playlists_section); + player_section.append_submenu ("Choose Playlist", submenu); + } + + MenuItem create_slider_menu_item (string action, double min, double max, double step, string min_icon_name, string max_icon_name) { + var min_icon = new ThemedIcon.with_default_fallbacks (min_icon_name); + var max_icon = new ThemedIcon.with_default_fallbacks (max_icon_name); + + var slider = new MenuItem (null, action); + slider.set_attribute ("x-canonical-type", "s", "com.canonical.unity.slider"); + slider.set_attribute_value ("min-icon", g_icon_serialize (min_icon)); + slider.set_attribute_value ("max-icon", g_icon_serialize (max_icon)); + slider.set_attribute ("min-value", "d", min); + slider.set_attribute ("max-value", "d", max); + slider.set_attribute ("step", "d", step); + + return slider; + } +} -- cgit v1.2.3 From 997864b9fc62596e8cc238f4c4b77d780651ffb4 Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Thu, 25 Jul 2013 16:21:44 +0200 Subject: service.vala: store menus in a hash table, keyed by profile name --- src/sound-menu.vala | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/sound-menu.vala') diff --git a/src/sound-menu.vala b/src/sound-menu.vala index acb9adf..376ef15 100644 --- a/src/sound-menu.vala +++ b/src/sound-menu.vala @@ -43,8 +43,12 @@ class SoundMenu: Object root.append_item (root_item); } - public void export (DBusConnection connection, string object_path) throws Error { - connection.export_menu_model (object_path, this.root); + public void export (DBusConnection connection, string object_path) { + try { + connection.export_menu_model (object_path, this.root); + } catch (Error e) { + critical ("%s", e.message); + } } public bool show_mic_volume { -- cgit v1.2.3 From ce5583c3d5c8f4cf390936ef567c7088062675df Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Thu, 25 Jul 2013 16:38:15 +0200 Subject: Export phone menu For now, this does the same as the one on the desktop, except for starting the the phone's settings ui. --- src/sound-menu.vala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/sound-menu.vala') diff --git a/src/sound-menu.vala b/src/sound-menu.vala index 376ef15..dd50f12 100644 --- a/src/sound-menu.vala +++ b/src/sound-menu.vala @@ -19,7 +19,7 @@ class SoundMenu: Object { - public SoundMenu () { + public SoundMenu (string settings_action) { /* A sound menu always has at least two sections: the volume section (this.volume_section) * at the start of the menu, and the settings section at the end. Between those two, * it has a dynamic amount of player sections, one for each registered player. @@ -33,7 +33,7 @@ class SoundMenu: Object this.menu = new Menu (); this.menu.append_section (null, volume_section); - this.menu.append (_("Sound Settings…"), "indicator.settings"); + this.menu.append (_("Sound Settings…"), settings_action); var root_item = new MenuItem (null, "indicator.root"); root_item.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.root"); -- cgit v1.2.3 From 8e34167a1a95fa4c6975af14100958ec734e8bda Mon Sep 17 00:00:00 2001 From: Nick Dedekind Date: Tue, 30 Jul 2013 15:19:44 +0100 Subject: Fixed max-icon icon source --- src/sound-menu.vala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/sound-menu.vala') diff --git a/src/sound-menu.vala b/src/sound-menu.vala index dd50f12..de5500e 100644 --- a/src/sound-menu.vala +++ b/src/sound-menu.vala @@ -29,7 +29,7 @@ class SoundMenu: Object volume_section.append (_("Mute"), "indicator.mute"); volume_section.append_item (this.create_slider_menu_item ("indicator.volume", 0.0, 1.0, 0.01, "audio-volume-low-zero-panel", - "audio-volume-low-high-panel")); + "audio-volume-high-panel")); this.menu = new Menu (); this.menu.append_section (null, volume_section); @@ -139,7 +139,7 @@ class SoundMenu: Object var playlist_id = player.get_playlist_id (i); playlists_section.append (player.get_playlist_name (i), @"indicator.play-playlist.$(player.id)::$playlist_id"); - + } var submenu = new Menu (); -- cgit v1.2.3 From 796a887cf88c1ad23ab06e89435ca1e04080aaee Mon Sep 17 00:00:00 2001 From: Pete Woods Date: Fri, 16 Aug 2013 04:13:41 +0100 Subject: Build using cmake --- src/sound-menu.vala | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/sound-menu.vala') diff --git a/src/sound-menu.vala b/src/sound-menu.vala index de5500e..415a5be 100644 --- a/src/sound-menu.vala +++ b/src/sound-menu.vala @@ -17,6 +17,9 @@ * Lars Uebernickel */ +/* Icon.serialize() is not yet in gio-2.0.vapi; remove this when it is */ +extern Variant? g_icon_serialize (Icon icon); + class SoundMenu: Object { public SoundMenu (string settings_action) { -- cgit v1.2.3