diff options
author | charles kerr <charlesk@canonical.com> | 2015-12-20 10:27:18 -0600 |
---|---|---|
committer | charles kerr <charlesk@canonical.com> | 2015-12-20 10:27:18 -0600 |
commit | 14156d350e60c1a5a194e0a599a2ca7808f6ff26 (patch) | |
tree | 55dc7fe21a578283fe92d66f4d674af3462ed140 /src/volume-warning.vala | |
parent | cfa2ff68cf0664c0d147e5a3792e8bd1f95be5d5 (diff) | |
download | ayatana-indicator-sound-14156d350e60c1a5a194e0a599a2ca7808f6ff26.tar.gz ayatana-indicator-sound-14156d350e60c1a5a194e0a599a2ca7808f6ff26.tar.bz2 ayatana-indicator-sound-14156d350e60c1a5a194e0a599a2ca7808f6ff26.zip |
move warning dialog from service to volume-warning
Diffstat (limited to 'src/volume-warning.vala')
-rw-r--r-- | src/volume-warning.vala | 105 |
1 files changed, 91 insertions, 14 deletions
diff --git a/src/volume-warning.vala b/src/volume-warning.vala index 423f129..29a049b 100644 --- a/src/volume-warning.vala +++ b/src/volume-warning.vala @@ -40,8 +40,6 @@ public class VolumeWarning : VolumeControl warning("set_mute not supported for VolumeWarning"); } - private IndicatorSound.WarnNotification _notification = new IndicatorSound.WarnNotification(); - /* this is static to ensure it being freed after @context (loop does not have ref counting) */ private static PulseAudio.GLibMainLoop loop; @@ -103,7 +101,10 @@ public class VolumeWarning : VolumeControl init_all_properties(); this.reconnect_to_pulse (); - } + + _notification = new IndicatorSound.WarnNotification(); + _notification.user_responded.connect((n, response) => on_user_response(response)); + } private void init_all_properties() { @@ -123,6 +124,7 @@ public class VolumeWarning : VolumeControl _reconnect_timer = 0; } stop_high_volume_approved_timer(); + stop_clamp_to_loud_timeout(); } private VolumeControl.ActiveOutput calculate_active_output (SinkInfo? sink) { @@ -672,16 +674,6 @@ public class VolumeWarning : VolumeControl && (stream == "multimedia"); } - public void clamp_to_high_volume() { - if (_high_volume && _options.is_loud(_volume)) { - var vol = new VolumeControl.Volume(); - vol.volume = _volume.volume.clamp(0, volume_to_double(_options.loud_volume())); - vol.reason = _volume.reason; - debug("Clamping from %f down to %f", _volume.volume, vol.volume); - volume = vol; - } - } - public void set_warning_volume() { var vol = new VolumeControl.Volume(); vol.volume = volume_to_double(_options.loud_volume()); @@ -694,7 +686,7 @@ public class VolumeWarning : VolumeControl public bool high_volume_approved { get; private set; default = false; } - public void approve_high_volume() { + private void approve_high_volume() { _high_volume_approved_at = GLib.get_monotonic_time(); update_high_volume_approved(); update_high_volume_approved_timer(); @@ -843,4 +835,89 @@ public class VolumeWarning : VolumeControl } return objp; } + + private void set_multimedia_volume(VolumeControl.Volume volume) + { + // FIXME + } + + // NOTIFICATION + + private IndicatorSound.WarnNotification _notification = new IndicatorSound.WarnNotification(); + + private VolumeControl.Volume _cancel_volume = null; + private VolumeControl.Volume _ok_volume = null; + + public void show(VolumeControl.Volume volume) { + + // the volume to use if user hits 'cancel' + _cancel_volume = new VolumeControl.Volume(); + _cancel_volume.volume = VolumeControlPulse.volume_to_double(_options.loud_volume()); + _cancel_volume.reason = VolumeControl.VolumeReasons.USER_KEYPRESS; + + // the volume to use if user hits 'ok' + _ok_volume = new VolumeControl.Volume(); + _ok_volume.volume = volume.volume; + _ok_volume.reason = VolumeControl.VolumeReasons.USER_KEYPRESS; + + _notification.show(); + this.active = true; + } + + public enum Key { + VOLUME_UP, + VOLUME_DOWN + } + + public void user_keypress(Key key) { + if (key == Key.VOLUME_DOWN) + on_user_response(IndicatorSound.WarnNotification.Response.CANCEL); + } + + private void on_user_response(IndicatorSound.WarnNotification.Response response) { + _notification.close(); + stop_clamp_to_loud_timeout(); + + if (response == IndicatorSound.WarnNotification.Response.OK) { + approve_high_volume(); + set_multimedia_volume(_ok_volume); + } else { // WarnNotification.CANCEL + set_multimedia_volume(_cancel_volume); + } + + _cancel_volume = null; + _ok_volume = null; + + this.active = false; + } + + // VOLUME CLAMPING + + private uint _clamp_to_loud_timeout = 0; + + private void stop_clamp_to_loud_timeout() { + if (_clamp_to_loud_timeout != 0) { + Source.remove(_clamp_to_loud_timeout); + _clamp_to_loud_timeout = 0; + } + } + + private void clamp_to_loud_soon() { + const uint interval_msec = 200; + if (_clamp_to_loud_timeout == 0) + _clamp_to_loud_timeout = Timeout.add(interval_msec, clamp_to_loud_idle); + } + + private bool clamp_to_loud_idle() { + _clamp_to_loud_timeout = 0; + clamp_to_loud_volume(); + return false; // Source.REMOVE; + } + + private void clamp_to_loud_volume() { + if ((_cancel_volume != null) && (_volume.volume > _cancel_volume.volume)) { + debug("Clamping from %f down to %f", _volume.volume, _cancel_volume.volume); + set_multimedia_volume (_cancel_volume); + } + } } |