~wasta-linux/wasta-core-wily/master

« back to all changes in this revision

Viewing changes to install-files/extensions/Applications_Overview_Tooltip_NG@Karlitos/extension.js

  • Committer: Rik Shaw
  • Date: 2015-11-01 13:28:40 UTC
  • Revision ID: git-v1:59c62c9b2e4f4f1cf62db1f5dc1cf630feb99933
initial 15.10 commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
const Main = imports.ui.main;
 
2
const Mainloop = imports.mainloop;
 
3
const St = imports.gi.St;
 
4
const Tweener = imports.ui.tweener;
 
5
const Gio = imports.gi.Gio
 
6
const ExtensionUtils = imports.misc.extensionUtils;
 
7
const Pango = imports.gi.Pango;
 
8
 
 
9
// get current extension
 
10
const extension = imports.misc.extensionUtils.getCurrentExtension();
 
11
 
 
12
// used to restore monkey patched function on disable
 
13
let _old_addItem = null;
 
14
// used to disconnect events on disable
 
15
let _tooltips = null;
 
16
// id of timer waiting for start
 
17
let _labelTimeoutId = 0;
 
18
// id of last (cancellable) timer
 
19
let _resetHoverTimeoutId = 0;
 
20
// actor for displaying the tooltip (or null)
 
21
let _label = null;
 
22
// self explainatory
 
23
let _labelShowing = false;
 
24
 
 
25
// stores settings from the schema
 
26
let _settings;
 
27
function init() {
 
28
  const GioSSS = Gio.SettingsSchemaSource;
 
29
 
 
30
  let schemaSource = GioSSS.new_from_directory(extension.path + "/schemas",
 
31
                                               GioSSS.get_default(), false);
 
32
 
 
33
  let schemaObj = schemaSource.lookup(extension.metadata["settings-schema"], true);
 
34
  if(!schemaObj) {
 
35
    throw new Error("Schema " + extension.metadata["settings-schema"] + " could not be found for extension " +
 
36
                    extension.uuid + ". Please check your installation.");
 
37
  }
 
38
 
 
39
  _settings = new Gio.Settings({ settings_schema: schemaObj });
 
40
}
 
41
 
 
42
function _get_tooltip_label_show_time() { return (_settings.get_int("label-show-time")/100); }
 
43
function _get_tooltip_label_hide_time() { return (_settings.get_int("label-hide-time")/100); }
 
44
function _get_tooltip_hover_timeout() { return (_settings.get_int("hoover-timeout")); }
 
45
function _get_always_show_tooltip() { return (_settings.get_boolean("allways-show-tooltips")); }
 
46
function _get_show_app_description() { return (_settings.get_boolean("show-app-description")); }
 
47
function _get_tooltip_text_font_size() { return (_settings.get_int("font-size")); }
 
48
function _get_tooltip_text_max_width() { return (_settings.get_int("max-width")); }
 
49
  
 
50
  
 
51
function enable() {
 
52
  _tooltips = new Array();
 
53
  // Enabling tooltips after _appIcons has been populated
 
54
  let appIcons = Main.overview.viewSelector.appDisplay._views[1].view._items;
 
55
  //global.log("appIcons after enable",appIcons, Object.keys(appIcons).length);
 
56
  for (let i in appIcons) {
 
57
    _connect(appIcons[i].actor);
 
58
  }
 
59
  // monkeypatching for the load time and for the search overview tooltips
 
60
  _old_addItem = imports.ui.iconGrid.IconGrid.prototype.addItem;
 
61
  imports.ui.iconGrid.IconGrid.prototype.addItem = function(item, index){
 
62
    _connect(item.actor);
 
63
    // original part of the function I'm overwriting
 
64
    _old_addItem.apply(this, arguments);
 
65
  };
 
66
}
 
67
 
 
68
function disable() {
 
69
  //restore the function
 
70
  imports.ui.iconGrid.IconGrid.prototype.addItem = _old_addItem;
 
71
  for (let i = 0; i < _tooltips.length; i++) {
 
72
    //disconnect hover signals
 
73
    _tooltips[i].actor.disconnect(_tooltips[i].connection);
 
74
  }
 
75
  _tooltips=null;
 
76
}
 
77
 
 
78
function _onHover(actor){
 
79
  if (actor.get_hover()) {
 
80
    if (_labelTimeoutId == 0) {
 
81
      let timeout = _labelShowing ? 0 : _get_tooltip_hover_timeout();
 
82
      _labelTimeoutId = Mainloop.timeout_add(timeout,
 
83
                                             function() {
 
84
                                               _labelShowing = true;
 
85
                                               _showTooltip(actor);
 
86
                                               return false;
 
87
                                             }
 
88
                                            );
 
89
      if (_resetHoverTimeoutId > 0) {
 
90
        Mainloop.source_remove(_resetHoverTimeoutId);
 
91
        _resetHoverTimeoutId = 0;
 
92
      }
 
93
    }
 
94
  } else {
 
95
    if (_labelTimeoutId > 0){
 
96
      Mainloop.source_remove(_labelTimeoutId);
 
97
    }
 
98
    _labelTimeoutId = 0;
 
99
    _hideTooltip();
 
100
    if (_labelShowing) {
 
101
      _resetHoverTimeoutId = Mainloop.timeout_add(_get_tooltip_hover_timeout(),
 
102
                                                  function() {
 
103
                                                    _labelShowing = false;
 
104
                                                    return false;
 
105
                                                  }
 
106
                                                 );
 
107
    }
 
108
  }
 
109
}
 
