Debian Codegraph @

src/raven/widgets/sound-output/app_sound_control.vala

raw
1 /*
2 * This file is part of budgie-desktop
3 *
4 * Copyright Budgie Desktop Developers
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12 namespace Budgie {
13 public class AppSoundControl : Gtk.Box {
14 private Gvc.MixerControl? mixer = null;
15 public Gvc.MixerStream? primary_stream = null;
16 public Gvc.MixerStream? stream = null;
17 private Gtk.Box? app_info_header = null;
18 private Gtk.Image? app_image = null;
19 private Gtk.Label? app_label = null;
20 private Gtk.Button? app_mute_button = null;
21 private Gtk.Scale? volume_slider = null;
22 private uint32? volume;
23
24 private Gtk.Image? audio_not_muted = null;
25 private Gtk.Image? audio_muted = null;
26
27 public string? app_name = "";
28 private ulong scale_id;
29
30 public AppSoundControl(Gvc.MixerControl c_mixer, Gvc.MixerStream c_primary, Gvc.MixerStream c_stream, string c_icon, string c_name) {
31 Object(orientation: Gtk.Orientation.HORIZONTAL, margin: 4);
32
33 valign = Gtk.Align.START;
34 margin_start = 10;
35 margin_end = 10;
36
37 if (c_mixer == null) {
38 return;
39 }
40
41 if (c_primary == null) {
42 return;
43 }
44
45 mixer = c_mixer;
46 primary_stream = c_primary;
47 stream = c_stream;
48 app_name = c_name;
49
50 /**
51 * App Desktop Logic
52 * Firstly, check if the app name has a related desktop app info, and if so use the desktop name for the application.
53 * Otherwise, use a titlized form of the app name if it'd match up with the description. Otherwise use app name.
54 */
55
56 string alsa_beg = "ALSA plug-in [";
57 if (app_name.has_prefix(alsa_beg)) {
58 app_name = app_name.replace(alsa_beg, ""); // Remove the ALSA prefix
59 app_name = app_name.substring(0, (app_name.length - 1)); // Replace the prefix
60 }
61
62 DesktopAppInfo info = new DesktopAppInfo(app_name + ".desktop"); // Attempt to get the application info
63
64 if (info != null) { // Successfully got app info
65 string desktop_app_name = info.get_string("Name");
66
67 if ((desktop_app_name != "") && (desktop_app_name != null)) { // If we got the desktop app name
68 app_name = desktop_app_name;
69 }
70 }
71
72 string stream_name = stream.get_name();
73
74 Gtk.IconTheme current_theme = Gtk.IconTheme.get_default(); // Get our default IconTheme
75 string usable_icon_name = c_icon;
76
77 if (current_theme.has_icon(app_name)) { // Has icon based on app name
78 usable_icon_name = app_name;
79 } else if (current_theme.has_icon(stream_name)) { // Has icon based on stream name
80 usable_icon_name = stream_name; // Set to icon name
81 }
82
83 if (usable_icon_name != "applications-multimedia") { // Successfully got an icon from a valid app
84 app_name = app_name.substring(0, 1).ascii_up() + app_name.substring(1); // Titalize the app name. Not doing this for non-compliant icons means apps like mocp don't get wrongly titalized.
85 }
86
87 /**
88 * Create initial elements
89 */
90 audio_not_muted = new Gtk.Image.from_icon_name("audio-volume-high-symbolic", Gtk.IconSize.MENU);
91 audio_not_muted.pixel_size = 12;
92 audio_muted = new Gtk.Image.from_icon_name("audio-volume-muted-symbolic", Gtk.IconSize.MENU);
93 audio_muted.pixel_size = 12;
94
95 Gtk.Box app_info = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
96 app_info_header = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
97
98 app_label = new Gtk.Label(app_name); // Create a new label with the app name
99 app_label.ellipsize = Pango.EllipsizeMode.END;
100 app_label.halign = Gtk.Align.START; // Align to edge (left for LTR; right for RTL)
101 app_label.justify = Gtk.Justification.LEFT;
102 app_label.margin_start = 10;
103
104 app_mute_button = new Gtk.Button();
105 app_mute_button.get_style_context().add_class("flat");
106 app_mute_button.clicked.connect(toggle_mute_state);
107
108 app_info_header.pack_start(app_label, false, true, 0);
109 app_info_header.pack_end(app_mute_button, false, false, 0);
110
111 var max_vol = this.get_base_volume();
112 var stream_volume = stream.get_volume();
113 var max_vol_step = max_vol / 20; // Default to steps of 20
114
115 volume_slider = new Gtk.Scale.with_range(Gtk.Orientation.HORIZONTAL, 0, max_vol, max_vol_step);
116 volume_slider.set_draw_value(false);
117 volume_slider.set_increments(max_vol_step, max_vol_step);
118
119 set_mute_ui();
120
121 volume = stream_volume;
122 volume_slider.set_value(stream_volume);
123
124 scale_id = volume_slider.value_changed.connect(on_slider_change);
125
126 app_info.pack_start(app_info_header, true, false, 0);
127 app_info.pack_end(volume_slider, true, false, 0);
128
129 app_image = new Gtk.Image.from_icon_name(usable_icon_name, Gtk.IconSize.DND);
130
131 if (app_image != null) {
132 pack_start(app_image, false, false, 0);
133 }
134
135 pack_end(app_info, true, true, 0);
136 }
137
138 /**
139 * get_base_volume will get the recommended base volume based on the stream and primary device stream
140 */
141 private uint32 get_base_volume() {
142 var base_vol = stream.get_volume();
143 var primary_stream_vol = primary_stream.get_base_volume();
144 return (base_vol < primary_stream_vol) ? primary_stream_vol : base_vol;
145 }
146
147 /**
148 * on_slider_change will handle when our volume_slider scale changes
149 */
150 private void on_slider_change() {
151 var slider_value = volume_slider.get_value();
152
153 SignalHandler.block(volume_slider, scale_id);
154 uint32 stream_vol = (uint32) slider_value;
155
156 volume = stream_vol;
157
158 if (stream.set_volume(stream_vol)) {
159 Gvc.push_volume(stream);
160 }
161
162 SignalHandler.unblock(volume_slider, scale_id);
163 }
164
165 /**
166 * refresh is responsible for performing UI refresh / updating
167 */
168 public void refresh() {
169 var stream_name = stream.get_name();
170
171 if (app_name != stream_name) { // If the application name has changed
172 app_name = stream_name;
173 app_label.label = stream_name;
174 }
175
176 refresh_volume(); // Refresh the volume
177 }
178
179 /**
180 * refresh_volume is our function for updating the volume slider and mute UI
181 */
182 public void refresh_volume() {
183 var vol = stream.get_volume();
184
185 if (volume_slider.get_value() != vol) { // If the volume has changed
186 volume_slider.set_value(vol); // Update volume slider value
187 }
188
189 volume = vol;
190 set_mute_ui(); // Ensure we have an updated mute
191 }
192
193 /**
194 * set_mute_ui will set the image for the app_mute_button and change dim state of the input
195 */
196 private void set_mute_ui() {
197 if (stream.get_is_muted()) {
198 app_mute_button.set_image(audio_muted);
199 } else {
200 app_mute_button.set_image(audio_not_muted);
201 }
202 }
203
204 private void toggle_mute_state() {
205 SignalHandler.block(volume_slider, scale_id);
206 stream.change_is_muted(!stream.get_is_muted());
207 stream.set_is_muted(!stream.get_is_muted());
208 set_mute_ui();
209 SignalHandler.unblock(volume_slider, scale_id);
210 }
211 }
212 }