Class: MittensUi::LayoutManager
- Inherits:
-
Object
- Object
- MittensUi::LayoutManager
- 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.
Constant Summary collapse
- UNITS =
Maps width symbols to their 12-unit column equivalents.
{ full: 12, half: 6, third: 4, quarter: 3 }.freeze
Instance Method Summary collapse
-
#add(gtk_widget, width:) ⇒ void
Adds a GTK widget to the grid at the current position.
-
#add_at_top(gtk_widget) ⇒ void
Adds a widget to the top of the grid at row 0, shifting all existing widgets down by one row.
-
#includes?(gtk_widget) ⇒ Boolean
Returns whether a widget is currently in the grid.
-
#initialize(container) ⇒ LayoutManager
constructor
Creates a new LayoutManager and attaches the grid to the container.
-
#remove(gtk_widget) ⇒ void
Removes a widget from the grid.
-
#reorder(gtk_widget, position) ⇒ void
Moves a widget to a specific row position in the grid.
Constructor Details
#initialize(container) ⇒ LayoutManager
Creates a new LayoutManager and attaches the grid to the container.
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.
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(, width:) units = UNITS.fetch(width, 12) if @current_col + units > 12 @current_row += 1 @current_col = 0 end @grid.attach(, @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.
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() return if includes?() 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(, 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.
79 80 81 82 83 84 85 86 87 |
# File 'lib/mittens_ui/layout_manager.rb', line 79 def includes?() child = @grid.first_child while child return true if child == 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.
68 69 70 71 72 |
# File 'lib/mittens_ui/layout_manager.rb', line 68 def remove() return unless includes?() @grid.remove() 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.
96 97 98 99 |
# File 'lib/mittens_ui/layout_manager.rb', line 96 def reorder(, position) @grid.remove() @grid.attach(, 0, position, 12, 1) end |