AndyObtiva / glimmer-dsl-libui

Glimmer DSL for LibUI - Prerequisite-Free Ruby Desktop Development Cross-Platform Native GUI Library - The Quickest Way From Zero To GUI - If You Liked Shoes, You'll Love Glimmer! - No need to pre-install any prerequisites. Just install the gem and have platform-independent GUI that just works on Mac, Windows, and Linux.
MIT License
458 stars 15 forks source link

on_focus_changed #37

Closed singpolyma closed 1 year ago

singpolyma commented 1 year ago

It looks like LIbUI has a callback for focus change on a window, but I'm not sure how to get at it from glimmer. That and also to ask a window if it has focus right now.

AndyObtiva commented 1 year ago

That should be an unreleased feature. We should be updating the version of C libui that is wrapped by Glimmer DSL for LibUI in the near future.

AndyObtiva commented 1 year ago

Apparently, the C libui library included in Glimmer DSL for LibUI already includes access to the window focus APIs.

I just tested this Glimmer DSL for LibUI code sample and it worked!

require 'glimmer-dsl-libui'

include Glimmer

window { |w|
  on_focus_changed do 
    puts 'focus changed'
    puts w.focused
  end
}.show

Basically, you can use:

I hope this helps.

AndyObtiva commented 1 year ago

I just made a release of Glimmer DSL for LibUI that officially supports window focused? property as boolean and documents window on_focus_changed listener: https://rubygems.org/gems/glimmer-dsl-libui/versions/0.6.0.pre.1

I also updated examples/basic_child_window.rb to demo focused? and on_focus_changed:

require 'glimmer-dsl-libui'

include Glimmer

window('Main Window') { |main_window|
  button('Spawn Child Window') {
    on_clicked do
      window('Child Window') { |child_window|
        on_focus_changed do
          puts 'Child window is focused' if child_window.focused?
        end

        on_closing do
          puts 'Child window is closing'
        end
      }.show
    end
  }

  on_focus_changed do
    puts 'Main window is focused' if main_window.focused?
  end

  on_closing do
    puts 'Main window is closing'
  end
}.show

That should address this issue completely, so I am closing (you may still comment if you have further questions, or even re-open if you discover another related issue).

AndyObtiva commented 1 year ago

One thing I forgot to mention is that the 0.6.0.pre.1 version finally includes a newer version of the C libui library (wasn't needed for this issue though), so it supports a new open_folder keyword, which works like open_file, but opens a directory instead.