|
1 | 1 | /* jshint esnext:true */ |
2 | 2 |
|
| 3 | +const GLib = imports.gi.GLib; |
3 | 4 | const Main = imports.ui.main; |
4 | | -const Lang = imports.lang; |
5 | | -const ExtensionSystem = imports.ui.extensionSystem; |
| 5 | +const Mainloop = imports.mainloop; |
6 | 6 |
|
7 | | -let _extensionChangedId; |
8 | 7 |
|
9 | | -function init() |
| 8 | +const MAX_RECURSE_DEPTH = 3; |
| 9 | + |
| 10 | +let signalConnections = []; |
| 11 | +let dropdowns = []; |
| 12 | + |
| 13 | + |
| 14 | +/** |
| 15 | + * Try hide a single dropdown actor. |
| 16 | + * |
| 17 | + * Return true on success. |
| 18 | + */ |
| 19 | +function _apply(actor) |
10 | 20 | { |
11 | | - /* no initialization required */ |
| 21 | + if (!actor.has_style_class_name || !actor.has_style_class_name('popup-menu-arrow')) |
| 22 | + { |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + actor.hide(); |
| 27 | + |
| 28 | + if (dropdowns.indexOf(actor) < 0) |
| 29 | + { |
| 30 | + let connection = { |
| 31 | + object: actor, |
| 32 | + id: actor.connect('destroy', function() |
| 33 | + { |
| 34 | + let index; |
| 35 | + |
| 36 | + index = signalConnections.indexOf(connection); |
| 37 | + if (index >= 0) |
| 38 | + { |
| 39 | + signalConnections.splice(index, 1); |
| 40 | + } |
| 41 | + |
| 42 | + index = dropdowns.indexOf(actor); |
| 43 | + if (index >= 0) |
| 44 | + { |
| 45 | + dropdowns.splice(index, 1); |
| 46 | + } |
| 47 | + }) |
| 48 | + }; |
| 49 | + signalConnections.push(connection); |
| 50 | + dropdowns.push(actor); |
| 51 | + } |
| 52 | + |
| 53 | + return true; |
12 | 54 | } |
13 | 55 |
|
14 | | -function enable() |
| 56 | +/** |
| 57 | + * Similar function to _recursiveApply(), but intended for containers. |
| 58 | + */ |
| 59 | +function _recursiveApplyInternal(actor, depth) |
| 60 | +{ |
| 61 | + if (typeof actor.get_children === 'undefined') |
| 62 | + { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + let children = actor.get_children(); |
| 67 | + |
| 68 | + // If there are no children then it's possible that actor hasn't been fully initialized yet. |
| 69 | + // Shedule to check later. |
| 70 | + if (children.length == 0) |
| 71 | + { |
| 72 | + _scheduleApply(actor); |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + // Check actor immediate children before using recursion |
| 77 | + for (let index = 0; index < children.length; index++) |
| 78 | + { |
| 79 | + if (_apply(children[index])) |
| 80 | + { |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Check children recursively |
| 86 | + if (depth < MAX_RECURSE_DEPTH) |
| 87 | + { |
| 88 | + for (let index = 0; index < children.length; index++) |
| 89 | + { |
| 90 | + if (_recursiveApplyInternal(children[index], depth + 1)) |
| 91 | + { |
| 92 | + return true; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | +} |
| 99 | + |
| 100 | +function _scheduleApply(actor) |
15 | 101 | { |
16 | | - edit(true); |
17 | | - _extensionChangedId = ExtensionSystem.connect('extension-state-changed', onExtensionStateChanged); |
| 102 | + let actorAddedId, destroyId, timeoutId; |
| 103 | + actorAddedId = actor.connect('actor-added', function(child) |
| 104 | + { |
| 105 | + if (_recursiveApply(child)) |
| 106 | + { |
| 107 | + actor.disconnect(actorAddedId); |
| 108 | + actor.disconnect(destroyId); |
| 109 | + Mainloop.source_remove(timeoutId); |
| 110 | + actorAddedId = destroyId = timeoutId = 0; |
| 111 | + } |
| 112 | + }); |
| 113 | + destroyId = actor.connect('destroy', function() |
| 114 | + { |
| 115 | + if (timeoutId != 0) { |
| 116 | + Mainloop.source_remove(timeoutId); |
| 117 | + timeoutId = 0; |
| 118 | + } |
| 119 | + }); |
| 120 | + timeoutId = Mainloop.idle_add(function() |
| 121 | + { |
| 122 | + actor.disconnect(actorAddedId); |
| 123 | + actor.disconnect(destroyId); |
| 124 | + actorAddedId = destroyId = timeoutId = 0; |
| 125 | + return GLib.SOURCE_REMOVE; |
| 126 | + }); |
18 | 127 | } |
19 | 128 |
|
20 | | -function disable() |
| 129 | +function _recursiveApply(actor) |
21 | 130 | { |
22 | | - ExtensionSystem.disconnect(_extensionChangedId); |
23 | | - edit(false); |
| 131 | + return _apply(actor) || _recursiveApplyInternal(actor, 0); |
24 | 132 | } |
25 | 133 |
|
26 | | -function onExtensionStateChanged() |
| 134 | +function init() |
27 | 135 | { |
28 | | - edit(true); |
| 136 | + // no initialization required |
29 | 137 | } |
30 | 138 |
|
31 | | -function edit(hide) |
| 139 | +function enable() |
32 | 140 | { |
33 | | - for (let id in Main.panel.statusArea) |
34 | | - { |
35 | | - let item = Main.panel.statusArea[id]; |
36 | | - if (typeof item.actor !== 'undefined') |
37 | | - { |
38 | | - recursiveEdit(item.actor, hide); |
39 | | - } |
40 | | - } |
| 141 | + Main.panel.actor.get_children().forEach( |
| 142 | + function(actor) |
| 143 | + { |
| 144 | + signalConnections.push({ |
| 145 | + object: actor, |
| 146 | + id: actor.connect('actor-added', _recursiveApply) |
| 147 | + }); |
| 148 | + |
| 149 | + actor.get_children().forEach(_recursiveApply); |
| 150 | + }); |
41 | 151 | } |
42 | 152 |
|
43 | | -function recursiveEdit(widget, hide) |
| 153 | +function disable() |
44 | 154 | { |
45 | | - if (widget.text === '\u25BE' || widget.text === '\u25B4' || // regular text drop down arrow (3.10) |
46 | | - (widget.has_style_class_name && widget.has_style_class_name('popup-menu-arrow'))) // image drop down arrow (3.12) |
| 155 | + while (signalConnections.length > 0) |
47 | 156 | { |
48 | | - if (hide) |
49 | | - { |
50 | | - widget.hide(); |
51 | | - } |
52 | | - else |
53 | | - { |
54 | | - widget.show(); |
55 | | - } |
56 | | - |
57 | | - return; |
| 157 | + let connection = signalConnections.pop(); |
| 158 | + connection.object.disconnect(connection.id); |
58 | 159 | } |
59 | | - |
60 | | - if (typeof widget.get_children !== 'undefined') |
| 160 | + |
| 161 | + while (dropdowns.length > 0) |
61 | 162 | { |
62 | | - widget.get_children().forEach(function(child) { recursiveEdit(child, hide); }); |
| 163 | + let actor = dropdowns.pop(); |
| 164 | + actor.show(); |
63 | 165 | } |
64 | 166 | } |
0 commit comments