clue/zenity-react

Zenity 允许您使用 PHP 构建图形桌面(GUI)应用程序,它建立在 ReactPHP 之上。

v0.4.4 2020-12-13 15:23 UTC

This package is auto-updated.

Last update: 2024-09-11 21:41:53 UTC


README

CI status installs on Packagist

Zenity 允许您使用 PHP 构建图形桌面(GUI)应用程序,它建立在 ReactPHP 之上。

https://help.gnome.org/users/zenity/3.24/question.html

Zenity 是一个小程序,允许在命令行脚本中创建简单的 GTK+ 对话框。Zenity 已经包含在基于 Ubuntu 的发行版中,无需安装 - 因此,这个库应该可以即插即用。否则,您可能需要自行 安装 Zenity。这个库提供了一个易于使用的包装器,用于启动 Zenity 进程以使用 PHP 构建图形桌面应用程序。

目录

支持我们

我们在开发、维护和更新我们出色的开源项目上投入了大量时间。您可以通过 在 GitHub 上成为赞助商 来帮助我们保持我们工作的这种高质量。赞助商将获得许多回报,请参阅我们的 赞助页面 以获取详细信息。

让我们一起把这些项目提升到新的高度! 🚀

快速入门示例

安装后,您可以使用以下代码打开一个提示,要求用户输入他的名字,并在另一个信息对话框中显示它。

<?php

require __DIR__ . '/vendor/autoload.php';

$launcher = new Clue\React\Zenity\Launcher();

$entry = new EntryDialog();
$entry->setText('What\'s your name?');
$entry->setEntryText(getenv('USER')); // prefill with current user

