<commit_before>import js.Browser;

class FocusManager {

	public static function addEvents(focus:Void -> Void, blur:Void -> Void) {
		if (Reflect.field(Browser.window, "addEventListener") != null) {
			untyped __js__("window").addEventListener("focus", focus);
			untyped __js__("window").addEventListener("blur", blur);
		}
		else if (Reflect.field(Browser.window, "attachEvent") != null) { //IE
			untyped __js__("window").attachEvent("onfocus", focus);
			untyped __js__("window").attachEvent("onblur", blur);
		}
		else {
			Browser.window.onload = function () {
				Browser.window.onfocus = focus;
				Browser.window.onblur = blur;
			};
		}
	}

	public static function removeEvents(focus:Void -> Void, blur:Void -> Void) {
		if (Reflect.field(Browser.window, "removeEventListener") != null) {
			untyped __js__("window").removeEventListener("focus", focus);
			untyped __js__("window").removeEventListener("blur", blur);
		}
		else if (Reflect.field(Browser.window, "removeEvent") != null) { //IE
			untyped __js__("window").removeEvent("onfocus", focus);
			untyped __js__("window").removeEvent("onblur", blur);
		}
		else {
			Browser.window.onfocus = null;
			Browser.window.onblur = null;
		}
	}
}<commit_msg>improved focus manager<commit_after>import js.Browser;

class FocusManager {

	static inline var FOCUS_STATE:String = "focus";
	static inline var BLUR_STATE:String = "blur";

	public var focus:Void -> Void;
	public var blur:Void -> Void;

	var _hidden:String;
	var _visibilityChange:String;
	var _currentState:String;

	public function new() {
		_hidden = "";
		_visibilityChange = "";
		_currentState = "";

		if (Reflect.field(Browser.document, "hidden") != null) {
			_hidden = "hidden";
			_visibilityChange = "visibilitychange";
		}
		else if (Reflect.field(Browser.document, "mozHidden") != null) {
			_hidden = "mozHidden";
			_visibilityChange = "mozvisibilitychange";
		}
		else if (Reflect.field(Browser.document, "msHidden") != null) {
			_hidden = "msHidden";
			_visibilityChange = "msvisibilitychange";
		}
		else if (Reflect.field(Browser.document, "webkitHidden") != null) {
			_hidden = "webkitHidden";
			_visibilityChange = "webkitvisibilitychange";
		}

		if (Reflect.field(Browser.window, "addEventListener") != null) {
			untyped __js__("window").addEventListener("focus", _focus);
			untyped __js__("window").addEventListener("blur", _blur);
			untyped __js__("window").addEventListener("pageshow", _focus);
			untyped __js__("window").addEventListener("pagehide", _blur);
			untyped __js__("document").addEventListener(_visibilityChange, _handleVisibilityChange);
		}
		else if (Reflect.field(Browser.window, "attachEvent") != null) {
			untyped __js__("window").attachEvent("onfocus", _focus);
			untyped __js__("window").attachEvent("onblur", _blur);
			untyped __js__("window").attachEvent("pageshow", _focus);
			untyped __js__("window").attachEvent("pagehide", _blur);
			untyped __js__("document").attachEvent(_visibilityChange, _handleVisibilityChange);
		}
		else {
			Browser.window.onload = function () {
				Browser.window.onfocus = _focus;
				Browser.window.onblur = _blur;
				Browser.window.onpageshow = _focus;
				Browser.window.onpagehide = _blur;
			};
		}
	}

	function _handleVisibilityChange() {
		if (Reflect.field(Browser.document, _hidden) != null && Reflect.field(Browser.document, _hidden)) blur();
		else focus();
	}

	function _focus() {
		if (_currentState != FOCUS_STATE && focus != null) focus();
		_currentState = FOCUS_STATE;
	}

	function _blur() {
		if (_currentState != BLUR_STATE && blur != null) blur();
		_currentState = BLUR_STATE;
	}

	public function clearEvents() {
		if (Reflect.field(Browser.window, "removeEventListener") != null) {
			untyped __js__("window").removeEventListener("focus", _focus);
			untyped __js__("window").removeEventListener("blur", _blur);
			untyped __js__("window").removeEventListener("pageshow", _focus);
			untyped __js__("window").removeEventListener("pagehide", _blur);
			untyped __js__("window").removeEventListener(_visibilityChange, _handleVisibilityChange);
		}
		else if (Reflect.field(Browser.window, "removeEvent") != null) {
			untyped __js__("window").removeEvent("onfocus", _focus);
			untyped __js__("window").removeEvent("onblur", _blur);
			untyped __js__("window").removeEvent("pageshow", _focus);
			untyped __js__("window").removeEvent("pagehide", _blur);
			untyped __js__("window").removeEvent(_visibilityChange, _handleVisibilityChange);
		}
		else {
			Browser.window.onfocus = null;
			Browser.window.onblur = null;
			Browser.window.onpageshow = null;
			Browser.window.onpagehide = null;
		}
	}
}
{'fraction_non_alphanumeric': 0.15172413793103448, 'fraction_numerical': 0.0, 'mean_word_length': 5.066666666666666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3667253', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1463, 'n_words': 277}