Skip to content

Commit

Permalink
Merge pull request #1027 from yast/cwm/restore_richtext_scroll
Browse files Browse the repository at this point in the history
Restore the CWM::RichText scroll after updating its content
  • Loading branch information
dgdavid committed Mar 9, 2020
2 parents 4be7ee8 + 6167ebb commit 0cc5361
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
41 changes: 41 additions & 0 deletions library/cwm/src/lib/cwm/common_widgets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,47 @@ class RichText < AbstractWidget
self.widget_type = :richtext

include ValueBasedWidget

# Determines if the vertical scroll must be kept after updating the content
#
# @note Useful only to keep the sense of continuity when redrawing basically with the same text
#
# Keeping the vertical scroll after changing the value is mostly intended to be used after a
# redraw because of a user action. However, using it after changing the content noticeably
# (e.g., displaying different product descriptions), will look like a randomly positioned
# vertical scroll.
#
# @return [Boolean] true if the vertical scroll must be kept; false otherwise
def keep_scroll?
false
end

# Updates the content
#
# Depending on #keep_scroll?, the vertical scroll will be saved and restored.
#
# @param val [String] the new content for the widget
def value=(val)
current_vscroll = vscroll
super
self.vscroll = current_vscroll if keep_scroll?
end

private

# Saves the current vertical scroll
#
# @return [String] current vertical scroll value
def vscroll
Yast::UI.QueryWidget(Id(widget_id), :VScrollValue)
end

# Sets vertical scroll
#
# @param value [String] the new vertical scroll value
def vscroll=(value)
Yast::UI.ChangeWidget(Id(widget_id), :VScrollValue, value)
end
end

# Time field widget
Expand Down
53 changes: 52 additions & 1 deletion library/cwm/test/common_widgets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require "cwm/rspec"

describe CWM::RadioButtons do

class TestRadioButtons < CWM::RadioButtons
def label
"Choose a number"
Expand Down Expand Up @@ -59,3 +58,55 @@ def hspacing
end
end
end

describe CWM::RichText do
subject { described_class.new }
let(:widget_id) { Id(subject.widget_id) }
let(:new_content) { "Updated content" }
let(:vscroll) { "40" }

describe "#value=" do
before do
allow(Yast::UI).to receive(:QueryWidget).with(widget_id, :VScrollValue).and_return(vscroll)
allow(Yast::UI).to receive(:ChangeWidget)
end

context "when set to keep the scroll" do
before do
allow(subject).to receive(:keep_scroll?).and_return(true)
end

it "changes the widget value" do
expect(Yast::UI).to receive(:ChangeWidget).with(widget_id, :Value, new_content)

subject.value = new_content
end

it "saves vertical scroll position" do
expect(Yast::UI).to receive(:QueryWidget).with(widget_id, :VScrollValue)

subject.value = new_content
end

it "restores vertical scroll" do
expect(Yast::UI).to receive(:ChangeWidget).with(widget_id, :VScrollValue, vscroll)

subject.value = new_content
end
end

context "when set to not keep the scroll" do
it "changes the widget value" do
expect(Yast::UI).to receive(:ChangeWidget).with(widget_id, :Value, new_content)

subject.value = new_content
end

it "does not restore the scroll" do
expect(Yast::UI).to_not receive(:ChangeWidget).with(widget_id, :VScrollValue, anything)

subject.value = new_content
end
end
end
end
7 changes: 7 additions & 0 deletions package/yast2.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Fri Mar 6 13:26:57 UTC 2020 - David Diaz <dgonzalez@suse.com>

- Allow to restore the vertical scroll of a CWM::RichText
(related to bsc#1049965)
- 4.2.70

-------------------------------------------------------------------
Fri Mar 6 12:00:43 UTC 2020 - David Diaz <dgonzalez@suse.com>

Expand Down
2 changes: 1 addition & 1 deletion package/yast2.spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


Name: yast2
Version: 4.2.69
Version: 4.2.70
Release: 0
Summary: YaST2 Main Package
License: GPL-2.0-only
Expand Down

0 comments on commit 0cc5361

Please sign in to comment.