• Stars
    star
    203
  • Rank 192,890 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A portable GUI library for Ruby

LibUI

build Gem Version glimmer-dsl-libui

LibUI is a Ruby wrapper for libui and libui-ng.

🚀 libui-ng - A cross-platform portable GUI library

🔘 libui - Original version by andlabs.

Installation

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

Notes:

  • If you are using the 32-bit (x86) version of Ruby, you need to download the 32-bit (x86) native dll. See the Development section.
  • On Windows, libui may not work due to missing DLLs. In that case, you need to install Visual C++ Redistributable. See (#48)
  • Users with Raspberry Pi or other platforms will need to compile the C libui library. See the Development section.

Usage

require 'libui'

UI = LibUI

UI.init

main_window = UI.new_window('hello world', 200, 100, 1)

button = UI.new_button('Button')

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

UI.window_on_closing(main_window) do
  puts 'Bye Bye'
  UI.control_destroy(main_window)
  UI.quit
  0
end

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

UI.main
UI.quit

For more examples, see the examples directory.

General Rules

Compared to the original libui library written in C:

  • Method names use snake_case.
  • The last argument can be omitted if it's nil.
  • A block can be passed as a callback.
    • The block will be converted to a Proc object and added as the last argument.
    • The last argument can still be omitted when nil.

You can use the documentation for libui's Go bindings as a reference.

DSLs for LibUI

LibUI is not object-oriented because it is a thin Ruby wrapper (binding) for the procedural C libui library, mirroring its API structure.

To build actual applications, it is recommended to use a DSL for LibUI, as they enable writing object-oriented code the Ruby way (instead of procedural code the C way):

Working with fiddle pointers

require 'libui'
UI = LibUI
UI.init

To 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
font_descriptor.to_ptr.free = Fiddle::RUBY_FREE
# font_descriptor = UI::FFI::FontDescriptor.malloc(Fiddle::RUBY_FREE) # fiddle 1.0.1 or higher

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
  • Callbacks
    • In Ruby/Fiddle, a C callback function is written as an object of Fiddle::Closure::BlockCaller or Fiddle::Closure. Be careful about Ruby's garbage collection - if the function object is collected, memory will be freed resulting in a segmentation violation when the callback is invoked.
# Assign to a local variable to prevent it from being collected by GC.
handler.MouseEvent   = (c1 = Fiddle::Closure::BlockCaller.new(0, [0]) {})
handler.MouseCrossed = (c2 = Fiddle::Closure::BlockCaller.new(0, [0]) {})
handler.DragBroken   = (c3 = Fiddle::Closure::BlockCaller.new(0, [0]) {})

Creating a Windows executable (.exe) with OCRA

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

To build an exe with Ocra, include 3 DLLs from the ruby_builtin_dlls folder:

ocra examples/control_gallery.rb        ^
  --dll ruby_builtin_dlls/libssp-0.dll  ^
  --dll ruby_builtin_dlls/libgmp-10.dll ^
  --dll ruby_builtin_dlls/libffi-7.dll  ^
  --gem-all=fiddle                      ^

Add additional options below if necessary:

  --window                              ^
  --add-all-core                        ^
  --chdir-first                         ^
  --icon assets\app.ico                 ^
  --verbose                             ^
  --output out\gallery.exe

Development

LibUI offers high portability with a minimal implementation.

git clone https://github.com/kojix2/libui
cd libui
bundle install
bundle exec rake vendor:default # download shared libraries for all platforms
bundle exec rake test

Use the following rake tasks to download the shared library required for your platform:

rake -T

rake vendor:kojix2:auto           # Download kojix2 pre-build for your platform to vendor directory
rake vendor:kojix2:mac            # Download kojix2 pre-build for Mac to vendor directory
rake vendor:kojix2:ubuntu_x64     # Download kojix2 pre-build for Ubuntu to vendor directory
rake vendor:kojix2:windows_x64    # Download kojix2 pre-build for Windows to vendor directory
rake vendor:kojix2:windows_x86    # Download kojix2 pre-build for Windows to vendor directory
rake vendor:libui-ng:build[hash]  # Build libui-ng latest master [commit hash]
rake vendor:libui-ng:mac          # Download latest dev build for Mac to vendor directory
rake vendor:libui-ng:ubuntu_x64   # Download latest dev build for Ubuntu to vendor directory

For example, if you are using a 32-bit (x86) version of Ruby on Windows, type vendor:kojix2:windows_x86. These shared libraries are built using Github Actions; if the pre-build branch of kojix2/libui-ng is not updated for 3 months, it will not be available for download. Please let me know when that happens.

Using C libui compiled from source code

You can compile C libui from source code on your platform and tell Ruby LibUI where to find the shared libraries. Set the environment variable LIBUIDIR to specify the path to the shared library. (See #46). This is especially useful on platforms where the LibUI gem does not provide shared library, such as the ARM architecture (used in devices like Raspberry Pi).

Another simple approach is to replace the shared libraries in the gem vendor directory with the ones you have compiled.

Publishing gems

ls vendor             # check the vendor directory
rm -rf pkg            # remove previously built gems
rake build_platform
rake release_platform 

libui or libui-ng

  • From version 0.1.X, we plan to support only libui-ng/libui-ng.
  • Version 0.0.X only supports andlabs/libui.

Contributing

Would you like to contribute to LibUI?

  • Please feel free to send us your pull requests.
    • Small corrections, such as typo fixes, are appreciated.
  • Did you find any bugs? Submit them in the issues section!

Many OSS projects become abandoned because only the founder has commit rights to the original repository. If you need commit rights to my repository or want to get admin rights and take over the project, please feel free to contact me @kojix2.

Acknowledgements

This project is inspired by libui-ruby.

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

License

MIT License.

More Repositories

1

cry-wasm

cry-wasm speeds up Ruby code using Crystal
Ruby
28
star
2

chatgpt-cli

Yet another ChatGPT command line tool
Crystal
25
star
3

deepl-cli

Simple command line tool for DeepL
Crystal
24
star
4

rubio-radio

Rubio is a simple GUI radio player
Ruby
14
star
5

ruby-minimap2

Powerful long read aligner for Ruby
Ruby
13
star
6

RDatasets

Ruby gem for loading datasets in R
Ruby
11
star
7

ruby-htslib

HTSlib bindings for Ruby
Ruby
10
star
8

ruby-umappp

Uniform Manifold Approximation and Projection for Ruby
C++
7
star
9

tiktoken-c

C API for tiktoken-rs
Rust
7
star
10

nuplot

Running Gnuplot with Crystal.
Crystal
6
star
11

ffi-bitfield

Bit field for Ruby-FFI
Ruby
5
star
12

rurema-chrome-extension

るりまサーチ
JavaScript
4
star
13

tiktoken-cr

Tiktoken for Crystalists
Crystal
4
star
14

blingfire-crystal

Crystal
3
star
15

ruby-alglib

C++
3
star
16

ruby-libssw

Fast Smith-Waterman algorithm for Ruby
Ruby
3
star
17

suikabox

Suika GUI
Ruby
3
star
18

seaborn-command

seaborn as a command line tool
Python
3
star
19

libui_paradise

[Unofficial] libui_paradise repository. This repository automatically fetches RubyGem and creates pull requests with Github Actions. The copyright of the code belongs to shevy, not to kojix2.
Ruby
2
star
20

covid-net-docker

Dockerfile
2
star
21

htsgrid

Simple HTS viewer
Ruby
2
star
22

chai

Daru https://github.com/SciRuby/daru
Ruby
2
star
23

zenity.cr

Zenity wrapper for Crystal
Crystal
2
star
24

icalmaker

Ruby
2
star
25

red-amber-view

R's View() for RedAmber
Ruby
2
star
26

ruby-edlib

ruby-edlib is a wrapper for edlib
C++
2
star
27

gray_scott_gtk3

Ruby implementation of the Reaction diffusion system (Gray-Scott model)
Ruby
2
star
28

ollamachat

Ruby
2
star
29

multipull

Git pull multiple repositories at once
Ruby
1
star
30

exline

Ruby
1
star
31

nn_visualization

Visualize neural network druby and Ruby/Tk using the MNIST
Ruby
1
star
32

sdust.cr

Reimplementation of Sdust in the Crystal language
Crystal
1
star
33

narray-vgg16

VGG16 object recognition network with Ruby NArray
Ruby
1
star
34

bat-c

C API for bat
Rust
1
star
35

cowsay.cr

The Crystal version of Cowsay
Crystal
1
star
36

syntect-c

Rust
1
star
37

wombat

Crystal
1
star
38

uing

experimental
C
1
star
39

pandoc-japanese-docker

Makefile
1
star
40

deepl.cr

Crystal library for the DeepL language translation API.
Crystal
1
star
41

randn.cr

Generate a normally-distributed random number
Crystal
1
star
42

fastx.cr

Crystal
1
star
43

kojix2

Forked from https://github.com/oprypin/oprypin
1
star
44

easyclip

Easy copy and paste for Crystal
Crystal
1
star
45

nworkers.cr

Crystal
1
star