Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically add tab_item after window creation #16

Closed
MauAbata opened this issue Jan 5, 2022 · 1 comment
Closed

Dynamically add tab_item after window creation #16

MauAbata opened this issue Jan 5, 2022 · 1 comment

Comments

@MauAbata
Copy link

MauAbata commented Jan 5, 2022

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
Copy link
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants