Github : https://github.com/kojix2/libui
Introduction
Yesterday is the day when Ruby 3.0 is released.
I released a binding of 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 |
---|---|---|
Background and motivation
Many GUI libraries for Ruby have been created so far. Ruby/Tk , Ruby/Gtk , the predominant shoes, Qt, 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.
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.
Installation
gem install libui
Quick Start
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 a site like dev.to in Japan. Qiitan 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.
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.
- All together, the file size is only 1.4MB. It is considerably smaller than Ruby/Tk. If you only use Windows
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!
Top comments (0)