$launcher->launch($entry)->then(function ($name) use ($launcher) {
    $launcher->launch(new InfoDialog('Welcome to zenity-react, ' . $name .'!'));
}, function (Exception $e) {
    echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

需要更多示例?请查看 示例文件夹

用法

启动器

如上例所示,必须实例化一次 Launcher,它负责启动每个 Zenity 对话框。它负责运行底层的 zenity 进程,并报告其状态和用户交互。

它使用 react/event-loop 组件来实现一个异步工作流程,您可以同时启动多个对话框并执行更多 I/O 工作。这个库公开了简单阻塞 API 和更高级的异步 API。

此类接受一个可选的 LoopInterface|null $loop 参数,可以用来传递用于此对象的事件循环实例。您可以使用 null 值来使用 默认循环。除非您确信要显式使用给定的事件循环实例,否则不应提供此值。

$launcher = new Clue\React\Zenity\Launcher();

setBin()

启动进程时,它假定您的 zenity 二进制文件位于您的系统 $PATH 中。如果不是,您可以通过以下方式显式设置其路径

$launcher->setBin('/some/other/path/zenity');

waitFor()

可以使用 waitFor($dialog) 方法启动指定的对话框,并等待 Zenity 进程返回结果。这个简单的阻塞 API 可以让您快速入门,而无需暴露所有复杂的异步细节——并且缺少一些高级功能。

$result = $launcher->waitFor($dialog);

launch()

可以使用 launch($dialog) 方法异步启动指定的对话框,并返回一个 Promise 对象,当 Zenity 进程返回时将得到解决。这个异步 API 允许您同时启动多个对话框,并同时执行更多的 I/O 操作。

$launcher->launch($dialog)->then(
    function ($result) {
        // info dialogs complete with a boolean true result
        // text dialogs complete with their respective text
    },
    function ($reason) {
        // dialog was cancelled or there was an error launching the process
    }
});

launchZen()

可以使用 launchZen($dialog) 方法异步启动指定的对话框,并返回 BaseZen 类的实例。这个实例提供了控制 Zenity 进程的方法,在等待结果的同时。某些对话框类型也支持修改向用户显示的信息。

$zen = $launcher->launchZen($dialog);
Loop::addTimer(3.0, function () use ($zen) {
    $zen->close();
});

$zen->promise()->then(function ($result) {
    // dialog completed
}, function (Exception $e) {
    echo 'Error: ' . $e->getMessage() . PHP_EOL;
});

构建器

此外,Builder 实现了更简单的接口,用于常用对话框。这主要是为了方便,让您更容易开始。这些方法应该相当直观,直接映射到下面的 Zenity 对话框列表。

$builder = new Builder();
$dialog = $builder->info('Hello world');

对于更复杂的情况,您也可以直接实例化下面的类。

对话框

Dialog API 严格模仿 Zenity 的命令行 API,因此如果您已经在任何其他命令行脚本中使用过它,那么它应该很熟悉。

AbstractDialog

所有 Zenity 对话框的抽象基类(下面详细介绍每个具体类型)。

日历对话框

https://help.gnome.org/users/zenity/3.24/calendar.html

颜色选择对话框

https://help.gnome.org/users/zenity/3.24/colorselection.html

输入对话框

$builder->entry($prompt = null, $prefill = null);

https://help.gnome.org/users/zenity/3.24/entry.html

错误对话框

$builder->error($text, $title = null);

https://help.gnome.org/users/zenity/3.24/error.html

文件选择对话框

$builder->fileSelection($title = null, $multiple = false);
$builder->fileSave($title = null, $previous = null);
$builder->directorySelection($title = null, $multiple = false);

https://help.gnome.org/users/zenity/3.24/fileselection.html

表单对话框

https://help.gnome.org/users/zenity/3.24/forms.html

信息对话框

$builder->info($text, $title = null);

https://help.gnome.org/users/zenity/3.24/info.html

列表对话框

$builder->listCheck(array $list, $text = null, array $selected = null);
$builder->listMenu(array $list, $text = null);
$builder->listRadio(array $list, $text = null, $selected = null);
$builder->table(array $rows, array $columns = null, $text = null);

在列表多选的情况下,$selected 是您希望预先选择的 $list 中项的键的数组。在列表单选的情况下,$selected 是您希望预先选择的 $list 中项的键。

https://help.gnome.org/users/zenity/3.24/list.html

通知对话框

$builder->notification($text);
$builder->notifier();

https://help.gnome.org/users/zenity/3.24/notification.html

密码对话框

https://help.gnome.org/users/zenity/3.24/password.html

进度对话框

$builder->progress($text = null);
$builder->pulsate($text = null);

https://help.gnome.org/users/zenity/3.24/progress.html

问题对话框

$builder->question($question, $title = null);

https://help.gnome.org/users/zenity/3.24/question.html

刻度对话框

https://help.gnome.org/users/zenity/3.24/scale.html

文本信息对话框

$builder->text($filename, $title = null);
$builder->editable($filename, $title = null);
$builder->confirmLicense($filename, $confirmation, $title = null);

https://help.gnome.org/users/zenity/3.24/text.html

警告对话框

$builder->warning($text, $title = null);

https://help.gnome.org/users/zenity/3.24/warning.html

安装

建议使用 Composer 安装此库。您是 Composer 新手吗?

这将安装最新支持的版本。

composer require clue/zenity-react:^0.4.4

有关版本升级的详细信息,请参阅 CHANGELOG

该项目旨在在所有平台上运行,因此不要求任何 PHP 扩展,并支持在从 PHP 5.3 到当前 PHP 8+ 和 HHVM 上运行。强烈建议您使用此项目的最新支持版本。

显然,此库需要 Zenity 二进制文件本身。Zenity 已经包含在基于 Ubuntu 的发行版中,并且在那里不需要安装。在 Debian 和 Ubuntu 基础的发行版上,您可以通过以下方式确保已安装:

# usually not required
sudo apt-get install zenity

否则,您可能需要自己安装 Zenity(使用您喜欢的搜索引擎,下载适当的发布 tarball 或从源代码编译)。Zenity 在其他平台上没有官方支持,但存在几个非官方版本。

目前不支持在 Windows 上运行。

此库假定 Zenity 已安装到您的 PATH 中。如果没有,您可以显式设置其路径如下:

$launcher = new Clue\React\Zenity\Launcher();
$launcher->setBin('/path/to/zenity');

测试

要运行测试套件,您首先需要克隆此存储库,然后通过 Composer 安装所有依赖项。

composer install

要运行测试套件,请转到项目根目录并运行:

vendor/bin/phpunit

许可

该项目在宽松的 MIT 许可证 下发布。

您知道吗?我提供定制开发服务,并为发行版赞助和贡献发布发票。有关详细信息,请联系我(@clue)。