-
Notifications
You must be signed in to change notification settings - Fork 203
Add the scroll progressions in the EPUB viewport
#620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking "Sign up for GitHub", you agree to our terms of service and privacy statement. We'll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add the scroll progressions in the EPUB viewport
#620
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -136,6 +136,10 @@ open class EPUBNavigatorViewController: InputObservableViewController, | |
| /// Indices of the visible reading order resources. | ||
| public var readingOrderIndices: ClosedRange<[Link].Index> | ||
|
|
||
| /// Range of visible scroll progressions for each visible reading order | ||
| /// resource. | ||
|
public var progressions: [[Link].Index: ClosedRange |
||
|
|
||
| /// Range of visible positions. | ||
|
public var positions: ClosedRange |
||
| } | ||
|
|
@@ -623,44 +627,57 @@ open class EPUBNavigatorViewController: InputObservableViewController, | |
| return (nil, nil) | ||
| } | ||
|
|
||
| let index = spreadView.focusedResource ?? spreadView.spread.leading | ||
| let link = readingOrder[index] | ||
| let href = link.url() | ||
| let progressionRange = spreadView.progression(in: index) | ||
| let firstProgression = min(max(progressionRange.lowerBound, 0.0), 1.0) | ||
| let lastProgression = min(max(progressionRange.upperBound, 0.0), 1.0) | ||
|
|
||
| let location: Locator? | ||
| var viewport = Viewport( | ||
| readingOrderIndices: spreadView.spread.readingOrderIndices, | ||
| progressions: spreadView.spread.readingOrderIndices.reduce([:]) { progressions, index in | ||
| var progressions = progressions | ||
| progressions[index] = spreadView.progression(in: index) | ||
| return progressions | ||
| }, | ||
| positions: nil | ||
| ) | ||
|
|
||
| let firstIndex = spreadView.spread.readingOrderIndices.lowerBound | ||
| let lastIndex = spreadView.spread.readingOrderIndices.upperBound | ||
| let progressionOfFirstResource = spreadView.progression(in: firstIndex) | ||
| let progressionOfLastResource = spreadView.progression(in: lastIndex) | ||
| let firstProgressionInFirstResource = min(max(progressionOfFirstResource.lowerBound, 0.0), 1.0) | ||
| let lastProgressionInLastResource = min(max(progressionOfLastResource.upperBound, 0.0), 1.0) | ||
|
|
||
| let link = readingOrder[firstIndex] | ||
| let location: Locator? | ||
|
|
||
| if | ||
| // The positions are not always available, for example a Readium | ||
| // WebPub doesn't have any unless a Publication Positions Web | ||
| // Service is provided | ||
| let index = readingOrder.firstIndexWithHREF(href), | ||
| let positionList = positionsByReadingOrder.getOrNil(index), | ||
| positionList.count > 0, | ||
| let positionOffset = positionList[0].locations.position | ||
| let positionsOfFirstResource = positionsByReadingOrder.getOrNil(firstIndex), | ||
| let positionsOfLastResource = positionsByReadingOrder.getOrNil(lastIndex), | ||
| !positionsOfFirstResource.isEmpty, | ||
| !positionsOfLastResource.isEmpty | ||
| { | ||
| // Gets the current locator from the positionList, and fill its missing data. | ||
| let firstPositionIndex = Int(ceil(firstProgression * Double(positionList.count - 1))) | ||
| let lastPositionIndex = (lastProgression == 1.0) | ||
| ? positionList.count - 1 | ||
| : max(firstPositionIndex, Int(ceil(lastProgression * Double(positionList.count - 1))) - 1) | ||
|
|
||
| location = await positionList[firstPositionIndex].copy( | ||
| title: tableOfContentsTitleByHref[equivalent: href], | ||
| locations: { $0.progression = firstProgression } | ||
| // Gets the current locator from the positions, and fill its missing | ||
| // data. | ||
| let firstPositionIndex = Int(ceil(firstProgressionInFirstResource * Double(positionsOfFirstResource.count - 1))) | ||
| let lastPositionIndex = (lastProgressionInLastResource == 1.0) | ||
| ? positionsOfLastResource.count - 1 | ||
| : max(firstPositionIndex, Int(ceil(lastProgressionInLastResource * Double(positionsOfLastResource.count - 1))) - 1) | ||
|
|
||
| location = await positionsOfFirstResource[firstPositionIndex].copy( | ||
| title: tableOfContentsTitleByHref[equivalent: link.url()], | ||
| locations: { $0.progression = firstProgressionInFirstResource } | ||
| ) | ||
|
|
||
| viewport.positions = (positionOffset + firstPositionIndex) ... (positionOffset + lastPositionIndex) | ||
| if | ||
| let firstPosition = location?.locations.position, | ||
| let lastPosition = positionsOfLastResource[lastPositionIndex].locations.position | ||
| { | ||
| viewport.positions = firstPosition ... lastPosition | ||
| } | ||
|
|
||
| } else { | ||
| location = await publication.locate(link)?.copy( | ||
| locations: { $0.progression = firstProgression } | ||
| locations: { $0.progression = firstProgressionInFirstResource } | ||
| ) | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -131,12 +131,12 @@ final class EPUBReflowableSpreadView: EPUBSpreadView { | |
|
|
||
| // MARK: - Location and progression | ||
|
|
||
|
override func progression(in index: ReadingOrder.Index) -> Range |
||
|
override func progression(in index: ReadingOrder.Index) -> ClosedRange |
||
| guard | ||
| spread.leading == index, | ||
| let progression = progression | ||
| else { | ||
| return 0 ..< 0 | ||
| return 0 ... 0 | ||
| } | ||
| return progression | ||
| } | ||
|
|
@@ -319,9 +319,9 @@ final class EPUBReflowableSpreadView: EPUBSpreadView { | |
| // MARK: - Progression | ||
|
|
||
| // Current progression range in the page. | ||
|
private var progression: Range |
||
|
private var progression: ClosedRange |
||
| // To check if a progression change was cancelled or not. | ||
|
private var previousProgression: Range |
||
|
private var previousProgression: ClosedRange |
||
|
|
||
| // Called by the javascript code to notify that scrolling ended. | ||
| private func progressionDidChange(_ body: Any) { | ||
|
|
@@ -340,7 +340,7 @@ final class EPUBReflowableSpreadView: EPUBSpreadView { | |
| if previousProgression == nil { | ||
| previousProgression = progression | ||
| } | ||
| progression = firstProgression ..< lastProgression | ||
| progression = firstProgression ... lastProgression | ||
|
|
||
| setNeedsNotifyPagesDidChange() | ||
| } | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -342,9 +342,9 @@ class EPUBSpreadView: UIView, Loggable, PageView { | |
| // MARK: - Location and progression. | ||
|
|
||
| /// Current progression in the resource with given href. | ||
|
func progression(in index: ReadingOrder.Index) -> Range |
||
|
func progression(in index: ReadingOrder.Index) -> ClosedRange |
||
| // To be overridden in subclasses if the resource supports a progression. | ||
| 0 ..< 0 | ||
| 0 ... 1 | ||
| } | ||
|
|
||
| func go(to location: PageLocation) async { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.