Class: CWM::AbstractWidget

Inherits:
Object
  • Object
show all
Includes:
Yast::I18n, Yast::Logger, Yast::UIShortcuts
Defined in:
library/cwm/src/lib/cwm/abstract_widget.rb

Overview

Represent base for any widget used in CWM. It can be passed as "widget" argument. For more details about usage see Yast::CWM.show

Underneath there is a widget library with a procedural API, using symbol parameters as widget IDs.

The call sequence is:

  • #initialize is called by the Ruby constructor new
  • CWM#show builds a widget tree, using

    • the AbstractWidget concrete class
    • #opt widget options: [:notify] is needed if #handle is defined
    • #label
    • #help
  • #init may initialize the widget state from persistent storage

  • loop:

    • #handle may update other widgets if this one tells them to
    • #validate may decide whether the user input is valid
  • #store may store the widget state to persistent storage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#handle_all_eventsBoolean

By default, #handle has no argument and it is called only for events of its own widget. If true, #handle(event) is called for events of any widget.

Returns:

  • (Boolean)

62
63
64
# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 62

def handle_all_events
  @handle_all_events.nil? ? false : @handle_all_events
end

#widget_idString

Returns An ID, unique within a dialog, used for the widget. By default, the class name is used.

Returns:

  • (String)

    An ID, unique within a dialog, used for the widget. By default, the class name is used.


69
70
71
# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 69

def widget_id
  @widget_id || self.class.to_s
end

Class Method Details

.widget_type=(type) ⇒ void

This method returns an undefined value.

Declare widget type for Yast::CWMClass. Your derived widgets will not need to do this.

Parameters:

  • type (Symbol)

78
79
80
# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 78

def self.widget_type=(type)
  define_method(:widget_type) { type }
end

Instance Method Details

#cleanupvoid

This method returns an undefined value.

Clean up after the widget is destroyed


# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 131

#cwm_definitionWidgetHash

Generate widget definition for Yast::CWMClass. It refers to #help, #label, #opt #validate, #init, #handle, #store, #cleanup.

Returns:

Raises:

  • (RuntimeError)

    if a required method is not implemented or widget_type is not set.


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 142

def cwm_definition
  raise "Widget '#{self.class}' does set its widget type" if !respond_to?(:widget_type)

  res = {}

  res["_cwm_key"] = widget_id
  if respond_to?(:help)
    res["help"] = help_method
  else
    res["no_help"] = ""
  end
  res["label"] = label if respond_to?(:label)
  res["opt"] = opt if respond_to?(:opt)
  if respond_to?(:validate)
    res["validate_function"] = validate_method
    res["validate_type"] = :function
  end
  res["handle_events"] = [widget_id] unless handle_all_events
  res["init"] = init_method if respond_to?(:init)
  res["handle"] = handle_method if respond_to?(:handle)
  res["store"] = store_method if respond_to?(:store)
  res["cleanup"] = cleanup_method if respond_to?(:cleanup)
  res["widget"] = widget_type

  res
end

#disablevoid

This method returns an undefined value.

Closes widget for interaction


182
183
184
# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 182

def disable
  Yast::UI.ChangeWidget(Id(widget_id), :Enabled, false)
end

#enablevoid

This method returns an undefined value.

Opens widget for interaction


176
177
178
# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 176

def enable
  Yast::UI.ChangeWidget(Id(widget_id), :Enabled, true)
end

#enabled?Boolean

Returns Is widget open for interaction?.

Returns:

  • (Boolean)

    Is widget open for interaction?


170
171
172
# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 170

def enabled?
  Yast::UI.QueryWidget(Id(widget_id), :Enabled)
end

#focusObject

Focus the widget. Useful when validation failed to highlight it.


187
188
189
# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 187

def focus
  Yast::UI.SetFocus(Id(widget_id))
end

#fun_ref(*args) ⇒ Yast::FunRef (protected)

TODO:

kill converts in CWM module, to avoid this workaround for funrefs

shortcut from Yast namespace to avoid including whole namespace

Returns:

  • (Yast::FunRef)

209
210
211
# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 209

def fun_ref(*args)
  Yast::FunRef.new(*args)
end

#handlenil, Symbol #handle(event) ⇒ nil, Symbol

Overloads:

  • #handlenil, Symbol
    Note:

    defining #handle itself does not pass :notify to widget. It has to be done with #opt method.

    Process an event generated by this widget. This method is invoked if #handle_all_events is false.

    Returns:

    • (nil, Symbol)

      what to return from the dialog, or nil to continue processing

  • #handle(event) ⇒ nil, Symbol
    Note:

    defining #handle itself does not pass :notify to widget. It has to be done with #opt method.

    Process an event generated a widget. This method is invoked if #handle_all_events is true.

    Parameters:

    • event (Hash)

      see CWMClass

    Returns:

    • (nil, Symbol)

      what to return from the dialog, or nil to continue processing


# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 105

#helpString

Called only once by default. Also triggered by #refresh_help.

Returns:

  • (String)

    translated help text for the widget


# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 86

#initvoid

This method returns an undefined value.

Initialize the widget: set initial value


# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 101

#labelString

Derived classes must override this method to specify a label.

Returns:

  • (String)

    translated label text for the widget


# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 90

#my_event?(event) ⇒ Boolean (protected)

A helper to check if an event is invoked by this widget

Parameters:

  • event (Hash)

    a UI event

Returns:

  • (Boolean)

195
196
197
# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 195

def my_event?(event)
  widget_id == event["ID"]
end

#optArray<Symbol>

Specifies options passed to widget. When #handle method is defined, it is often expected to be immediatelly notified from widget and for that purpose opt method with [:notify] as return value is needed.

Returns:

  • (Array<Symbol>)

    options passed to widget like [:hstretch, :vstretch]


# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 94

#refresh_helpObject (protected)

A widget will need to call this if its #help text has changed,to make the change effective.


200
201
202
203
204
# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 200

def refresh_help
  Yast.import "CWM"

  Yast::CWM.ReplaceWidgetHelp
end

#storevoid

This method returns an undefined value.

Store the widget value for further processing


# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 127

#validateBoolean

Validate widgets before ending the loop and storing.

Returns:

  • (Boolean)

    validate widget value. If it fails (false) the dialog will not return yet.


# File 'library/cwm/src/lib/cwm/abstract_widget.rb', line 122