<commit_before>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2011 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */
var EventEmitter = require("events").EventEmitter,
	assets = kroll.binding("assets"),
	vm = require("vm");
var TAG = "Window";

exports.bootstrapWindow = function(Titanium) {
	var newActivityRequiredKeys = ["fullscreen", "navBarHidden", "modal", "windowSoftInputMode"];

	// Backward compatibility for lightweight windows
	var UI = Titanium.UI;
	var Window = Titanium.TiBaseWindow;
	var ActivityWindow = UI.ActivityWindow;
	var Proxy = Titanium.Proxy;

	Window.prototype.getTopActivity = function() {
		var topActivity = Titanium.App.Android.getTopActivity();
		if (topActivity) {
			return topActivity;
		}

		kroll.log(TAG, "unable to find valid top activity");
		return null;
	}

	Window.prototype.getActivityDecorView = function() {
		var topActivity = this.getTopActivity();
		if (topActivity) {
			return topActivity.getDecorView();
		}

		kroll.log(TAG, "unable to find valid activity for decor view");
		return null;
	}

	Window.prototype._cacheSetProperty = function(setter, value) {
		var cache = this.propertyCache;

		if (!cache) {
			cache = this.propertyCache = {};
		}

		cache[setter] = value;
	}

	var wrappedPropsAndMethods = [
		{
			name: 'activity',
			getter: 'getActivity'
		},
		{
			name: 'orientation',
			getter: 'getOrientation'
		},
		{
			name: 'orientationModes',
			getter: 'getOrientationModes',
			setter: 'setOrientationModes'
		},
		{
			name: 'windowPixelFormat',
			getter: 'getWindowPixelFormat',
			setter: 'setWindowPixelFormat'
		}
	]

	function wrapPropsAndMethods(wrapperWindow) {
		wrappedPropsAndMethods.forEach(function(info) {
			var name = info.name,
			    descriptor = {};

			// Define getter and setter methods.
			if (info.getter) {
				function getProperty() {
					return this.window[name];
				}
				wrapperWindow[info.getter] = getProperty;
				descriptor.get = getProperty;
			}
			if (info.setter) {
				function setProperty(value) {
					this.window[name] = value;
				}
				wrapperWindow[info.setter] = setProperty;
				descriptor.set = setProperty;

				// Sync inner window with any property values set
				// on the wrapper before it was opened.
				wrapperWindow.window[name] = wrapperWindow[name];
			}

			// Re-define new property on wrapper which delegates
			// to the inner window proxy.
			delete wrapperWindow[name];
			Object.defineProperty(wrapperWindow, name, descriptor);
		});
	}

	Window.prototype.addChildren = function() {
		if (this._children) {
			var length = this._children.length;

			for (var i = 0; i < length; i++) {
				this.view.add(this._children[i]);
			}
		}

		// don't delete the children once finished in case the window
		// needs to be opened again
	}

	Window.prototype.removeChildren = function() {
		if (this._children) {
			var length = this._children.length;

			for (var i = 0; i < length; i++) {
				this.view.remove(this._children[i]);
			}
		}
	}

	Window.prototype.open = function(options) {
		if (!options) {
			options = {};
		} else {
			this._properties.extend(options);
		}

		// Determine if we should create a heavy or light weight window.
		this.isActivity = false;
		newActivityRequiredKeys.forEach(function(key) {
			if (key in this._properties) {
				this.isActivity = true;
			}
		}, this);

		if (!this.isActivity && "tabOpen" in this._properties && options.tabOpen) {
			this.isActivity = true;
		}

		var needsOpen = false;

		if (this.isActivity) {
			this.window = new ActivityWindow(this._properties);
			this.view = this.window;
			needsOpen = true;

		} else {
			this.window = this.getActivityDecorView();
			this.view = new UI.View(this._properties);
			this.view.zIndex = Math.MAX_INT - 2;
			this.window.add(this.view);
		}

		this.setWindowView(this.view);
		this.addChildren();

		// Expose the real window's properties by creating wrappers.
		wrapPropsAndMethods(this);

		if (needsOpen) {
			var self = this;
			this.window.on("open", function () {
				self.postOpen();
			});

			this.window.open();

		} else {
			this.postOpen();
		}
	}

	Window.prototype.setWindow = function(existingWindow) {
		this.window = existingWindow;
		this.view = this.window;
		this.setWindowView(this.view);

		this.addChildren();<commit_msg>Wrap props and methods when setWindow() is used.<commit_after>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2011 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */
var EventEmitter = require("events").EventEmitter,
	assets = kroll.binding("assets"),
	vm = require("vm");
var TAG = "Window";

exports.bootstrapWindow = function(Titanium) {
	var newActivityRequiredKeys = ["fullscreen", "navBarHidden", "modal", "windowSoftInputMode"];

	// Backward compatibility for lightweight windows
	var UI = Titanium.UI;
	var Window = Titanium.TiBaseWindow;
	var ActivityWindow = UI.ActivityWindow;
	var Proxy = Titanium.Proxy;

	Window.prototype.getTopActivity = function() {
		var topActivity = Titanium.App.Android.getTopActivity();
		if (topActivity) {
			return topActivity;
		}

		kroll.log(TAG, "unable to find valid top activity");
		return null;
	}

	Window.prototype.getActivityDecorView = function() {
		var topActivity = this.getTopActivity();
		if (topActivity) {
			return topActivity.getDecorView();
		}

		kroll.log(TAG, "unable to find valid activity for decor view");
		return null;
	}

	Window.prototype._cacheSetProperty = function(setter, value) {
		var cache = this.propertyCache;

		if (!cache) {
			cache = this.propertyCache = {};
		}

		cache[setter] = value;
	}

	var wrappedPropsAndMethods = [
		{
			name: 'activity',
			getter: 'getActivity'
		},
		{
			name: 'orientation',
			getter: 'getOrientation'
		},
		{
			name: 'orientationModes',
			getter: 'getOrientationModes',
			setter: 'setOrientationModes'
		},
		{
			name: 'windowPixelFormat',
			getter: 'getWindowPixelFormat',
			setter: 'setWindowPixelFormat'
		}
	]

	function wrapPropsAndMethods(wrapperWindow) {
		wrappedPropsAndMethods.forEach(function(info) {
			var name = info.name,
			    descriptor = {};

			// Define getter and setter methods.
			if (info.getter) {
				function getProperty() {
					return this.window[name];
				}
				wrapperWindow[info.getter] = getProperty;
				descriptor.get = getProperty;
			}
			if (info.setter) {
				function setProperty(value) {
					this.window[name] = value;
				}
				wrapperWindow[info.setter] = setProperty;
				descriptor.set = setProperty;

				// Sync inner window with any property values set
				// on the wrapper before it was opened.
				wrapperWindow.window[name] = wrapperWindow[name];
			}

			// Re-define new property on wrapper which delegates
			// to the inner window proxy.
			delete wrapperWindow[name];
			Object.defineProperty(wrapperWindow, name, descriptor);
		});
	}

	Window.prototype.addChildren = function() {
		if (this._children) {
			var length = this._children.length;

			for (var i = 0; i < length; i++) {
				this.view.add(this._children[i]);
			}
		}

		// don't delete the children once finished in case the window
		// needs to be opened again
	}

	Window.prototype.removeChildren = function() {
		if (this._children) {
			var length = this._children.length;

			for (var i = 0; i < length; i++) {
				this.view.remove(this._children[i]);
			}
		}
	}

	Window.prototype.open = function(options) {
		if (!options) {
			options = {};
		} else {
			this._properties.extend(options);
		}

		// Determine if we should create a heavy or light weight window.
		this.isActivity = false;
		newActivityRequiredKeys.forEach(function(key) {
			if (key in this._properties) {
				this.isActivity = true;
			}
		}, this);

		if (!this.isActivity && "tabOpen" in this._properties && options.tabOpen) {
			this.isActivity = true;
		}

		var needsOpen = false;

		if (this.isActivity) {
			this.window = new ActivityWindow(this._properties);
			this.view = this.window;
			needsOpen = true;

		} else {
			this.window = this.getActivityDecorView();
			this.view = new UI.View(this._properties);
			this.view.zIndex = Math.MAX_INT - 2;
			this.window.add(this.view);
		}

		this.setWindowView(this.view);
		this.addChildren();

		// Expose the real window's properties by creating wrappers.
		wrapPropsAndMethods(this);

		if (needsOpen) {
			var self = this;
			this.window.on("open", function () {
				self.postOpen();
			});

			this.window.open();

		} else {
			this.postOpen();
		}
	}

	Window.prototype.setWindow = function(existingWindow) {
		this.window = existingWindow;
		this.view = this.window;
		this.setWindowView(this.view);

		wrappedPropsAndMethods(this);

		this.addChildren();
{'fraction_non_alphanumeric': 0.12488748874887488, 'fraction_numerical': 0.0015751575157515751, 'mean_word_length': 3.4467233616808404, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '4650583', 'n_tokens_mistral': 3284, 'n_tokens_neox': 2767, 'n_words': 683}