da04193e06cb3978f8f263872f5f2f54bd8813db
[idea/community.git] / platform / platform-impl / src / com / intellij / openapi / editor / impl / VisibleEditorsTracker.java
1 // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.openapi.editor.impl;
3
4 import com.intellij.openapi.application.ApplicationManager;
5 import com.intellij.openapi.command.CommandEvent;
6 import com.intellij.openapi.command.CommandListener;
7 import com.intellij.openapi.editor.Editor;
8 import com.intellij.openapi.editor.EditorFactory;
9 import org.jetbrains.annotations.NotNull;
10
11 import java.util.HashSet;
12 import java.util.Set;
13
14 public class VisibleEditorsTracker implements CommandListener {
15   private final Set<Editor> myEditorsVisibleOnCommandStart = new HashSet<>();
16   private long myCurrentCommandStart;
17   private long myLastCommandFinish;
18
19   public static VisibleEditorsTracker getInstance() {
20     return ApplicationManager.getApplication().getComponent(VisibleEditorsTracker.class);
21   }
22
23   public VisibleEditorsTracker() {
24     ApplicationManager.getApplication().getMessageBus().connect().subscribe(CommandListener.TOPIC, this);
25   }
26
27   public boolean wasEditorVisibleOnCommandStart(Editor editor){
28     return myEditorsVisibleOnCommandStart.contains(editor);
29   }
30
31   public long getCurrentCommandStart() { return myCurrentCommandStart; }
32
33   public long getLastCommandFinish() { return myLastCommandFinish; }
34
35   @Override
36   public void commandStarted(@NotNull CommandEvent event) {
37     for (Editor editor : EditorFactory.getInstance().getAllEditors()) {
38       if (editor.getComponent().isShowing()) {
39         myEditorsVisibleOnCommandStart.add(editor);
40       }
41
42       ((ScrollingModelImpl)editor.getScrollingModel()).finishAnimation();
43       myCurrentCommandStart = System.currentTimeMillis();
44     }
45   }
46
47   @Override
48   public void commandFinished(@NotNull CommandEvent event) {
49     myEditorsVisibleOnCommandStart.clear();
50     myLastCommandFinish = System.currentTimeMillis();
51   }
52 }