<commit_before>package com.waicool20.skrypton.sikulix

import com.waicool20.skrypton.jni.objects.WebViewHighlighter
import com.waicool20.skrypton.sikulix.input.SKryptonKeyboard
import org.sikuli.script.*
import java.awt.Color
import java.awt.Rectangle
import java.awt.event.KeyEvent
import java.util.concurrent.TimeUnit

open class SKryptonRegion(xPos: Int, yPos: Int, width: Int, height: Int) : Region(), SikuliInterface {
    constructor(region: Region, screen: SKryptonScreen) : this(region.x, region.y, region.w, region.h, screen)
    constructor(rect: Rectangle, screen: SKryptonScreen) : this(rect.x, rect.y, rect.width, rect.height, screen)
    constructor(xPos: Int, yPos: Int, width: Int, height: Int, screen: SKryptonScreen) : this(xPos, yPos, width, height) {
        this.screen = screen
    }
    
    init {
        x = xPos
        y = yPos
        w = width
        h = height
    }

    private val mouse by lazy { skryptonScreen().mouse }
    private val keyboard by lazy { skryptonScreen().keyboard }

    fun skryptonScreen() = screen as SKryptonScreen

    override fun setLocation(loc: Location): SKryptonRegion {
        x = loc.x
        y = loc.y
        return this
    }

    override fun setROI() = setROI(screen.bounds)
    override fun setROI(rect: Rectangle) = with(rect) { setROI(x, y, width, height) }
    override fun setROI(region: Region) = with(region) { setROI(x, y, w, h) }
    override fun setROI(X: Int, Y: Int, W: Int, H: Int) {
        x = X
        y = Y
        w = if (W > 1) W else 1
        h = if (H > 1) H else 1
    }

    override fun getCenter() = Location(x + (w / 2), y + (h / 2))
    override fun getTopLeft() = Location(x, y)
    override fun getTopRight() = Location(x + w - 1, y)
    override fun getBottomLeft() = Location(x, y + h - 1)
    override fun getBottomRight() = Location(x + w - 1, y + h - 1)

    override fun getLastMatch() = super.getLastMatch()?.let { SKryptonMatch(it, skryptonScreen()) }
    override fun getLastMatches() =
            super.getLastMatches().asSequence().toList().map { SKryptonMatch(it, skryptonScreen()) }.iterator()

    override fun offset(loc: Location?) = SKryptonRegion(super.offset(loc), skryptonScreen())
    override fun above() = SKryptonRegion(super.above(), skryptonScreen())
    override fun below() = SKryptonRegion(super.below(), skryptonScreen())
    override fun left() = SKryptonRegion(super.left(), skryptonScreen())
    override fun right() = SKryptonRegion(super.right(), skryptonScreen())

    override fun above(height: Int) = SKryptonRegion(super.above(height), skryptonScreen())
    override fun below(height: Int) = SKryptonRegion(super.below(height), skryptonScreen())
    override fun left(width: Int) = SKryptonRegion(super.left(width), skryptonScreen())
    override fun right(width: Int) = SKryptonRegion(super.right(width), skryptonScreen())

    /* Search operations */

    override fun <PSI : Any?> find(target: PSI) = super.find(target)?.let { SKryptonMatch(it, skryptonScreen()) }
    override fun <PSI : Any?> findAll(target: PSI) =
            super.findAll(target).asSequence().toList().map { SKryptonMatch(it, skryptonScreen()) }.iterator()

    override fun <PSI : Any?> wait(target: PSI) = super.wait(target).let { SKryptonMatch(it, skryptonScreen()) }
    override fun <PSI : Any?> wait(target: PSI, timeout: Double) = super.wait(target, timeout)?.let { SKryptonMatch(it, skryptonScreen()) }

    override fun <PSI : Any?> exists(target: PSI) = super.exists(target)?.let { SKryptonMatch(it, skryptonScreen()) }
    override fun <PSI : Any?> exists(target: PSI, timeout: Double) = super.exists(target, timeout)?.let { SKryptonMatch(it, skryptonScreen()) }

    //<editor-fold desc="Mouse and KeyboardActions">
    override fun click(): Int = click(center, 0)

    override fun <PFRML : Any?> click(target: PFRML): Int = click(target, 0)
    override fun <PFRML : Any?> click(target: PFRML, modifiers: Int): Int =
            try {
                getLocationFromTarget(target)?.let {
                    mouse.click(it, Mouse.LEFT, modifiers)
                    1
                } ?: 0
            } catch (e: FindFailed) {
                0
            }


    override fun doubleClick(): Int = doubleClick(center, 0)
    override fun <PFRML : Any?> doubleClick(target: PFRML): Int = doubleClick(target, 0)
    override fun <PFRML : Any?> doubleClick(target: PFRML, modifiers: Int): Int =
            try {
                getLocationFromTarget(target)?.let {
                    mouse.doubleClick(it, Mouse.LEFT, modifiers)
                    1
                } ?: 0
            } catch (e: FindFailed) {
                0
            }


    override fun rightClick(): Int = rightClick(center, 0)
    override fun <PFRML : Any?> rightClick(target: PFRML): Int = rightClick(target, 0)
    override fun <PFRML : Any?> rightClick(target: PFRML, modifiers: Int): Int =
            try {
                getLocationFromTarget(target)?.let {
                    mouse.click(it, Mouse.RIGHT, modifiers)
                    1
                } ?: 0
            } catch (e: FindFailed) {
                0
            }

    override fun hover(): Int = hover(center)
    override fun <PFRML : Any?> hover(target: PFRML): Int =
            try {
                getLocationFromTarget(target)?.let {
                    mouse.moveTo(it)
                    1
                } ?: 0
            } catch (e: FindFailed) {
                0
            }

    override fun <PFRML : Any?> dragDrop(target: PFRML): Int = dragDrop(lastMatch, target)

    override fun <PFRML : Any?> dragDrop(t1: PFRML, t2: PFRML): Int =
            getLocationFromTarget(t1)?.let { l1 -> getLocationFromTarget(t2)?.let { mouse.dragDrop(l1, it) } } ?: 0

    override fun <PFRML : Any?> drag(target: PFRML): Int = getLocationFromTarget(target)?.let { mouse.drag(it) } ?: 0

    override fun <PFRML : Any?> dropAt(target: PFRML): Int = getLocationFromTarget(target)?.let { mouse.dropAt(it) } ?: 0

    override fun type(text: String): Int = type(text, 0)
    override fun type(text: String, modifiers: String): Int = type(text, SKryptonKeyboard.parseModifiers(modifiers))
    override fun type(text: String, modifiers: Int): Int {
        keyboard.type(null, text, modifiers)
        return 1
    }

    override fun <PFRML : Any?> type(target: PFRML, text: String): Int = type(target, text, 0)
    override fun <PFRML : Any?> type(target: PFRML, text: String, modifiers: String): Int =
            type(target, text, SKryptonKeyboard.parseModifiers(modifiers))

    override fun <PFRML : Any?> type(target: PFRML, text: String, modifiers: Int): Int =<commit_msg>Using center as location for type function<commit_after>package com.waicool20.skrypton.sikulix

import com.waicool20.skrypton.jni.objects.WebViewHighlighter
import com.waicool20.skrypton.sikulix.input.SKryptonKeyboard
import org.sikuli.script.*
import java.awt.Color
import java.awt.Rectangle
import java.awt.event.KeyEvent
import java.util.concurrent.TimeUnit

open class SKryptonRegion(xPos: Int, yPos: Int, width: Int, height: Int) : Region(), SikuliInterface {
    constructor(region: Region, screen: SKryptonScreen) : this(region.x, region.y, region.w, region.h, screen)
    constructor(rect: Rectangle, screen: SKryptonScreen) : this(rect.x, rect.y, rect.width, rect.height, screen)
    constructor(xPos: Int, yPos: Int, width: Int, height: Int, screen: SKryptonScreen) : this(xPos, yPos, width, height) {
        this.screen = screen
    }

    init {
        x = xPos
        y = yPos
        w = width
        h = height
    }

    private val mouse by lazy { skryptonScreen().mouse }
    private val keyboard by lazy { skryptonScreen().keyboard }

    fun skryptonScreen() = screen as SKryptonScreen

    override fun setLocation(loc: Location): SKryptonRegion {
        x = loc.x
        y = loc.y
        return this
    }

    override fun setROI() = setROI(screen.bounds)
    override fun setROI(rect: Rectangle) = with(rect) { setROI(x, y, width, height) }
    override fun setROI(region: Region) = with(region) { setROI(x, y, w, h) }
    override fun setROI(X: Int, Y: Int, W: Int, H: Int) {
        x = X
        y = Y
        w = if (W > 1) W else 1
        h = if (H > 1) H else 1
    }

    override fun getCenter() = Location(x + (w / 2), y + (h / 2))
    override fun getTopLeft() = Location(x, y)
    override fun getTopRight() = Location(x + w - 1, y)
    override fun getBottomLeft() = Location(x, y + h - 1)
    override fun getBottomRight() = Location(x + w - 1, y + h - 1)

    override fun getLastMatch() = super.getLastMatch()?.let { SKryptonMatch(it, skryptonScreen()) }
    override fun getLastMatches() =
            super.getLastMatches().asSequence().toList().map { SKryptonMatch(it, skryptonScreen()) }.iterator()

    override fun offset(loc: Location?) = SKryptonRegion(super.offset(loc), skryptonScreen())
    override fun above() = SKryptonRegion(super.above(), skryptonScreen())
    override fun below() = SKryptonRegion(super.below(), skryptonScreen())
    override fun left() = SKryptonRegion(super.left(), skryptonScreen())
    override fun right() = SKryptonRegion(super.right(), skryptonScreen())

