Skip to content

Commit

Permalink
Restore the CWM::RichText scroll after updating its content
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Mar 6, 2020
1 parent b0eebf0 commit a15fcaf
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
34 changes: 34 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,40 @@ class RichText < AbstractWidget
self.widget_type = :richtext

include ValueBasedWidget

# Determines if the scroll should be restored after updating the content
#
# @return [Boolean] true if the scroll should be restored; false otherwise
def keep_scroll?
false
end

# Updates the content
#
# Depending on #keep_scroll?, the 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
45 changes: 44 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,47 @@ def hspacing
end
end
end

describe CWM::RichText do
subject { described_class.new }
let(:widget_id) { Id(subject.widget_id) }

describe "#value=" do
before do
allow(subject).to receive(:keep_scroll?).and_return(keep_scroll)
allow(Yast::UI).to receive(:ChangeWidget)
end

context "when set to restore the scroll" do
let(:keep_scroll) { true }

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

subject.value = "Test"
end

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

subject.value = "Test"
end
end

context "when set to not restore the scroll" do
let(:keep_scroll) { false }

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

subject.value = "Test"
end

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

subject.value = "Test"
end
end
end
end

0 comments on commit a15fcaf

Please sign in to comment.