Skip to content

[core:sys/darwin/Foundation] Add bindings for NSPopover and related classes#6311

Open
JacobEvelyn wants to merge 2 commits intoodin-lang:masterfrom
JacobEvelyn:NSPopover-and-related
Open

[core:sys/darwin/Foundation] Add bindings for NSPopover and related classes#6311
JacobEvelyn wants to merge 2 commits intoodin-lang:masterfrom
JacobEvelyn:NSPopover-and-related

Conversation

@JacobEvelyn
Copy link
Contributor

@JacobEvelyn JacobEvelyn commented Feb 21, 2026

This PR is split into two commits for easier review:


[core:sys/darwin/Foundation] Extract NSView bindings to its own file

This commit extracts NSView bindings from NSWindow.odin to a new
NSView.odin file to better match Odin conventions and give us a
better place to add additional NSView bindings in the next commit.

[core:sys/darwin/Foundation] Add bindings for NSPopover and related classes

This commit adds bindings for NSPopover, as well as related classes used
to display the popover and change its content, layout, animation, appearance,
etc.


And here's a "minimal" example that showcases much of the functionality working together:

package main

import NS "core:sys/darwin/Foundation"

quit := false

main :: proc() {
	app := NS.Application.sharedApplication()
	app->setActivationPolicy(.Regular)
	app->finishLaunching()
	window := NS.Window.alloc()->initWithContentRect(
		{{500, 500}, {300, 300}},
		{.Closable, .Titled},
		.Buffered,
		NS.NO,
	)
	window->setDelegate(NS.window_delegate_register_and_alloc({
				windowWillClose = proc(_: ^NS.Notification) {
					quit = true
				},
			}, "CustomWindowDelegate", context))
	window->setTitle(NS.MakeConstantString("NSPopover test"))
	window->setIsVisible(true)
	app->activate()

	label := NS.TextField.labelWithString(NS.String.alloc()->initWithOdinString("Hellope"))
	label->setTranslatesAutoresizingMaskIntoConstraints(false) // Required for layout constraints to work

	view := NS.View.alloc()->init()
	view->addSubview(label)
	constraints := [?]^NS.LayoutConstraint {
		label->leadingAnchor()->constraintEqualToAnchorConstant(view->leadingAnchor(), 10),
		label->trailingAnchor()->constraintEqualToAnchorConstant(view->trailingAnchor(), -10),
		label->topAnchor()->constraintEqualToAnchorConstant(view->topAnchor(), 10),
		label->bottomAnchor()->constraintEqualToAnchorConstant(view->bottomAnchor(), -10),
	}
	NS.LayoutConstraint.activateConstraints(
		NS.Array.alloc()->initWithObjects(
			cast([^]^NS.Object)raw_data(constraints[:]),
			len(constraints),
		),
	)

	popover_view_controller := NS.ViewController.alloc()->init()
	popover_view_controller->setView(view)

	popover := NS.Popover.alloc()->init()
	popover->setBehavior(.ApplicationDefined)
	popover->setAppearance(NS.Appearance.appearanceNamed(NS.AppearanceNameAqua))
	popover->setContentViewController(popover_view_controller)
	popover->showRelativeToRect(NS.Rect{{150, 150}, {1, 1}}, window->contentView(), .MaxYEdge)

	for !quit {
		NS.scoped_autoreleasepool()
		event := app->nextEventMatchingMask(
			NS.EventMaskAny,
			NS.Date.distantFuture(),
			NS.DefaultRunLoopMode,
			true,
		)
		for event != nil {
			if event->type() == .LeftMouseDown {
				label->setStringValue(NS.String.alloc()->initWithOdinString("Hellope world!"))

				animation_context := NS.AnimationContext.currentContext()
				animation_context->setAllowsImplicitAnimation(true)
				animation_context->setDuration(1)
				NS.AnimationContext.beginGrouping()
				popover->setContentSize(popover->contentViewController()->view()->fittingSize())
				NS.AnimationContext.endGrouping()
			}
			app->sendEvent(event)
			event = app->nextEventMatchingMask(NS.EventMaskAny, nil, NS.DefaultRunLoopMode, true)
		}
	}
}

This commit extracts `NSView` bindings from `NSWindow.odin` to a new
`NSView.odin` file to better match Odin conventions and give us a
better place to add additional `NSView` bindings in the next commit.
@JacobEvelyn JacobEvelyn force-pushed the NSPopover-and-related branch from 53d4985 to 6465f70 Compare February 21, 2026 12:01
… classes

This commit adds bindings for `NSPopover`, as well as related classes used
to display the popover and change its content, layout, animation, appearance,
etc.
@JacobEvelyn JacobEvelyn force-pushed the NSPopover-and-related branch from 6465f70 to 4be813a Compare February 21, 2026 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant