Skip to content
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

Added option to preserve the preview window to avoid refreshing when … #607

Merged
merged 8 commits into from
Jul 27, 2022

Conversation

rben01
Copy link
Contributor

@rben01 rben01 commented Jul 18, 2022

…switching away, and added setting to control this behavior.

(Simple fix; vscode.window.createWebviewPanel takes a parameter for this very thing.)

If accepted, it would close #603

@ggrossetie
Copy link
Member

It seems like a simple fix but I'm concerned about unexpected side effects. This extension is largely inspired by the Markdown extension which does not use this option: https://github.com/microsoft/vscode/blob/6a4e5cc26b29359472378c2a8951c33f4ea73244/extensions/markdown-language-features/src/preview/preview.ts#L643-L646

Since the Markdown is developed by the VS Code core team, I tend to believe that there's a good reason.

Also, I don't think we should introduce an new option.

@rben01
Copy link
Contributor Author

rben01 commented Jul 20, 2022

https://code.visualstudio.com/api/references/vscode-api#WebviewPanelOptions says

retainContextWhenHidden has a high memory overhead and should only be used if your panel's context cannot be quickly saved and restored.

I don't actually know what the overhead is — is it just that the web view is consuming its normal amount of memory even when not visible, or does enabling this option somehow use even more memory than normal? I added an option because if this is really detrimental, users may want the option of trading restoration speed for memory use.

Saving and restoring scroll position is also an option but would still require a re-render on load, which is slower than just restoring.

I've been using my fork of this extension and haven't noticed any problems — on the contrary I've really been enjoying the quick (nearly zero) loading speed when switching between tabs. Then again I am just one user.

@ggrossetie
Copy link
Member

Saving and restoring scroll position is also an option but would still require a re-render on load, which is slower than just restoring.

That's a good point.
In this case, I think we should try to restore scroll position by expanding the setState/getState logic that we currently have in place:

const originalState = vscode.getState()
const state = {
...(typeof originalState === 'object' ? originalState : {}),
...getData<any>('data-state'),
}
// Make sure to sync VS Code state here
vscode.setState(state)

Then, we can add an option (named "Instant Restore"?) to retain the WebView context when hidden that clearly states that this feature has a high memory overhead (quoting the documentation).

What do you think?

@rben01
Copy link
Contributor Author

rben01 commented Jul 22, 2022

Then, we can add an option (named "Instant Restore"?) to retain the WebView context when hidden that clearly states that this feature has a high memory overhead (quoting the documentation).

I think this makes sense. So all that would need to be added is the ability to restore the scroll position when asciidoc.preview.preservePreviewWhenHidden is disabled (since when it's enabled the scroll position is automatically restored). I'll try to work on this this weekend.

@ggrossetie
Copy link
Member

That's correct!

You will also need to add a description for this new option and update the README but it's just small details.

rben01 added 2 commits July 24, 2022 15:28
…e background

Added setting to enable/disable fast reloading at expense of memory usage
Updated extension docs accordingly
@rben01
Copy link
Contributor Author

rben01 commented Jul 24, 2022

@Mogztter I pushed my changes. The only thing I couldn't do was the French and Japanese translations of the setting.

@ggrossetie
Copy link
Member

Awesome, thanks @rben01 !
It looks good but I will take a closer look at it tomorrow.

rben01 and others added 3 commits July 26, 2022 15:12
…e background

Added setting to enable/disable fast reloading at expense of memory usage
Updated extension docs accordingly
@ggrossetie
Copy link
Member

It does not scroll automatically when the webview is restored:

auto-scroll

As you can see, I need to click again otherwise it won't scroll.

When asciidoc.preview.preservePreviewWhenHidden is checked, then it's working. It scrolls the preview even when the preview is hidden:
scroll-hidden

@ggrossetie ggrossetie force-pushed the preserve-preview-pane branch from f71b5b9 to f54962e Compare July 26, 2022 13:30
@ggrossetie
Copy link
Member

ggrossetie commented Jul 26, 2022

I rebased on master and force-pushed (to fix the conflict)

@rben01
Copy link
Contributor Author

rben01 commented Jul 26, 2022

Actually I think that this behavior was always present in the extension; if I'm not mistaken it always required a scroll to re-sync the preview and the editor. In any case I think I've fixed that issue and have now implemented 100% scroll position restoration. The logic I've implemented tries to respect the most recent user action by saving the corresponding line position and restoring it when the web view is restored. Whichever is the most recent of: 1. scrolling the editor 2. changing the selection in the editor 3. scrolling the preview — will be used to restore the preview's scroll position when it comes back from the background. This happens regardless of whether the new preservePreviewWhenHidden setting is enabled, but of course restoration is faster if it is.

@ggrossetie
Copy link
Member

It's working nicely now, thanks!

The only case when it does not restore the scroll position is when:

  1. hide the preview
  2. scroll (but don't click on the editor)
  3. restore the preview

click

Anyway, that's a small detail and it's already a huge improvement so I'm willing to merge. Let me know if you want to fix this last case or not.

@rben01
Copy link
Contributor Author

rben01 commented Jul 27, 2022

Odd, I'm not seeing that behavior — if I try to reproduce your video I get the intended behavior.

Screen.Recording.2022-07-27.at.11.08.49.AM.mp4

All I can think is that maybe you changed a preview setting without closing and reopening the window? (I think for the the most part the settings are only loaded when the window is created, and are never refreshed.) Anyway, please do merge! I'll see if there's something else that might be causing this discrepancy.

@ggrossetie
Copy link
Member

You need to scroll further down when the preview is hidden. In your video you scrolled back to the top.

@rben01
Copy link
Contributor Author

rben01 commented Jul 27, 2022

Hmm, scrolling down also works for me.

@ggrossetie ggrossetie merged commit 6e09457 into asciidoctor:master Jul 27, 2022
@ggrossetie
Copy link
Member

Anyway, that's not a big deal. Thanks again for your contribution 🤗

@rben01 rben01 deleted the preserve-preview-pane branch July 27, 2022 18:15
@rben01 rben01 restored the preserve-preview-pane branch July 27, 2022 18:15
@rben01 rben01 deleted the preserve-preview-pane branch July 27, 2022 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Full-window preview completely refreshes when switching tabs, losing scroll position
2 participants