<commit_before>/**
 * @file	KCLayouter.swift
 * @brief	Define KCLayouter class
 * @par Copyright
 *   Copyright (C) 2015 Steel Wheels Project
 */

import CoconutData
import Foundation

public class KCLayouter: KCViewVisitor
{
	private var mViewController:	KCSingleViewController
	private var mCurrentFrames:	CNStack<KCRect>
	private var mConsole:		CNConsole

	public init(viewController vcont: KCSingleViewController, console cons: CNConsole){
		mViewController	= vcont
		mCurrentFrames  = CNStack<KCRect>()
		mConsole	= cons

		/* Push screen size */
		let winsize = KCLayouter.windowSize(viewController: vcont)
		mCurrentFrames.push(KCRect(origin: KCPoint.zero, size: winsize))
	}

	public func layout(rootView view: KCRootView){
		view.accept(visitor: self)
	}

	open override func visit(rootView view: KCRootView){
		if let frame = mCurrentFrames.peek() {
			/* Setup root view */
			view.frame     = frame
			view.bounds    = frame
			view.fixedSize = frame.size
			view.setFixedSizeForLayout(size: frame.size)
			view.translatesAutoresizingMaskIntoConstraints = true

			/* Setup content view */
			if let core: KCView = view.getCoreView() {
				let cinset = KCLayouter.safeAreaInset(viewController: mViewController)
				view.allocateSubviewLayout(subView: core, in: cinset)

				let cframe = KCEdgeInsetsInsetRect(frame, cinset)
				mCurrentFrames.push(cframe)
				core.accept(visitor: self)
				let _ = mCurrentFrames.pop()
			}
		} else {
			fatalError("\(#function): Can not happen")
		}
	}

	open override func visit(iconView view: KCIconView){
		self.visit(coreView: view)
	}

	open override func visit(button view: KCButton){
		self.visit(coreView: view)
	}

	open override func visit(checkBox view: KCCheckBox){
		self.visit(coreView: view)
	}

	open override func visit(stepper view: KCStepper){
		self.visit(coreView: view)
	}

	open override func visit(textField view: KCTextField){
		self.visit(coreView: view)
	}

	open override func visit(textEdit view: KCTextEdit){
		self.visit(coreView: view)
	}

	open override func visit(tableView view: KCTableView){
		self.visit(coreView: view)
	}

	open override func visit(stackView view: KCStackView){
		self.visit(coreView: view)

		if let frame = mCurrentFrames.peek() {
			/* Decide size of children */
			var cframe: KCRect
			switch view.alignment {
			case .horizontal(_):
				let newx      = KCView.noIntrinsicMetric
				let newy      = frame.origin.y
				let newwidth  = KCView.noIntrinsicMetric
				let newheight = frame.size.height
				cframe = KCRect(x: newx, y: newy, width: newwidth, height: newheight)
			case .vertical(_):
				let newx      = frame.origin.x
				let newy      = KCView.noIntrinsicMetric
				let newwidth  = frame.size.width
				let newheight = KCView.noIntrinsicMetric
				cframe = KCRect(x: newx, y: newy, width: newwidth, height: newheight)
			}
			/* Visit subviews */
			mCurrentFrames.push(cframe)
			for subview in view.arrangedSubviews() {
				subview.accept(visitor: self)
			}
			let _ = mCurrentFrames.pop()
		}
	}

	open override func visit(consoleView view: KCConsoleView){
		self.visit(coreView: view)
	}

	open override func visit(imageView view: KCImageView){
		self.visit(coreView: view)
	}

	open override func visit(coreView view: KCCoreView){
		if let frame = mCurrentFrames.peek() {
			view.fixedSize   = frame.size

			let width = frame.size.width
			if width > 0.0 {
				view.frame.size.width   = width
				view.bounds.size.width  = width
			}

			let height = frame.size.height
			if height > 0.0 {
				view.frame.size.height  = height
				view.bounds.size.height = height
			}
		}
	}

