Skip to content

Commit

Permalink
Quick and dirty CWM::Tree, ripping off the CWM::Tabs API
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidner committed May 12, 2017
1 parent f6b1b4f commit 4ba143d
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 0 deletions.
164 changes: 164 additions & 0 deletions library/cwm/examples/object_api_tree.rb
@@ -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
42 changes: 42 additions & 0 deletions library/cwm/src/lib/cwm/tree.rb
@@ -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

0 comments on commit 4ba143d

Please sign in to comment.