Skip to content

Commit

Permalink
Merge pull request #196 from Susko3/add-SDL_GetTrayEntries-helper
Browse files Browse the repository at this point in the history
Implement helper for `SDL_GetTrayEntries()`
  • Loading branch information
smoogipoo authored Jan 5, 2025
2 parents 3681588 + f988cb2 commit ab7c7f5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
7 changes: 7 additions & 0 deletions SDL3-CS.Tests/TestTray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public void TestBasic()
Assert.That(exit != null, SDL_GetError);
SetCallback(exit, () => running = false);

var entries = SDL_GetTrayEntries(RootMenu);
Assert.That(entries, Is.Not.Null, SDL_GetError);
Assert.That(entries!.Count, Is.EqualTo(3));

for (int i = 0; i < entries.Count; i++)
Console.WriteLine($"{i}. {SDL_GetTrayEntryLabel(entries[i]) ?? "<null>"}");

running = true;

while (running)
Expand Down
14 changes: 6 additions & 8 deletions SDL3-CS/SDL3/SDL_tray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ public enum SDL_TrayEntryFlags : UInt32

public static partial class SDL3
{
// The code below is currently incorrect because of https://github.com/libsdl-org/SDL/issues/11787.
// [MustDisposeResource]
// public static unsafe SDLOpaquePointerArray<SDL_TrayEntry>? SDL_GetTrayEntries(SDL_TrayMenu* menu)
// {
// int count;
// var array = SDL_GetTrayEntries(menu, &count);
// return SDLArray.CreateOpaque(array, count);
// }
public static unsafe SDLConstOpaquePointerArray<SDL_TrayEntry>? SDL_GetTrayEntries(SDL_TrayMenu* menu)
{
int count;
var array = SDL_GetTrayEntries(menu, &count);
return SDLArray.CreateConstOpaque(array, count);
}
}
}
9 changes: 9 additions & 0 deletions SDL3-CS/SDLArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,14 @@ internal static unsafe class SDLArray

return new SDLOpaquePointerArray<T>(array, count);
}

internal static SDLConstOpaquePointerArray<T>? CreateConstOpaque<T>(T** array, int count)
where T : unmanaged
{
if (array == null)
return null;

return new SDLConstOpaquePointerArray<T>(array, count);
}
}
}
32 changes: 32 additions & 0 deletions SDL3-CS/SDLConstOpaquePointerArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Diagnostics;

namespace SDL
{
public sealed unsafe class SDLConstOpaquePointerArray<T>
where T : unmanaged
{
private readonly T** array;
public readonly int Count;

internal SDLConstOpaquePointerArray(T** array, int count)
{
this.array = array;
Count = count;
}

public T* this[int index]
{
get
{
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
Debug.Assert(array[index] != null);
return array[index];
}
}
}
}

0 comments on commit ab7c7f5

Please sign in to comment.