popupSliderMenuItem.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. // Global modules
  3. const Lang = imports.lang;
  4. const St = imports.gi.St;
  5. const Slider = imports.ui.slider;
  6. const PopupMenu = imports.ui.popupMenu;
  7. // Internal modules
  8. const ExtensionUtils = imports.misc.extensionUtils;
  9. const Me = ExtensionUtils.getCurrentExtension();
  10. const Bundle = Me.imports.bundle;
  11. // Utilities
  12. const normalizeRange = Bundle.normalizeRange;
  13. const deNormalizeRange = Bundle.deNormalizeRange;
  14. var PopupSliderMenuItem = new Lang.Class({
  15. Name: "WindowCornerPreview.PopupSliderMenuItem",
  16. Extends: PopupMenu.PopupBaseMenuItem,
  17. _init: function(text, value, min, max, step, params) {
  18. this.min = (min !== undefined ? min : 0.0);
  19. this.max = (max !== undefined ? max : 1.0);
  20. this.defaultValue = (value !== undefined ? value : (this.max + this.min) / 2.0);
  21. // *** KNOWN ISSUE: Scrolling may get stucked if step value > 1.0 (and |min-max| is a low value)
  22. // due to const SLIDER_SCROLL_STEP = 0.02 on js/ui/slider.js ***
  23. this.step = step;
  24. params = params || {};
  25. params.activate = false;
  26. this.parent(params);
  27. this.label = new St.Label({
  28. text: text || ""
  29. });
  30. // Setting text to false allow a little bit extra space on the left
  31. if (text !== false) this.actor.add_child(this.label);
  32. this.actor.label_actor = this.label;
  33. this.slider = new Slider.Slider(0.0);
  34. this.value = this.defaultValue;
  35. // PopupSliderMenuItem emits its own value-change event which provides a normalized value
  36. this.slider.connect("value-changed", Lang.bind(this, function(x) {
  37. let normalValue = this.value;
  38. // Force the slider to set position on a stepped value (if necessary)
  39. if (this.step !== undefined) this.value = normalValue;
  40. // Don't through any event if step rounded it to the same value
  41. if (normalValue !== this._lastValue) this.emit("value-changed", normalValue);
  42. this._lastValue = normalValue;
  43. }));
  44. this.actor.add(this.slider.actor, {
  45. expand: true,
  46. align: St.Align.END
  47. });
  48. },
  49. get value() {
  50. return deNormalizeRange(this.slider.value, this.min, this.max, this.step);
  51. },
  52. set value(newValue) {
  53. this._lastValue = normalizeRange(newValue, this.min, this.max, this.step);
  54. this.slider.setValue(this._lastValue);
  55. }
  56. });