-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathInactivityHelper.cs
75 lines (65 loc) · 1.91 KB
/
InactivityHelper.cs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using System.Threading;
using Telegram.Native;
namespace Telegram.Common
{
public static class InactivityHelper
{
private static Timer _timer;
private static int _timeout = 60 * 1000;
private static int _lastTime = int.MinValue;
public static void Initialize(int timeout)
{
if (ApiInfo.IsDesktop)
{
if (timeout > 0)
{
_timeout = timeout * 1000;
if (_timer != null)
{
_timer.Change(0, 1000);
}
else
{
_timer = new Timer(OnTick, null, 0, 1000);
}
}
else if (_timer != null)
{
_timer.Dispose();
_timer = null;
}
}
}
private static void OnTick(object state)
{
var lastInput = NativeUtils.GetLastInputTime();
var idleTime = Environment.TickCount - lastInput;
if (idleTime >= _timeout && _lastTime < lastInput)
{
_lastTime = lastInput;
Detected?.Invoke(null, EventArgs.Empty);
}
}
public static event EventHandler Detected;
public static bool IsActive
{
get
{
var lastInput = NativeUtils.GetLastInputTime();
var idleTime = Environment.TickCount - lastInput;
if (idleTime >= 60 * 1000)
{
return false;
}
return true;
}
}
}
}