diff --git a/library/cwm/examples/popup.rb b/library/cwm/examples/popup.rb new file mode 100644 index 000000000..d86ed8f20 --- /dev/null +++ b/library/cwm/examples/popup.rb @@ -0,0 +1,74 @@ +# Simple example to demonstrate object API for CWM + +require_relative "example_helper" + +require "cwm" +require "cwm/popup" + +Yast.import "CWM" + +class Goat < CWM::CheckBox + def initialize + textdomain "example" + end + + def label + _("Goat") + end + + def help + _("

Goat will eat cabbage.

") + end +end + +class Cabbage < CWM::CheckBox + def initialize + textdomain "example" + end + + def label + _("Cabbage") + end + + def help + _("

Poor cabbage cannot eat anyone.

") + end +end + +class Wolf < CWM::CheckBox + def initialize + textdomain "example" + end + + def label + _("Wolf") + end + + def help + _("

Wolf hates vegans, so will eat goat and won't even touch cabbage.

") + end +end + +class Ferryman < ::CWM::Popup + def initialize + textdomain "example" + end + + def contents + HBox( + Cabbage.new, + Goat.new, + Wolf.new + ) + end + + def help + _("

Ferryman

Represents common Ferryman challenge with two place in boat and following rules for passengers:

") + end + + def title + _("Ferryman") + end +end + +Ferryman.new.run diff --git a/library/cwm/src/lib/cwm/abstract_widget.rb b/library/cwm/src/lib/cwm/abstract_widget.rb index 5e4f802c2..3e6d7cb2c 100644 --- a/library/cwm/src/lib/cwm/abstract_widget.rb +++ b/library/cwm/src/lib/cwm/abstract_widget.rb @@ -1,6 +1,8 @@ require "abstract_method" require "yast" +Yast.import "UI" + module CWM # A Yast::Term that can be passed as is to Yast::UI methods # (OpenDialog, ReplaceWidget) @@ -183,6 +185,11 @@ def disable Yast::UI.ChangeWidget(Id(widget_id), :Enabled, false) end + # Focus the widget. Useful when validation failed to highlight it. + def focus + Yast::UI.SetFocus(Id(widget_id)) + end + protected # A helper to check if an event is invoked by this widget diff --git a/library/cwm/src/lib/cwm/dialog.rb b/library/cwm/src/lib/cwm/dialog.rb index 22ac8f897..d640c4cc0 100644 --- a/library/cwm/src/lib/cwm/dialog.rb +++ b/library/cwm/src/lib/cwm/dialog.rb @@ -1,5 +1,6 @@ require "yast" require "abstract_method" +require "cwm/common_widgets" Yast.import "CWM" Yast.import "Wizard" @@ -8,6 +9,7 @@ module CWM # An OOP API and the pieces missing from {Yast::CWMClass#show Yast::CWM.show}: # - creating and closing a wizard dialog # - Back/Abort/Next buttons + # - Help introduction for dialog # # @see UI::Dialog # @see CWM::AbstractWidget @@ -103,8 +105,33 @@ def abort_handler true end + # Introduction for help. This help text will be displayed before widget specific help. + # Default implementation is empty. If dialog want its help it need to + # overwrite the method with own text. + # @note text should be marked for translation + # @return [String] help text with rich text formatting + def help + "" + end + private + # Empty widget which is first on the page and contain help for whole dialog + class FakeHelp < Empty + def initialize(text) + @text = text + end + + def init + Yast::CWM.ReplaceWidgetHelp # needed for popup to properly set help text outside wizard + log.info "calling init on fake help" + end + + def help + @text + end + end + # Create a wizard dialog, run the *block*, ensure the dialog is closed. # @param block def wizard_create_dialog(&block) @@ -118,7 +145,7 @@ def wizard_create_dialog(&block) # @return [Symbol] wizard sequencer symbol def cwm_show Yast::CWM.show( - contents, + Yast::Term.new(:HBox, FakeHelp.new(help), contents), caption: title, back_button: back_button, abort_button: abort_button, diff --git a/library/cwm/src/lib/cwm/popup.rb b/library/cwm/src/lib/cwm/popup.rb index 47ed6b657..87e47a04c 100644 --- a/library/cwm/src/lib/cwm/popup.rb +++ b/library/cwm/src/lib/cwm/popup.rb @@ -20,10 +20,13 @@ # find current contact information at www.suse.com. require "cwm/dialog" +Yast.import "Popup" + module CWM # CWM pop-up dialog # # This class offers a CWM dialog which behaves as a pop-up. + # @see {CWM::Dialog} for remaining configuration options. class Popup < Dialog # Determines that a dialog should always be open # @@ -34,6 +37,12 @@ def should_open_dialog? true end + # Popup does not allow nil, so overwrite Dialog default value. + # @return [String] The dialog title. + def title + "" + end + private # Redefines the mechanism to open the dialog to use the adapted layout @@ -52,6 +61,7 @@ def wizard_create_dialog(&block) # @return [Yast::Term] def layout VBox( + Id(:WizardDialog), HSpacing(50), Left(Heading(Id(:title), title)), VStretch(), diff --git a/library/cwm/src/lib/cwm/rspec.rb b/library/cwm/src/lib/cwm/rspec.rb index 467844bb4..1246b5ea3 100644 --- a/library/cwm/src/lib/cwm/rspec.rb +++ b/library/cwm/src/lib/cwm/rspec.rb @@ -89,6 +89,16 @@ include_examples "CWM::ItemsSelection" end +RSpec.shared_examples "CWM::SelectionBox" do + include_examples "CWM::AbstractWidget" + include_examples "CWM::ItemsSelection" +end + +RSpec.shared_examples "CWM::MultiSelectionBox" do + include_examples "CWM::AbstractWidget" + include_examples "CWM::ItemsSelection" +end + RSpec.shared_examples "CWM::PushButton" do include_examples "CWM::AbstractWidget" end @@ -128,6 +138,21 @@ include_examples "CWM::ValueBasedWidget" end +RSpec.shared_examples "CWM::InputField" do + include_examples "CWM::AbstractWidget" + include_examples "CWM::ValueBasedWidget" +end + +RSpec.shared_examples "CWM::Password" do + include_examples "CWM::AbstractWidget" + include_examples "CWM::ValueBasedWidget" +end + +RSpec.shared_examples "CWM::IntField" do + include_examples "CWM::AbstractWidget" + include_examples "CWM::ValueBasedWidget" +end + RSpec.shared_examples "CWM::Table" do include_examples "CWM::AbstractWidget" diff --git a/library/cwm/test/abstract_widget_test.rb b/library/cwm/test/abstract_widget_test.rb index befb9ac9c..08b375926 100755 --- a/library/cwm/test/abstract_widget_test.rb +++ b/library/cwm/test/abstract_widget_test.rb @@ -5,6 +5,8 @@ require "cwm/abstract_widget" require "cwm/rspec" +Yast.import "UI" + describe CWM::AbstractWidget do include_examples "CWM::AbstractWidget" @@ -190,4 +192,20 @@ def cleanup expect(TCleanup.new.cwm_definition).to be_key("cleanup") end end + + describe "#focus" do + class TFocus < CWM::AbstractWidget + self.widget_type = :empty + + def initialize + self.widget_id = "test" + end + end + + it "sets focus on given widget" do + expect(Yast::UI).to receive(:SetFocus).with(Id("test")) + + TFocus.new.focus + end + end end diff --git a/package/yast2.changes b/package/yast2.changes index b26ba6cff..17783879f 100644 --- a/package/yast2.changes +++ b/package/yast2.changes @@ -1,3 +1,12 @@ +------------------------------------------------------------------- +Mon Jan 14 10:55:20 UTC 2019 - Josef Reidinger + +- CWM: Add method focus to object CWM widgets (FATE#324662) +- CWM: Add rspec helper for common CWM widgets +- CWM: Allow CWM dialogs/popups to have own help +- CWM: Fix showing help for CWM Popup (FATE#324662) +- 4.1.50 + ------------------------------------------------------------------- Thu Jan 10 14:45:03 UTC 2019 - Josef Reidinger diff --git a/package/yast2.spec b/package/yast2.spec index 5b502e08c..02997a48e 100644 --- a/package/yast2.spec +++ b/package/yast2.spec @@ -17,7 +17,7 @@ Name: yast2 -Version: 4.1.49 +Version: 4.1.50 Release: 0 Summary: YaST2 - Main Package License: GPL-2.0-only