2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2010-02-22 19:53:41 +01:00
|
|
|
|
2023-07-10 02:53:00 -07:00
|
|
|
import GObject from 'gi://GObject';
|
|
|
|
import Shell from 'gi://Shell';
|
2010-02-22 19:53:41 +01:00
|
|
|
|
2023-07-10 02:53:00 -07:00
|
|
|
import * as Main from './main.js';
|
|
|
|
import * as MessageTray from './messageTray.js';
|
2010-02-22 19:53:41 +01:00
|
|
|
|
2023-07-10 02:53:00 -07:00
|
|
|
export class WindowAttentionHandler {
|
2017-10-31 02:19:44 +01:00
|
|
|
constructor() {
|
2011-03-03 18:10:32 +01:00
|
|
|
this._tracker = Shell.WindowTracker.get_default();
|
2021-08-16 00:36:59 +02:00
|
|
|
global.display.connectObject(
|
|
|
|
'window-demands-attention', this._onWindowDemandsAttention.bind(this),
|
|
|
|
'window-marked-urgent', this._onWindowDemandsAttention.bind(this),
|
|
|
|
this);
|
2017-10-31 02:19:44 +01:00
|
|
|
}
|
2010-02-22 19:53:41 +01:00
|
|
|
|
2017-10-31 01:03:21 +01:00
|
|
|
_getTitleAndBanner(app, window) {
|
2012-02-28 17:12:20 -05:00
|
|
|
let title = app.get_name();
|
2023-08-07 00:34:20 +02:00
|
|
|
let banner = _('“%s” is ready').format(window.get_title());
|
2019-01-29 02:18:52 +01:00
|
|
|
return [title, banner];
|
2017-10-31 02:19:44 +01:00
|
|
|
}
|
2012-02-28 17:12:20 -05:00
|
|
|
|
2017-10-31 01:03:21 +01:00
|
|
|
_onWindowDemandsAttention(display, window) {
|
2010-02-22 19:53:41 +01:00
|
|
|
// We don't want to show the notification when the window is already focused,
|
|
|
|
// because this is rather pointless.
|
|
|
|
// Some apps (like GIMP) do things like setting the urgency hint on the
|
|
|
|
// toolbar windows which would result into a notification even though GIMP itself is
|
|
|
|
// focused.
|
|
|
|
// We are just ignoring the hint on skip_taskbar windows for now.
|
|
|
|
// (Which is the same behaviour as with metacity + panel)
|
|
|
|
|
|
|
|
if (!window || window.has_focus() || window.is_skip_taskbar())
|
|
|
|
return;
|
|
|
|
|
2011-03-03 18:10:32 +01:00
|
|
|
let app = this._tracker.get_window_app(window);
|
2019-05-13 23:32:31 +02:00
|
|
|
let source = new WindowAttentionSource(app, window);
|
2011-03-03 18:10:32 +01:00
|
|
|
Main.messageTray.add(source);
|
2010-02-22 19:53:41 +01:00
|
|
|
|
2012-02-28 17:12:20 -05:00
|
|
|
let [title, banner] = this._getTitleAndBanner(app, window);
|
2011-09-27 21:03:53 -04:00
|
|
|
|
|
|
|
let notification = new MessageTray.Notification(source, title, banner);
|
2017-10-31 01:38:18 +01:00
|
|
|
notification.connect('activated', () => {
|
2013-10-13 23:20:50 -04:00
|
|
|
source.open();
|
|
|
|
});
|
2012-11-02 18:06:40 +01:00
|
|
|
notification.setForFeedback(true);
|
|
|
|
|
2019-05-13 23:32:31 +02:00
|
|
|
source.showNotification(notification);
|
2010-02-22 19:53:41 +01:00
|
|
|
|
2021-08-16 00:36:59 +02:00
|
|
|
window.connectObject('notify::title', () => {
|
2019-08-20 02:20:08 +02:00
|
|
|
[title, banner] = this._getTitleAndBanner(app, window);
|
2012-02-28 17:12:20 -05:00
|
|
|
notification.update(title, banner);
|
2021-08-16 00:36:59 +02:00
|
|
|
}, source);
|
2010-02-22 19:53:41 +01:00
|
|
|
}
|
2023-07-10 02:53:00 -07:00
|
|
|
}
|
2010-02-22 19:53:41 +01:00
|
|
|
|
2023-07-10 02:53:00 -07:00
|
|
|
const WindowAttentionSource = GObject.registerClass(
|
2019-05-13 23:32:31 +02:00
|
|
|
class WindowAttentionSource extends MessageTray.Source {
|
|
|
|
_init(app, window) {
|
2010-02-22 19:53:41 +01:00
|
|
|
this._window = window;
|
|
|
|
this._app = app;
|
2011-10-08 18:00:32 -04:00
|
|
|
|
2020-03-23 20:59:31 +01:00
|
|
|
super._init(app.get_name());
|
|
|
|
|
2021-08-16 00:36:59 +02:00
|
|
|
this._window.connectObject(
|
|
|
|
'notify::demands-attention', this._sync.bind(this),
|
|
|
|
'notify::urgent', this._sync.bind(this),
|
|
|
|
'focus', () => this.destroy(),
|
|
|
|
'unmanaged', () => this.destroy(), this);
|
2017-10-31 02:19:44 +01:00
|
|
|
}
|
2011-03-03 18:10:32 +01:00
|
|
|
|
2018-07-13 19:16:14 +02:00
|
|
|
_sync() {
|
|
|
|
if (this._window.demands_attention || this._window.urgent)
|
|
|
|
return;
|
|
|
|
this.destroy();
|
2017-10-31 02:19:44 +01:00
|
|
|
}
|
2018-07-13 19:16:14 +02:00
|
|
|
|
2017-10-31 01:03:21 +01:00
|
|
|
_createPolicy() {
|
2017-10-23 10:06:18 +02:00
|
|
|
if (this._app && this._app.get_app_info()) {
|
2019-01-29 02:27:05 +01:00
|
|
|
let id = this._app.get_id().replace(/\.desktop$/, '');
|
2017-10-23 10:06:18 +02:00
|
|
|
return new MessageTray.NotificationApplicationPolicy(id);
|
|
|
|
} else {
|
|
|
|
return new MessageTray.NotificationGenericPolicy();
|
|
|
|
}
|
2017-10-31 02:19:44 +01:00
|
|
|
}
|
2017-10-23 10:06:18 +02:00
|
|
|
|
2017-10-31 01:03:21 +01:00
|
|
|
createIcon(size) {
|
2012-07-19 15:05:17 +02:00
|
|
|
return this._app.create_icon_texture(size);
|
2017-10-31 02:19:44 +01:00
|
|
|
}
|
2010-02-22 19:53:41 +01:00
|
|
|
|
2019-05-26 08:31:07 -07:00
|
|
|
destroy(params) {
|
2021-08-16 00:36:59 +02:00
|
|
|
this._window.disconnectObject(this);
|
2019-05-26 08:31:07 -07:00
|
|
|
|
|
|
|
super.destroy(params);
|
|
|
|
}
|
|
|
|
|
2017-10-31 01:03:21 +01:00
|
|
|
open() {
|
2010-02-22 19:53:41 +01:00
|
|
|
Main.activateWindow(this._window);
|
|
|
|
}
|
2019-05-13 23:32:31 +02:00
|
|
|
});
|