Class: MittensUi::LayoutManager

Inherits:
Object
  • Object
show all
Defined in:
lib/mittens_ui/layout_manager.rb

Overview

Manages the placement of widgets in the application window using a 12-unit column grid system backed by Gtk::Grid.

Widgets are placed left to right, wrapping to the next row automatically when the current row is full. The column width of each widget is specified in units from 1 to 12, with convenience symbols :full, :half, :third, and :quarter.

Examples:

Adding widgets with different widths

layout.add(label_widget,   width: :full)    # spans all 12 units
layout.add(textbox_widget, width: :half)    # spans 6 units
layout.add(button_widget,  width: :quarter) # spans 3 units

Constant Summary collapse

UNITS =

Maps width symbols to their 12-unit column equivalents.

Returns:

  • (Hash{Symbol => Integer})
{ full: 12, half: 6, third: 4, quarter: 3 }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(container) ⇒ LayoutManager

Creates a new LayoutManager and attaches the grid to the container.

Parameters:

  • container (Gtk::Box)

    the vertical box to attach the grid to



25
26
27
28
29
30
31
32
33
34
# File 'lib/mittens_ui/layout_manager.rb', line 25

def initialize(container)
  @container = container
  @grid = Gtk::Grid.new
  @grid.column_homogeneous = true
  @grid.row_spacing = 5
  @grid.column_spacing = 5
  @container.append(@grid)
  @current_row = 0
  @current_col = 0
end

Instance Method Details

#add(gtk_widget, width:) ⇒ void

This method returns an undefined value.

Adds a GTK widget to the grid at the current position. Wraps to the next row automatically if the widget won’t fit.

Examples:

layout.add(label.core_widget, width: :full)
layout.add(btn.core_widget,   width: :half)

Parameters:

  • gtk_widget (Gtk::Widget)

    the widget to add

  • width (Symbol)

    the column width. Accepted values are :full (12), :half (6), :third (4), :quarter (3)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mittens_ui/layout_manager.rb', line 46

def add(gtk_widget, width:)
  units = UNITS.fetch(width, 12)

  if @current_col + units > 12
    @current_row += 1
    @current_col = 0
  end

  @grid.attach(gtk_widget, @current_col, @current_row, units, 1)
  @current_col += units

  if @current_col >= 12
    @current_row += 1
    @current_col = 0
  end
end

#add_at_top(gtk_widget) ⇒ void

This method returns an undefined value.

Adds a widget to the top of the grid at row 0, shifting all existing widgets down by one row. Used by Notify to ensure notification banners always appear at the top of the window. Does nothing if the widget is already in the grid.

Parameters:

  • gtk_widget (Gtk::Widget)

    the widget to add at the top



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mittens_ui/layout_manager.rb', line 108

def add_at_top(gtk_widget)
  return if includes?(gtk_widget)

  child = @grid.first_child
  while child
    layout_child = @grid.layout_manager.get_layout_child(child)
    layout_child.row = layout_child.row + 1
    child = child.next_sibling
  end

  @grid.attach(gtk_widget, 0, 0, 12, 1)
  @current_row += 1
end

#includes?(gtk_widget) ⇒ Boolean

Returns whether a widget is currently in the grid. Traverses the grid’s child widget linked list.

Parameters:

  • gtk_widget (Gtk::Widget)

    the widget to check for

Returns:

  • (Boolean)

    true if the widget is in the grid, false otherwise



79
80
81
82
83
84
85
86
87
# File 'lib/mittens_ui/layout_manager.rb', line 79

def includes?(gtk_widget)
  child = @grid.first_child
  while child
    return true if child == gtk_widget

    child = child.next_sibling
  end
  false
end

#remove(gtk_widget) ⇒ void

This method returns an undefined value.

Removes a widget from the grid. Does nothing if the widget is not currently in the grid.

Parameters:

  • gtk_widget (Gtk::Widget)

    the widget to remove



68
69
70
71
72
# File 'lib/mittens_ui/layout_manager.rb', line 68

def remove(gtk_widget)
  return unless includes?(gtk_widget)

  @grid.remove(gtk_widget)
end

#reorder(gtk_widget, position) ⇒ void

This method returns an undefined value.

Moves a widget to a specific row position in the grid. Removes the widget from its current position and reattaches it at column 0 of the given row, spanning all 12 units.

Parameters:

  • gtk_widget (Gtk::Widget)

    the widget to reorder

  • position (Integer)

    the row index to move the widget to



96
97
98
99
# File 'lib/mittens_ui/layout_manager.rb', line 96

def reorder(gtk_widget, position)
  @grid.remove(gtk_widget)
  @grid.attach(gtk_widget, 0, position, 12, 1)
end