	private class func windowSize(viewController vcont: KCSingleViewController) -> KCSize {<commit_msg>KCLayouter: Remove fixed size constraint of KCRootView<commit_after>/**
 * @file	KCLayouter.swift
 * @brief	Define KCLayouter class
 * @par Copyright
 *   Copyright (C) 2015 Steel Wheels Project
 */

import CoconutData
import Foundation

public class KCLayouter: KCViewVisitor
{
	private var mViewController:	KCSingleViewController
	private var mCurrentFrames:	CNStack<KCRect>
	private var mConsole:		CNConsole

	public init(viewController vcont: KCSingleViewController, console cons: CNConsole){
		mViewController	= vcont
		mCurrentFrames  = CNStack<KCRect>()
		mConsole	= cons

		/* Push screen size */
		let winsize = KCLayouter.windowSize(viewController: vcont)
		mCurrentFrames.push(KCRect(origin: KCPoint.zero, size: winsize))
	}

	public func layout(rootView view: KCRootView){
		view.accept(visitor: self)
	}

	open override func visit(rootView view: KCRootView){
		if let frame = mCurrentFrames.peek() {
			/* Setup root view */
			view.frame     = frame
			view.bounds    = frame
			view.fixedSize = frame.size
			//view.setFixedSizeForLayout(size: frame.size)
			view.translatesAutoresizingMaskIntoConstraints = true

			/* Setup content view */
			if let core: KCView = view.getCoreView() {
				let cinset = KCLayouter.safeAreaInset(viewController: mViewController)
				view.allocateSubviewLayout(subView: core, in: cinset)

				let cframe = KCEdgeInsetsInsetRect(frame, cinset)
				mCurrentFrames.push(cframe)
				core.accept(visitor: self)
				let _ = mCurrentFrames.pop()
			}
		} else {
			fatalError("\(#function): Can not happen")
		}
	}

	open override func visit(iconView view: KCIconView){
		self.visit(coreView: view)
	}

	open override func visit(button view: KCButton){
		self.visit(coreView: view)
	}

	open override func visit(checkBox view: KCCheckBox){
		self.visit(coreView: view)
	}

	open override func visit(stepper view: KCStepper){
		self.visit(coreView: view)
	}

	open override func visit(textField view: KCTextField){
		self.visit(coreView: view)
	}

	open override func visit(textEdit view: KCTextEdit){
		self.visit(coreView: view)
	}

	open override func visit(tableView view: KCTableView){
		self.visit(coreView: view)
	}

	open override func visit(stackView view: KCStackView){
		self.visit(coreView: view)

		if let frame = mCurrentFrames.peek() {
			/* Decide size of children */
			var cframe: KCRect
			switch view.alignment {
			case .horizontal(_):
				let newx      = KCView.noIntrinsicMetric
				let newy      = frame.origin.y
				let newwidth  = KCView.noIntrinsicMetric
				let newheight = frame.size.height
				cframe = KCRect(x: newx, y: newy, width: newwidth, height: newheight)
			case .vertical(_):
				let newx      = frame.origin.x
				let newy      = KCView.noIntrinsicMetric
				let newwidth  = frame.size.width
				let newheight = KCView.noIntrinsicMetric
				cframe = KCRect(x: newx, y: newy, width: newwidth, height: newheight)
			}
			/* Visit subviews */
			mCurrentFrames.push(cframe)
			for subview in view.arrangedSubviews() {
				subview.accept(visitor: self)
			}
			let _ = mCurrentFrames.pop()
		}
	}

	open override func visit(consoleView view: KCConsoleView){
		self.visit(coreView: view)
	}

	open override func visit(imageView view: KCImageView){
		self.visit(coreView: view)
	}

	open override func visit(coreView view: KCCoreView){
		if let frame = mCurrentFrames.peek() {
			view.fixedSize   = frame.size

			let width  = frame.size.width
			if width > 0.0 {
				view.frame.size.width   = width
				view.bounds.size.width  = width
			}
			let height = frame.size.height
			if height > 0.0 {
				view.frame.size.height  = height
				view.bounds.size.height = height
			}
		}
	}

	private class func windowSize(viewController vcont: KCSingleViewController) -> KCSize {
{'fraction_non_alphanumeric': 0.09840247013021883, 'fraction_numerical': 0.0021479393207141898, 'mean_word_length': 3.670846394984326, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4014646', 'n_tokens_mistral': 2867, 'n_tokens_neox': 2443, 'n_words': 622}