Hello.
Today is the day when Ruby 3.0 is released. The other day, I released a Gem called libui that allows you to create a GUI with Ruby. (Binding of libui) I think there are still many parts that are missing, but I will write an article.
Windows | Mac | Linux |
---|---|---|
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. This is a barrier for beginners. Also, even veterans may want to quickly add a GUI to the utility and distribute it. To solve this problem, I created a binding for a portable GUI library called libui.
As of December 25, 2020, libui is probably the easiest GUI library to install in the Ruby world (probably).
libui is a very lightweight multi-platform GUI tool written in C. The Ruby binding is compatible with version 4.1.
gem install libui
A simple usage example is shown below.
require 'libui'
UI = LibUI
UI.init
#Create window
main_window = UI.new_window('hello world', 300, 50, 1)
#Allow the window to disappear with the X button
UI.window_on_closing(main_window) do
puts 'Bye Bye'
UI.control_destroy(main_window)
UI.quit
0
end
#Box for placement
hbox = UI.new_horizontal_box
#Add box to window
UI.window_set_child(main_window, hbox)
#Creating an input field
entry = UI.new_entry
#Displayed in the terminal where the characters are entered
UI.entry_on_changed(entry) do
puts UI.entry_text(entry).to_s
$stdout.flush #Display on the terminal in real time
end
#Add to box
UI.box_append(hbox, entry, 1)
#button
button = UI.new_button('Button')
#Brings up a dialog when the button is pressed
UI.button_on_clicked(button) do
text = UI.entry_text(entry).to_s
UI.msg_box(main_window, 'The characters you typed are:', text)
0
end
#Add to box
UI.box_append(hbox, button, 0)
#Show window
UI.control_show(main_window)
UI.main
UI.quit
(Please note that it cannot be executed well with irb or pry)
If you want to know how to use it in a little more detail, see Examples in the repository.
Compared to the original libui written in C,
Ruby bindings already exist in libui.
However, this package has the weakness of being cumbersome to install. libui solves this problem.
libui.so
, libui.dylib
, libui.dll
.libui.so
and libui.dylib
later. In this case, libui.dll
is only 269K. It's very lightweight.That's all for this article.
Creating desktop applications with GUIs in Ruby seems to be less fashionable, except for the temporary boom in Shoes. I think one of the reasons is that asynchronous processing of GUI side and Ruby logic is troublesome. In this case, using druby may be one solution. for your information.
Recommended Posts