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

Focus is not reset after a model change #194

Closed
akosyakov opened this issue Sep 18, 2016 · 8 comments
Closed

Focus is not reset after a model change #194

akosyakov opened this issue Sep 18, 2016 · 8 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug
Milestone

Comments

@akosyakov
Copy link

akosyakov commented Sep 18, 2016

Steps to reproduce:

  1. call editor.focus
  2. call editor.setModel with a new model

the editor should be focused after a change but it is not

@alexdima alexdima added the bug Issue identified by VS Code Team member as probable bug label Sep 19, 2016
@alexdima alexdima added this to the Backlog milestone Sep 19, 2016
@alexdima alexdima self-assigned this Sep 19, 2016
@alexdima
Copy link
Member

Most likely happening because we destroy and recreate the view when a model changes, so the dom node that holds focus gets detached from the DOM and a new textarea is created.

You can workaround on your side by restoring focus via editor.focus() if editor.isFocused() is true before the model is set.

@akosyakov
Copy link
Author

@alexandrudima yes, i do exactly that, but there is one undesirable effect: if one has already started typing before editor.focus() is called, then the cursor jumps to the start of a model

@alexdima
Copy link
Member

@akosyakov Can you explain in greater detail the order of things, or the shape of your code, how could one start typing before editor.focus() if the code is written synchronously:

function setModelMaintainFocus(editor, model) {
  let isFocused = editor.isFocused();
  editor.setModel(model);
  if (isFocused) {
    editor.focus();
  }
}

@akosyakov
Copy link
Author

@alexandrudima I see, you are right.

We create a model just to change an URI and expect that the state of an editor will be preserved, e.g. focus and position of the cursor. But it happens that the cursor is moved to the beginning of an editor after a model change. We could also store a cursor offset and reset it after, similar to focus.

@alexdima
Copy link
Member

Yes, the view state is reset when changing models (e.g. cursor position, scroll position, folding state, etc.) because we can't assume that we get 100% the same text as it appears to be in your case. I suggest using editor.saveViewState() and editor.restoreViewState().

@akosyakov
Copy link
Author

@alexandrudima will it help with focus either, or we still have to apply the workaround?

@alexdima
Copy link
Member

The focus preserving is a valid bug.

restoreViewState will restore the scroll position, the selection state, etc. This is usually useful when implementing something as tabs, where there is one editor instance that consistently gets calls to setModel(..) to switch from one tab to another. For each tab, the view state can be maintained via saveViewState() and restoreViewState().

The view state gets reset with each setModel(...) call because one file could have 1000 lines and the next could have only 10, etc. So the cursor position, the scroll state from one file don't necessarily make sense for the next file...

@akosyakov
Copy link
Author

got it, thx

@alexdima alexdima modified the milestones: Backlog, On Deck, December 2019 Dec 10, 2019
alexdima added a commit to microsoft/vscode that referenced this issue Dec 20, 2019
@vscodebot vscodebot bot locked and limited conversation to collaborators Jan 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests

2 participants