The story of making a binding for libui, a GUI library for Ruby that is easy to install

Introduction

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

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. 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).

What is libui

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

Installation

gem install libui

Easy to use

A simple usage example is shown below.

entry.gif

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.

General rules of notation

Compared to the original libui written in C,

Isn't it object-oriented?

Comparison with existing libui binding

Ruby bindings already exist in libui.

However, this package has the weakness of being cumbersome to install. libui solves this problem.

That's all for this article.

Bonus There is druby when you have to process asynchronously

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

The story of making a binding for libui, a GUI library for Ruby that is easy to install
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
Determine that the value is a multiple of 〇 in Ruby
The story of Collectors.groupingBy that I want to keep for posterity
How to identify the path that is easy to make a mistake
The story that a model is not a "correct representation of the real world" / the need for a bounded context
The story of making it possible to build a project that was built by Maven with Ant
The story of introducing Ajax communication to ruby
A story about making a Builder that inherits the Builder
[Ruby] I want to make a program that displays today's day of the week!
The story of making a reverse proxy with ProxyServlet
A story about taking an HTTP trace using Charles to find out what requests the Java library is making to Slack
A memo of the program that allows you to realize that the probability of dice rolling is about 1/6
[Docker] Is it good enough to call it a multi-stage build? → The story that became so good
Review the basic knowledge of ruby that is often forgotten
[Ruby] How to retrieve the contents of a double hash
A program that searches for a character string, and when the search character string is found, displays the character string from the beginning of the line to just before the search character string.
A story that made it as easy as possible to check the operation when automatically creating a library update PR with Github Dependabot
Is it easy for the user to use when implementing general-purpose functions? Let's be aware of
I made a GitHub Action that makes it easy to understand the execution result of RSpec
Mechanism for converting to a language that the browser can recognize
Want to know what Ruby n is the power of 2? (Power judgment of 2)
[Ruby 3.0] A memo that I added a type definition to a library I wrote
A memorial service for the library used when competing in Ruby
A story about making a calculator to calculate the shell mound rate
A story that struggled with the introduction of Web Apple Pay
[Swift] The story that switch is often used for enum judgment
The story of making a communication type Othello game using Scala.
A story that confirmed the profile of Yasuko Sawaguchi 36 years ago
How to check for the contents of a java fixed-length string
Let's write a code that is easy to maintain (Part 2) Name
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
About the matter that tends to be confused with ARG of Dockerfile which is a multi-stage build
The story of introducing Gradle as a retrofit to an existing system that did not manage packages
A memo that was soberly addicted to the request of multipart / form-data
The story of forgetting to close a file in Java and failing
The story of making a game launcher with automatic loading function [Java]
To you who lament that the conversion of JODConverter + LibreOffice is slow
A program that determines whether the entered integer is close to an integer
The story of switching from Amazon RDS for MySQL to Amazon Aurora Serverless
Think of test code that is easy to understand through Comparator testing
The story of releasing the Android app to the Play Store for the first time.
Created a library that makes it easy to handle Android Shared Prefences
[Java small story] Monitor when a value is added to the List
A story that was embarrassing to give anison file to the production environment
I tried to make a program that searches for the target class from the process that is overloaded with Java
If it is Ruby, it is efficient to make it a method and stock the processing.
Think of RxJava as a library that makes asynchronous processing easier to write
The story of making an Android application that can adjust the sampling frequency of the accelerometer
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
Initialize Ruby array with 0 like Java, that is, set the default value to 0
A story confirming the implementation of the SendGrid Java library when mail delivery fails
[Ruby on Rails] Implement a pie chart that specifies the percentage of colors
A story about creating a library that operates next-generation sequencer data with Ruby ruby-htslib
A story that people who did iOS solidly may be addicted to the implementation of Listener when moving to Android