Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Add option to shuffle URL display order
Browse files Browse the repository at this point in the history
  • Loading branch information
cwc committed Nov 4, 2015
1 parent 0baf20c commit 1445d2b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
19 changes: 16 additions & 3 deletions PreferencesForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion PreferencesForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ public partial class PreferencesForm : Form
public const string URL_PREF = "Url";
public const string CLOSE_ON_ACTIVITY_PREF = "CloseOnActivity";
public const string INTERVAL_PREF = "RotationInterval";
public const string RANDOMIZE_PREF = "RandomOrder";

public const string URL_PREF_DEFAULT = "https://www.google.com/trends/hottrends/visualize?nrow=5&ncol=5 https://screensaver.twingly.com/ http://map.ipviking.com/";
public const string URL_PREF_DEFAULT = "https://www.google.com/trends/hottrends/visualize?nrow=5&ncol=5 https://screensaver.twingly.com/";
public const string CLOSE_ON_ACTIVITY_PREF_DEFAULT = "True";
public const string INTERVAL_PREF_DEFAULT = "30";
public const string RANDOMIZE_PREF_DEFAULT = "False";

private ContextMenuStrip urlsContextMenu;

public PreferencesForm()
Expand Down Expand Up @@ -88,6 +91,7 @@ private void PreferencesForm_Load(object sender, EventArgs e)
loadUrls(reg);
cbCloseOnActivity.Checked = Boolean.Parse((string)reg.GetValue(CLOSE_ON_ACTIVITY_PREF, CLOSE_ON_ACTIVITY_PREF_DEFAULT));
nudRotationInterval.Value = int.Parse((string)reg.GetValue(INTERVAL_PREF, INTERVAL_PREF_DEFAULT));
cbRandomize.Checked = Boolean.Parse((string)reg.GetValue(RANDOMIZE_PREF, RANDOMIZE_PREF_DEFAULT));
reg.Close();
}

Expand All @@ -111,6 +115,7 @@ protected override void OnClosed(EventArgs e)
saveUrls(reg);
reg.SetValue(CLOSE_ON_ACTIVITY_PREF, cbCloseOnActivity.Checked);
reg.SetValue(INTERVAL_PREF, nudRotationInterval.Value);
reg.SetValue(RANDOMIZE_PREF, cbRandomize.Checked);
reg.Close();
}

Expand Down
4 changes: 2 additions & 2 deletions PreferencesForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
40 changes: 33 additions & 7 deletions ScreensaverForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ public partial class ScreensaverForm : Form
{
private DateTime StartTime;
private Timer timer;
private int currentSiteIndex = 0;
private int currentSiteIndex = -1;
private GlobalUserEventHandler userEventHandler;
private bool shuffleOrder;
private string[] urls;

[ThreadStatic]
private static Random random;

public ScreensaverForm()
{
Expand All @@ -32,29 +37,50 @@ public string[] Urls
{
get
{
RegistryKey reg = Registry.CurrentUser.CreateSubKey(Program.KEY);
var urls = ((string)reg.GetValue(PreferencesForm.URL_PREF, PreferencesForm.URL_PREF_DEFAULT)).Split(' ');
reg.Close();
if (urls == null)
{
RegistryKey reg = Registry.CurrentUser.CreateSubKey(Program.KEY);
urls = ((string)reg.GetValue(PreferencesForm.URL_PREF, PreferencesForm.URL_PREF_DEFAULT)).Split(' ');
reg.Close();
}

return urls;
}
}

private void ScreensaverForm_Load(object sender, EventArgs e)
{
BrowseTo(Urls[0]);

if (Urls.Length > 1)
{
RegistryKey reg = Registry.CurrentUser.CreateSubKey(Program.KEY);

currentSiteIndex = 0;
// Shuffle the URLs if necessary
shuffleOrder = Boolean.Parse((string)reg.GetValue(PreferencesForm.RANDOMIZE_PREF, PreferencesForm.RANDOMIZE_PREF_DEFAULT));
if (shuffleOrder)
{
random = new Random();

int n = urls.Length;
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 = int.Parse((string)reg.GetValue(PreferencesForm.INTERVAL_PREF, PreferencesForm.INTERVAL_PREF_DEFAULT)) * 1000;
timer.Tick += (s, ee) => RotateSite();
timer.Start();
}

// Display the first site in the list
RotateSite();

StartTime = DateTime.Now;
}

Expand Down

0 comments on commit 1445d2b

Please sign in to comment.