menuItem.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import Clutter from 'gi://Clutter';
  2. import GObject from 'gi://GObject';
  3. import St from 'gi://St'
  4. import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
  5. export const MenuItem = GObject.registerClass({
  6. Signals: {
  7. 'toggle': { param_types: [Clutter.Event.$gtype] },
  8. },
  9. }, class MenuItem extends PopupMenu.PopupBaseMenuItem {
  10. _init(icon, key, label, value, checked) {
  11. super._init({ reactive: true });
  12. this._checked = checked;
  13. this._updateOrnament();
  14. this._key = key;
  15. this._gIcon = icon;
  16. // add icon
  17. this.add(new St.Icon({ style_class: 'popup-menu-icon', gicon : this._gIcon }));
  18. // add label
  19. this._labelActor = new St.Label({ text: label });
  20. this.add(this._labelActor);
  21. // add value
  22. this._valueLabel = new St.Label({ text: value });
  23. this._valueLabel.set_x_align(Clutter.ActorAlign.END);
  24. this._valueLabel.set_x_expand(true);
  25. this._valueLabel.set_y_expand(true);
  26. this.add(this._valueLabel);
  27. this.actor._delegate = this;
  28. }
  29. get checked() {
  30. return this._checked;
  31. }
  32. get key() {
  33. return this._key;
  34. }
  35. get gicon() {
  36. return this._gIcon;
  37. }
  38. set value(value) {
  39. this._valueLabel.text = value;
  40. }
  41. get value() {
  42. return this._valueLabel.text;
  43. }
  44. // prevents menu from being closed
  45. activate(event) {
  46. this._checked = !this._checked;
  47. this._updateOrnament();
  48. this.emit('toggle', event);
  49. }
  50. _updateOrnament() {
  51. if (this._checked)
  52. this.setOrnament(PopupMenu.Ornament.CHECK);
  53. else
  54. this.setOrnament(PopupMenu.Ornament.NONE);
  55. }
  56. get label() {
  57. return this._labelActor.text;
  58. }
  59. });