Skip to content

Commit

Permalink
Changed namespaces to file-scoped.
Browse files Browse the repository at this point in the history
  • Loading branch information
skst committed Apr 30, 2024
1 parent 4a1f0d8 commit 8d12b44
Show file tree
Hide file tree
Showing 8 changed files with 929 additions and 936 deletions.
137 changes: 68 additions & 69 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,97 +6,96 @@
using System.Threading;
using System.Windows;

namespace Sandman
namespace Sandman;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public static readonly string ExecutableFolder = string.Empty;
public StringBuilder ConsoleLog { get; set; } = new StringBuilder();
public static readonly string ExecutableFolder = string.Empty;
public StringBuilder ConsoleLog { get; set; } = new StringBuilder();

private readonly MyLibrary.NotificationIcon _notificationIcon;
private readonly Shared.NotificationIcon _notificationIcon;


static MainWindow()
{
var asm = System.Reflection.Assembly.GetEntryAssembly();
if (asm is not null)
{
ExecutableFolder = Path.GetDirectoryName(asm.Location) ?? string.Empty;
}
}
public MainWindow()
static MainWindow()
{
var asm = System.Reflection.Assembly.GetEntryAssembly();
if (asm is not null)
{
// remove original default trace listener
Trace.Listeners.Clear();

// form path to log file
string pathLog = Path.Combine(ExecutableFolder, "Sandman.log");

// add new default listener and set the filename
DefaultTraceListener listener = new DefaultTraceListener
{
LogFileName = pathLog
};
Trace.Listeners.Add(listener);
Trace.AutoFlush = true;

/*
* Set up notification area icon
*/
_notificationIcon = new MyLibrary.NotificationIcon(this, Properties.Resources.Sandman, nameof(Sandman), menuItems: []);

InitializeComponent();
DataContext = this;
ExecutableFolder = Path.GetDirectoryName(asm.Location) ?? string.Empty;
}
}
public MainWindow()
{
// remove original default trace listener
Trace.Listeners.Clear();

// form path to log file
string pathLog = Path.Combine(ExecutableFolder, "Sandman.log");

private async void Window_Loaded(object sender, RoutedEventArgs e)
// add new default listener and set the filename
DefaultTraceListener listener = new DefaultTraceListener
{
await WatchWMC.StartAsync(this).ConfigureAwait(continueOnCapturedContext: false);
}
LogFileName = pathLog
};
Trace.Listeners.Add(listener);
Trace.AutoFlush = true;

/*
* Set up notification area icon
*/
_notificationIcon = new Shared.NotificationIcon(this, Properties.Resources.Sandman, nameof(Sandman), menuItems: []);

InitializeComponent();
DataContext = this;
}


private void Window_Closing(object sender, CancelEventArgs e)
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
await WatchWMC.StartAsync(this).ConfigureAwait(continueOnCapturedContext: false);
}


private void Window_Closing(object sender, CancelEventArgs e)
{
if (_notificationIcon.ShouldClose())
{
if (_notificationIcon.ShouldClose())
{
Application.Current.Shutdown();
return;
}

e.Cancel = true;
Hide();
Trace.Flush();
Application.Current.Shutdown();
return;
}

e.Cancel = true;
Hide();
Trace.Flush();
}

public void WriteOutput(string s)
{
string timestampedMessage = $"[{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss}] (Thread {Thread.CurrentThread.ManagedThreadId:00}) {s}";
Trace.TraceInformation(timestampedMessage);
ConsoleLog.AppendLine(timestampedMessage);

RaisePropertyChanged(nameof(ConsoleLog));
}
public void WriteOutput(string s)
{
string timestampedMessage = $"[{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss}] (Thread {Thread.CurrentThread.ManagedThreadId:00}) {s}";
Trace.TraceInformation(timestampedMessage);
ConsoleLog.AppendLine(timestampedMessage);

RaisePropertyChanged(nameof(ConsoleLog));
}

// Boilerplate code
#region IRaisePropertyChanged

#region INotifyPropertyChanged
// Boilerplate code
#region IRaisePropertyChanged

public event PropertyChangedEventHandler? PropertyChanged;
#region INotifyPropertyChanged

#endregion INotifyPropertyChanged
public event PropertyChangedEventHandler? PropertyChanged;

public void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion INotifyPropertyChanged

#endregion IRaisePropertyChanged
public void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

#endregion IRaisePropertyChanged
}
73 changes: 36 additions & 37 deletions NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
using System;
using System.Runtime.InteropServices;

namespace Sandman
namespace Sandman;

public class NativeMethods
{
public class NativeMethods
public static TimeSpan GetTimeSinceLastActivity()
{
public static TimeSpan GetTimeSinceLastActivity()
LASTINPUTINFO lastInputInfo = LASTINPUTINFO.Create();
if (!GetLastInputInfo(ref lastInputInfo))
{
LASTINPUTINFO lastInputInfo = LASTINPUTINFO.Create();
if (!GetLastInputInfo(ref lastInputInfo))
{
return TimeSpan.Zero;
}

// Even though it says "ticks," it's really milliseconds.
/// <see cref="https://docs.microsoft.com/en-us/dotnet/api/system.environment.tickcount"/>
uint msecEnvTicks = (uint)Environment.TickCount;
uint lastInputTick = lastInputInfo.dwTime;
uint idleTime = msecEnvTicks - lastInputTick;
return TimeSpan.FromMilliseconds(idleTime);
return TimeSpan.Zero;
}

// Even though it says "ticks," it's really milliseconds.
/// <see cref="https://docs.microsoft.com/en-us/dotnet/api/system.environment.tickcount"/>
uint msecEnvTicks = (uint)Environment.TickCount;
uint lastInputTick = lastInputInfo.dwTime;
uint idleTime = msecEnvTicks - lastInputTick;
return TimeSpan.FromMilliseconds(idleTime);
}

/// <see cref="https://www.pinvoke.net/default.aspx/Structures/LASTINPUTINFO.html"/>
[StructLayout(LayoutKind.Sequential)]
private struct LASTINPUTINFO
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));

[MarshalAs(UnmanagedType.U4)]
public UInt32 cbSize;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dwTime; // Even though it says "ticks," it's really milliseconds.

public static LASTINPUTINFO Create() => new LASTINPUTINFO()
{
cbSize = (uint)SizeOf,
dwTime = 0,
};
}

/// <see cref="https://www.pinvoke.net/default.aspx/user32.GetLastInputInfo"/>
/// <param name="plii"></param>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
/// <see cref="https://www.pinvoke.net/default.aspx/Structures/LASTINPUTINFO.html"/>
[StructLayout(LayoutKind.Sequential)]
private struct LASTINPUTINFO
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));

[MarshalAs(UnmanagedType.U4)]
public UInt32 cbSize;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dwTime; // Even though it says "ticks," it's really milliseconds.

public static LASTINPUTINFO Create() => new LASTINPUTINFO()
{
cbSize = (uint)SizeOf,
dwTime = 0,
};
}

/// <see cref="https://www.pinvoke.net/default.aspx/user32.GetLastInputInfo"/>
/// <param name="plii"></param>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
}
Loading

0 comments on commit 8d12b44

Please sign in to comment.