Skip to content

Commit

Permalink
Added support to reset stopwatch by long pressing
Browse files Browse the repository at this point in the history
  • Loading branch information
BarRaider committed Jan 11, 2019
1 parent 0ae4f9e commit 0ad1e69
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
Binary file removed Install/com.barraider.stopwatch.streamDeckPlugin
Binary file not shown.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ A C# client library for the Elgato Stream Deck device.
This library uses the [streamdeck-client-csharp](https://github.com/TyrenDe/streamdeck-client-csharp) wrapper

## Current functionality
- New: Long press (2 seconds) on the key to reset the stopwatch
- Multiple Stopwatches suppport
- Stopping/Starting a watch
- Option to not reset the watch after it's restarted
Expand Down
19 changes: 19 additions & 0 deletions Stopwatch/PluginContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void Run(StreamDeckOptions options)
connection.OnConnected += Connection_OnConnected;
connection.OnDisconnected += Connection_OnDisconnected;
connection.OnKeyDown += Connection_OnKeyDown;
connection.OnKeyUp += Connection_OnKeyUp;
connection.OnWillAppear += Connection_OnWillAppear;
connection.OnWillDisappear += Connection_OnWillDisappear;

Expand Down Expand Up @@ -66,6 +67,24 @@ private async void Connection_OnKeyDown(object sender, StreamDeckEventReceivedEv
}
}

// Button released
private async void Connection_OnKeyUp(object sender, StreamDeckEventReceivedEventArgs<KeyUpEvent> e)
{
await instancesLock.WaitAsync();
try
{
if (instances.ContainsKey(e.Event.Context))
{
instances[e.Event.Context].KeyReleased();
}
}
finally
{
instancesLock.Release();
}
}


// Function runs every second, used to update UI
private async void RunTick()
{
Expand Down
44 changes: 35 additions & 9 deletions Stopwatch/StopwatchTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,15 @@ public async void SetSettingsAsync()
public streamdeck_client_csharp.StreamDeckConnection StreamDeckConnection { private get; set; }
}

#region Public properties

public long StopwatchSeconds { get; private set; }

#endregion

#region Private members

private const int RESET_COUNTER_KEYPRESS_LENGTH = 2;

private Timer tmrStopwatch;
private InspectorSettings settings;
private bool keyPressed = false;
private DateTime keyPressStart;
private long stopwatchSeconds;

#endregion

Expand All @@ -88,6 +87,10 @@ public StopwatchTimer(streamdeck_client_csharp.StreamDeckConnection connection,

public void TriggerStopwatch()
{
// Used for long press
keyPressStart = DateTime.Now;
keyPressed = true;

if (tmrStopwatch != null && tmrStopwatch.Enabled)
{
PauseStopwatch();
Expand All @@ -103,12 +106,21 @@ public void TriggerStopwatch()
}
}

public void KeyReleased()
{
keyPressed = false;
}

public string GetCurrentStopwatchValue()
{
long total, minutes, seconds, hours;
string delimiter = settings.Multiline ? "\n" : ":";

total = StopwatchSeconds;
// Stream Deck calls this function every second,
// so this is the best place to determine if we need to reset (versus the internal timer which may be paused)
CheckIfResetNeeded();

total = stopwatchSeconds;
minutes = total / 60;
seconds = total % 60;
hours = minutes / 60;
Expand Down Expand Up @@ -146,7 +158,7 @@ public void UpdateSettings(JObject payload)

private void ResetCounter()
{
StopwatchSeconds = 0;
stopwatchSeconds = 0;
}

private void ResumeStopwatch()
Expand All @@ -160,9 +172,23 @@ private void ResumeStopwatch()
tmrStopwatch.Start();
}

private void CheckIfResetNeeded()
{
if (!keyPressed)
{
return;
}

if ((DateTime.Now - keyPressStart).TotalSeconds > RESET_COUNTER_KEYPRESS_LENGTH)
{
PauseStopwatch();
ResetCounter();
}
}

private void TmrStopwatch_Elapsed(object sender, ElapsedEventArgs e)
{
StopwatchSeconds++;
stopwatchSeconds++;
}

private void PauseStopwatch()
Expand Down
2 changes: 1 addition & 1 deletion Stopwatch/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"Name": "Stopwatch",
"Icon": "Images/pluginIcon",
"URL": "https://github.com/BarRaider/streamdeck-stopwatch",
"Version": "0.5",
"Version": "0.6",
"CodePath": "stopwatch.exe",
"Category": "BarRaider",
"CategoryIcon": "Images/categoryIcon",
Expand Down

0 comments on commit 0ad1e69

Please sign in to comment.