core: darwin: Document the command key fix

This commit is contained in:
foxnne 2024-12-13 09:43:19 -06:00 committed by Emi Gutekanst
parent 4a6101241c
commit 4ada021a15

View file

@ -134,37 +134,41 @@ fn initWindow(
window_id: mach.ObjectID, window_id: mach.ObjectID,
) !void { ) !void {
var core_window = core.windows.getValue(window_id); var core_window = core.windows.getValue(window_id);
const context = try core.allocator.create(Context);
context.* = .{ .core = core, .window_id = window_id };
// If the application is not headless, we need to make the application a genuine UI application // If the application is not headless, we need to make the application a genuine UI application
// by setting the activation policy, this moves the process to foreground // by setting the activation policy, this moves the process to foreground
// TODO: Only call this on the first window creation // TODO: Only call this on the first window creation
_ = objc.app_kit.Application.sharedApplication().setActivationPolicy(objc.app_kit.ApplicationActivationPolicyRegular); _ = objc.app_kit.Application.sharedApplication().setActivationPolicy(objc.app_kit.ApplicationActivationPolicyRegular);
const commandFn = struct { {
pub fn commandFn(block: *objc.foundation.BlockLiteral(*Context), event: *objc.app_kit.Event) callconv(.C) ?*objc.app_kit.Event { // On macos, the command key in particular seems to be handled a bit differently and tends to block the `keyUp` event
const core_: *Core = block.context.core; // from firing. To remedy this, we borrow the same fix GLFW uses and add a monitor.
const window_id_ = block.context.window_id; const commandFn = struct {
pub fn commandFn(block: *objc.foundation.BlockLiteral(*Context), event: *objc.app_kit.Event) callconv(.C) ?*objc.app_kit.Event {
const core_: *Core = block.context.core;
const window_id_ = block.context.window_id;
if (core_.windows.get(window_id_, .native)) |native| { if (core_.windows.get(window_id_, .native)) |native| {
const native_window: *objc.app_kit.Window = native.window; const native_window: *objc.app_kit.Window = native.window;
if (event.modifierFlags() & objc.app_kit.EventModifierFlagCommand != 0) if (event.modifierFlags() & objc.app_kit.EventModifierFlagCommand != 0)
native_window.sendEvent(event); native_window.sendEvent(event);
}
return event;
} }
return event; }.commandFn;
}
}.commandFn;
const context = try core.allocator.create(Context); var commandBlock = objc.foundation.stackBlockLiteral(
context.* = .{ .core = core, .window_id = window_id }; commandFn,
context,
null,
null,
);
var commandBlock = objc.foundation.stackBlockLiteral( _ = objc.app_kit.Event.addLocalMonitorForEventsMatchingMask_handler(objc.app_kit.EventMaskKeyUp, commandBlock.asBlock().copy());
commandFn, }
context,
null,
null,
);
_ = objc.app_kit.Event.addLocalMonitorForEventsMatchingMask_handler(objc.app_kit.EventMaskKeyUp, commandBlock.asBlock().copy());
const metal_descriptor = try core.allocator.create(gpu.Surface.DescriptorFromMetalLayer); const metal_descriptor = try core.allocator.create(gpu.Surface.DescriptorFromMetalLayer);
const layer = objc.quartz_core.MetalLayer.new(); const layer = objc.quartz_core.MetalLayer.new();