Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WinoServer Code fixes #585

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
});
}
}