<commit_before>/*
 * Copyright 2019 Poly Forest, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.acornui.focus

import com.acornui.Disposable
import com.acornui.component.UiComponentRo
import com.acornui.component.createOrReuseAttachment
import com.acornui.component.parentWalk
import com.acornui.component.stage
import com.acornui.di.Context
import com.acornui.di.ContextImpl
import com.acornui.input.*
import com.acornui.input.interaction.*
import com.acornui.time.nowMs
import com.acornui.toDisposable

/**
 * Dispatches mouse events when using SPACE or ENTER key presses on the focused element.
 */
class FakeFocusMouse(
		owner: Context
) : ContextImpl(owner) {

	private val focus = inject(FocusManager)
	private val interactivity = inject(InteractivityManager)

	private val fakeMouseEvent = MouseInteraction()
	private var downKey: Int? = null
	private var downElement: UiComponentRo? = null

	private val keyDownHandler = { event: KeyInteractionRo ->
		if (!event.handled) {
			val target = getTarget(event)
			if (target != null) {
				val isRepeat = event.isRepeat && downKey == event.keyCode && target.downRepeatEnabled()
				if ((downKey == null || isRepeat) && !event.hasAnyModifier && (event.keyCode == Ascii.SPACE || event.isEnterOrReturn)) {
					event.handled = true
					downKey = event.keyCode
					downElement = target
					dispatchFakeMouseEvent(target, MouseInteractionRo.MOUSE_DOWN)
				}
			}
		}
	}

	private val keyUpHandler = { event: KeyInteractionRo ->
		if (event.keyCode == downKey) {
			downKey = null

			val downElement = downElement!!
			this.downElement = null
			dispatchFakeMouseEvent(downElement, MouseInteractionRo.MOUSE_UP)
			if (!event.handled && getTarget(event) == focus.focused) {
				downElement.dispatchClick()
			}
			event.handled = true
		}
	}

	private fun dispatchFakeMouseEvent(target: UiComponentRo, type: InteractionType<MouseInteractionRo>) {
		fakeMouseEvent.clear()
		fakeMouseEvent.isFabricated = true
		fakeMouseEvent.type = type
		fakeMouseEvent.button = WhichButton.LEFT
		fakeMouseEvent.timestamp = nowMs()
		interactivity.dispatch(target, fakeMouseEvent)
	}

	private fun getTarget(event: KeyInteractionRo): UiComponentRo? {
		val focused = focus.focused ?: return null
		var target: UiComponentRo = focused
		focused.parentWalk {
			if (it.click().isNotEmpty()) {
				// If a parent has a click handler, use the focused element, don't continue to check for an
				// EnterTarget attachment.
				false
			} else {
				val attachment = it.getAttachment<EnterTarget>(EnterTarget)
				if (attachment != null) {
					if (attachment.filter(event)) {
						target = attachment.target
						false
					} else {
						true
					}
				} else {
					true
				}
			}

		}
		return target
	}

	init {
		stage.keyDown().add(keyDownHandler)
		stage.keyUp().add(keyUpHandler)
	}

	override fun dispose() {
		super.dispose()
		stage.keyDown().remove(keyDownHandler)
		stage.keyUp().remove(keyUpHandler)
	}
}

fun Context.fakeFocusMouse(): FakeFocusMouse {
	return FakeFocusMouse(this)
}

/**
 * The target for when ENTER or RETURN is pressed.
 */
class EnterTarget(val target: UiComponentRo, val filter: (KeyInteractionRo) -> Boolean) {

	companion object
}

/**
 * The FakeFocusMouse by default will respond to ENTER or RETURN key presses and fabricate MOUSE_DOWN and MOUSE_UP
 * events on the focused element.  If the enter target is set on an ancestor of the focused element, that target will
 * be used instead.
 */
