PHP code example of yangweijie / libui-builder
1. Go to this page and download the library: Download yangweijie/libui-builder library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yangweijie / libui-builder example snippets
use Kingbes\Libui\App;
use Kingbes\Libui\View\Builder;
use Kingbes\Libui\View\State\StateManager;
App::init();
$state = StateManager::instance();
$state->set('username', '');
$app = Builder::window()
->title('登录窗口')
->size(400, 300)
->contains([
Builder::vbox()->padded(true)->contains([
// 使用 GroupBuilder 创建带标题的分组
Builder::group()
->title('用户信息')
->margined(true)
->contains([
Builder::grid()->padded(true)->form([
[
'label' => Builder::label()->text('用户名:'),
'control' => Builder::entry()
->id('usernameInput')
->bind('username')
->placeholder('请输入用户名')
]
])
]),
Builder::button()
->text('登录')
->onClick(function($button, $state) {
echo "登录: " . $state->get('username') . "\n";
})
])
]);
$app->show();
use Kingbes\Libui\App;
use Kingbes\Libui\View\HtmlRenderer;
use Kingbes\Libui\View\State\StateManager;
App::init();
$state = StateManager::instance();
$state->set('username', '');
$handlers = [
'handleLogin' => function($button, $state) {
echo "登录: " . $state->get('username') . "\n";
}
];
$app = HtmlRenderer::render('views/login.ui.html', $handlers);
$app->show();
// 初始化状态
$state = StateManager::instance();
$state->set('username', '');
$state->set('count', 0);
// 监听变化
$state->watch('count', function($newValue) {
echo "Count 变更为: {$newValue}\n";
});
// 批量更新
$state->update([
'username' => 'admin',
'count' => 10
]);
// PHP 中处理事件
$handlers = [
'handleClick' => function($button, $state) {
echo "按钮被点击\n";
},
'handleChange' => function($value, $component) {
echo "新值: {$value}\n";
},
'handleSelect' => function($index) {
echo "选择了索引: {$index}\n";
}
];
Builder::window()
->title('我的应用')
->size(800, 600)
->contains([
Builder::grid()->...
]);
class LoginHandlers {
public static function getHandlers(): array {
return [
'handleLogin' => [self::class, 'login'],
'handleReset' => [self::class, 'reset'],
];
}
public static function login($button, $state) {
// 登录逻辑
}
public static function reset($button, $state) {
// 重置逻辑
}
}
html
<group title="用户信息" margined="true">
<grid padded="true">
<label row="0" col="0">用户名:</label>
<input row="0" col="1" bind="username"/>
<label row="1" col="0">密码:</label>
<input row="1" col="1" type="password" bind="password"/>
</grid>
</group>
bash
php example/htmlLogin.php
php example/htmlFull.php
html
<window title="我的应用" size="800,600">
<grid padded="true">
<!-- 清晰的界面定义 -->
</grid>
</window>
project/
├── views/ # HTML 模板
│ ├── login.ui.html
│ └── dashboard.ui.html
├── handlers/ # 事件处理器
│ ├── LoginHandlers.php
│ └── DashboardHandlers.php
├── state/ # 状态管理
│ └── AppState.php
└── app.php # 主入口