Skip to content

Commit

Permalink
linux file chooser
Browse files Browse the repository at this point in the history
  • Loading branch information
reymondzzzz committed Nov 28, 2024
1 parent 3d5565c commit 36087c9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class Events {
class Config {
abstract class BaseFeatures()

data class Features(val ast: Boolean, val vecdb: Boolean, val images: Boolean? = false) : BaseFeatures()
data class Features(val ast: Boolean, val vecdb: Boolean, val images: Boolean? = true) : BaseFeatures()

data class ThemeProps(
val mode: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.fileChooser.FileChooser
import com.intellij.openapi.fileChooser.FileChooserDescriptor
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.keymap.Keymap
import com.intellij.openapi.keymap.KeymapManager
import com.intellij.openapi.keymap.KeymapUtil
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.SystemInfo
import com.intellij.ui.jcef.JBCefBrowser
import com.intellij.ui.jcef.JBCefBrowserBase
Expand All @@ -26,12 +29,13 @@ import kotlinx.coroutines.launch
import org.cef.CefApp
import org.cef.CefSettings
import org.cef.browser.CefBrowser
import org.cef.handler.CefDisplayHandlerAdapter
import org.cef.handler.CefKeyboardHandler
import org.cef.handler.CefKeyboardHandlerAdapter
import org.cef.handler.CefLoadHandlerAdapter
import org.cef.callback.CefFileDialogCallback
import org.cef.handler.*
import java.util.*
import java.util.concurrent.atomic.AtomicReference
import javax.swing.JComponent


fun getActionKeybinding(actionId: String): String {
// Get the KeymapManager instance
val keymapManager: KeymapManager = KeymapManager.getInstance()
Expand Down Expand Up @@ -77,6 +81,23 @@ class ChatWebView(val editor: Editor, val messageHandler: (event: Events.FromCha
}
}

fun showFileChooserDialog(project: Project?, title: String?, isMultiple: Boolean, filters: Vector<String>): String {
val filePath: AtomicReference<String> = AtomicReference("")
ApplicationManager.getApplication().invokeAndWait {
var fileChooserDescriptor =
FileChooserDescriptor(true, false, false, false, false, false)
fileChooserDescriptor.title = if (title.isNullOrEmpty() || title.isBlank()) "Choose File" else title
fileChooserDescriptor =
fileChooserDescriptor.withFileFilter { file -> filters.any { filter -> file.name.endsWith(filter) } }
val file = FileChooser.chooseFile(fileChooserDescriptor, project, null)
if (file != null) {
filePath.set(file.canonicalPath)
}
}
return filePath.get()
}


val webView by lazy {
val isOSREnable = when {
SystemInfo.isWindows -> false
Expand All @@ -98,11 +119,13 @@ class ChatWebView(val editor: Editor, val messageHandler: (event: Events.FromCha
if (!isOSREnable) {
val onTabHandler: CefKeyboardHandler = object : CefKeyboardHandlerAdapter() {
override fun onKeyEvent(browser: CefBrowser?, event: CefKeyboardHandler.CefKeyEvent?): Boolean {
val wasTabPressed = event?.type == CefKeyboardHandler.CefKeyEvent.EventType.KEYEVENT_KEYUP && event.modifiers == 0 && event.character == '\t';
val wasTabPressed =
event?.type == CefKeyboardHandler.CefKeyEvent.EventType.KEYEVENT_KEYUP && event.modifiers == 0 && event.character == '\t';
val currentEditor = FileEditorManager.getInstance(editor.project).selectedTextEditor
val isInDiffMode = currentEditor != null && ModeProvider.getOrCreateModeProvider(currentEditor).isDiffMode()
val isInDiffMode =
currentEditor != null && ModeProvider.getOrCreateModeProvider(currentEditor).isDiffMode()

if(wasTabPressed && currentEditor != null && isInDiffMode) {
if (wasTabPressed && currentEditor != null && isInDiffMode) {
ApplicationManager.getApplication().invokeLater {
ModeProvider.getOrCreateModeProvider(currentEditor)
.onTabPressed(currentEditor, null, DataContext.EMPTY_CONTEXT)
Expand Down Expand Up @@ -144,6 +167,28 @@ class ChatWebView(val editor: Editor, val messageHandler: (event: Events.FromCha
return super.onConsoleMessage(browser, level, message, source, line)
}
}, browser.cefBrowser)
if (SystemInfo.isLinux) {
browser.jbCefClient.addDialogHandler(object : CefDialogHandler {
override fun onFileDialog(
cefBrowser: CefBrowser?,
mode: CefDialogHandler.FileDialogMode,
title: String,
defaultFilePath: String,
filters: Vector<String>,
callback: CefFileDialogCallback
): Boolean {
val filePath = showFileChooserDialog(
editor.project,
title,
mode == CefDialogHandler.FileDialogMode.FILE_DIALOG_OPEN_MULTIPLE,
filters
)
filters.add(filePath)
callback.Continue(filters)
return true
}
}, browser.cefBrowser)
}

CefApp.getInstance().registerSchemeHandlerFactory("http", "refactai", RequestHandlerFactory())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class EventsTest {
Events.Config.KeyBindings("foo"))
val message = Events.Config.Update(payload)
val result = Events.stringify(message)
val expected = """{"type":"config/update","payload":{"features":{"ast":true,"vecdb":false,"images":false},"themeProps":{"mode":"light","hasBackground":false,"scale":"90%","accentColor":"gray"},"lspPort":8001,"apiKey":"apiKey","addressURL":"http://127.0.0.1;8001","keyBindings":{"completeManual":"foo"},"tabbed":false,"host":"jetbrains"}}"""
val expected = """{"type":"config/update","payload":{"features":{"ast":true,"vecdb":false,"images":true},"themeProps":{"mode":"light","hasBackground":false,"scale":"90%","accentColor":"gray"},"lspPort":8001,"apiKey":"apiKey","addressURL":"http://127.0.0.1;8001","keyBindings":{"completeManual":"foo"},"tabbed":false,"host":"jetbrains"}}"""
assertEquals(expected, result)
}

Expand Down

0 comments on commit 36087c9

Please sign in to comment.