kojix2 / LibUI

A portable GUI library for Ruby
MIT License
207 stars 10 forks source link

callback for UI.timer #33

Closed kojix2 closed 3 years ago

kojix2 commented 3 years ago

You can't write this way.

UI.timer(1000) do
  puts "hello"
  0
end
can't modify frozen Integer: 1000 (FrozenError)

Cannot add callbacks as instance variables to 1000. (This is necessary to avoid garbage collection.)

You can write it this way.

c = Fiddle::Closure::BlockCaller.new(4, [0]) { puts "hello"; 0}
UI.timer(1000, c)

However, the first way of writing is better.

kojix2 commented 3 years ago

An easy way to solve this problem is to override UI#timer. But before doing that, it would be better to check other callbacks used in libui. There may be other functions that cause the same problem.

AndyObtiva commented 3 years ago

I just added support for timer and queue_main block syntax to Glimmer DSL for LibUI today (0.2.0)

Here are examples using it:

https://github.com/AndyObtiva/glimmer-dsl-libui/#timer

https://github.com/AndyObtiva/glimmer-dsl-libui/#color-the-circles

The implementation includes some extra convenience options like specifying repeat option as a finite integer or disabling repetition after one run (currently at the bottom of this file):

https://github.com/AndyObtiva/glimmer-dsl-libui/blob/master/lib/glimmer/libui.rb

So, your implementation in LibUI is more than good enough at the binding layer to enable Glimmer’s code at the higher DSL layer. I don’t think you have to worry about it.

kojix2 commented 3 years ago

Amazing! You work so fast, I can't keep up with you at all. Well, I'll take my time.

kojix2 commented 3 years ago
kojix2 commented 3 years ago

Maybe I could have solved this issue, but I haven't tested it enough, so I'll wait and see.

AndyObtiva commented 3 years ago

Given the Layered Architecture of Glimmer DSL for LibUI, I think we have the following layers from top to bottom, and each is performing its function sufficiently, so I wouldn't worry: