forked from cwc/web-page-screensaver
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScreensaverForm.cs
180 lines (148 loc) · 5.25 KB
/
ScreensaverForm.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;
using Microsoft.Web.WebView2;
namespace Miceli.Web_Page_Screensaver
{
public partial class ScreensaverForm : Form
{
private DateTime StartTime;
private Timer timer;
private int currentSiteIndex = -1;
private GlobalUserEventHandler userEventHandler;
private bool shuffleOrder;
private List<string> urls;
private PreferencesManager prefsManager = new PreferencesManager();
private int screenNum;
[ThreadStatic]
private static Random random;
public ScreensaverForm(int? screenNumber = null)
{
userEventHandler = new GlobalUserEventHandler();
userEventHandler.Event += new GlobalUserEventHandler.UserEvent(HandleUserActivity);
if (screenNumber == null) screenNum = prefsManager.EffectiveScreensList.FindIndex(s => s.IsPrimary);
else screenNum = (int)screenNumber;
InitializeComponent();
Cursor.Hide();
}
public List<string> Urls
{
get
{
if (urls == null)
{
urls = prefsManager.GetUrlsByScreen(screenNum);
}
return urls;
}
set => urls = value;
}
private void ScreensaverForm_Load(object sender, EventArgs e)
{
if (Urls.Any())
{
if (Urls.Count > 1)
{
// Shuffle the URLs if necessary
shuffleOrder = prefsManager.GetRandomizeFlagByScreen(screenNum);
if (shuffleOrder)
{
random = new Random();
int n = urls.Count;
while (n > 1)
{
n--;
int k = random.Next(n + 1);
var value = urls[k];
urls[k] = urls[n];
urls[n] = value;
}
}
// Set up timer to rotate to the next URL
timer = new Timer();
timer.Interval = prefsManager.GetRotationIntervalByScreen(screenNum) * 1000;
timer.Tick += (s, ee) => RotateSite();
timer.Start();
}
// Display the first site in the list
RotateSite();
StartTime = DateTime.Now;
}
}
private void BrowseTo(string url)
{
// Disable the user event handler while navigating
Application.RemoveMessageFilter(userEventHandler);
if (string.IsNullOrWhiteSpace(url))
{
webView1.Visible = false;
}
else
{
webView1.Visible = true;
try
{
Debug.WriteLine($"Navigating: {url}");
webView1.Source = new System.Uri(url);
}
catch
{
// This can happen if IE pops up a window that isn't closed before the next call to Navigate()
}
}
Application.AddMessageFilter(userEventHandler);
}
private void RotateSite()
{
currentSiteIndex++;
if (currentSiteIndex >= Urls.Count)
{
currentSiteIndex = 0;
}
var url = Urls[currentSiteIndex];
BrowseTo(url);
}
private void HandleUserActivity()
{
if (StartTime.AddSeconds(1) > DateTime.Now) return;
if (prefsManager.CloseOnActivity)
{
Close();
}
else
{
closeButton.Visible = true;
Cursor.Show();
}
}
private void closeButton_Click(object sender, EventArgs e)
{
Close();
}
}
public class GlobalUserEventHandler : IMessageFilter
{
public delegate void UserEvent();
private const int WM_MOUSEMOVE = 0x0200;
private const int WM_MBUTTONDBLCLK = 0x209;
private const int WM_KEYDOWN = 0x100;
private const int WM_KEYUP = 0x101;
// screensavers and especially multi-window apps can get spurrious WM_MOUSEMOVE events
// that don't actually involve any movement (cursor chnages and some mouse driver software
// can generate them, for example) - so we record the actual mouse position and compare against it for actual movement.
//private Point? lastMousePos;
public event UserEvent Event;
public bool PreFilterMessage(ref Message m)
{
Event?.Invoke();
return false;
}
}
}