Skip to content

Commit

Permalink
Merge 5b6fcc7 into 623024c
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidner committed Oct 18, 2018
2 parents 623024c + 5b6fcc7 commit 8ed7fef
Show file tree
Hide file tree
Showing 7 changed files with 376 additions and 236 deletions.
154 changes: 71 additions & 83 deletions library/cwm/src/modules/CWM.rb
Expand Up @@ -55,36 +55,36 @@ def main

# Handler to be called after validation of a dialog fails
@validation_failed_handler = nil

# UI containers, layout helpers that contain other widgets. Used by
# functions that recurse through "contents" to decide whether to go
# deeper.
@ContainerWidgets = [
:Frame,
:RadioButtonGroup,
:VBox,
:HBox,
:MarginBox,
:MinWidth,
:MinHeight,
:MinSize,
:Left,
:Right,
:Top,
:Bottom,
:HCenter,
:VCenter,
:HVCenter,
:HSquash,
:VSquash,
:HVSquash,
:HWeight,
:VWeight,
:DumbTab,
:ReplacePoint
]
end

# UI containers, layout helpers that contain other widgets. Used by
# functions that recurse through "contents" to decide whether to go
# deeper.
CONTAINER_WIDGETS = [
:Frame,
:RadioButtonGroup,
:VBox,
:HBox,
:MarginBox,
:MinWidth,
:MinHeight,
:MinSize,
:Left,
:Right,
:Top,
:Bottom,
:HCenter,
:VCenter,
:HVCenter,
:HSquash,
:VSquash,
:HVSquash,
:HWeight,
:VWeight,
:DumbTab,
:ReplacePoint
].freeze

# local functions

# Push the settings of the currently run dialog to the stack
Expand Down Expand Up @@ -113,49 +113,35 @@ def PopSettings
# @param widgets [Hash{String => ::CWM::WidgetHash}] widget name -> widget description
# @return [::CWM::UITerm] updated term ready to be used as a dialog
def ProcessTerm(t, widgets)
t = deep_copy(t)
widgets = deep_copy(widgets)
args = Builtins.size(t)
return deep_copy(t) if args == 0
ret = Builtins.toterm(
Builtins.substring(Builtins.sformat("%1", Builtins.symbolof(t)), 1)
)
id_frame = false
index = 0
current = Builtins.symbolof(t)
while Ops.less_than(index, args)
arg = Ops.get(t, index)
# FIXME: still there is a problem for frames without label
if current == :Frame && index == 0 # no action
return t if t.empty?
ret = Yast::Term.new(t.value)

is_frame = t.value == :Frame
is_id_frame = false

t.each.with_index do |arg, index|
if is_frame && index == 0 # no action
# frame can have id and also label, so mark if id is used
id_frame = true if arg.is_a?(Yast::Term) && arg.value == :id
is_id_frame = arg.is_a?(Yast::Term) && arg.value == :id
Builtins.y2debug("Leaving untouched %1", arg)
elsif current == :Frame && index == 1 && id_frame && arg.is_a?(::String) # no action
id_frame = false
elsif is_frame && index == 1 && is_id_frame && arg.is_a?(::String) # no action
Builtins.y2debug("Leaving untouched %1", arg)
elsif Ops.is_term?(arg) && !arg.nil? # recurse
s = Builtins.symbolof(Convert.to_term(arg))
if Builtins.contains(@ContainerWidgets, s)
arg = ProcessTerm(Convert.to_term(arg), widgets)
elsif Ops.is_term?(arg) # recurse
if CONTAINER_WIDGETS.include?(arg.value)
arg = ProcessTerm(arg, widgets)
end
elsif Ops.is_string?(arg) # action
Builtins.y2error("find string '#{arg}' without associated widget in StringTerm #{t.inspect}") unless widgets[arg]
Builtins.y2milestone("Known widgets #{widgets.inspect}") unless widgets[arg]

