Hokusai

A Ruby library for authoring GUI applications

Getting started

The build tooling of this project is xmake. You will need it to compile dependencies and run demos.

Hokusai contains a tree-sitter grammar to parse templates, and uses md4c to parse markdown.

When compiling the C portion of hokusai, tree-sitter will be statically linked.

In order to run an application, you will also need to install a backend

Raylib

  • Install raylib >= 5.0

  • Write your app

  • Run app with RAYLIB_PATH=/libpath/for/libraylib.(so|dylib) ruby <your app>.rb

SDL

Gemfile

gem "hokusai-zero"

Example counter application

require "hokusai"
require "hokusai/backends/raylib"

class Counter < Hokusai::Block
  style <<~EOF
  [style]
  additionStyles {
    background: rgb(214, 49, 24);
    rounding: 0.0;
    outline: outline(1,4,4,1);
    outline_color: rgb(216, 26, 137);
  }

  additionLabel {
    size: 40;
    color: rgb(255,255,255);
  }

  subtractStyles {
    background: rgb(0, 85, 170);
    rounding: 0.0;
  }

  subtractLabel {
    size: 50;
    color: rgb(255,255,255);
  }

  scrollbar {
    width: 25;
    control_height: 100;
    control_padding: 5;
    control_rounding: 5;
  }
  EOF

  template <<-EOF
  [template]
    hblock { @keypress="update_keys"}
      vblock
        hblock
          label#count {
            :content="count"
            size="130" 
            :color="count_color"
          }
        hblock
          vblock#add { ...additionStyles }
            label { 
              content="Add"
              @click="increment" 
              ...additionLabel 
            }
          vblock#subtract { ...subtractStyles }
            label { 
              content="Subtract"
              @click="decrement" 
              ...subtractLabel 
            }
      [if="count_positive"]
        scrollbar { ...scrollbar }
  EOF

  uses(
    vblock: Hokusai::Blocks::Vblock,
    hblock: Hokusai::Blocks::Hblock,
    label: Hokusai::Blocks::Label,
    scrollbar: Hokusai::Blocks::Scrollbar,
    image: Hokusai::Blocks::Image
  )

  attr_accessor :count, :keys

  def count_positive
    count > 0
  end

  def modal
    !keys.empty?
  end

  def update_keys(event)
    @keys << event.char
  end

  def increment(event)
    self.count += 1
  end

  def decrement(event)
    self.count -= 1
  end

  def count_color
    self.count > 0 ? [0,0,255] : [255,0,0]
  end

  def initialize(**args)
    @count = 0
    @keys = ""
    super(**args)
  end
end

Hokusai::Backends::RaylibBackend.run(Counter) do |config|
  config.width = 500
  config.height = 500
  config.title = "Counter application"
end

Development

Requirements: * xmake to build dependencies * Ruby to run applications

Steps: * Download project * Install dependencies * bundle install * xmake q hoku-tree-sitter * xmake q hoku-md4c * For Raylib * xmake q raylib * For SDL2 * xmake q libsdl * xmake q libsdl_gfx * xmake q lbsdl_ttf * xmake q libsdl_image * Build grammar and ast code * xmake b hokusai * Run specs * xmake b -g test * Run a demo * xmake demo counter

License

Hokusai is released under the Peer Production License