Skip to content

Commit

Permalink
Merge pull request AvaloniaUI#6982 from rmbadmin/master
Browse files Browse the repository at this point in the history
fixes: TrayIcon should be re-added when the Explorer is restarted
  • Loading branch information
maxkatz6 authored and Dan Walmsley committed May 10, 2022
1 parent 4dc6f60 commit 2980e94
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 37 deletions.
75 changes: 39 additions & 36 deletions src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,9 @@ public struct MINMAXINFO
[DllImport("user32.dll")]
public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip,
MonitorEnumDelegate lpfnEnum, IntPtr dwData);

public delegate bool MonitorEnumDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetDC(IntPtr hWnd);

Expand Down Expand Up @@ -1007,7 +1007,7 @@ public struct MINMAXINFO

public static uint GetWindowLong(IntPtr hWnd, int nIndex)
{
if(IntPtr.Size == 4)
if (IntPtr.Size == 4)
{
return GetWindowLong32b(hWnd, nIndex);
}
Expand All @@ -1034,7 +1034,7 @@ public static uint SetWindowLong(IntPtr hWnd, int nIndex, uint value)
return (uint)SetWindowLong64b(hWnd, nIndex, new IntPtr((uint)value)).ToInt32();
}
}

public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr handle)
{
if (IntPtr.Size == 4)
Expand Down Expand Up @@ -1068,14 +1068,14 @@ public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr handle)

[DllImport("user32.dll")]
public static extern bool InvalidateRect(IntPtr hWnd, RECT* lpRect, bool bErase);


[DllImport("user32.dll")]
public static extern bool ValidateRect(IntPtr hWnd, IntPtr lpRect);

[DllImport("user32.dll")]
public static extern bool IsWindow(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern bool IsWindowEnabled(IntPtr hWnd);

Expand All @@ -1102,22 +1102,25 @@ public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr handle)

[DllImport("user32")]
public static extern IntPtr GetMessageExtraInfo();

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "RegisterClassExW")]
public static extern ushort RegisterClassEx(ref WNDCLASSEX lpwcx);

[DllImport("user32.dll")]
public static extern void RegisterTouchWindow(IntPtr hWnd, int flags);

[DllImport("user32.dll")]
public static extern bool ReleaseCapture();

[DllImport("user32.dll", SetLastError = true)]
public static extern uint RegisterWindowMessage(string lpString);

[DllImport("user32.dll")]
public static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetActiveWindow();

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetActiveWindow(IntPtr hWnd);

Expand Down Expand Up @@ -1304,7 +1307,7 @@ public static IntPtr GetClassLongPtr(IntPtr hWnd, int nIndex)

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr LoadLibrary(string fileName);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr LoadLibraryEx(string fileName, IntPtr hFile, int flags);

Expand Down Expand Up @@ -1348,22 +1351,22 @@ public static IntPtr GetClassLongPtr(IntPtr hWnd, int nIndex)

[DllImport("user32.dll")]
public static extern IntPtr MonitorFromWindow(IntPtr hwnd, MONITOR dwFlags);

[DllImport("user32", EntryPoint = "GetMonitorInfoW", ExactSpelling = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetMonitorInfo([In] IntPtr hMonitor, ref MONITORINFO lpmi);

[DllImport("user32")]
public static extern unsafe bool GetTouchInputInfo(
IntPtr hTouchInput,
uint cInputs,
uint cInputs,
TOUCHINPUT* pInputs,
int cbSize
int cbSize
);

[DllImport("user32")]
public static extern bool CloseTouchInputHandle(IntPtr hTouchInput);

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PostMessageW")]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
Expand All @@ -1372,7 +1375,7 @@ int cbSize
public static extern int SetDIBitsToDevice(IntPtr hdc, int XDest, int YDest, uint
dwWidth, uint dwHeight, int XSrc, int YSrc, uint uStartScan, uint cScanLines,
IntPtr lpvBits, [In] ref BITMAPINFOHEADER lpbmi, uint fuColorUse);

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);
Expand All @@ -1387,27 +1390,27 @@ int cbSize

[DllImport("gdi32.dll")]
public static extern int ChoosePixelFormat(IntPtr hdc, ref PixelFormatDescriptor pfd);

[DllImport("gdi32.dll")]
public static extern int DescribePixelFormat(IntPtr hdc, ref PixelFormatDescriptor pfd);

[DllImport("gdi32.dll")]
public static extern int SetPixelFormat(IntPtr hdc, int iPixelFormat, ref PixelFormatDescriptor pfd);


[DllImport("gdi32.dll")]
public static extern int DescribePixelFormat(IntPtr hdc, int iPixelFormat, int bytes, ref PixelFormatDescriptor pfd);

[DllImport("gdi32.dll")]
public static extern bool SwapBuffers(IntPtr hdc);

[DllImport("opengl32.dll")]
public static extern IntPtr wglCreateContext(IntPtr hdc);

[DllImport("opengl32.dll")]
public static extern bool wglDeleteContext(IntPtr context);


[DllImport("opengl32.dll")]
public static extern bool wglMakeCurrent(IntPtr hdc, IntPtr context);

Expand All @@ -1428,9 +1431,9 @@ int cbSize
uint dwMaximumSizeLow,
string lpName);

