utilnotify.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Copyright (C) 2016 Borsato Ivano
  3. The JavaScript code in this page is free software: you can
  4. redistribute it and/or modify it under the terms of the GNU
  5. General Public License (GNU GPL) as published by the Free Software
  6. Foundation, either version 3 of the License, or (at your option)
  7. any later version. The code is distributed WITHOUT ANY WARRANTY;
  8. without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  10. */
  11. 'use strict';
  12. import GObject from 'gi://GObject';
  13. import * as Main from 'resource:///org/gnome/shell/ui/main.js';
  14. // https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/main/js/ui/messageTray.js
  15. import * as MessageTray from 'resource:///org/gnome/shell/ui/messageTray.js';
  16. import St from 'gi://St';
  17. import * as Lib from './convenience.js';
  18. import * as Settings from './settings.js';
  19. import * as Ext from './extension.js';
  20. import {gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js';
  21. /**
  22. * @type {NotifyManager}
  23. */
  24. export const NotifyManager = GObject.registerClass({
  25. GTypeName: 'EasyScreenCast_NotifyManager',
  26. }, class NotifyManager extends GObject.Object {
  27. /**
  28. * Create a notify manager
  29. */
  30. constructor() {
  31. super();
  32. Lib.TalkativeLog('-°-init notify manager');
  33. this._alertWidget = null;
  34. }
  35. /**
  36. * create notify
  37. *
  38. * @param {string} msg the title
  39. * @param {Gio.FileIcon} icon the icon
  40. * @param {boolean} sound whether to play a sound
  41. * @returns {MessageTray.Notification}
  42. */
  43. createNotify(msg, icon, sound) {
  44. Lib.TalkativeLog(`-°-create notify :${msg}`);
  45. var source = new MessageTray.Source({
  46. title: _('EasyScreenCast'),
  47. });
  48. var notify = new MessageTray.Notification({
  49. source,
  50. title: msg,
  51. body: null,
  52. gicon: icon,
  53. isTransient: false,
  54. resident: true,
  55. });
  56. Main.messageTray.add(source);
  57. source.addNotification(notify);
  58. if (sound)
  59. notify.playSound();
  60. return notify;
  61. }
  62. /**
  63. * update notify
  64. *
  65. * @param {MessageTray.Notification} notify the already existing notification to update
  66. * @param {string} msg the title
  67. * @param {Gio.FileIcon} icon the icon
  68. * @param {boolean} sound whether to play a sound
  69. */
  70. updateNotify(notify, msg, icon, sound) {
  71. Lib.TalkativeLog('-°-update notify');
  72. notify.set({
  73. title: msg,
  74. body: null,
  75. gicon: icon,
  76. });
  77. if (sound)
  78. notify.playSound();
  79. }
  80. /**
  81. * create alert
  82. *
  83. * @param {string} msg the message
  84. */
  85. createAlert(msg) {
  86. Lib.TalkativeLog(`-°-show alert tweener : ${msg}`);
  87. if (Ext.Indicator.getSettings().getOption('b', Settings.SHOW_NOTIFY_ALERT_SETTING_KEY)) {
  88. var monitor = Main.layoutManager.focusMonitor;
  89. this.resetAlert();
  90. this._alertWidget = new St.Label({
  91. style_class: 'alert-msg',
  92. opacity: 255,
  93. text: msg,
  94. });
  95. Main.uiGroup.add_child(this._alertWidget);
  96. this._alertWidget.set_position(
  97. Math.floor(monitor.width / 2 - this._alertWidget.width / 2),
  98. Math.floor(monitor.height / 2 - this._alertWidget.height / 2)
  99. );
  100. Lib.TalkativeLog(`-°-show alert tweener : opacity=${this._alertWidget.opacity}`);
  101. // see org/gnome/shell/ui/environment.js#_easeActor
  102. // TODO: for some reason, no transition is created, so the onComplete
  103. // callback is called _immediately_
  104. /*
  105. import Clutter from 'gi://Clutter';
  106. this._alertWidget.ease({
  107. opacity: 0,
  108. duration: 400,
  109. mode: Clutter.AnimationMode.EASE_OUT_QUAD,
  110. onComplete: () => {
  111. Lib.TalkativeLog(`-°-show alert tweener completed: opacity=${this._alertWidget.opacity}`);
  112. Main.uiGroup.remove_child(this._alertWidget);
  113. this._alertWidget = null;
  114. },
  115. });
  116. */
  117. }
  118. }
  119. resetAlert() {
  120. if (this._alertWidget !== null) {
  121. this._alertWidget.hide();
  122. Main.uiGroup.remove_child(this._alertWidget);
  123. this._alertWidget = null;
  124. }
  125. }
  126. });