forked from qt-creator/qt-creator
Copilot: Update authenticator widget while changing settings
Use a separate client for the authenticator widget that can react on changes in the settings widget. This removes the need to manually apply the settings between setting up the nodejs and agent.js paths and signing into github. Change-Id: I6628a3a8906ccbba208f9dac968262df962f93c0 Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
@@ -19,22 +19,6 @@ using namespace Copilot::Internal;
|
|||||||
|
|
||||||
namespace Copilot {
|
namespace Copilot {
|
||||||
|
|
||||||
bool isCopilotClient(Client *client)
|
|
||||||
{
|
|
||||||
return dynamic_cast<CopilotClient *>(client) != nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
CopilotClient *asCoPilotClient(Client *client)
|
|
||||||
{
|
|
||||||
return static_cast<CopilotClient *>(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
Internal::CopilotClient *findClient()
|
|
||||||
{
|
|
||||||
CopilotClient *client = Internal::CopilotClient::instance();
|
|
||||||
return client && client->reachable() ? client : nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
AuthWidget::AuthWidget(QWidget *parent)
|
AuthWidget::AuthWidget(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
{
|
{
|
||||||
@@ -56,22 +40,12 @@ AuthWidget::AuthWidget(QWidget *parent)
|
|||||||
}.attachTo(this);
|
}.attachTo(this);
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
connect(LanguageClientManager::instance(),
|
|
||||||
&LanguageClientManager::clientAdded,
|
|
||||||
this,
|
|
||||||
&AuthWidget::onClientAdded);
|
|
||||||
|
|
||||||
connect(m_button, &QPushButton::clicked, this, [this]() {
|
connect(m_button, &QPushButton::clicked, this, [this]() {
|
||||||
if (m_status == Status::SignedIn)
|
if (m_status == Status::SignedIn)
|
||||||
signOut();
|
signOut();
|
||||||
else if (m_status == Status::SignedOut)
|
else if (m_status == Status::SignedOut)
|
||||||
signIn();
|
signIn();
|
||||||
});
|
});
|
||||||
|
|
||||||
CopilotClient *client = findClient();
|
|
||||||
|
|
||||||
if (client)
|
|
||||||
checkStatus(client);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AuthWidget::setState(const QString &buttonText, bool working)
|
void AuthWidget::setState(const QString &buttonText, bool working)
|
||||||
@@ -83,25 +57,13 @@ void AuthWidget::setState(const QString &buttonText, bool working)
|
|||||||
m_button->setEnabled(!working);
|
m_button->setEnabled(!working);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AuthWidget::onClientAdded(Client *client)
|
void AuthWidget::checkStatus()
|
||||||
{
|
{
|
||||||
if (isCopilotClient(client)) {
|
QTC_ASSERT(m_client && m_client->reachable(), return);
|
||||||
auto coPilotClient = asCoPilotClient(client);
|
|
||||||
if (coPilotClient->reachable()) {
|
|
||||||
checkStatus(coPilotClient);
|
|
||||||
} else {
|
|
||||||
connect(client, &Client::initialized, this, [this, coPilotClient] {
|
|
||||||
checkStatus(coPilotClient);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void AuthWidget::checkStatus(CopilotClient *client)
|
|
||||||
{
|
|
||||||
setState("Checking status ...", true);
|
setState("Checking status ...", true);
|
||||||
|
|
||||||
client->requestCheckStatus(false, [this](const CheckStatusRequest::Response &response) {
|
m_client->requestCheckStatus(false, [this](const CheckStatusRequest::Response &response) {
|
||||||
if (response.error()) {
|
if (response.error()) {
|
||||||
setState("failed: " + response.error()->message(), false);
|
setState("failed: " + response.error()->message(), false);
|
||||||
return;
|
return;
|
||||||
@@ -119,15 +81,26 @@ void AuthWidget::checkStatus(CopilotClient *client)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AuthWidget::updateClient(const Utils::FilePath &nodeJs, const Utils::FilePath &agent)
|
||||||
|
{
|
||||||
|
LanguageClientManager::shutdownClient(m_client);
|
||||||
|
m_client = nullptr;
|
||||||
|
setState(Tr::tr("Sign in"), true);
|
||||||
|
if (!nodeJs.exists() || !agent.exists())
|
||||||
|
return;
|
||||||
|
|
||||||
|
m_client = new CopilotClient(nodeJs, agent);
|
||||||
|
connect(m_client, &Client::initialized, this, &AuthWidget::checkStatus);
|
||||||
|
}
|
||||||
|
|
||||||
void AuthWidget::signIn()
|
void AuthWidget::signIn()
|
||||||
{
|
{
|
||||||
qCritical() << "Not implemented";
|
qCritical() << "Not implemented";
|
||||||
auto client = findClient();
|
QTC_ASSERT(m_client && m_client->reachable(), return);
|
||||||
QTC_ASSERT(client, return);
|
|
||||||
|
|
||||||
setState("Signing in ...", true);
|
setState("Signing in ...", true);
|
||||||
|
|
||||||
client->requestSignInInitiate([this, client](const SignInInitiateRequest::Response &response) {
|
m_client->requestSignInInitiate([this](const SignInInitiateRequest::Response &response) {
|
||||||
QTC_ASSERT(!response.error(), return);
|
QTC_ASSERT(!response.error(), return);
|
||||||
|
|
||||||
Utils::setClipboardAndSelection(response.result()->userCode());
|
Utils::setClipboardAndSelection(response.result()->userCode());
|
||||||
@@ -139,7 +112,8 @@ void AuthWidget::signIn()
|
|||||||
.arg(response.result()->userCode()));
|
.arg(response.result()->userCode()));
|
||||||
m_statusLabel->setVisible(true);
|
m_statusLabel->setVisible(true);
|
||||||
|
|
||||||
client->requestSignInConfirm(response.result()->userCode(),
|
m_client
|
||||||
|
->requestSignInConfirm(response.result()->userCode(),
|
||||||
[this](const SignInConfirmRequest::Response &response) {
|
[this](const SignInConfirmRequest::Response &response) {
|
||||||
m_statusLabel->setText("");
|
m_statusLabel->setText("");
|
||||||
|
|
||||||
@@ -160,16 +134,15 @@ void AuthWidget::signIn()
|
|||||||
|
|
||||||
void AuthWidget::signOut()
|
void AuthWidget::signOut()
|
||||||
{
|
{
|
||||||
auto client = findClient();
|
QTC_ASSERT(m_client && m_client->reachable(), return);
|
||||||
QTC_ASSERT(client, return);
|
|
||||||
|
|
||||||
setState("Signing out ...", true);
|
setState("Signing out ...", true);
|
||||||
|
|
||||||
client->requestSignOut([this, client](const SignOutRequest::Response &response) {
|
m_client->requestSignOut([this](const SignOutRequest::Response &response) {
|
||||||
QTC_ASSERT(!response.error(), return);
|
QTC_ASSERT(!response.error(), return);
|
||||||
QTC_ASSERT(response.result()->status() == "NotSignedIn", return);
|
QTC_ASSERT(response.result()->status() == "NotSignedIn", return);
|
||||||
|
|
||||||
checkStatus(client);
|
checkStatus();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,11 +25,12 @@ class AuthWidget : public QWidget
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
explicit AuthWidget(QWidget *parent = nullptr);
|
explicit AuthWidget(QWidget *parent = nullptr);
|
||||||
|
void updateClient(const Utils::FilePath &nodeJs, const Utils::FilePath &agent);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void onClientAdded(LanguageClient::Client *client);
|
|
||||||
void setState(const QString &buttonText, bool working);
|
void setState(const QString &buttonText, bool working);
|
||||||
void checkStatus(Internal::CopilotClient *client);
|
void checkStatus();
|
||||||
|
|
||||||
|
|
||||||
void signIn();
|
void signIn();
|
||||||
void signOut();
|
void signOut();
|
||||||
@@ -39,6 +40,7 @@ private:
|
|||||||
QPushButton *m_button = nullptr;
|
QPushButton *m_button = nullptr;
|
||||||
QLabel *m_statusLabel = nullptr;
|
QLabel *m_statusLabel = nullptr;
|
||||||
Utils::ProgressIndicator *m_progressIndicator = nullptr;
|
Utils::ProgressIndicator *m_progressIndicator = nullptr;
|
||||||
|
Internal::CopilotClient *m_client = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Copilot
|
} // namespace Copilot
|
||||||
|
|||||||
@@ -54,6 +54,24 @@ file from the Copilot neovim plugin.
|
|||||||
st
|
st
|
||||||
}.attachTo(this);
|
}.attachTo(this);
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
|
auto updateAuthWidget = [authWidget]() {
|
||||||
|
authWidget->updateClient(
|
||||||
|
FilePath::fromUserInput(
|
||||||
|
CopilotSettings::instance().nodeJsPath.volatileValue().toString()),
|
||||||
|
FilePath::fromUserInput(
|
||||||
|
CopilotSettings::instance().distPath.volatileValue().toString()));
|
||||||
|
};
|
||||||
|
|
||||||
|
connect(CopilotSettings::instance().nodeJsPath.pathChooser(),
|
||||||
|
&PathChooser::textChanged,
|
||||||
|
authWidget,
|
||||||
|
updateAuthWidget);
|
||||||
|
connect(CopilotSettings::instance().distPath.pathChooser(),
|
||||||
|
&PathChooser::textChanged,
|
||||||
|
authWidget,
|
||||||
|
updateAuthWidget);
|
||||||
|
updateAuthWidget();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user