Skip to content

Commit

Permalink
Merge pull request #69 from slideclimb/editor-view
Browse files Browse the repository at this point in the history
Improve experience when having multiple PDFs open
  • Loading branch information
slideclimb committed May 3, 2023
2 parents 350b567 + 5edb2bf commit 1502c70
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.PlatformDataKeys
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project

abstract class PdfAction(protected val viewModeAwareness: ViewModeAwareness = ViewModeAwareness.IDE) : AnAction() {
override fun update(event: AnActionEvent) {
val editor = findAnyEditor(event)
val editor = findEditorInView(event)
with(event.presentation) {
isVisible = editor != null
isEnabled = findController(editor) != null
Expand All @@ -38,22 +39,29 @@ abstract class PdfAction(protected val viewModeAwareness: ViewModeAwareness = Vi
}

companion object {
fun findEditor(event: AnActionEvent): PdfFileEditor? {
return event.getData(PlatformDataKeys.FILE_EDITOR) as? PdfFileEditor
}

fun findAnyEditor(event: AnActionEvent): PdfFileEditor? {
val project = event.project ?: return null
val editorManager = FileEditorManager.getInstance(project)
return editorManager.allEditors.firstOrNull { it is PdfFileEditor } as? PdfFileEditor
/**
* Find the editor that belongs to the given [event].
*
* If the editor that currently has focus is a [PdfFileEditor], return that one. If not, look for any other PdfFileEditors that are open
* (split view) and select the first we find there. If there is no [PdfFileEditor] selected, this returns null. Note that in that case it
* is possible that there is a PDF open somewhere, but it is not in view.
*/
fun findEditorInView(event: AnActionEvent): PdfFileEditor? {
val focusedEditor = event.getData(PlatformDataKeys.FILE_EDITOR) as? PdfFileEditor

return focusedEditor ?: run {
val project = event.project ?: return null
FileEditorManager.getInstance(project).selectedEditors.firstOrNull {it is PdfFileEditor} as? PdfFileEditor
}
}

fun hasOpenedEditor(event: AnActionEvent): Boolean {
return findAnyEditor(event) != null
fun hasEditorInView(event: AnActionEvent): Boolean {
return findEditorInView(event) != null
}

fun findController(event: AnActionEvent): PdfJcefPreviewController? {
return findAnyEditor(event)?.viewComponent?.controller
return findEditorInView(event)?.viewComponent?.controller
}

fun findController(editor: PdfFileEditor?): PdfJcefPreviewController? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class PdfPageSpreadActionGroup : DefaultActionGroup() {
override fun isPopup(): Boolean = true

override fun update(event: AnActionEvent) {
event.presentation.isVisible = PdfAction.hasOpenedEditor(event)
event.presentation.isVisible = PdfAction.hasEditorInView(event)
event.presentation.isEnabled = PdfAction.findController(event) != null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class PdfSidebarViewModeActionGroup : DefaultActionGroup() {
override fun isPopup(): Boolean = true

override fun update(event: AnActionEvent) {
event.presentation.isVisible = PdfAction.hasOpenedEditor(event)
event.presentation.isVisible = PdfAction.hasEditorInView(event)
event.presentation.isEnabled = PdfAction.findController(event) != null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import com.intellij.openapi.actionSystem.AnActionEvent

sealed class PdfSearchAction(private val direction: SearchDirection) : PdfDumbAwareAction() {
override fun actionPerformed(event: AnActionEvent) {
val editor = findEditor(event) ?: return
val editor = findEditorInView(event) ?: return
val controller = findController(event) ?: return
val searchQuery = editor.viewComponent.searchPanel.searchQuery
controller.find(searchQuery, direction)
}

override fun update(event: AnActionEvent) {
super.update(event)
val searchPanel = findEditor(event)?.viewComponent?.searchPanel
val searchPanel = findEditorInView(event)?.viewComponent?.searchPanel
event.presentation.isEnabled = searchPanel?.searchText?.isNotEmpty() == true && searchPanel.isVisible
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent

class PdfShowFindPopupAction: PdfDumbAwareAction() {
override fun actionPerformed(event: AnActionEvent) {
val searchPanel = findEditor(event)?.viewComponent?.searchPanel ?: return
val searchPanel = findEditorInView(event)?.viewComponent?.searchPanel ?: return
if (!searchPanel.isVisible) {
searchPanel.setEnabledState(true)
}
Expand All @@ -14,7 +14,7 @@ class PdfShowFindPopupAction: PdfDumbAwareAction() {
override fun update(event: AnActionEvent) {
super.update(event)
// TODO: Refactor
val searchPanel = findEditor(event)?.viewComponent?.searchPanel ?: return
val searchPanel = findEditorInView(event)?.viewComponent?.searchPanel ?: return
event.presentation.isEnabled = !searchPanel.isVisible
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ sealed class PdfSetSidebarViewModeAction(private val targetViewMode: SidebarView

override fun update(event: AnActionEvent) {
super.update(event)
event.presentation.isVisible = PdfAction.hasOpenedEditor(event)
event.presentation.isVisible = PdfAction.hasEditorInView(event)
event.presentation.isEnabled = canBeEnabled(PdfAction.findController(event))
}

Expand Down

0 comments on commit 1502c70

Please sign in to comment.