Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Quick and dirty CWM::Tree, ripping off the CWM::Tabs API
- Loading branch information
Showing
2 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
# Simple example to demonstrate object API for CWM | ||
|
||
require_relative "example_helper" | ||
|
||
require "cwm/widget" | ||
|
||
Yast.import "CWM" | ||
Yast.import "Popup" | ||
Yast.import "Wizard" | ||
|
||
class LuckyNumberWidget < ::CWM::IntField | ||
attr_reader :result, :minimum, :maximum | ||
|
||
def initialize | ||
@minimum = 0 | ||
@maximum = 1000 | ||
end | ||
|
||
def label | ||
_("Lucky number") | ||
end | ||
|
||
def init | ||
self.value = @result if @result | ||
end | ||
|
||
def store | ||
@result = value | ||
end | ||
end | ||
|
||
class GenerateButton < ::CWM::PushButton | ||
def initialize(lucky_number_widget) | ||
@lucky_number_widget = lucky_number_widget | ||
end | ||
|
||
def label | ||
_("Generate Lucky Number") | ||
end | ||
|
||
def handle | ||
@lucky_number_widget.value = rand(1000) | ||
|
||
nil | ||
end | ||
end | ||
|
||
class LuckyNumberTab < ::CWM::Tab | ||
def initialize | ||
self.initial = true | ||
end | ||
|
||
def contents | ||
HBox( | ||
button_widget, | ||
lucky_number_widget | ||
) | ||
end | ||
|
||
def result | ||
lucky_number_widget.result | ||
end | ||
|
||
def label | ||
_("Lucky Number") | ||
end | ||
|
||
private | ||
|
||
def button_widget | ||
@button_widget ||= GenerateButton.new(lucky_number_widget) | ||
end | ||
|
||
def lucky_number_widget | ||
@lucky_number_widget ||= LuckyNumberWidget.new | ||
end | ||
end | ||
|
||
class TrueLoveSelector < ::CWM::RadioButtons | ||
def initialize | ||
@chosen = nil | ||
end | ||
|
||
def label | ||
_("Select true love") | ||
end | ||
|
||
def items | ||
[ | ||
[:human, "Human"], | ||
[:pc, "PC"] | ||
] | ||
end | ||
|
||
def init | ||
self.value = @chosen if @chosen | ||
end | ||
|
||
def validate | ||
if value == :pc | ||
Yast::Popup.Error(_("Human will be exterminated, pc cannot allow you to be your true love")) | ||
return false | ||
end | ||
|
||
true | ||
end | ||
|
||
def store | ||
@chosen = value | ||
end | ||
|
||
def result | ||
@chosen | ||
end | ||
end | ||
|
||
class TrueLoveTab < ::CWM::Tab | ||
def contents | ||
HBox( | ||
true_love_selector | ||
) | ||
end | ||
|
||
def label | ||
_("True Love") | ||
end | ||
|
||
def result | ||
true_love_selector.result | ||
end | ||
|
||
private | ||
|
||
def true_love_selector | ||
@true_love_selector ||= TrueLoveSelector.new | ||
end | ||
end | ||
|
||
module Yast | ||
class ExampleDialog | ||
include Yast::I18n | ||
include Yast::UIShortcuts | ||
include Yast::Logger | ||
|
||
def run | ||
textdomain "example" | ||
|
||
lucky_number_tab = LuckyNumberTab.new | ||
true_love_tab = TrueLoveTab.new | ||
|
||
tabs = ::CWM::Tree.new(lucky_number_tab, true_love_tab) | ||
|
||
contents = VBox(tabs) | ||
|
||
Yast::Wizard.CreateDialog | ||
CWM.show(contents, caption: _("Tabs example")) | ||
Yast::Wizard.CloseDialog | ||
|
||
log.info "Lucky number: #{lucky_number_tab.result}, true love: #{true_love_tab.result}" | ||
end | ||
end | ||
end | ||
|
||
Yast::ExampleDialog.new.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module CWM | ||
class Tree < Tabs | ||
def contents | ||
tree = Tree(Id(widget_id), Opt(:notify), "TODO Tree title", items) | ||
HBox( | ||
HWeight(30, tree), | ||
HWeight(70, replace_point) | ||
) | ||
end | ||
|
||
private | ||
|
||
def tab_for_id(id) | ||
if id == widget_id | ||
id = Yast::UI.QueryWidget(Id(id), :CurrentBranch).last | ||
end | ||
super(id) | ||
end | ||
|
||
def items | ||
# FIXME too simple | ||
panes = tab_order.map do |tab_id| | ||
tab = tab_for_id(tab_id) | ||
item_for(tab.widget_id, tab.label) | ||
end | ||
panes | ||
end | ||
|
||
def item_for(id, title, icon: nil, subtree: []) | ||
args = [Id(id)] | ||
args << Yast::Term.new(:icon, icon) if icon | ||
args << title | ||
args << open?(id) | ||
args << subtree | ||
Item(*args) | ||
end | ||
|
||
def open?(id) | ||
id == initial_tab_id | ||
end | ||
end | ||
end |