Files
maui-linux/Services/GtkContextMenuService.cs
Dave Friedel 1f096c38dc Update with recovered code from VM binaries (Jan 1)
Recovered from decompiled OpenMaui.Controls.Linux.dll:
- SkiaShell.cs: FlyoutHeader, FlyoutFooter, scroll support (918 -> 1325 lines)
- X11Window.cs: Cursor support (XCreateFontCursor, XDefineCursor)
- All handlers with dark mode support
- All services with latest implementations
- LinuxApplication with theme change handling
2026-01-01 06:22:48 -05:00

69 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Maui.Platform.Linux.Native;
namespace Microsoft.Maui.Platform.Linux.Services;
public static class GtkContextMenuService
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void ActivateCallback(IntPtr menuItem, IntPtr userData);
private static readonly List<ActivateCallback> _callbacks = new List<ActivateCallback>();
private static readonly List<Action> _actions = new List<Action>();
public static void ShowContextMenu(List<GtkMenuItem> items)
{
if (items == null || items.Count == 0)
{
return;
}
_callbacks.Clear();
_actions.Clear();
IntPtr intPtr = GtkNative.gtk_menu_new();
if (intPtr == IntPtr.Zero)
{
Console.WriteLine("[GtkContextMenuService] Failed to create GTK menu");
return;
}
foreach (GtkMenuItem item in items)
{
IntPtr intPtr2;
if (item.IsSeparator)
{
intPtr2 = GtkNative.gtk_separator_menu_item_new();
}
else
{
intPtr2 = GtkNative.gtk_menu_item_new_with_label(item.Text);
GtkNative.gtk_widget_set_sensitive(intPtr2, item.IsEnabled);
if (item.IsEnabled && item.Action != null)
{
Action action = item.Action;
_actions.Add(action);
int actionIndex = _actions.Count - 1;
ActivateCallback activateCallback = delegate
{
Console.WriteLine("[GtkContextMenuService] Menu item activated: " + item.Text);
_actions[actionIndex]?.Invoke();
};
_callbacks.Add(activateCallback);
GtkNative.g_signal_connect_data(intPtr2, "activate", Marshal.GetFunctionPointerForDelegate(activateCallback), IntPtr.Zero, IntPtr.Zero, 0);
}
}
GtkNative.gtk_menu_shell_append(intPtr, intPtr2);
GtkNative.gtk_widget_show(intPtr2);
}
GtkNative.gtk_widget_show(intPtr);
IntPtr intPtr3 = GtkNative.gtk_get_current_event();
GtkNative.gtk_menu_popup_at_pointer(intPtr, intPtr3);
if (intPtr3 != IntPtr.Zero)
{
GtkNative.gdk_event_free(intPtr3);
}
Console.WriteLine($"[GtkContextMenuService] Showed GTK menu with {items.Count} items");
}
}