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

Dynamically add tab_item after window creation #16

Closed MauAbata closed 2 years ago

MauAbata commented 2 years ago

Hey there,

I'm trying to create an application that has a tab item for each opened file, for example. Currently, the only way I can see to declare tabs is during the initial declaration of the app, but I do not see any way to add new items or edit the tabs after the app is initialized.

Here's a skeleton of the code I'm working on:

require 'glimmer-dsl-libui'
include Glimmer

open_files = []

menu('File') {
  menu_item('Open work file...') {
    on_clicked do
      file = open_file
      unless file.nil?
        open_files.push file
        # somehow convince main to re-initialize the tabs?
      end
    end
  }
}

main = window('File Tabs') {
  tab {
    open_files.each do |filename|
      tab_item filename
    end
  }
}

main.show

Any help would be appreciated.

AndyObtiva commented 2 years ago

You must use the .content {} method to re-open the tab content and add new tab items in. The .content {} method works on all GUI controls.

Here is the code updated to add tab items properly and also open the file content inside the tab item:

require 'glimmer-dsl-libui'
include Glimmer

open_files = []

menu('File') {
  menu_item('Open work file...') {
    on_clicked do
      file = open_file
      puts file
      unless file.nil?
        open_files.push file
        @tab.content { # re-open tab content and add a new tab item
          filename = File.basename(file)
          tab_item(filename) {
            non_wrapping_multiline_entry {
              text File.read(file)
            }
          }
        }
      end
    end
  }
  quit_menu_item
}

main = window('File Tabs', 900, 600) {
  @tab = tab {
    open_files.each do |filename|
      tab_item(filename)
    end
  }
}

main.show
Tab 1 Tab 2 Tab 3
Screen Shot 2022-01-05 at 5 33 07 PM Screen Shot 2022-01-05 at 5 33 09 PM Screen Shot 2022-01-05 at 5 33 11 PM

Cheers!