Skip to content

Commit

Permalink
Refactor UI (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
TarCV committed Jul 21, 2024
1 parent 26f8478 commit 424e98e
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ object DroidNotices {

private val droidUiAutomatorIntro = """
depends on UIAutomator library which is covered by
the following copyright and permission notices:
""".trimStart()
the following copyright and permission notices:""".trimStart()

private val droidUiAutomatorNotices = """
Copyright (C) 2014 The Android Open Source Project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import androidx.test.uiautomator.UiSelector
import com.intellij.codeInsight.daemon.impl.analysis.JavaModuleGraphUtil
import com.intellij.ide.highlighter.JavaFileType
import com.intellij.openapi.application.invokeLater
import com.intellij.openapi.fileTypes.LanguageFileType
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.module.ModuleTypeId
Expand All @@ -43,7 +42,7 @@ import java.nio.file.Path
import java.nio.file.Paths

@Suppress("UnstableApiUsage", "unused") // Used by string reference in LocateToolWindowFactory.createToolWindowContent
class JvmLocateToolWindow(project: Project) : LocateToolWindow(project) {
class JvmLocateToolWindow(project: Project) : LocateToolWindow(project, JavaFileType.INSTANCE) {
companion object {
const val OLD_MODULE_NAME = "UISurveyor_Highlighting"
const val MODULE_NAME = "__UISurveyor_Highlighting"
Expand All @@ -56,8 +55,6 @@ class JvmLocateToolWindow(project: Project) : LocateToolWindow(project) {
return PsiDocumentManager.getInstance(project).getPsiFile(docField.document)
}

override val fileType: LanguageFileType = JavaFileType.INSTANCE

override fun switchToDroidUiAutomator(editorField: EditorTextField) {
invokeLater {
removeModuleIfExists(OLD_MODULE_NAME)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,41 @@ import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.fileTypes.FileTypes
import com.intellij.openapi.fileTypes.LanguageFileType
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.SimpleToolWindowPanel
import com.intellij.openapi.ui.playback.commands.ActionCommand
import com.intellij.openapi.util.SystemInfo
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiDocumentManager
import com.intellij.ui.EditorTextField
import com.intellij.util.ui.components.BorderLayoutPanel
import java.awt.event.InputEvent
import java.awt.event.KeyEvent
import javax.swing.JComponent
import javax.swing.JPanel
import javax.swing.KeyStroke


abstract class LocateToolWindow(protected val project: Project) : LocatorTypeChangedListener {
private lateinit var content: JPanel
protected lateinit var locatorField: JPanel
private lateinit var toolbar: JComponent
abstract class LocateToolWindow(
protected val project: Project,
droidSnapshotFileType: LanguageFileType
) : SimpleToolWindowPanel(/* vertical = */ true, /* borderless = */ false), LocatorTypeChangedListener {
protected var locatorField: JPanel
val preferredFocusedComponent: JComponent
get() = locatorField

protected abstract val fileType: LanguageFileType
private var locatorProvider: () -> String = { "" }

init {
project.messageBus.connect().subscribe(LocatorTypeChangedListener.topic, this)
}

fun createUIComponents() {
val actionToolbar = with(ActionManager.getInstance()) {
createActionToolbar(
ActionPlaces.TOOLWINDOW_CONTENT,
getAction("com.github.tarcv.testingteam.surveyoridea.gui.LocateToolWindow.toolbar") as ActionGroup,
true
)
}
toolbar = actionToolbar.component

// TODO: Set initial locator depending on the selected type
val editorField = EditorTextField("new UiSelector()", project, fileType).apply {
val editorField = EditorTextField("new UiSelector()", project, droidSnapshotFileType).apply {
setOneLineMode(false)
}

Expand Down Expand Up @@ -99,20 +98,32 @@ abstract class LocateToolWindow(protected val project: Project) : LocatorTypeCha
onLocatorTypeChanged(locatorType)
registerToolWindow(this@LocateToolWindow)
}
actionToolbar.setTargetComponent(editorField)

@Suppress("LeakingThis")
setContent(BorderLayoutPanel().apply {
addToCenter(editorField)
})

actionToolbar.targetComponent = editorField
@Suppress("LeakingThis") setToolbar(actionToolbar.component)

locatorField = editorField
}

override fun onLocatorTypeChanged(newType: LocatorType?): Unit = invokeLater {
val editorField = locatorField as EditorTextField
val oldCaretPosition = if (editorField.isFocusOwner) {
editorField.caretModel?.primaryCaret?.offset
} else {
null
}
editorField.isEnabled = false
try {
when (newType) {
DroidUiSelectorLocatorType, null -> {
switchToDroidUiAutomator(editorField)
locatorProvider = ::getCurrentDroidUiAutomatorLocator
}
// TODO: Don't require completed indexing when highlighting is not supported
IClassChainLocatorType, IPredicateLocatorType -> {
with(editorField) {
document = EditorFactory.getInstance().createDocument(StringUtil.convertLineSeparators(text))
Expand All @@ -122,7 +133,19 @@ abstract class LocateToolWindow(protected val project: Project) : LocatorTypeCha
}
}
} finally {
editorField.isEnabled = true
with(editorField) {
isEnabled = true
invokeLater { // TODO: Consider modality values everywhere
if (oldCaretPosition != null) {
try {
setCaretPosition(oldCaretPosition)
} catch (e: IllegalArgumentException) {
// no-op
}
requestFocusInWindow()
}
}
}
}
}

Expand All @@ -139,7 +162,5 @@ abstract class LocateToolWindow(protected val project: Project) : LocatorTypeCha

abstract fun switchToDroidUiAutomator(editorField: EditorTextField)

fun getContent(): JPanel = content

abstract fun getCurrentDroidUiAutomatorLocator(): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
*/
package com.github.tarcv.testingteam.surveyoridea.gui

import com.github.tarcv.testingteam.surveyoridea.services.LocatorTypeChangedListener
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.ui.content.ContentFactory


class LocateToolWindowFactory : ToolWindowFactory {
class LocateToolWindowFactory : ToolWindowFactory, DumbAware {
private val contentFactory = ContentFactory.getInstance()

override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
Expand All @@ -34,7 +36,10 @@ class LocateToolWindowFactory : ToolWindowFactory {
} else {
SimpleLocateToolWindow(project)
}
val content = contentFactory.createContent(locateToolWindow.getContent(), null, false)
project.messageBus.connect().subscribe(LocatorTypeChangedListener.topic, locateToolWindow)
val content = contentFactory.createContent(locateToolWindow, null, false).apply {
preferredFocusableComponent = locateToolWindow.preferredFocusedComponent
}
toolWindow.contentManager.addContent(content)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,5 @@ class SelectLocatorTypeAction(private val locatorType: LocatorType) : AnAction()
val project = getEventProject(e)
val service = project?.getService(LocateToolHoldingService::class.java) ?: return
service.locatorType = locatorType
// TODO: update textarea highlighting language
}
}

This file was deleted.

Loading

0 comments on commit 424e98e

Please sign in to comment.