Skip to content

Commit

Permalink
Fix compiler warnings from upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Mertsch committed Sep 12, 2024
1 parent ac24024 commit e93406e
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 61 deletions.
3 changes: 1 addition & 2 deletions Source/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<!-- https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#enforcecodestyleinbuild -->
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<!-- TODO: enable this -->
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion Source/Launchbar.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
# 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Launchbar", "Launchbar\Launchbar.csproj", "{55B26A32-A0F3-4E58-8B0F-D992D199069F}"
Expand Down
23 changes: 14 additions & 9 deletions Source/Launchbar/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

namespace Launchbar;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public sealed partial class App : Application
public sealed partial class App : Application, IDisposable
{
#if DEBUG
private const string MutexName = @"Global\LaunchbarSingleInstanceMutex(Debug)";
Expand All @@ -25,11 +22,7 @@ public sealed partial class App : Application

private static SplashScreen? splashScreen;

// ReSharper disable NotAccessedField.Local
#pragma warning disable IDE0052 // Remove unread private members - We need to keep the mutex alive during the lifetime of the app.
private readonly Mutex instanceMutex;
#pragma warning restore IDE0052 // Remove unread private members
// ReSharper restore NotAccessedField.Local

private WindowBar? barLeft;

Expand Down Expand Up @@ -109,7 +102,7 @@ public App()

setting.PropertyChanged += this.settingsPropertyChanged;

this.contextMenu = this.FindResource("contextMenuTemplate") as ContextMenu ?? throw new InvalidOperationException("Missing 'contextMenuTemplate'.");
this.contextMenu = this.FindResource("ContextMenuTemplate") as ContextMenu ?? throw new InvalidOperationException("Missing 'contextMenuTemplate'.");
this.forceContextMenuLayout();

try
Expand Down Expand Up @@ -286,4 +279,16 @@ public static ContextMenu RequestContextMenu()
// Attach to this object.
return app.contextMenu;
}

protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);

this.instanceMutex.Dispose();
}

