libui: A new Ruby GUI library that is easy to install
Tags: ruby, tutorial
Published: 2020-12-26 12:02:44

---
title: libui: A new Ruby GUI library that is easy to install
published: true
description: libui: A new Ruby GUI library that is easy to install
tags: ruby, tutorial
---

Github : [https://github.com/kojix2/libui](https://github.com/kojix2/libui)

## Introduction

Yesterday is the day when Ruby 3.0 is released.
I released a binding of [libui](https://github.com/andlabs/libui) that allows you to create a simple GUI with Ruby. I know there are still many room for improvemdents, but I will write the article.

| Windows | Mac | Linux |
|---------|-----|-------|
|<img src="https://user-images.githubusercontent.com/5798442/103118046-900ea780-46b0-11eb-81fc-32626762e4df.png">|<img src="https://user-images.githubusercontent.com/5798442/103118059-99980f80-46b0-11eb-9d12-324ec4d297c9.png">|<img src="https://user-images.githubusercontent.com/5798442/103118068-a0bf1d80-46b0-11eb-8c5c-3bdcc3dcfb26.png">|

## Background and motivation

Many GUI libraries for Ruby have been created so far. [Ruby/Tk](https://github.com/ruby/tk) , [Ruby/Gtk](https://github.com/ruby-gnome/ruby-gnome) , the predominant [shoes](http://shoesrb.com/), [Qt](https://github.com/ryanmelt/qtbindings), [FXRuby](https://github.com/larskanis/fxruby), and much more.

However, there were still few easy ways to create GUI applications in a Windows environment. To use Ruby/Gtk or Ruby/Tk, you need to install a toolkit such as Tk or GTK. Experienced users may want to quickly add a GUI to their utility and distribute it to their friends. However, it is not as easy as it sounds. To meet these needs, I created a binding for a portable GUI library called libui.

* [https://github.com/kojix2/LibUI](https://github.com/kojix2/LibUI)

## What is libui?

libui is a very lightweight multi-platform GUI tool written in C. The Ruby binding is compatible with version 4.1-alpha.

* [https://github.com/andlabs/libui](https://github.com/andlabs/libui)

## Installation

```sh
gem install libui
```

## Quick Start

![libui](https://dev-to-uploads.s3.amazonaws.com/i/mqi541pacojlkkprs2iq.gif)

```ruby
require 'libui'

UI = LibUI

UI.init

main_window = UI.new_window('Basic Entry', 300, 50, 1)
UI.window_on_closing(main_window) do
  puts 'Bye Bye'
  UI.control_destroy(main_window)
  UI.quit
  0
end

hbox = UI.new_horizontal_box
UI.window_set_child(main_window, hbox)

entry = UI.new_entry
UI.entry_on_changed(entry) do
  puts UI.entry_text(entry).to_s
  $stdout.flush # For Windows
end
UI.box_append(hbox, entry, 1)

button = UI.new_button('Button')
UI.button_on_clicked(button) do
  text = UI.entry_text(entry).to_s
  UI.msg_box(main_window, 'You entered', text)
  0
end

UI.box_append(hbox, button, 0)

UI.control_show(main_window)
UI.main
UI.quit
```

(Please note that it cannot be executed well with irb or pry)

(What is Qiitan? : The original article was posted on [Qiita](https://qiita.com/) a site like dev.to in Japan. [Qiitan](https://suzuri.jp/qiita/753918/sticker/m/white) is a green, round, cat-like animal.)

If you want to know how to use it, see Examples in the repository.

## General Rules

Compared to original libui written in C,

* The method names are snake_case.
* If the last argument is nil, it can be omitted.
*You can pass a block as a callback.
    * Please return 0 explicitly in the block.
    * The block will be converted to a Proc object and added to the last argument.
    * Even in that case, it is possible to omit the last argument nil.

## Not object oriented?

* At the moment, it is not object-oriented.
    * Instead of providing a half-baked object-oriented approach, leave it as is.
    * Several DSLs are under development.

## Comparison with existing libui binding

Ruby bindings to libui already exist.

* [https://github.com/jamescook/libui-ruby](https://github.com/jamescook/libui-ruby)

However, this package is not so easy to install. LibUI solves this problem.

* The C language function is called using the Ruby standard library fiddle. (No additional libraries required!)
* The gem package contains the official release of the libui shared library versions 4.1 for Windows, Mac, and Linux. 
    * All together, the file size is only 1.4MB. It is considerably smaller than Ruby/Tk. If you only use Windows `libui.so`, `libui.dylib` can be deleted. In this case, `libui.dll` is only 269K.

## Pros and cons

Libui is good for creating small tools for Windows. Parhaps, It may be the best Ruby gem available today for this purpose. Libui is not a general-purpose GUI library. It is not as expressive as a comprehensive library such as ruby-gtk3. Not object-oriented. You may need to work with pointers. I hope that these shortcomings can be improved in the future.


Have a nice day!