toknot/php-libui

PHP 对 libui C 库的绑定。

0.2.4 2024-06-10 10:00 UTC

This package is auto-updated.

Last update: 2024-09-11 01:48:38 UTC


README

php-libui

PHP 对 libui C 库的绑定。

libui 是一个轻量级、可移植的 GUI 库,它使用每个平台支持的本机 GUI 技术。

要求

  • PHP >= 8.1
  • PHP FFI 扩展可用
  • libui 最新版本

简单示例

https://github.com/chopins/http-request

首先从 https://github.com/andlabs/libui/releases 下载 libui 动态库或检出源代码自行构建,然后在 php 代码中使用以下代码加载

include '/src/UI.php';
$ui = new \UI\UI('/usr/lib64/libui.so'); //load libui dynamic library
$ui->init();
$mainwin = $ui->newWindow("libui Control Gallery", 640, 480,1);
$ui->controlShow($mainwin);
$ui->main();

注意:调用 libui C 函数时,需要去掉名称中的 ui 前缀,并将首字母转换为小写

使用 UIBuild 创建 UI

基本用法

include '/src/UI.php';
$ui = new \UI\UI('/usr/lib64/libui.so');
$config = ['title' => 'test', 'width' => 600,'height' => 450];
$build = $ui->build($config);
$build->show();

构建配置结构

  • 构建配置是 xml 文件,见 tests/uibuild.xml
  • 在 xml 文件中,所有事件属性名称必须以 on 前缀,例如 onclickonchangeondraw,值是可调用名称,它不是事件实例,可调用名称支持 php 变量和 php 可调用:$varfunc_nameCLASS::method,不支持数组可调用。变量是 GLOBALS
  • 属性值支持 GLOBALS 变量和常量,前缀为 $``@ 作为 $var@CONST
  • 此外,构建配置支持数组,主键包含 bodymenu窗口属性键;在配置数组中,元素键是属性名称,元素值是属性值,类似于以下内容
[
    'title' => 'window title name',
    'width' => 600,
    'menu' => [],
    'body'  => [
        'name' => 'box'
        'childs' => []
    ]
]
<?xml version="1.0" encoding="UTF-8"?>
<window title="window title name" width="600" height="800" fullscreen="0" onclose="HTTP::quit">
<menu>
    </menu>
<body>
</body>
</window>

窗口属性键列表

菜单数组

项目元素的级别 1 数组是一个菜单,类似于以下内容

[
    [
        'title' => 'File',
        'id'    => 'menu_id_1',
        'childs' => [
            ['title' => 'New File'],
            ['title' => 'Open File'],
        ]
    ],
    [
        'title' => 'Edit',
        'id'    => 'menu_id_1',
        'childs' => [
            ['title' => 'Undo'],
            ['title' => 'Copy'],
        ]
    ],
]

顶级菜单只包含 titleidchilds,其中 title 的值将在窗口中显示,childs 数组中的每个元素都是显示在下拉菜单中的子菜单。如果元素是字符串且等于 hr,将显示一个分隔符 当前构建 UI 配置的子菜单只包含以下属性:

body 数组

body 数组的每个元素键是控件配置,widget 元素是控件名称,attr 元素是控件属性。见 examples/table.php 构建 UI 当前仅支持以下控件

  1. button,按钮控件,包含以下属性

  2. box 盒子布局,以下属性

  3. group 组布局,具有 titlemarginchildid 属性

  4. label 文本控件,只有 titleid 属性

  5. hr 水平分隔符,无属性

  6. vr 垂直分隔符,无属性

  7. input 输入控件,以下属性

  8. form 表单布局,具有 paddedchildsid 属性,子控件有 label 属性

  9. grid 网格布局,以下属性

  10. table 表格控件,表格单元格有更改事件,以下是子键

  11. tab 标签控件,有 page 子数组,page 数组中的每个元素值是页面子控件,键是页面标题

  12. img 图片控件,有以下属性

  13. datetime 日期时间控件

  14. progress,有 id 属性

  15. areacanvas 创建 Area

  16. attribute 创建 Attribute

  17. drawtext 创建 TextLayout

  18. string 创建一个 AttributeString

  19. 不支持控件必须通过 UI\UI 调用 libui C 函数

  20. UI\Event,所有事件回调类,回调函数的签名如下

/**
 * @param UI\Event $callable   The object instance of current event 
 * 
 * */
function (UI\Event $callable) {}

控件通用方法

  • show()
  • hide()
  • 启用()
  • 禁用()
  • 销毁()
  • 父级()
  • 设置父级($parent)
  • 是否可见()
  • 是否启用()

指定控件,请查看控件目录中的类声明

UI方法

查看UI.php

UIBuild方法

查看UIBuild.php