arg = Ops.get_term(
widgets,
[Convert.to_string(arg), "widget"],
VBox()
)
s = Builtins.symbolof(arg)
if Builtins.contains(@ContainerWidgets, s)
arg = widgets.fetch(arg, {}).fetch("widget") { VBox() }
if CONTAINER_WIDGETS.include?(arg.value)
arg = ProcessTerm(arg, widgets)
end
end
ret = Builtins.add(ret, arg)
index = Ops.add(index, 1)
ret << arg
end
deep_copy(ret)
ret
end

# Process term with the dialog, return all strings.
Expand All @@ -172,9 +158,8 @@ def StringsOfTerm(t)
current = Builtins.symbolof(t)
if current == :Frame && index == 0 # no action
Builtins.y2debug("Leaving untouched %1", arg)
elsif Ops.is_term?(arg) && !arg.nil? # recurse
s = Builtins.symbolof(Convert.to_term(arg))
if Builtins.contains(@ContainerWidgets, s)
elsif Ops.is_term?(arg)
if CONTAINER_WIDGETS.include?(arg.value)
rets = Ops.add(rets, StringsOfTerm(Convert.to_term(arg)))
end
elsif Ops.is_string?(arg) # action
Expand All @@ -190,7 +175,6 @@ def StringsOfTerm(t)
# @param [String] type string type information
# @return [Boolean] true on success or if do not know how to validate
def ValidateBasicType(value, type)
value = deep_copy(value)
return Ops.is_term?(value) if type == "term"
return Ops.is_string?(value) if type == "string"
return Ops.is_symbol?(value) if type == "symbol"
Expand All @@ -210,7 +194,6 @@ def ValidateBasicType(value, type)
# @param [String] widget any name of the widget/option
# @return [Boolean] true if validation succeeded
def ValidateValueType(key, value, widget)
value = deep_copy(value)
types = {
# general
"widget" => "symbol",
Expand Down Expand Up @@ -268,7 +251,6 @@ def ValidateValueType(key, value, widget)
# @param [String] widget any name of the widget/option
# @return [Boolean] true if validation succeeded
def ValidateValueContents(key, value, widget)
value = deep_copy(value)
error = ""
if key == "label"
s = Convert.to_string(value)
Expand All @@ -291,8 +273,9 @@ def ValidateValueContents(key, value, widget)
false
end

# @param widgets [Array<::CWM::WidgetHash>] list of widget maps
# @return [Integer] the lowest 'ui_timeout' value, or 0
def GetLowestTimeout(widgets)
widgets = deep_copy(widgets)
minimum = 0
Builtins.foreach(widgets) do |w|
timeout = Ops.get_integer(w, "ui_timeout", 0)
Expand Down Expand Up @@ -343,7 +326,7 @@ def initWidgets(widgets)
)
end
# set initial values
@processed_widget = deep_copy(w)
self.processed_widget = deep_copy(w)
toEval = Convert.convert(
Ops.get(w, "init"),
from: "any",
Expand All @@ -361,12 +344,11 @@ def initWidgets(widgets)
# @param [Hash] event_descr map event that occured
# @return [Symbol] modified action (sometimes may be needed) or nil
def handleWidgets(widgets, event_descr)
widgets = deep_copy(widgets)
event_descr = deep_copy(event_descr)
ret = nil
Builtins.foreach(widgets) do |w|
if ret.nil?
@processed_widget = deep_copy(w)
self.processed_widget = deep_copy(w)
events = Ops.get_list(w, "handle_events", [])
toEval = Convert.convert(
Ops.get(w, "handle"),
Expand All @@ -392,7 +374,7 @@ def saveWidgets(widgets, event)
widgets = deep_copy(widgets)
event = deep_copy(event)
Builtins.foreach(widgets) do |w|
@processed_widget = deep_copy(w)
self.processed_widget = deep_copy(w)
toEval = Convert.convert(
Ops.get(w, "store"),
from: "any",
Expand All @@ -410,7 +392,7 @@ def saveWidgets(widgets, event)
def cleanupWidgets(widgets)
widgets = deep_copy(widgets)
Builtins.foreach(widgets) do |w|
@processed_widget = deep_copy(w)
self.processed_widget = deep_copy(w)
toEval = Convert.convert(
Ops.get(w, "cleanup"),
from: "any",
Expand Down Expand Up @@ -648,7 +630,7 @@ def prepareWidget(widget_descr)
def validateWidget(widget, event, key)
widget = deep_copy(widget)
event = deep_copy(event)
@processed_widget = deep_copy(widget)
self.processed_widget = deep_copy(widget)
failed = false
val_type = Ops.get_symbol(widget, "validate_type")
if val_type == :function || val_type == :function_no_popup
Expand Down Expand Up @@ -749,14 +731,11 @@ def MergeHelps(widgets)
# @param widgets [Array<::CWM::WidgetHash>] list of widget description maps
# @return [::CWM::UITerm] updated term ready to be used as a dialog
def PrepareDialog(dialog, widgets)
dialog = deep_copy(dialog)
widgets = deep_copy(widgets)
args = Builtins.size(dialog)
return deep_copy(dialog) if args == 0
m = Builtins.listmap(widgets) do |w|
widget_key = Ops.get_string(w, "_cwm_key", "")
{ widget_key => w }
end
return dialog.clone if dialog.empty?
m = widgets.map do |w|
widget_key = w.fetch("_cwm_key", "")
[widget_key, w]
end.to_h
ProcessTerm(dialog, m)
end

Expand Down Expand Up @@ -862,9 +841,9 @@ def Run(widgets, functions, skip_store_for: [])
end

# Disable given bottom buttons of the wizard sequencer
# @param buttons list of buttons to be disabled
# @param buttons [Array<String>] list of buttons to be disabled:
# "back_button", "abort_button", "next_button"
def DisableButtons(buttons)
buttons = deep_copy(buttons)
Builtins.foreach(buttons) do |button|
Wizard.DisableBackButton if button == "back_button"
Wizard.DisableAbortButton if button == "abort_button"
Expand Down Expand Up @@ -907,7 +886,6 @@ def AdjustButtons(next_, back, abort, _help)
# Set handler to be called after validation of a dialog failed
# @param [void ()] handler a function reference to be caled. If nil, nothing is called
def SetValidationFailedHandler(handler)
handler = deep_copy(handler)
@validation_failed_handler = deep_copy(handler)

nil
Expand All @@ -926,6 +904,8 @@ def SetValidationFailedHandler(handler)
# Useful mainly when some widget returns an event that should not trigger the storing,
# like a reset button or a redrawing. It will skip also validation, because it is not needed
# as nothing is stored.
# @param disable_buttons [Array<String>] buttons to disable:
# "back_button", "abort_button", "next_button"
# @param next_handler [Proc] handler that is called after clicking on next. If it returns false,
# then it does not go next. If it returns true, then :next symbol is returned. If handler is not
# defined, then it acts like if it returns true.
Expand Down Expand Up @@ -1016,6 +996,7 @@ def ShowAndRun(settings)
# @param [Hash] fallback map initialize/save/handle fallbacks if not specified
# with the widgets.
# @return [Symbol] wizard sequencer symbol
# @deprecated Use {#show} or {#ShowAndRun}
def ShowAndRunOrig(widget_names, widget_descr, contents, caption, back_button, next_button, fallback)
widget_names = deep_copy(widget_names)
widget_descr = deep_copy(widget_descr)
Expand Down Expand Up @@ -1120,6 +1101,13 @@ def widgets_contents(contents)

res
end

private

# such an accessor enables testing
def processed_widget=(w)
@processed_widget = w
end
end

CWM = CWMClass.new
Expand Down

0 comments on commit 8ed7fef

Please sign in to comment.