[DllImport("msvcrt.dll", EntryPoint="memcpy", SetLastError = false, CallingConvention=CallingConvention.Cdecl)]
public static extern IntPtr CopyMemory(IntPtr dest, IntPtr src, UIntPtr count);
[DllImport("msvcrt.dll", EntryPoint = "memcpy", SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CopyMemory(IntPtr dest, IntPtr src, UIntPtr count);

[DllImport("ole32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern HRESULT RegisterDragDrop(IntPtr hwnd, IDropTarget target);

Expand Down Expand Up @@ -1472,10 +1475,10 @@ int cbSize

[DllImport("dwmapi.dll")]
public static extern void DwmFlush();

[DllImport("dwmapi.dll")]
public static extern bool DwmDefWindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam, ref IntPtr plResult);

[DllImport("dwmapi.dll")]
public static extern void DwmEnableBlurBehindWindow(IntPtr hwnd, ref DWM_BLURBEHIND blurBehind);

Expand Down Expand Up @@ -1542,16 +1545,16 @@ internal static Version RtlGetVersion()
throw new Exception("RtlGetVersion failed!");
}
}
[DllImport("kernel32", EntryPoint="WaitForMultipleObjectsEx", SetLastError = true, CharSet = CharSet.Auto)]

[DllImport("kernel32", EntryPoint = "WaitForMultipleObjectsEx", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int IntWaitForMultipleObjectsEx(int nCount, IntPtr[] pHandles, bool bWaitAll, int dwMilliseconds, bool bAlertable);

public const int WAIT_FAILED = unchecked((int)0xFFFFFFFF);

internal static int WaitForMultipleObjectsEx(int nCount, IntPtr[] pHandles, bool bWaitAll, int dwMilliseconds, bool bAlertable)
{
int result = IntWaitForMultipleObjectsEx(nCount, pHandles, bWaitAll, dwMilliseconds, bAlertable);
if(result == WAIT_FAILED)
if (result == WAIT_FAILED)
{
throw new Win32Exception();
}
Expand Down Expand Up @@ -1699,7 +1702,7 @@ internal enum AccentFlags
DrawLeftBorder = 0x20,
DrawTopBorder = 0x40,
DrawRightBorder = 0x80,
DrawBottomBorder = 0x100,
DrawBottomBorder = 0x100,
}

[StructLayout(LayoutKind.Sequential)]
Expand Down Expand Up @@ -1767,9 +1770,9 @@ public enum MONITOR_DPI_TYPE
MDT_ANGULAR_DPI = 1,
MDT_RAW_DPI = 2,
MDT_DEFAULT = MDT_EFFECTIVE_DPI
}
}

public enum ClipboardFormat
public enum ClipboardFormat
{
/// <summary>
/// Text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals the end of the data. Use this format for ANSI text.
Expand Down Expand Up @@ -1820,7 +1823,7 @@ public struct POINT
public int X;
public int Y;
}

public struct SIZE
{
public int X;
Expand Down Expand Up @@ -2021,7 +2024,7 @@ public enum OpenFileNameFlags
OFN_NOREADONLYRETURN = 0x00008000,
OFN_OVERWRITEPROMPT = 0x00000002
}

public enum HRESULT : uint
{
S_FALSE = 0x0001,
Expand Down
15 changes: 14 additions & 1 deletion src/Windows/Avalonia.Win32/TrayIconImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class TrayIconImpl : ITrayIconImpl
private readonly Win32NativeToManagedMenuExporter _exporter;
private static readonly Dictionary<int, TrayIconImpl> s_trayIcons = new Dictionary<int, TrayIconImpl>();
private bool _disposedValue;
private static readonly uint WM_TASKBARCREATED = UnmanagedMethods.RegisterWindowMessage("TaskbarCreated");

public TrayIconImpl()
{
Expand All @@ -44,6 +45,18 @@ internal static void ProcWnd(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam
{
s_trayIcons[wParam.ToInt32()].WndProc(hWnd, msg, wParam, lParam);
}

if (msg == WM_TASKBARCREATED)
{
foreach (var tray in s_trayIcons.Values)
{
if (tray._iconAdded)
{
tray.UpdateIcon(true);
tray.UpdateIcon();
}
}
}
}

public void SetIcon(IWindowIconImpl? icon)
Expand Down Expand Up @@ -145,7 +158,7 @@ private void OnRightClicked()
private enum CustomWindowsMessage : uint
{
WM_TRAYICON = WindowsMessage.WM_APP + 1024,
WM_TRAYMOUSE = WindowsMessage.WM_USER + 1024
WM_TRAYMOUSE = WindowsMessage.WM_USER + 1024,
}

private class TrayIconMenuFlyoutPresenter : MenuFlyoutPresenter, IStyleable
Expand Down

0 comments on commit 2980e94

Please sign in to comment.