I like to understand AppKits layout so I made a example

I like to understand AppKits layout so I made a example.

The user could change the width or the height for NSWindow and still remain the overall look
of 2 NSBox in the window. I used the devided height with offset to feet the 2 boxes.

use framework "Foundation"
use framework "AppKit"
use scripting additions

property arguments : missing value
property theWindow : missing value

property textFieldTop : missing value
property textFieldBottom : missing value
property buttonAction : missing value

on run
	try
		if my NSThread's isMainThread() as boolean then
			my performDialog:arguments
		else
			my performSelectorOnMainThread:"performDialog:" withObject:"arguments" waitUntilDone:true
		end if
		
	on error the errorMessage number the errorNumber
		set the ErrorText to "Error: " & the errorNumber & ". " & the errorMessage
		return the ErrorText
	end try
end run

on performDialog:arguments
	-- Window size.
	set {width, height} to {440, 240}
	
	-- We make a box view with label.
	set {offsetX, offsetY, boxWidth, boxHeight} to {24, height / 2 + 24, width - 50, height - (height / 2) - 34}
	set textFieldBox1 to createBoxWithRect("Label 1", "System Small", 14, offsetX, offsetY, boxWidth, boxHeight)
	
	set {offsetX, offsetY, boxWidth, boxHeight} to {24, 34, width - 50, height / 2 - 34}
	set textFieldBox2 to createBoxWithRect("Label 2", "System Small", 14, offsetX, offsetY, boxWidth, boxHeight)
	
	set subviewItems to {textFieldBox1, textFieldBox2}
	set theWindow to createWindowWithRectAndSubview(subviewItems, 0, 0, width, height)
	theWindow's |center|()
	theWindow's makeKeyAndOrderFront:me
end performDialog:

on createTextFieldWithRect(x, y, width, height)
	set textFieldSize to current application's NSMakeRect(x, y, width, height)
	set theTextField to current application's NSTextField's alloc()'s initWithFrame:textFieldSize
	return theTextField
end createTextFieldWithRect

on createBoxWithRect(title, fontName, fontSize, x, y, width, height)
	set boxSize to current application's NSMakeRect(x, y, width, height)
	set theBox to current application's NSBox's alloc()'s initWithFrame:boxSize
	theBox's setTitle:title
	theBox's setTitleFont:(current application's NSFont's fontWithName:fontName |size|:fontSize)
	-- Specify the location of a box’s title with respect to its border.
	theBox's setTitlePosition:(current application's NSAtTop)
	-- theBox's setTransparent:false
	-- theBox's setBoxType:(current application's NSBoxCustom)
	theBox's setBorderType:(current application's NSLineBorder)
	theBox's setBorderWidth:0.5
	theBox's setCornerRadius:1
	-- theBox's setFillColor:(current application's NSColor's lightBlueColor)
	-- theBox's setBorderColor:(current application's NSColor's orangeColor)
	return theBox
end createBoxWithRect


on createWindowWithRectAndSubview(subviewItems, x, y, width, height)
	set windowSize to current application's NSMakeRect(x, y, width, height)
	set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer) + (current application's NSWindowStyleMaskMiniaturizable as integer) + (current application's NSWindowStyleMaskResizable as integer)
	set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:true
	
	repeat with anSubview in subviewItems
		(theWindow's contentView()'s addSubview:anSubview)
	end repeat
	return theWindow
end createWindowWithRectAndSubview

1 Like