Class: MittensUi::HeaderBar

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

Overview

A header bar widget that replaces the window title bar. Wraps Gtk::HeaderBar. Widgets passed in are placed on the left or right side of the header bar.

Examples:

Left-aligned buttons

add_btn = MittensUi::Button.new(title: "Add", defer_render: true)
MittensUi::HeaderBar.new([add_btn], title: "My App", position: :left)

Right-aligned buttons

MittensUi::HeaderBar.new([btn], title: "My App", position: :right)

Instance Attribute Summary

Attributes inherited from Core

#core_widget

Instance Method Summary collapse

Methods inherited from Core

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

Methods included from Helpers

#icon_map, #list_system_icons, #set_margin_from_opts_for

Constructor Details

#initialize(widgets, options = {}) ⇒ HeaderBar

Creates a new HeaderBar widget.

Parameters:

  • widgets (Array<MittensUi::Core>)

    widgets to place in the header bar. Each widget must be created with defer_render: true.

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

    configuration options

Options Hash (options):

  • :title (String) — default: ""

    the header bar title text

  • :position (Symbol) — default: :left

    placement of widgets. Accepted values are :left and :right

  • :defer_render (Boolean) — default: false

    skip auto-rendering into layout



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mittens_ui/header_bar.rb', line 27

def initialize(widgets, options = {})
  title    = options.fetch(:title, '')
  position = options.fetch(:position, :left)

  @header = Gtk::HeaderBar.new

  @header.show_title_buttons = true

  # use a Gtk::Label as the title widget
  title_label = Gtk::Label.new(title)
  @header.title_widget = title_label

  box = Gtk::Box.new(:horizontal, 0)
  box.add_css_class('linked')

  widgets.each do |w|
    w.remove
    box.append(w.core_widget)
  end

  if position == :right
    @header.pack_end(box)
  else
    @header.pack_start(box)
  end

  super(@header, options)
end

Instance Method Details

#rendervoid

This method returns an undefined value.

Places the header bar as the window titlebar. Called automatically by Core#render.



60
61
62
# File 'lib/mittens_ui/header_bar.rb', line 60

def render
  MittensUi::Application.window.titlebar = @header
end