class BBS::Menubar::Dropdown
The box an open menu drops down in, under its label on the bar, shifted left when it would run off the right edge: its place, the items drawn in it and which item a click lands on.
Constants
- CHROME
-
The columns around the longest label and shortcut: the borders and the padding.
Public Class Methods
Source
# File lib/bbs/menu/dropdown.rb, line 26 def initialize(menu, bar, style) @menu = menu @style = style @width = width_of(menu) @height = menu.items.size + 2 @x = [menu.x.to_i, bar.x + bar.width - @width].min.clamp(1..) @y = bar.y + 1 end
The box of menu under a bar with bounds bar (a Widget::Rect); style is a callable returning the SGR of a theme role.
# File lib/bbs/menu/dropdown.rb, line 15 def self.write_label(frame, label, sgr, hotkey_sgr) before, letter, after = Hotkey.split(label) return frame.write(Hotkey.plain(label), sgr: sgr) unless letter frame.write(before, sgr: sgr) frame.write(letter, sgr: hotkey_sgr) frame.write(after, sgr: sgr) end
Writes label at the cursor of frame in style sgr, its hotkey letter (see BBS::Hotkey) in hotkey_sgr.
Public Instance Methods
Source
# File lib/bbs/menu/dropdown.rb, line 46 def item_at(x, y) return nil unless x >= @x && x < @x + @width && y >= @y && y < @y + @height y - @y - 1 end
The index of the item row at screen cell x, y (-1 or the item count on the borders), or nil when the cell is outside the box.
Source
# File lib/bbs/menu/dropdown.rb, line 37 def render(frame, selected) frame.shadow(x: @x, y: @y, width: @width, height: @height, sgr: @style.call(:window_shadow)) frame.box(x: @x, y: @y, width: @width, height: @height, sgr: @style.call(:menu_border), fill_sgr: @style.call(:menu_bg)) @menu.items.each_with_index { |item, i| render_item(frame, item, @y + 1 + i, i == selected) } end
Draws the shadow, the box and the items into frame, item selected highlighted.