From 1445d2b99e5ad1463b875239f5823b9edea840e7 Mon Sep 17 00:00:00 2001 From: Cameron Currie Date: Tue, 3 Nov 2015 18:45:16 -0600 Subject: [PATCH] Add option to shuffle URL display order --- PreferencesForm.Designer.cs | 19 +++++++++++++++--- PreferencesForm.cs | 7 ++++++- PreferencesForm.resx | 4 ++-- ScreensaverForm.cs | 40 ++++++++++++++++++++++++++++++------- 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/PreferencesForm.Designer.cs b/PreferencesForm.Designer.cs index 54af3d2..6d9658c 100644 --- a/PreferencesForm.Designer.cs +++ b/PreferencesForm.Designer.cs @@ -39,6 +39,7 @@ private void InitializeComponent() this.lbUrls = new System.Windows.Forms.ListBox(); this.tbUrlToAdd = new System.Windows.Forms.TextBox(); this.addUrlButton = new System.Windows.Forms.Button(); + this.cbRandomize = new System.Windows.Forms.CheckBox(); ((System.ComponentModel.ISupportInitialize)(this.nudRotationInterval)).BeginInit(); this.SuspendLayout(); // @@ -98,7 +99,7 @@ private void InitializeComponent() this.cbCloseOnActivity.AutoSize = true; this.cbCloseOnActivity.Checked = true; this.cbCloseOnActivity.CheckState = System.Windows.Forms.CheckState.Checked; - this.cbCloseOnActivity.Location = new System.Drawing.Point(15, 258); + this.cbCloseOnActivity.Location = new System.Drawing.Point(15, 256); this.cbCloseOnActivity.Name = "cbCloseOnActivity"; this.cbCloseOnActivity.Size = new System.Drawing.Size(153, 17); this.cbCloseOnActivity.TabIndex = 6; @@ -107,7 +108,7 @@ private void InitializeComponent() // // nudRotationInterval // - this.nudRotationInterval.Location = new System.Drawing.Point(15, 232); + this.nudRotationInterval.Location = new System.Drawing.Point(15, 230); this.nudRotationInterval.Name = "nudRotationInterval"; this.nudRotationInterval.Size = new System.Drawing.Size(40, 20); this.nudRotationInterval.TabIndex = 7; @@ -120,7 +121,7 @@ private void InitializeComponent() // label3 // this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(61, 234); + this.label3.Location = new System.Drawing.Point(61, 232); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(142, 13); this.label3.TabIndex = 8; @@ -151,6 +152,16 @@ private void InitializeComponent() this.addUrlButton.UseVisualStyleBackColor = true; this.addUrlButton.Click += new System.EventHandler(this.addUrlButton_Click); // + // cbRandomize + // + this.cbRandomize.AutoSize = true; + this.cbRandomize.Location = new System.Drawing.Point(15, 279); + this.cbRandomize.Name = "cbRandomize"; + this.cbRandomize.Size = new System.Drawing.Size(121, 17); + this.cbRandomize.TabIndex = 12; + this.cbRandomize.Text = "Shuffle display order"; + this.cbRandomize.UseVisualStyleBackColor = true; + // // PreferencesForm // this.AcceptButton = this.okButton; @@ -158,6 +169,7 @@ private void InitializeComponent() this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.CancelButton = this.cancelButton; this.ClientSize = new System.Drawing.Size(277, 332); + this.Controls.Add(this.cbRandomize); this.Controls.Add(this.addUrlButton); this.Controls.Add(this.tbUrlToAdd); this.Controls.Add(this.lbUrls); @@ -195,5 +207,6 @@ private void InitializeComponent() private System.Windows.Forms.ListBox lbUrls; private System.Windows.Forms.TextBox tbUrlToAdd; private System.Windows.Forms.Button addUrlButton; + private System.Windows.Forms.CheckBox cbRandomize; } } \ No newline at end of file diff --git a/PreferencesForm.cs b/PreferencesForm.cs index e56bfbd..1e0038f 100644 --- a/PreferencesForm.cs +++ b/PreferencesForm.cs @@ -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() @@ -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(); } @@ -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(); } diff --git a/PreferencesForm.resx b/PreferencesForm.resx index ff31a6d..c7e0d4b 100644 --- a/PreferencesForm.resx +++ b/PreferencesForm.resx @@ -112,9 +112,9 @@ 2.0 - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 \ No newline at end of file diff --git a/ScreensaverForm.cs b/ScreensaverForm.cs index 2d80924..26362ef 100644 --- a/ScreensaverForm.cs +++ b/ScreensaverForm.cs @@ -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() { @@ -32,9 +37,12 @@ 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; } @@ -42,19 +50,37 @@ public string[] 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; }