    override fun above(height: Int) = SKryptonRegion(super.above(height), skryptonScreen())
    override fun below(height: Int) = SKryptonRegion(super.below(height), skryptonScreen())
    override fun left(width: Int) = SKryptonRegion(super.left(width), skryptonScreen())
    override fun right(width: Int) = SKryptonRegion(super.right(width), skryptonScreen())

    /* Search operations */

    override fun <PSI : Any?> find(target: PSI) = super.find(target)?.let { SKryptonMatch(it, skryptonScreen()) }
    override fun <PSI : Any?> findAll(target: PSI) =
            super.findAll(target).asSequence().toList().map { SKryptonMatch(it, skryptonScreen()) }.iterator()

    override fun <PSI : Any?> wait(target: PSI) = super.wait(target).let { SKryptonMatch(it, skryptonScreen()) }
    override fun <PSI : Any?> wait(target: PSI, timeout: Double) = super.wait(target, timeout)?.let { SKryptonMatch(it, skryptonScreen()) }

    override fun <PSI : Any?> exists(target: PSI) = super.exists(target)?.let { SKryptonMatch(it, skryptonScreen()) }
    override fun <PSI : Any?> exists(target: PSI, timeout: Double) = super.exists(target, timeout)?.let { SKryptonMatch(it, skryptonScreen()) }

    //<editor-fold desc="Mouse and KeyboardActions">
    override fun click(): Int = click(center, 0)

    override fun <PFRML : Any?> click(target: PFRML): Int = click(target, 0)
    override fun <PFRML : Any?> click(target: PFRML, modifiers: Int): Int =
            try {
                getLocationFromTarget(target)?.let {
                    mouse.click(it, Mouse.LEFT, modifiers)
                    1
                } ?: 0
            } catch (e: FindFailed) {
                0
            }


    override fun doubleClick(): Int = doubleClick(center, 0)
    override fun <PFRML : Any?> doubleClick(target: PFRML): Int = doubleClick(target, 0)
    override fun <PFRML : Any?> doubleClick(target: PFRML, modifiers: Int): Int =
            try {
                getLocationFromTarget(target)?.let {
                    mouse.doubleClick(it, Mouse.LEFT, modifiers)
                    1
                } ?: 0
            } catch (e: FindFailed) {
                0
            }


    override fun rightClick(): Int = rightClick(center, 0)
    override fun <PFRML : Any?> rightClick(target: PFRML): Int = rightClick(target, 0)
    override fun <PFRML : Any?> rightClick(target: PFRML, modifiers: Int): Int =
            try {
                getLocationFromTarget(target)?.let {
                    mouse.click(it, Mouse.RIGHT, modifiers)
                    1
                } ?: 0
            } catch (e: FindFailed) {
                0
            }

    override fun hover(): Int = hover(center)
    override fun <PFRML : Any?> hover(target: PFRML): Int =
            try {
                getLocationFromTarget(target)?.let {
                    mouse.moveTo(it)
                    1
                } ?: 0
            } catch (e: FindFailed) {
                0
            }

    override fun <PFRML : Any?> dragDrop(target: PFRML): Int = dragDrop(lastMatch, target)

    override fun <PFRML : Any?> dragDrop(t1: PFRML, t2: PFRML): Int =
            getLocationFromTarget(t1)?.let { l1 -> getLocationFromTarget(t2)?.let { mouse.dragDrop(l1, it) } } ?: 0

    override fun <PFRML : Any?> drag(target: PFRML): Int = getLocationFromTarget(target)?.let { mouse.drag(it) } ?: 0

    override fun <PFRML : Any?> dropAt(target: PFRML): Int = getLocationFromTarget(target)?.let { mouse.dropAt(it) } ?: 0

    override fun type(text: String): Int = type(text, 0)
    override fun type(text: String, modifiers: String): Int = type(text, SKryptonKeyboard.parseModifiers(modifiers))
    override fun type(text: String, modifiers: Int): Int {
        keyboard.type(center, text, modifiers)
        return 1
    }

    override fun <PFRML : Any?> type(target: PFRML, text: String): Int = type(target, text, 0)
    override fun <PFRML : Any?> type(target: PFRML, text: String, modifiers: String): Int =
            type(target, text, SKryptonKeyboard.parseModifiers(modifiers))

    override fun <PFRML : Any?> type(target: PFRML, text: String, modifiers: Int): Int =
{'fraction_non_alphanumeric': 0.12921558363867494, 'fraction_numerical': 0.006879533388170194, 'mean_word_length': 2.794042553191489, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 51, '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': '3522298', 'n_tokens_mistral': 4290, 'n_tokens_neox': 4146, 'n_words': 1108}