From 7940c11b2f470c9cfa3706e17a1d93f70a52bee1 Mon Sep 17 00:00:00 2001 From: Amir Bilu Date: Thu, 9 Nov 2023 09:10:17 +0200 Subject: [PATCH] DEV2-4144 fix sign in flow (#679) --- .../chat/ChatDisabledComponents.kt | 80 ++++++++----------- .../java/com/tabnineCommon/chat/ChatFrame.kt | 19 ++++- 2 files changed, 48 insertions(+), 51 deletions(-) diff --git a/Common/src/main/java/com/tabnineCommon/chat/ChatDisabledComponents.kt b/Common/src/main/java/com/tabnineCommon/chat/ChatDisabledComponents.kt index 78b80b7fa..4687c96bb 100644 --- a/Common/src/main/java/com/tabnineCommon/chat/ChatDisabledComponents.kt +++ b/Common/src/main/java/com/tabnineCommon/chat/ChatDisabledComponents.kt @@ -1,60 +1,44 @@ package com.tabnineCommon.chat -import java.awt.Desktop -import java.awt.event.MouseEvent -import java.awt.event.MouseListener -import java.io.IOException -import java.net.URI -import java.net.URISyntaxException -import javax.swing.BorderFactory -import javax.swing.JLabel -import javax.swing.SwingConstants - -val CHAT_DISABLED_PAGE = """ - +import com.intellij.ide.BrowserUtil +import com.tabnineCommon.binary.requests.login.LoginRequest +import com.tabnineCommon.general.DependencyContainer +import javax.swing.JEditorPane +import javax.swing.event.HyperlinkEvent +import javax.swing.event.HyperlinkListener + +val LOGIN_BUTTON = "

If you already have access to chat, please sign in

".trimIndent() + +fun createChatDisabledJPane(isLoggedIn: Boolean = false): JEditorPane { + val pane = JEditorPane( + "text/html", + """
Tabnine Chat is currently in Beta

We understand that waiting for this awesome feature isn’t easy, but we guarantee it will be worth it.

-

Tabnine Chat will soon be available to all users, and we'll make sure to keep you informed. Thank you for your patience! Learn more

-

Received Tabnine Chat beta access? Please ensure you're signed in

+

Tabnine Chat will soon be available to all users, and we'll make sure to keep you informed. Thank you for your patience! Learn more

+ ${if (!isLoggedIn) LOGIN_BUTTON else ""}
-""".trimIndent() - -fun createChatDisabledJLabel(): JLabel { - val label = JLabel( - CHAT_DISABLED_PAGE, SwingConstants.CENTER - ) - - label.border = BorderFactory.createEmptyBorder(10, 0, 0, 0) - label.addMouseListener(NaiveOpenLinkListener("https://www.tabnine.com/#ChatSection")) - - return label -} - -class NaiveOpenLinkListener(private val link: String) : MouseListener { - override fun mouseClicked(e: MouseEvent?) { - try { - Desktop.getDesktop().browse(URI(link)) - return - } catch (e1: IOException) { - e1.printStackTrace() - } catch (e1: URISyntaxException) { - e1.printStackTrace() - } - } - - override fun mousePressed(e: MouseEvent?) { - } - - override fun mouseReleased(e: MouseEvent?) { - } - - override fun mouseEntered(e: MouseEvent?) { + """.trimIndent() + ).apply { + isEditable = false + isOpaque = false } + pane.addHyperlinkListener( + HyperlinkListener { it -> + if (it.eventType !== HyperlinkEvent.EventType.ACTIVATED) { + return@HyperlinkListener + } + if (it.url.toString() == "https://www.tabnine.com/#ChatSection") { + BrowserUtil.browse(it.url.toString()) + } else if (it.url.toString() == "https://app.tabnine.com/signin") { + DependencyContainer.instanceOfBinaryRequestFacade().executeRequest(LoginRequest {}) + } + } + ) - override fun mouseExited(e: MouseEvent?) { - } + return pane } diff --git a/Common/src/main/java/com/tabnineCommon/chat/ChatFrame.kt b/Common/src/main/java/com/tabnineCommon/chat/ChatFrame.kt index f21390655..5c352aaf7 100644 --- a/Common/src/main/java/com/tabnineCommon/chat/ChatFrame.kt +++ b/Common/src/main/java/com/tabnineCommon/chat/ChatFrame.kt @@ -13,6 +13,7 @@ import com.tabnineCommon.binary.requests.analytics.EventRequest import com.tabnineCommon.chat.actions.TabnineActionsGroup import com.tabnineCommon.config.Config import com.tabnineCommon.lifecycle.BinaryCapabilitiesChangeNotifier +import com.tabnineCommon.lifecycle.BinaryStateChangeNotifier import java.awt.BorderLayout import java.awt.Color import javax.swing.BorderFactory @@ -27,6 +28,7 @@ import javax.swing.event.HyperlinkEvent class ChatFrame(private val project: Project, private val binaryRequestFacade: BinaryRequestFacade) : JPanel(true), Disposable { private var capabilitiesFetched = false + private var isLoggedIn = false init { layout = BorderLayout() @@ -34,6 +36,17 @@ class ChatFrame(private val project: Project, private val binaryRequestFacade: B updateDisplay() val connection = ApplicationManager.getApplication().messageBus.connect(this) + + connection.subscribe( + BinaryStateChangeNotifier.STATE_CHANGED_TOPIC, + BinaryStateChangeNotifier { state -> + isLoggedIn = state.isLoggedIn == true + ApplicationManager.getApplication().invokeLater { + updateDisplay() + } + } + ) + connection.subscribe( ChatEnabled.ENABLED_TOPIC, ChatEnabledChanged { @@ -59,10 +72,10 @@ class ChatFrame(private val project: Project, private val binaryRequestFacade: B } private fun updateDisplay() { - if (ChatEnabled.getInstance().enabled) { + if (ChatEnabled.getInstance().enabled && isLoggedIn) { displayChat() } else { - if (capabilitiesFetched || Config.IS_SELF_HOSTED) { + if (capabilitiesFetched || Config.IS_SELF_HOSTED || !isLoggedIn) { displayChatNotEnabled() } else { displayText("Loading...") @@ -71,7 +84,7 @@ class ChatFrame(private val project: Project, private val binaryRequestFacade: B } private fun displayChatNotEnabled() { - displayJLabel(createChatDisabledJLabel()) + setComponents(listOf(Pair(createChatDisabledJPane(isLoggedIn), BorderLayout.CENTER))) } private fun displayText(text: String) {