/
device_map_dialog.rb
189 lines (161 loc) · 4.3 KB
/
device_map_dialog.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
require "yast"
Yast.import "BootCommon"
Yast.import "BootStorage"
Yast.import "Label"
Yast.import "Popup"
require "bootloader/device_map"
module Bootloader
# Represents dialog for modification of device map
class DeviceMapDialog
include Yast::UIShortcuts
include Yast::I18n
def self.run
new.run
end
def run
textdomain "bootloader"
return unless create_dialog
begin
return controller_loop
ensure
close_dialog
end
end
private
def create_dialog
res = Yast::UI.OpenDialog dialog_content
return false unless res
refresh_buttons
true
end
def close_dialog
Yast::UI.CloseDialog
end
def controller_loop
loop do
input = Yast::UI.UserInput
pos = selected_disk_index
case input
when :ok
if disks.empty?
Yast::Popup.Error(_("Device map must contain at least one device"))
next
end
store_order
return :back # we just go back to original dialog
when :cancel
return :back
when :up
disks.insert(pos - 1, disks.delete_at(pos))
pos -= 1
when :down
disks.insert(pos + 1, disks.delete_at(pos))
pos += 1
when :delete
disks.delete_at(pos)
pos = pos == disks.size ? pos - 1 : pos
when :add
disk = add_device_popup
disks << disk if disk
pos = disks.size - 1
when :disks
refresh_buttons
next
else
raise "Unknown action #{input}"
end
refresh_disks
Yast::UI.ChangeWidget(Id(:disks), :CurrentItem, disks[pos])
refresh_buttons
end
end
def dialog_content
VBox(
headings,
VSpacing(1),
contents,
VSpacing(1),
ending_buttons
)
end
def headings
Heading(_("Disk order settings"))
end
def contents
HBox(
SelectionBox(Id(:disks), Opt(:notify), _("D&isks"), disks),
action_buttons
)
end
def action_buttons
VBox(
PushButton(Id(:add), Opt(:key_F3), Yast::Label.AddButton),
PushButton(Id(:delete), Opt(:key_F5), Yast::Label.DeleteButton),
VSpacing(1),
PushButton(Id(:up), Opt(:hstretch), Yast::Label.UpButton),
PushButton(Id(:down), Opt(:hstretch), Yast::Label.DownButton),
VStretch()
)
end
def ending_buttons
ButtonBox(
PushButton(Id(:ok), Yast::Label.OKButton),
PushButton(Id(:cancel), Yast::Label.CancelButton)
)
end
def disks
@disks ||= Yast::BootStorage.DisksOrder
end
def refresh_disks
Yast::UI.ChangeWidget(Id(:disks), :Items, disks)
end
def store_order
Yast::BootCommon.mbrDisk = disks.first
mapping = disks.each_with_object({}) do |disk, res|
res[disk] = "hd#{res.size}"
end
Yast::BootStorage.device_map = ::Bootloader::DeviceMap.new(mapping)
end
def add_device_popup
popup = VBox(
VSpacing(1),
# textentry header
InputField(Id(:devname), Opt(:hstretch), _("&Device")),
VSpacing(1),
ButtonBox(
PushButton(Id(:ok), Opt(:key_F10, :default), Yast::Label.OKButton),
PushButton(Id(:cancel), Opt(:key_F8), Yast::Label.CancelButton)
),
VSpacing(1)
)
Yast::UI.OpenDialog(popup)
Yast::UI.SetFocus(:devname)
pushed = Yast::UI.UserInput
new_dev = Yast::UI.QueryWidget(Id(:devname), :Value)
Yast::UI.CloseDialog
pushed == :ok ? new_dev : nil
end
def selected_disk_index
disks.index(Yast::UI.QueryWidget(Id(:disks), :CurrentItem))
end
def refresh_buttons
pos = selected_disk_index
if !pos # nothing selected
disk_to_select = disks.first
# there is no disks
if !disk_to_select
up_down_enablement(false, false)
return
end
Yast::UI.ChangeWidget(Id(:disks), :CurrentItem, disk_to_select)
refresh_buttons
return
end
up_down_enablement(pos > 0, pos < (disks.size - 1))
end
def up_down_enablement(up, down)
Yast::UI.ChangeWidget(Id(:up), :Enabled, up)
Yast::UI.ChangeWidget(Id(:down), :Enabled, down)
end
end
end