summaryrefslogtreecommitdiff
path: root/apps/mail/modules/appIdleManager.js
blob: 3c457364b30a56b1576d7e8b1bd9df3cb30daaae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

this.EXPORTED_SYMBOLS = ['appIdleManager'];

Components.utils.import("resource://gre/modules/Services.jsm");

var Cc = Components.classes;
var Ci = Components.interfaces;

// This module provides a mechanism to turn window focus and blur events
// into app idle notifications. If we get a blur notification that is not
// followed by a focus notification in less than some small number of seconds,
// then we send a begin app idle notification.
// If we get a focus event, and we're app idle, then we send an end app idle
// notification.
// The notification topic is "mail:appIdle", the values are "idle", and "back"

var appIdleManager =
{
  _appIdle: false,
  _timerInterval: 5000, // 5 seconds ought to be plenty
  get _timer()
  {
    delete this._timer;
    return this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  },

  _timerCallback: function()
  {
    appIdleManager._appIdle = true;
    Services.obs.notifyObservers(null, "mail:appIdle", "idle");

  },

  onBlur: function()
  {
    appIdleManager._timer.initWithCallback(appIdleManager._timerCallback,
                                 appIdleManager._timerInterval,
                                 Ci.nsITimer.TYPE_ONE_SHOT);
   },

  onFocus: function()
  {
    appIdleManager._timer.cancel();
    if (appIdleManager._appIdle)
    {
      appIdleManager._appIdle = false;
      Services.obs.notifyObservers(null, "mail:appIdle", "back");
    }
  }
};