"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerScreenMouseMonitor = registerScreenMouseMonitor;
exports.unregisterScreenMouseMonitor = unregisterScreenMouseMonitor;
var electron_1 = require("electron");
var constant_1 = require("../constant");
var mouseMonitorInterval = null;
var transparentWindow = null;
// 创建一个透明窗口，用于监听鼠标事件
function createTransparentWindow() {
    return constant_1.isLinux
        ? new electron_1.BrowserWindow({
            width: 1,
            height: 1,
            transparent: true,
            backgroundColor: 'rgba(0, 0, 0, 0)',
            frame: false,
            hasShadow: false,
            movable: false,
            resizable: false,
            skipTaskbar: true,
        })
        : null;
}
function registerScreenMouseMonitor(mainWindow) {
    if (!mainWindow.isVisible()) {
        return;
    }
    if (transparentWindow) {
        transparentWindow.destroy();
        transparentWindow = null;
    }
    transparentWindow = createTransparentWindow();
    if (mouseMonitorInterval) {
        clearInterval(mouseMonitorInterval);
        mouseMonitorInterval = null;
    }
    mouseMonitorInterval = setInterval(function () {
        if (!mainWindow || mainWindow.isDestroyed()) {
            unregisterScreenMouseMonitor();
            return;
        }
        var pos = electron_1.screen.getCursorScreenPoint();
        var bounds = mainWindow.getBounds();
        if (transparentWindow && !transparentWindow.isDestroyed()) {
            transparentWindow.setBounds(bounds);
        }
        var centerX = bounds.x + bounds.width / 2;
        var centerY = bounds.y + bounds.height / 2;
        // 判断鼠标是否在窗口中心区域附近
        var isNearCenter = Math.abs(pos.x - centerX) < bounds.width / 4 + 40 &&
            Math.abs(pos.y - centerY) < bounds.height / 2;
        if (isNearCenter) {
            mainWindow.setIgnoreMouseEvents(false);
            mainWindow.moveTop();
            mainWindow.focus();
        }
    }, 500);
}
function unregisterScreenMouseMonitor() {
    if (mouseMonitorInterval) {
        clearInterval(mouseMonitorInterval);
        mouseMonitorInterval = null;
        if (transparentWindow && !transparentWindow.isDestroyed()) {
            transparentWindow.destroy();
            transparentWindow = null;
        }
    }
}