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

Implement helper for SDL_GetTrayEntries() #196

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
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
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 <[email protected]>. 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];
}
}
}
}