LibUI

build Gem Version

:radio_button: libui - a portable GUI library -for Ruby

Installation

gem install libui
  • The gem package contains the official release of the libui shared library versions 4.1 for Windows, Mac, and Linux.
    • Namely libui.dll, libui.dylib, and libui.so (only 1.4MB in total).
  • No dependency
    • The libui gem uses the standard Ruby library Fiddle to call C functions.
Windows Mac Linux

Usage

require 'libui'
UI = LibUI

UI.init

main_window = UI.new_window('hello world', 300, 200, 1)
UI.window_on_closing(main_window) do
  puts 'Bye Bye'
  UI.control_destroy(main_window)
  UI.quit
  0
end

button = UI.new_button('Button')
UI.button_on_clicked(button) do
  UI.msg_box(main_window, 'Information', 'You clicked the button')
  0
end

UI.window_set_child(main_window, button)
UI.control_show(main_window)

UI.main
UI.quit

See examples directory.

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.

How to use fiddle pointers?

require 'libui'
UI = LibUI
UI.init

Convert a pointer to a string.

label = UI.new_label("Ruby")
p pointer = UI.label_text(label) # #<Fiddle::Pointer>
p pointer.to_s # Ruby

If you need to use C structs, you can do the following.

font_button = UI.new_font_button

# Allocate memory 
font_descriptor = UI::FFI::FontDescriptor.malloc

UI.font_button_on_changed(font_button) do
  UI.font_button_font(font_button, font_descriptor)
  p family:  font_descriptor.Family.to_s,
    size:    font_descriptor.Size,
    weight:  font_descriptor.Weight,
    italic:  font_descriptor.Italic,
    stretch: font_descriptor.Stretch
end

How to create an executable (.exe) on Windows

OCRA (One-Click Ruby Application) builds Windows executables from Ruby source code.

Development

git clone https://github.com/kojix2/libui
cd libui
bundle install
bundle exec rake vendor:all
bundle exec rake test

Use the following rake tasks to download the libui binary files and save them in the vendor directory.

rake -T

rake vendor:all       # Download libui.so, libui.dylib, and libui.dll to ve...
rake vendor:linux     # Download libui.so for Linux to vendor directory
rake vendor:mac       # Download libui.dylib for Mac to vendor directory
rake vendor:windows   # Download libui.dll for Windows to vendor directory

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/kojix2/libui.

Acknowledgement

This project is inspired by libui-ruby.

While libui-ruby uses Ruby-FFI, this gem uses Fiddle.

License

MIT License.