-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Popup countdown before lockin only when user is idle for at least 3 s…
…econds (#68) * add UserActivity class * set default idle time to 3 seconds
- Loading branch information
Showing
3 changed files
with
103 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NeedABreak.Utils | ||
{ | ||
public class UserActivity | ||
{ | ||
private TimeSpan _idleTime; | ||
|
||
/// <summary> | ||
/// UserActivity constructor | ||
/// </summary> | ||
/// <param name="idleTime">time after which the user will be considered idle.</param> | ||
public UserActivity(TimeSpan idleTime) | ||
{ | ||
if (idleTime == TimeSpan.Zero) | ||
{ | ||
idleTime = TimeSpan.FromSeconds(3); | ||
} | ||
|
||
_idleTime = idleTime; | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Minor Code Smell", "S101:Types should be named in PascalCase", Justification = "<Pending>")] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "<Pending>")] | ||
public struct LASTINPUTINFO | ||
{ | ||
public uint cbSize; | ||
public uint dwTime; | ||
} | ||
|
||
[DllImport("user32.dll")] | ||
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); | ||
|
||
public static TimeSpan GetInactiveTime() | ||
{ | ||
LASTINPUTINFO info = new LASTINPUTINFO(); | ||
info.cbSize = (uint)Marshal.SizeOf(info); | ||
|
||
if (GetLastInputInfo(ref info)) | ||
{ | ||
return TimeSpan.FromMilliseconds(Environment.TickCount - info.dwTime); | ||
} | ||
else | ||
{ | ||
return TimeSpan.Zero; | ||
} | ||
} | ||
|
||
public async Task WaitForUserToBeIdleAsync() | ||
{ | ||
TimeSpan inactiveTime = GetInactiveTime(); | ||
|
||
while (inactiveTime < _idleTime) | ||
{ | ||
await Task.Delay(1000); | ||
inactiveTime = GetInactiveTime(); | ||
} | ||
} | ||
} | ||
} |