Class: Fidgit::MessageDialog

Inherits:
DialogState show all
Defined in:
lib/fidgit/states/message_dialog.rb

Overview

A simple dialog that manages a message with a set of buttons beneath it.

Constant Summary collapse

VALID_TYPES =
[:ok, :ok_cancel, :yes_no, :yes_no_cancel, :quit_cancel, :quit_save_cancel]

Constants inherited from DialogState

DialogState::DEFAULT_BACKGROUND_COLOR, DialogState::DEFAULT_BORDER_COLOR, DialogState::DEFAULT_SHADOW_COLOR, DialogState::DEFAULT_SHADOW_OFFSET

Constants inherited from GuiState

GuiState::PIXEL_IMAGE

Instance Attribute Summary collapse

Attributes inherited from GuiState

#container, #focus

Instance Method Summary collapse

Methods inherited from DialogState

#draw, #hide, #show

Methods inherited from GuiState

clear, #clear, #cursor, #distance, #draw, #draw_frame, #draw_rect, #file_dialog, #finalize, #flush, #hide, #hide_menu, #menu, #message, #setup, #show, #show_menu, #t, #tool_tip_delay, #unset_mouse_over, #update, #write_tree

Constructor Details

#initialize(message, options = {}) {|result| ... } ⇒ MessageDialog

Returns a new instance of MessageDialog.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :type (Symbol) — default: :ok

    One from :ok, :ok_cancel, :yes_no, :yes_no_cancel, :quit_cancel or :quit_save_cancel

  • :ok_text (String) — default: "OK"
  • :yes_text (String) — default: "Yes"
  • :no_text (String) — default: "No"
  • :cancel_text (String) — default: "Cancel"
  • :save_text (String) — default: "Save"
  • :quit_text (String) — default: "Quit"
  • :show (Boolean) — default: true

    Whether to show the message immediately (otherwise need to use #show later).

Yields:

  • when the dialog is closed.

Yield Parameters:

  • result (Symbol)

    :ok, :yes, :no, :quit, :save or :cancel, depending on the button pressed.

Raises:

  • (ArgumentError)


22
23
24
25
26
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
55
56
57
58
59
# File 'lib/fidgit/states/message_dialog.rb', line 22

def initialize(message, options = {}, &block)
  options = {
    type: :ok,
    ok_text: "OK",
    yes_text: "Yes",
    no_text: "No",
    quit_text: "Quit",
    save_text: "Save",
    cancel_text: "Cancel",
    show: true,
    background_color: DEFAULT_BACKGROUND_COLOR,
    border_color: DEFAULT_BORDER_COLOR,
    width: $window.width / 2
  }.merge! options

  @type = options[:type]
  raise ArgumentError, ":type must be one of #{VALID_TYPES}, not #{@type}" unless VALID_TYPES.include? @type

  super(options)

  # Dialog is forced to the centre.
  options[:align_h] = options[:align_v] = :center

  vertical options do
    text_area(text: message, enabled: false, width: options[:width] - padding_left - padding_right)

    horizontal align_h: :center do
      @type.to_s.split('_').each do |type|
        button(options[:"#{type}_text"]) do
          hide
          block.call type.to_sym if block
        end
      end
    end
  end

  show if options[:show]
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/fidgit/states/message_dialog.rb', line 7

def type
  @type
end