public void Dispose()
{
this.instanceMutex.Dispose();
}
}
10 changes: 5 additions & 5 deletions Source/Launchbar/Launchbar.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
<TargetFramework>net8.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<OutputType>WinExe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<ApplicationIcon>Resources\Next.ico</ApplicationIcon>
<Version>5.2.0-preview.1</Version>
<Version>5.2.0-preview.2</Version>
<Authors>Launchbar</Authors>
<Description>RunIT to the next level.</Description>
<Copyright>Copyright © Mertsch $([System.DateTime]::UtcNow.Year)</Copyright>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<IsPublishable>true</IsPublishable>
<!-- https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#generateassemblyinfo -->
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="fm.Extensions.Wpf" Version="5.0.0" />
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0-eap1" />
<PackageReference Include="fm.Extensions.Wpf" Version="5.0.1" />
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0" />
<PackageReference Include="Markdig.Wpf" Version="0.5.0.1" />
<PackageReference Include="WpfScreenHelper" Version="2.1.0" />
<PackageReference Include="WpfScreenHelper" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Source/Launchbar/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public sealed class Menu : NotifyBase
{
#region Fields

private MenuEntryCollection entries;
private MenuEntryCollection? entries;

#endregion

/// <summary>
/// Gets or sets a list of menu entries.
/// </summary>
public MenuEntryCollection Entries
public MenuEntryCollection? Entries
{
get { return this.entries; }
set
Expand Down
16 changes: 5 additions & 11 deletions Source/Launchbar/MenuEntryAdvanced.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Text;
using Launchbar.Win32;
using System.Windows.Media;
using System.Xml.Serialization;
using Launchbar.Win32;

namespace Launchbar;

Expand Down Expand Up @@ -150,7 +149,7 @@ public void UpdateIcon()
{
if (p.IsValidFile) // Valid file?
{
newIcon = WinHelper.ExtractAssociatedIcon(p.PathAbsolute);
newIcon = WinHelper.ExtractAssociatedIcon(p.PathAbsolute!); // IsValidFile tests the path
newType = IconType.Custom;
}
else if (p.IsValidPath) // Valid dictionary?
Expand Down Expand Up @@ -181,18 +180,13 @@ public void ChooseIcon()
{
if (this is Program { IsValidFile: true } p) // When the program path is valid, use that path as default.
{
path = p.Path;
path = p.Path!; // IsValidFile tests the path
}
}

// We need to have a string that is long enough to handle a more complex path than
// the default one (more characters in length).
StringBuilder sb = new StringBuilder(path, 4096); // 4095 + null-char should be enough

// Methods returns one when pressing OK in the dialog.
if (SafeNativeMethods.PickIconDlg(nint.Zero, sb, (uint)sb.Capacity, ref index) == 1)
if (WinHelper.PickIconDialog(ref path, ref index))
{
this.IconPath = sb.ToString(); //save the information
this.IconPath = path;
this.IconIndex = index;
}
this.UpdateIcon();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.InteropServices;
using System.Security;
using System.Text;

// ReSharper disable InconsistentNaming
// ReSharper disable UnusedMember.Global
Expand All @@ -9,8 +7,7 @@

namespace Launchbar.Win32;

[SuppressUnmanagedCodeSecurity]
internal static partial class SafeNativeMethods
internal static partial class LibraryImport
{
internal const string User32 = @"user32.dll";

Expand Down Expand Up @@ -94,8 +91,8 @@ public static nint SetWindowLongPtr(nint hWnd, int nIndex, nint dwNewLong)
/// <param name="piIconIndex">[in, out] A pointer to an integer that, on entry, specified the index of
/// the initial selection. On exit, the integer specifies the index of the icon that was selected.</param>
/// <returns>Returns 1 if successful; otherwise, 0.</returns>
[DllImport(Shell32, EntryPoint = "PickIconDlg", CharSet = CharSet.Unicode)]
public static extern int PickIconDlg(nint hwnd, StringBuilder pszIconPath, uint cchIconPath, ref int piIconIndex);
[LibraryImport(Shell32, EntryPoint = "PickIconDlg", StringMarshalling = StringMarshalling.Utf16)]
public static partial int PickIconDlg(nint hwnd, [In, Out] char[] pszIconPath, uint cchIconPath, ref int piIconIndex);

/// <summary>
/// Destroys an icon and frees any memory the icon occupied.
Expand Down
63 changes: 39 additions & 24 deletions Source/Launchbar/Win32/WinHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ internal static class WinHelper
public static BitmapSource? ExtractAssociatedIcon(string path)
{
int i = 0;
nint hIcon = SafeNativeMethods.ExtractAssociatedIcon(nint.Zero, path, ref i);
nint hIcon = LibraryImport.ExtractAssociatedIcon(nint.Zero, path, ref i);
if (hIcon != nint.Zero)
{
BitmapSource bms = Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, null);
SafeNativeMethods.DestroyIcon(hIcon);
LibraryImport.DestroyIcon(hIcon);
return bms;
}
return null;
Expand All @@ -39,11 +39,11 @@ internal static class WinHelper
/// <returns>The extracted icon as <see cref="BitmapSource"/>.</returns>
public static BitmapSource? ExtractIcon(string path, int index)
{
nint hIcon = SafeNativeMethods.ExtractIcon(nint.Zero, path, (uint)index);
nint hIcon = LibraryImport.ExtractIcon(nint.Zero, path, (uint)index);
if (hIcon != nint.Zero)
{
BitmapSource bms = Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, null);
SafeNativeMethods.DestroyIcon(hIcon);
LibraryImport.DestroyIcon(hIcon);
return bms;
}
return null;
Expand All @@ -55,21 +55,21 @@ internal static class WinHelper
/// </summary>
public static void SendMouseButtonDown(MouseButton button)
{
SafeNativeMethods.MOUSEINPUT mi = new SafeNativeMethods.MOUSEINPUT
LibraryImport.MOUSEINPUT mi = new LibraryImport.MOUSEINPUT
{
dwFlags = button switch
{
MouseButton.Left => SafeNativeMethods.MOUSEINPUTFLAGS.MOUSEEVENTF_LEFTDOWN,
MouseButton.Right => SafeNativeMethods.MOUSEINPUTFLAGS.MOUSEEVENTF_RIGHTDOWN,
MouseButton.Left => LibraryImport.MOUSEINPUTFLAGS.MOUSEEVENTF_LEFTDOWN,
MouseButton.Right => LibraryImport.MOUSEINPUTFLAGS.MOUSEEVENTF_RIGHTDOWN,
_ => throw new NotSupportedException(),
},
};

SafeNativeMethods.MOUSEKEYBDHARDWAREINPUT mkhInput = new SafeNativeMethods.MOUSEKEYBDHARDWAREINPUT { mi = mi };
LibraryImport.MOUSEKEYBDHARDWAREINPUT mkhInput = new LibraryImport.MOUSEKEYBDHARDWAREINPUT { mi = mi };

SafeNativeMethods.INPUT input = new SafeNativeMethods.INPUT { type = SafeNativeMethods.INPUT_TYPE.MOUSE, mkhi = mkhInput };
LibraryImport.INPUT input = new LibraryImport.INPUT { type = LibraryImport.INPUT_TYPE.MOUSE, mkhi = mkhInput };

SafeNativeMethods.SendInput(1, ref input, Marshal.SizeOf(input));
LibraryImport.SendInput(1, ref input, Marshal.SizeOf(input));
}

/// <summary>
Expand All @@ -78,21 +78,21 @@ public static void SendMouseButtonDown(MouseButton button)
/// </summary>
public static void SendMouseButtonUp(MouseButton button)
{
SafeNativeMethods.MOUSEINPUT mi = new SafeNativeMethods.MOUSEINPUT
LibraryImport.MOUSEINPUT mi = new LibraryImport.MOUSEINPUT
{
dwFlags = button switch
{
MouseButton.Left => SafeNativeMethods.MOUSEINPUTFLAGS.MOUSEEVENTF_LEFTUP,
MouseButton.Right => SafeNativeMethods.MOUSEINPUTFLAGS.MOUSEEVENTF_RIGHTUP,
MouseButton.Left => LibraryImport.MOUSEINPUTFLAGS.MOUSEEVENTF_LEFTUP,
MouseButton.Right => LibraryImport.MOUSEINPUTFLAGS.MOUSEEVENTF_RIGHTUP,
_ => throw new NotSupportedException(),
},
};

SafeNativeMethods.MOUSEKEYBDHARDWAREINPUT mkhInput = new SafeNativeMethods.MOUSEKEYBDHARDWAREINPUT { mi = mi };
LibraryImport.MOUSEKEYBDHARDWAREINPUT mkhInput = new LibraryImport.MOUSEKEYBDHARDWAREINPUT { mi = mi };

SafeNativeMethods.INPUT input = new SafeNativeMethods.INPUT { type = SafeNativeMethods.INPUT_TYPE.MOUSE, mkhi = mkhInput };
LibraryImport.INPUT input = new LibraryImport.INPUT { type = LibraryImport.INPUT_TYPE.MOUSE, mkhi = mkhInput };

SafeNativeMethods.SendInput(1, ref input, Marshal.SizeOf(input));
LibraryImport.SendInput(1, ref input, Marshal.SizeOf(input));
}

/// <summary>
Expand All @@ -101,35 +101,50 @@ public static void SendMouseButtonUp(MouseButton button)
/// </summary>
public static void SendMouseMoveRelative(int x, int y)
{
SafeNativeMethods.MOUSEINPUT mi = new SafeNativeMethods.MOUSEINPUT
LibraryImport.MOUSEINPUT mi = new LibraryImport.MOUSEINPUT
{
dwFlags = SafeNativeMethods.MOUSEINPUTFLAGS.MOUSEEVENTF_MOVE,
dwFlags = LibraryImport.MOUSEINPUTFLAGS.MOUSEEVENTF_MOVE,
dx = x,
dy = y,
};

SafeNativeMethods.MOUSEKEYBDHARDWAREINPUT mkhInput = new SafeNativeMethods.MOUSEKEYBDHARDWAREINPUT
LibraryImport.MOUSEKEYBDHARDWAREINPUT mkhInput = new LibraryImport.MOUSEKEYBDHARDWAREINPUT
{
mi = mi,
};

SafeNativeMethods.INPUT input = new SafeNativeMethods.INPUT
LibraryImport.INPUT input = new LibraryImport.INPUT
{
type = SafeNativeMethods.INPUT_TYPE.MOUSE,
type = LibraryImport.INPUT_TYPE.MOUSE,
mkhi = mkhInput,
};

SafeNativeMethods.SendInput(1, ref input, Marshal.SizeOf(input));
LibraryImport.SendInput(1, ref input, Marshal.SizeOf(input));
}

public static void SetAsToolWindow(this Window window)
{
nint handle = new WindowInteropHelper(window).EnsureHandle();
nint oldFlags = SafeNativeMethods.GetWindowLongPtr(handle, GWL.GWL_EXSTYLE);
nint oldFlags = LibraryImport.GetWindowLongPtr(handle, GWL.GWL_EXSTYLE);
if (oldFlags != nint.Zero)
{
nint newFlags = new nint(oldFlags.ToInt64() | ExtendedWindowStyles.WS_EX_TOOLWINDOW);
SafeNativeMethods.SetWindowLongPtr(handle, GWL.GWL_EXSTYLE, newFlags);
LibraryImport.SetWindowLongPtr(handle, GWL.GWL_EXSTYLE, newFlags);
}
}

public static bool PickIconDialog(ref string path, ref int index)
{
// We need to have a string that is long enough to handle a more complex path than the default one (more characters in length).
char[] pathBuffer = new char[32 * 1024]; // https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
path.CopyTo(pathBuffer);

// Method returns one when pressing OK in the dialog.
if (LibraryImport.PickIconDlg(nint.Zero, pathBuffer, (uint)pathBuffer.Length, ref index) == 1)
{
path = new string(pathBuffer, 0, Array.IndexOf(pathBuffer, '\0')); // Extract string from null terminated string.
return true;
}
return false;
}
}
2 changes: 1 addition & 1 deletion Source/Launchbar/WindowSettings.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private void addMenuEntry(MenuEntry newEntry)
}
else
{
Settings.Default.Menu.Entries.Add(newEntry);
Settings.Default.Menu.Entries?.Add(newEntry);
}
}

Expand Down

0 comments on commit e93406e

Please sign in to comment.