Module: Lich::Common::GUI::Components

Defined in:
documented/common/gui/components.rb

Class Method Summary collapse

Class Method Details

.create_button(label: nil, css_provider: nil) ⇒ Gtk::Button

Creates a new button with an optional label and CSS provider.

Examples:

Creating a button

button = Lich::Common::GUI::Components.create_button(label: "Click Me")

Parameters:

  • label (String, nil) (defaults to: nil)

    The label for the button.

  • css_provider (Gtk::CssProvider, nil) (defaults to: nil)

    The CSS provider to style the button.

Returns:

  • (Gtk::Button)

    The created button.



12
13
14
15
16
# File 'documented/common/gui/components.rb', line 12

def self.create_button(label: nil, css_provider: nil)
  button = label ? Gtk::Button.new(label: label) : Gtk::Button.new
  button.style_context.add_provider(css_provider, Gtk::StyleProvider::PRIORITY_USER) if css_provider
  button
end

.create_button_box(buttons, expand: false, fill: false, padding: 5) ⇒ Gtk::Box

Creates a horizontal button box containing the specified buttons.

Examples:

Creating a button box

box = Lich::Common::GUI::Components.create_button_box([button1, button2])

Parameters:

  • buttons (Array<Gtk::Button>)

    The buttons to include in the box.

  • expand (Boolean) (defaults to: false)

    Whether the buttons should expand to fill the box.

  • fill (Boolean) (defaults to: false)

    Whether the buttons should fill the space allocated to them.

  • padding (Integer) (defaults to: 5)

    The padding between buttons.

Returns:

  • (Gtk::Box)

    The created button box.



26
27
28
29
30
31
32
33
34
# File 'documented/common/gui/components.rb', line 26

def self.create_button_box(buttons, expand: false, fill: false, padding: 5)
  box = Gtk::Box.new(:horizontal)

  buttons.each do |button|
    box.pack_end(button, expand: expand, fill: fill, padding: padding)
  end

  box
end

.create_labeled_entry(label_text, entry_width: 15, password: false) ⇒ Hash

Creates a labeled entry field with an optional password visibility.

Examples:

Creating a labeled entry

entry = Lich::Common::GUI::Components.create_labeled_entry("Username:", entry_width: 20)

Parameters:

  • label_text (String)

    The text for the label.

  • entry_width (Integer) (defaults to: 15)

    The width of the entry field in characters.

  • password (Boolean) (defaults to: false)

    Whether the entry should be a password field.

Returns:

  • (Hash)

    A hash containing the label, entry, and box.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'documented/common/gui/components.rb', line 43

def self.create_labeled_entry(label_text, entry_width: 15, password: false)
  label = Gtk::Label.new(label_text)
  label.set_width_chars(entry_width)

  entry = Gtk::Entry.new
  entry.visibility = !password if password

  pane = Gtk::Paned.new(:horizontal)
  pane.add1(label)
  pane.add2(entry)

  { label: label, entry: entry, box: pane }
end

.create_notebook(pages, tab_position: :top, show_border: true, css_provider: nil) ⇒ Gtk::Notebook

Creates a notebook widget with the specified pages and settings.

Examples:

Creating a notebook

notebook = Lich::Common::GUI::Components.create_notebook([{ label: "Tab 1", widget: widget1 }, { label: "Tab 2", widget: widget2 }])

Parameters:

  • pages (Array<Hash>)

    An array of pages, each containing a :label and :widget.

  • tab_position (Symbol) (defaults to: :top)

    The position of the tabs (:top, :bottom, :left, :right).

  • show_border (Boolean) (defaults to: true)

    Whether to show a border around the notebook.

  • css_provider (Gtk::CssProvider, nil) (defaults to: nil)

    The CSS provider to style the notebook.

Returns:

  • (Gtk::Notebook)

    The created notebook.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'documented/common/gui/components.rb', line 65

def self.create_notebook(pages, tab_position: :top, show_border: true, css_provider: nil)
  notebook = Gtk::Notebook.new
  notebook.set_tab_pos(tab_position)
  notebook.show_border = show_border

  if css_provider
    notebook.style_context.add_provider(css_provider, Gtk::StyleProvider::PRIORITY_USER)
  end

  # Track tab indices to avoid hardcoding page numbers
  notebook.define_singleton_method(:tab_indices) do
    @tab_indices ||= {}
  end

  pages.each do |page|
    label = page[:label]
    index = notebook.append_page(page[:widget], Gtk::Label.new(label))
    notebook.tab_indices[label] = index
  end

  notebook
end