Class: MittensUi::HBox

Inherits:
Core
  • Object
show all
Includes:
Helpers
Defined in:
lib/mittens_ui/hbox.rb

Overview

A horizontal layout container that places widgets side by side in a row. Wraps Gtk::Box with :horizontal orientation. Supports nesting — HBox instances can be placed inside other HBox instances.

Examples:

Block style

MittensUi::HBox.new(spacing: 8) do
  MittensUi::Label.new("Name:")
  MittensUi::Textbox.new(can_edit: true)
end

Nested HBox

MittensUi::HBox.new(spacing: 8) do
  MittensUi::Label.new("Left")
  MittensUi::HBox.new(spacing: 4) do
    MittensUi::Button.new(title: "A")
    MittensUi::Button.new(title: "B")
  end
end

Array style

MittensUi::HBox.new([
  MittensUi::Button.new(title: "OK",     defer_render: true),
  MittensUi::Button.new(title: "Cancel", defer_render: true)
], spacing: 6)

Instance Attribute Summary

Attributes inherited from Core

#core_widget

Instance Method Summary collapse

Methods included from Helpers

#icon_map, #list_system_icons, #set_margin_from_opts_for

Methods inherited from Core

#hidden?, #hide, #keyboard_shortcut, #remove_keyboard_shortcut, #render, #shortcuts, #show

Constructor Details

#initialize(widgets_or_options = [], options = {}) { ... } ⇒ HBox

Creates a new HBox container.

Parameters:

  • widgets_or_options (Array, Hash) (defaults to: [])

    either an array of pre-built widgets or an options hash when using block style

  • options (Hash) (defaults to: {})

    configuration options (only used with array style)

Options Hash (options):

  • :spacing (Integer) — default: 6

    space in pixels between widgets

  • :width (Symbol) — default: :full

    column width in the layout grid

  • :top (Integer)

    top margin in pixels

  • :bottom (Integer)

    bottom margin in pixels

  • :left (Integer)

    left margin in pixels

  • :right (Integer)

    right margin in pixels

  • :defer_render (Boolean) — default: false

    skip auto-rendering into layout

Yields:

  • optional block — widgets created inside are automatically added to the row



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mittens_ui/hbox.rb', line 47

def initialize(widgets_or_options = [], options = {}, &block)
  if widgets_or_options.is_a?(Hash)
    options = widgets_or_options
    widgets = []
  else
    widgets = widgets_or_options
  end

  box_spacing = options[:spacing] || 6
  @box = Gtk::Box.new(:horizontal, box_spacing)
  set_margin_from_opts_for(@box, options)

  if block_given?
    MittensUi::Application.push_container(self)
    instance_eval(&block)
    MittensUi::Application.pop_container
  else
    widgets.each do |w|
      w.remove if w.respond_to?(:remove)
      attach(w.core_widget)
    end
  end

  super(@box, options)
end

Instance Method Details

#attach_widget(gtk_widget) ⇒ void

This method returns an undefined value.

Attaches a raw GTK widget to the horizontal box. Called automatically by Core#render when widgets are created inside an HBox block.

Parameters:

  • gtk_widget (Gtk::Widget)

    the GTK widget to attach



79
80
81
# File 'lib/mittens_ui/hbox.rb', line 79

def attach_widget(gtk_widget)
  attach(gtk_widget)
end

#removevoid

This method returns an undefined value.

Removes the HBox from the application layout.



86
87
88
89
90
# File 'lib/mittens_ui/hbox.rb', line 86

def remove
  return if @box.nil?

  MittensUi::Application.layout.remove(@box)
end