Skip to content

Latest commit

History

102 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

php-libui

PHP bindings to the libui C library.

libui is a lightweight, portable GUI library that uses the native GUI technologies of each platform it supports.

Requirements

  • PHP >= 8.1
  • PHP FFI extension available
  • libui latest version

A Simple Example

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

first download libui dynamic library from https://github.com/andlabs/libui/releases or checkout source for self-build, then load in php code use following code

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();

Note: When Call libui C function need remove ui prefix of name, then to lower case first char

Use UIBuild create UI

Basic Usage:

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();

build config structure

  • build config is xml file, see tests/uibuild.xml
  • In xml file, all event attribute name must be prefix on, onclick,onchange,ondraw, value is callable name, it is not Event instance, callable name support php variable and php callable:$var,func_name,CLASS::method, do not support array callable. variable is GLOBALS
  • attr value support GLOBALS variable and constant, prefix $``@ as $var or @CONST
  • also build config support array, main key contain body,menu and window attribute key; in config array, element key is attr name, element value is attr value,similar the following:
[
'title' => 'window title name',
'width' => 600,
'menu' => [],
'body' => [
'name' => 'box'
'childs' => []
]
]
<?xml version="1.0" encoding="UTF-8"?>
<windowtitle="window title name"width="600"height="800"fullscreen="0"onclose="HTTP::quit">
<menu>
</menu>
<body>
</body>
</window>

window attribute key list:

attrtypeDescriptionDefault Vlaue
titlestringwindow titleNo Win Title
widthintwindow width800
heightintwindow height640
borderintwindow border0
marginintwindow margin0
fullscreenintwindow margin0
quit\UI\Eventquit callbacknull
close\UI\Eventclose callbacknull
resize\UI\Eventresize callbacknull

menu array

The array level 1 of item element is one menu, similar:

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

Top menu only contain title, id, childs , the title value will display in window, every element of childs array is submenu that display in drop-down menu. if element is string and equral hr will display a separator current Build UI Config of submenu only contain the following attr:

attrtypeDescriptionrequire
titlestringmenu titleyes
typestringmenu type, value is text,quit,about,preferences or checkbox, default is textno
click\UI\Eventclick callbackno
childsarraychild menu listno

body array

every element key of body array is control config, widget element is control name and attr element is control attr. see examples/table.phpBuild UI current only support the following control:

  1. button, Button control,contain the following attr:

    attrtypeDescriptionrequire
    typestringcontrol type, smiliar HTML <button> tag of type attr, potential value is following:
    1. file, open file button
    2. save, save file button
    3. font, select font button
    4. color, select color button
    5. button, is default value
    yes
    titlestringbutton label nameyes
    click\UI\Eventwhen click callback, when type file and save is after select file callno
    change\UI\Eventonly when color and font available, select color or font be callno
    idstringno
  2. box box layout,the following attr:

    attrtypeDescriptionrequire
    dirstringlayout direction, Specify one of h is horizontal and default value, v is verticalyes
    paddedintpadding value, default is 0no
    child_fitintWhether to automatically adaptno
    childsarraysub control listno
    idstringno
  3. group group layout, have title and margin, child,id attr

  4. label text control, only has title and id attr

  5. hr horizontal separator, no attr

  6. vr vertical separator, no attr

  7. input input control, the following attr:

    attrtypeDescriptionrequire
    typestringmiliar HTML <input> tag of type attr, specify one of the following value:
    1. password password entry control
    2. search search entry control
    3. textarea multiline entry control
    4. radio radio
    5. select select
    6. checkbox checkbox
    7. text, is default value
    8.number is Spinbox
    9.slider
    yes
    readonlyboolwhether readonlyno
    wrapboolonly textarea is available, false is non wrapping textareano
    optionarrayradio and select available, element value is option title, key is natural order numberno
    change\UI\Eventexclude checkbox and radiono
    titlestringcheckbox availableyes
    click\UI\Eventonly radio and checkbox availableno
    minintnumber and slider availableyes
    maxintnumber and slider availableyes
    idstringno
    editableboolselect availableno
  8. form form layout, has padded, childs,id attr, child widget has label attr

  9. grid grid layout, the following attr:

    attrtypeDescriptionrequire
    paddedintno
    child_leftintno
    child_topintno
    child_widthintno
    child_heightintno
    child_hexpandintno
    child_halignintno
    child_vexpandintno
    child_valignintno
    childsarrayno
    idstringno
  10. table table control, table cell has change event, it is following sub key :

    attrtypeDescriptionrequire
    tharrayevery element of value is array, key is id, has the following attr:
    1. editable, bool type, the column is whether editable
    2. textColor
    3. title
    4. type, specify value of button, image, imgtext, progress, checkbox, checkboxtext, color, text
    5. widget has change attr, for row column change callback
    yes
    tbodyarraythe table row value list, every element is one row value, when row of column is array has the following attr:
    1. image type, has srcwidthheight
    2. color type has r,g,b
    3. rowBgcolor
    yes
    idstringno
  11. tab tab control, has page sub array, page array every element value is page child control and key is page title

  12. img image control, has flowing attr:

    attrtypeDescriptionrequire
    srcarrayis image paths list, every element value is image file path, key is natural order numberyes
    widthintthe image control width, default is src first element image widthno
    heightintthe image control heigth, default is src first element image widthno
    idstringno
  13. datetime datetime control

    attrtypedescriptionrequire
    typestringspecify one of value time,date,datetimeyes
    change\UI\Eventno
    idstringno
  14. progress, has id attr

  15. area,canvas create Area

  16. attribute create Attribute

  17. drawtext create TextLayout

  18. string create a AttributeString

  19. unsupport control must call libui C function by UI\UI

  20. UI\Event, all event callback class, The signature of the callback is as follows:

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

Control common method:

  • show()
  • hide()
  • enable()
  • disable()
  • destroy()
  • parent()
  • setParent($parent)
  • isVisible()
  • isEnabled()

specify control see class statement in control directory

UI method

see UI.php

UIBuild method

see UIBuild.php

About

php bindings to the libui C library.

Topics

Resources

Stars

72 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages