Skip to content

Commit

Permalink
Code cleanup (#585)
Browse files Browse the repository at this point in the history
1.  Moved the IsBackground property assignment into the object initializer for the Thread object.

2. Replaced e.Args[e.Args.Length - 1] with e.Args[^1]

3. Added a conditional check to see if GetWindowThreadProcessId returns 0, which indicates failure. If it fails, throw a Win32Exception with the last Win32 error.

4. Removed unused assignment to the variable process

5. Changed the return type of the ConfigureServices method from IServiceProvider to ServiceProvider. It is more specific and faster.

6. Changed notifyIcon to _notifyIcon according to private var naming scheme.

7. Added the CharSet = CharSet.Unicode attribute to the DllImport declarations to specify that the string arguments should be marshaled as Unicode.
  • Loading branch information
DinuruSeniya authored Feb 24, 2025
1 parent ee51298 commit cf8f1ec
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions Wino.Server/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ namespace Wino.Server;
/// </summary>
public partial class App : Application
{
[DllImport("user32.dll", SetLastError = true)]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll", SetLastError = true)]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

private const string FRAME_WINDOW = "ApplicationFrameWindow";
Expand All @@ -54,13 +54,13 @@ public partial class App : Application

public WinoAppType WinoServerType { get; private set; }

private TaskbarIcon? notifyIcon;
private TaskbarIcon? _notifyIcon;
private static Mutex _mutex = null;
private EventWaitHandle _eventWaitHandle;

public IServiceProvider Services { get; private set; }

private IServiceProvider ConfigureServices()
private ServiceProvider ConfigureServices()
{
var services = new ServiceCollection();

Expand Down Expand Up @@ -150,8 +150,6 @@ private IntPtr FindUWPClientWindowHandle()
{
string processName = WinoServerType == WinoAppType.Mail ? "Wino.Mail" : "Wino.Calendar";

var processs = Process.GetProcesses();

var proc = Process.GetProcessesByName(processName).FirstOrDefault() ?? throw new Exception($"{processName} client is not running.");

for (IntPtr appWindow = FindWindowEx(IntPtr.Zero, IntPtr.Zero, FRAME_WINDOW, null); appWindow != IntPtr.Zero;
Expand All @@ -160,7 +158,10 @@ private IntPtr FindUWPClientWindowHandle()
IntPtr coreWindow = FindWindowEx(appWindow, IntPtr.Zero, "Windows.UI.Core.CoreWindow", null);
if (coreWindow != IntPtr.Zero)
{
GetWindowThreadProcessId(coreWindow, out var corePid);
if (GetWindowThreadProcessId(coreWindow, out var corePid) == 0)
{
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
}
if (corePid == proc.Id)
{
return appWindow;
Expand All @@ -175,7 +176,7 @@ protected override async void OnStartup(StartupEventArgs e)
{
// Same server code runs for both Mail and Calendar.

string winoAppTypeParameter = e.Args.Length > 0 ? e.Args[e.Args.Length - 1] : "Mail";
string winoAppTypeParameter = e.Args.Length > 0 ? e.Args[^1] : "Mail";

WinoServerType = winoAppTypeParameter == "Mail" ? WinoAppType.Mail : WinoAppType.Calendar;

Expand All @@ -201,20 +202,21 @@ protected override async void OnStartup(StartupEventArgs e)
{
while (_eventWaitHandle.WaitOne())
{
if (notifyIcon == null) return;
if (_notifyIcon == null) return;

Current.Dispatcher.BeginInvoke(async () =>
{
if (notifyIcon.DataContext is ServerViewModel trayIconViewModel)
if (_notifyIcon.DataContext is ServerViewModel trayIconViewModel)
{
await trayIconViewModel.ReconnectAsync();
}
});
}
});

// It is important mark it as background otherwise it will prevent app from exiting.
thread.IsBackground = true;
})
{
// It is important mark it as background otherwise it will prevent app from exiting.
IsBackground = true
};
thread.Start();

Services = ConfigureServices();
Expand All @@ -224,9 +226,9 @@ protected override async void OnStartup(StartupEventArgs e)
var serverViewModel = await InitializeNewServerAsync();

// Create taskbar icon for the new server.
notifyIcon = (TaskbarIcon)FindResource(NotifyIconResourceKey);
notifyIcon.DataContext = serverViewModel;
notifyIcon.ForceCreate(enablesEfficiencyMode: true);
_notifyIcon = (TaskbarIcon)FindResource(NotifyIconResourceKey);
_notifyIcon.DataContext = serverViewModel;
_notifyIcon.ForceCreate(enablesEfficiencyMode: true);

// Hide the icon if user has set it to invisible.
var preferencesService = Services.GetService<IPreferencesService>();
Expand All @@ -250,17 +252,17 @@ protected override async void OnStartup(StartupEventArgs e)

protected override void OnExit(ExitEventArgs e)
{
notifyIcon?.Dispose();
_notifyIcon?.Dispose();
base.OnExit(e);
}

public void ChangeNotifyIconVisiblity(bool isVisible)
{
if (notifyIcon == null) return;
if (_notifyIcon == null) return;

Current.Dispatcher.BeginInvoke(() =>
{
notifyIcon.Visibility = isVisible ? Visibility.Visible : Visibility.Collapsed;
_notifyIcon.Visibility = isVisible ? Visibility.Visible : Visibility.Collapsed;
});
}
}

0 comments on commit cf8f1ec

Please sign in to comment.