fun UiComponentRo.enterTarget(target: UiComponentRo, filter: (KeyInteractionRo) -> Boolean = { !it.handled && it.keyCode != Ascii.SPACE }): Disposable {
	createOrReuseAttachment(EnterTarget) {
		EnterTarget(target, filter)
	}
	return {
		removeAttachment<EnterTarget>(EnterTarget)
	}.toDisposable()
}<commit_msg>Improve enterTarget behavior for fake focus mouse<commit_after>/*
 * Copyright 2019 Poly Forest, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.acornui.focus

import com.acornui.Disposable
import com.acornui.component.*
import com.acornui.di.Context
import com.acornui.di.ContextImpl
import com.acornui.input.*
import com.acornui.input.interaction.*
import com.acornui.time.nowMs
import com.acornui.toDisposable

/**
 * Dispatches mouse events when using SPACE or ENTER key presses on the focused element.
 */
class FakeFocusMouse(
		owner: Context
) : ContextImpl(owner) {

	private val focus = inject(FocusManager)
	private val interactivity = inject(InteractivityManager)

	private val fakeMouseEvent = MouseInteraction()
	private var downKey: Int? = null
	private var downElement: UiComponentRo? = null

	private val keyDownHandler = { event: KeyInteractionRo ->
		if (!event.handled) {
			val target = focus.focused
			if (target != null) {
				val isRepeat = event.isRepeat && target.downRepeatEnabled()
				if ((downKey == null || isRepeat) && !event.hasAnyModifier && (event.keyCode == Ascii.SPACE || event.isEnterOrReturn)) {
					downKey = event.keyCode
					downElement = target
					dispatchFakeMouseEvent(target, MouseInteractionRo.MOUSE_DOWN)
				}
			}
		}
	}

	private val keyUpHandler = { event: KeyInteractionRo ->
		if (event.keyCode == downKey) {
			downKey = null

			val downElement = downElement!!
			this.downElement = null
			dispatchFakeMouseEvent(downElement, MouseInteractionRo.MOUSE_UP)
			if (!downElement.dispatchClick().handled)
				getEnterTarget(event)?.dispatchClick()
		}
	}

	private fun dispatchFakeMouseEvent(target: UiComponentRo, type: InteractionType<MouseInteractionRo>) {
		fakeMouseEvent.clear()
		fakeMouseEvent.isFabricated = true
		fakeMouseEvent.type = type
		fakeMouseEvent.button = WhichButton.LEFT
		fakeMouseEvent.timestamp = nowMs()
		interactivity.dispatch(target, fakeMouseEvent)
	}

	private fun getEnterTarget(event: KeyInteractionRo): UiComponentRo? {
		val target = focus.focused?.findParent {
			it.getAttachment<EnterTarget>(EnterTarget)?.filter?.invoke(event) == true
		}
		return target?.enterTarget ?: target
	}

	init {
		stage.keyDown().add(keyDownHandler)
		stage.keyUp().add(keyUpHandler)
	}

	override fun dispose() {
		super.dispose()
		stage.keyDown().remove(keyDownHandler)
		stage.keyUp().remove(keyUpHandler)
	}
}

/**
 * The target for when ENTER or RETURN is pressed.
 */
class EnterTarget(val target: UiComponentRo, val filter: (KeyInteractionRo) -> Boolean) {

	companion object
}

private val UiComponentRo.enterTarget: UiComponentRo?
	get() = getAttachment<EnterTarget>(EnterTarget)?.target

/**
 * The FakeFocusMouse by default will respond to ENTER or RETURN key presses and fabricate MOUSE_DOWN and MOUSE_UP
 * events on the focused element.  If the enter target is set on an ancestor of the focused element, that target will
 * be used instead.
 */
fun UiComponentRo.enterTarget(target: UiComponentRo, filter: (KeyInteractionRo) -> Boolean = { !it.handled && it.keyCode != Ascii.SPACE }): Disposable {
	createOrReuseAttachment(EnterTarget) {
		EnterTarget(target, filter)
	}
	return {
		removeAttachment<EnterTarget>(EnterTarget)
	}.toDisposable()
}
{'fraction_non_alphanumeric': 0.09728926847382102, 'fraction_numerical': 0.0019804431241490284, 'mean_word_length': 4.233160621761658, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1793838', 'n_tokens_mistral': 2646, 'n_tokens_neox': 2414, 'n_words': 691}