-
Notifications
You must be signed in to change notification settings - Fork 43
/
custom_widget.rb
81 lines (68 loc) · 1.9 KB
/
custom_widget.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require "abstract_method"
require "cwm/abstract_widget"
module CWM
# A custom widget that has its UI content defined in the method {#contents}.
# Useful mainly when a specialized widget including more subwidgets should be
# reusable at more places.
#
# @example custom widget child
# class MyWidget < CWM::CustomWidget
# def initialize
# self.handle_all_events = true
# end
#
# def contents
# HBox(
# PushButton(Id(:reset), _("Reset")),
# PushButton(Id(:undo), _("Undo"))
# )
# end
#
# def handle(event)
# case event["ID"]
# when :reset then ...
# when :undo then ...
# else ...
# end
# nil
# end
# end
class CustomWidget < AbstractWidget
self.widget_type = :custom
# @!method contents
# Must be defined by subclasses
# @return [Yast::Term] a UI term; {AbstractWidget} are not allowed inside
abstract_method :contents
def cwm_definition
res = { "custom_widget" => cwm_contents }
res["handle_events"] = ids_in_contents unless handle_all_events
super.merge(res)
end
# Returns all nested widgets used in contents
# @return [Array<AbstractWidget>]
def nested_widgets
Yast.import "CWM"
Yast::CWM.widgets_in_contents(contents)
end
protected
# return contents converted to format understandable by CWM module
# Basically it replace instance of AbstractWidget by its widget_id
def cwm_contents
Yast.import "CWM"
Yast::CWM.widgets_contents(contents)
end
def ids_in_contents
find_ids(contents) << widget_id
end
def find_ids(term)
term.each_with_object([]) do |arg, res|
next unless arg.is_a? Yast::Term
if arg.value == :id
res << arg.params[0]
else
res.concat(find_ids(arg))
end
end
end
end
end