function createMainWindow() {
if (mainWindow) {
try { mainWindow.close(); } catch (e) {}
mainWindow = null;
}

mainWindow = floaty.window(
    <frame>
        <horizontal padding="1" bg="#00000000" w="125" h="40">
            <button id="startBtn" text="开始" bg="#4CAF50" w="35" h="35" marginLeft="5"/>
            <button id="pauseBtn" text="暂停" bg="#FF9800" w="35" h="35" marginLeft="5"/>
            <button id="configBtn" text="参数" bg="#2196F3" w="35" h="35" marginLeft="5"/>
        </horizontal>
    </frame>
);

mainWindow.setPosition(150, 50);

let startBtn = mainWindow.findView("startBtn");
let pauseBtn = mainWindow.findView("pauseBtn");
let configBtn = mainWindow.findView("configBtn");

ui.run(() => {
    pauseBtn.setEnabled(false);
});

startBtn.click(() => {
    ui.run(() => {
        handlers.start();
        startBtn.setEnabled(false);
        pauseBtn.setEnabled(true);
    });
});

pauseBtn.click(() => {
    ui.run(() => {
        handlers.pause();
        startBtn.setEnabled(true);
        pauseBtn.setEnabled(false);
    });
});

configBtn.click(() => {
    ui.run(handlers.showConfigDialog);
});

let closeCheckInterval = setInterval(() => {
    if (mainWindow && mainWindow.getX() < 0) {
        ui.run(() => {
            try {
                if (mainWindow) {
                    mainWindow.close();
                    mainWindow = null;
                }
            } catch (e) {
                console.error("关闭主窗口失败:", e);
            }
        });
        clearInterval(closeCheckInterval);
    }
}, 500);

}

createMainWindow();
如何增加一个控制台按钮,下面有开始、暂停、参数、重置、退出,点击控制台按钮,可以弹出或者隐藏下面的功能按钮。请帮我生成代码

已存档
xv7j3ndpak · 2 个月前

回答

Hamibot AI
1
· 2 个月前

根据你的需求,我将修改 createMainWindow 函数,在现有基础上添加一个“控制台”按钮,点击它可展开/收起功能按钮区域(开始、暂停、参数等)。以下是完整的实现代码:

function createMainWindow() {
    if (mainWindow) {
        try { mainWindow.close(); } catch (e) {}
        mainWindow = null;
    }

    // 主窗口布局
    mainWindow = floaty.window(
        <frame>
            <!-- 控制台按钮 -->
            <button id="consoleBtn" text="控制台 ▼" bg="#9C27B0" w="125" h="40" margin="1"/>

            <!-- 功能按钮区域,默认隐藏 -->
            <vertical id="funcPanel" bg="#00000000" w="125" h="auto" padding="1" visibility="gone">
                <horizontal bg="#00000000" w="match_parent" h="wrap_content">
                    <button id="startBtn" text="开始" bg="#4CAF50" w="35" h="35" marginLeft="5"/>
                    <button id="pauseBtn" text="暂停" bg="#FF9800" w="35" h="35" marginLeft="5"/>
                    <button id="configBtn" text="参数" bg="#2196F3" w="35" h="35" marginLeft="5"/>
                </horizontal>
                <horizontal bg="#00000000" w="match_parent" h="wrap_content" marginTop="5">
                    <button id="resetBtn" text="重置" bg="#F44336" w="35" h="35" marginLeft="5"/>
                    <button id="exitBtn" text="退出" bg="#FF5722" w="35" h="35" marginLeft="5"/>
                </horizontal>
            </vertical>
        </frame>
    );

    mainWindow.setPosition(150, 50);

    let consoleBtn = mainWindow.findView("consoleBtn");
    let funcPanel = mainWindow.findView("funcPanel");
    let startBtn = mainWindow.findView("startBtn");
    let pauseBtn = mainWindow.findView("pauseBtn");
    let configBtn = mainWindow.findView("configBtn");
    let resetBtn = mainWindow.findView("resetBtn");
    let exitBtn = mainWindow.findView("exitBtn");

    // 初始状态:暂停按钮不可用
    ui.run(() => {
        pauseBtn.setEnabled(false);
    });

    // 控制台按钮点击事件
    consoleBtn.click(() => {
        ui.run(() => {
            let isVisible = funcPanel.getVisibility() == 0; // View.VISIBLE is 0
            funcPanel.setVisibility(isVisible ? 8 : 0); // GONE is 8
            consoleBtn.setText(isVisible ? "控制台 ▼" : "控制台 ▲");
        });
    });

    // 开始按钮
    startBtn.click(() => {
        ui.run(() => {
            handlers.start();
            startBtn.setEnabled(false);
            pauseBtn.setEnabled(true);
        });
    });

    // 暂停按钮
    pauseBtn.click(() => {
        ui.run(() => {
            handlers.pause();
            startBtn.setEnabled(true);
            pauseBtn.setEnabled(false);
        });
    });

    // 参数按钮
    configBtn.click(() => {
        ui.run(handlers.showConfigDialog);
    });

    // 重置按钮
    resetBtn.click(() => {
        ui.run(() => {
            handlers.reset(); // 假设有 reset 方法处理逻辑
            startBtn.setEnabled(true);
            pauseBtn.setEnabled(false);
        });
    });

    // 退出按钮
    exitBtn.click(() => {
        ui.run(() => {
            handlers.exit(); // 假设有 exit 方法处理逻辑
            mainWindow.close();
            hamibot.exit();
        });
    });

    // 窗口位置超出屏幕左侧时自动关闭
    let closeCheckInterval = setInterval(() => {
        if (mainWindow && mainWindow.getX() < 0) {
            ui.run(() => {
                try {
                    if (mainWindow) {
                        mainWindow.close();
                        mainWindow = null;
                    }
                } catch (e) {
                    console.error("关闭主窗口失败:", e);
                }
            });
            clearInterval(closeCheckInterval);
        }
    }, 500);
}

// 调用创建函数
createMainWindow();

功能说明:

  • 控制台按钮 (consoleBtn):用于切换功能面板的显示与隐藏。
  • 功能面板 (funcPanel):包含两个水平布局,分别放置“开始、暂停、参数”和“重置、退出”按钮。
  • 动态显示/隐藏:通过 `setVisibility
ℹ️ 本回答由 AI 生成,内容仅供参考