aboutsummaryrefslogtreecommitdiffstats
path: root/WarframeClock/OverlayWindow.xaml.cs
diff options
context:
space:
mode:
Diffstat (limited to 'WarframeClock/OverlayWindow.xaml.cs')
-rw-r--r--WarframeClock/OverlayWindow.xaml.cs76
1 files changed, 35 insertions, 41 deletions
diff --git a/WarframeClock/OverlayWindow.xaml.cs b/WarframeClock/OverlayWindow.xaml.cs
index e019dde..520828e 100644
--- a/WarframeClock/OverlayWindow.xaml.cs
+++ b/WarframeClock/OverlayWindow.xaml.cs
@@ -1,8 +1,8 @@
using System;
-using System.Threading;
-using System.Threading.Tasks;
+using System.Diagnostics;
using System.Windows;
using System.Windows.Interop;
+using System.Windows.Threading;
namespace WarframeClock
{
@@ -11,16 +11,11 @@ namespace WarframeClock
/// </summary>
public partial class OverlayWindow : Window
{
- private IntPtr windowHandle;
- private IntPtr targetWindowHandle;
+ private readonly IntPtr windowHandle;
+ private readonly DispatcherTimer updateTimer;
private bool xTopmost;
- private Native.Rect parentBounds = new Native.Rect()
- {
- Top = 0,
- Left = 0,
- Right = 300,
- Bottom = 300,
- };
+ private IntPtr targetWindowHandle;
+ private Native.Rect parentBounds;
public OverlayWindow()
{
@@ -28,20 +23,20 @@ namespace WarframeClock
windowHandle = new WindowInteropHelper(this).EnsureHandle();
xTopmost = false;
- var updateLoop = Task.Factory.StartNew(() =>
- {
- while (true)
- {
- Dispatcher.BeginInvoke(new Action(() => Update()));
- Thread.Sleep(100);
- }
- });
- System.Windows.Threading.Dispatcher.Run();
+ updateTimer = new DispatcherTimer(TimeSpan.FromMilliseconds(100), DispatcherPriority.Background,
+ UpdateTimerTick, Dispatcher.CurrentDispatcher);
+ updateTimer.Start();
}
- private void Update()
+ protected override void OnClosed(EventArgs e)
{
- if (!GetTargetWindow(out Native.Rect newBounds))
+ base.OnClosed(e);
+ updateTimer.Stop();
+ }
+
+ private void UpdateTimerTick(object sender, EventArgs e)
+ {
+ if (!GetTargetWindow(out var newBounds))
{
if (IsVisible)
{
@@ -79,9 +74,10 @@ namespace WarframeClock
0x0010 /* SWP_NOACTIVATE */ |
0x0200 /* SWP_NOOWNERZORDER */ |
0x0400 /* SWP_NOSENDCHANGING */;
- Native.SetWindowPos(windowHandle, xTopmost ? (IntPtr)(-1) : currentActiveWindow, 0, 0, 0, 0, flags);
+ Native.SetWindowPos(windowHandle, xTopmost ? (IntPtr) (-1) : currentActiveWindow, 0, 0, 0, 0, flags);
}
+ // Adjust window position
if (!IsVisible ||
parentBounds.Left != newBounds.Left || parentBounds.Right != newBounds.Right ||
parentBounds.Top != newBounds.Top || parentBounds.Bottom != newBounds.Bottom)
@@ -99,8 +95,7 @@ namespace WarframeClock
}
// Render text
- var infoText = $"{Clock.CetusExpiryString()} {Clock.VallisExpiryString()}";
- OverlayLabel.Content = infoText;
+ OverlayLabel.Content = Clock.OverlayString();
if (!IsVisible)
{
Logging("Show overlay");
@@ -110,10 +105,9 @@ namespace WarframeClock
private bool GetTargetWindow(out Native.Rect bounds)
{
- if (targetWindowHandle == IntPtr.Zero || Native.IsWindow(targetWindowHandle) == 0)
+ if (targetWindowHandle == IntPtr.Zero)
{
- targetWindowHandle = IntPtr.Zero;
- foreach (var process in System.Diagnostics.Process.GetProcesses())
+ foreach (var process in Process.GetProcesses())
{
if (targetWindowHandle == IntPtr.Zero &&
(process.ProcessName == "Warframe" || process.ProcessName == "Warframe.x64"))
@@ -124,7 +118,7 @@ namespace WarframeClock
if (targetWindowHandle != IntPtr.Zero)
{
Logging($"Found a process with name={process.ProcessName}; " +
- $"MainWindowHandle={targetWindowHandle}");
+ $"MainWindowHandle={targetWindowHandle}");
}
}
catch (Exception)
@@ -132,31 +126,31 @@ namespace WarframeClock
// Ignore errors; maybe the process is just exiting. Let's retry during the next cycle.
}
}
+
process.Dispose();
}
}
- if (targetWindowHandle != IntPtr.Zero)
- {
- if (Native.GetWindowRect(targetWindowHandle, out bounds) == 0)
- {
- Logging($"GetWindowRect failed; aborting this cycle");
- targetWindowHandle = IntPtr.Zero;
- return false;
- }
- return true;
- }
- else
+ if (targetWindowHandle == IntPtr.Zero)
{
// Suppress compile error; caller must not use the value
bounds = new Native.Rect();
return false;
}
+
+ if (!Native.GetWindowRect(targetWindowHandle, out bounds))
+ {
+ Logging($"GetWindowRect failed; aborting this cycle");
+ targetWindowHandle = IntPtr.Zero;
+ return false;
+ }
+
+ return true;
}
private void Logging(string message)
{
- Console.WriteLine($"ClockOverlay ({windowHandle}): {message}");
+ Console.WriteLine($"ClockOverlay: {message}");
}
}
}