110
 
 
111
function _showTooltip(actor) {
 
112
  let icontext = '';
 
113
  let should_display = false;
 
114
  if (actor._delegate.app){
 
115
    //applications overview
 
116
    icontext = actor._delegate.app.get_name();
 
117
    if (_get_show_app_description()) {
 
118
      let appDescription = actor._delegate.app.get_description();
 
119
      // allow only valid description-text (not null)
 
120
      if (appDescription){
 
121
        icontext = icontext.concat(" :\n", appDescription);
 
122
      }
 
123
    }
 
124
    if (!_get_always_show_tooltip()){
 
125
      // will be displayed if elipsized/text cut-off (is_ellipsized)
 
126
      should_display = actor._delegate.icon.label.get_clutter_text().get_layout().is_ellipsized();
 
127
    } else {
 
128
      // show always
 
129
      should_display = true;
 
130
    }
 
131
  }/*else if (actor._delegate._content._delegate){
 
132
        //app and settings searchs results
 
133
        icontext = actor._delegate.metaInfo['name'];
 
134
        should_display = actor._delegate._content._delegate.icon.label.get_clutter_text().get_layout().is_ellipsized();
 
135
    }else if (actor._delegate._content.label_actor){
 
136
        //locations and other (generic) search results (wanda wouldn't work)
 
137
        icontext = actor._delegate.metaInfo['name'];
 
138
        should_display = actor._delegate._content.label_actor.get_clutter_text().get_layout().is_ellipsized();
 
139
    }*/
 
140
  else if (actor._delegate.hasOwnProperty('_folder')){
 
141
    // folder in the application overview
 
142
    icontext = 'Group: '.concat(actor._delegate['name']);
 
143
    if (!_get_always_show_tooltip()){
 
144
      // will be displayed if elipsized/text cut-off (is_ellipsized)
 
145
      should_display = actor._delegate.icon.label.get_clutter_text().get_layout().is_ellipsized();
 
146
    } else {
 
147
      // show always
 
148
      should_display = true;
 
149
    }
 
150
  }else{
 
151
    //app and settings searchs results
 
152
    icontext = actor._delegate.metaInfo['name'];
 
153
    if (!_get_always_show_tooltip()){
 
154
      // will be displayed if elipsized/text cut-off (is_ellipsized)
 
155
      should_display = actor._delegate.icon.label.get_clutter_text().get_layout().is_ellipsized();
 
156
    } else {
 
157
      // show always
 
158
      should_display = true;
 
159
    }
 
160
  }
 
161
 
 
162
  if (!should_display){
 
163
    return;
 
164
  }
 
165
 
 
166
  if (!_label) {
 
167
    _label = new St.Label({text: icontext});
 
168
    Main.uiGroup.add_actor(_label);
 
169
  }else{
 
170
    _label.text = icontext;
 
171
  }
 
172
 
 
173
  // (inline) styling
 
174
  _label.style = 'font-size: ' + _get_tooltip_text_font_size() + 'px; max-width:' + _get_tooltip_text_max_width() + 'px; font-weight: bold; color: ' + '#ffffff' +'; text-align: center; background-color: ' + 'rgba(10,10,10,0.6)' + '; border-radius: 5px; padding: 5px;';
 
175
  _label.clutter_text.line_wrap = true;
 
176
  _label.clutter_text.line_wrap_mode = Pango.WrapMode.WORD;
 
177
  _label.clutter_text.ellipsize = Pango.EllipsizeMode.NONE;
 
178
 
 
179
  [stageX, stageY] = actor.get_transformed_position();
 
180
  [iconWidth, iconHeight] = actor.get_transformed_size();
 
181
 
 
182
  let y = stageY + iconHeight + 5;
 
183
  let x = stageX - Math.round((_label.get_width() - iconWidth)/2);
 
184
  _label.opacity = 0;
 
185
  _label.set_position(x, y);
 
186
  Tweener.addTween(_label,{
 
187
    opacity: 255,
 
188
    time: _get_tooltip_label_show_time(),
 
189
    transition: 'easeOutQuad',
 
190
  });
 
191
}
 
192
 
 
193
function _hideTooltip() {
 
194
  if (_label){
 
195
    Tweener.addTween(_label, {
 
196
      opacity: 0,
 
197
      time: _get_tooltip_label_hide_time(),
 
198
      transition: 'easeOutQuad',
 
199
      onComplete: function() {
 
200
        Main.uiGroup.remove_actor(_label);
 
201
        _label = null;
 
202
      }
 
203
    });
 
204
  }
 
205
}
 
206
 
 
207
function _connect(actr){
 
208
  let con = actr.connect('notify::hover', _onHover);
 
209
  _tooltips.push({actor: actr, connection: con});
 
210
}