mirror of
https://github.com/SpartanJ/eepp.git
synced 2026-08-10 16:51:19 +03:00
Migrate to onFocus listener; Project detection; Minor header refactor
This commit is contained in:
@@ -33,16 +33,16 @@ DiscordRPCplugin::DiscordRPCplugin( PluginManager* pluginManager, bool sync ) :
|
||||
DiscordRPCplugin::~DiscordRPCplugin() {
|
||||
waitUntilLoaded();
|
||||
mShuttingDown = true;
|
||||
{
|
||||
Lock l( mClientsMutex );
|
||||
for ( const auto& client : mClients )
|
||||
client.first->unregisterClient( client.second.get() );
|
||||
}
|
||||
|
||||
}
|
||||
void DiscordRPCplugin::load( PluginManager* pluginManager ) {
|
||||
Clock clock;
|
||||
AtomicBoolScopedOp loading( mLoading, true );
|
||||
|
||||
pluginManager->subscribeMessages( this,
|
||||
[this]( const auto& notification ) -> PluginRequestHandle {
|
||||
return processMessage( notification );
|
||||
} );
|
||||
|
||||
std::vector<std::string> paths;
|
||||
std::string path( pluginManager->getResourcesPath() + "plugins/discordRPC.json" );
|
||||
@@ -97,41 +97,52 @@ void DiscordRPCplugin::load( PluginManager* pluginManager ) {
|
||||
setReady( clock.getElapsedTime() );
|
||||
}
|
||||
|
||||
// Functiality impl starts here
|
||||
void DiscordRPCplugin::onRegisterDocument( TextDocument* doc ) {
|
||||
Lock l( mClientsMutex );
|
||||
mClients[doc] = std::make_unique<DiscordRPCpluginClient>( this, doc );
|
||||
doc->registerClient( mClients[doc].get() );
|
||||
}
|
||||
void DiscordRPCplugin::onUnregisterDocument( TextDocument* doc ) {
|
||||
Lock l( mClientsMutex );
|
||||
doc->unregisterClient( mClients[doc].get() );
|
||||
mClients.erase( doc );
|
||||
PluginRequestHandle DiscordRPCplugin::processMessage( const PluginMessage& msg ) {
|
||||
switch ( msg.type ) {
|
||||
case PluginMessageType::WorkspaceFolderChanged: {
|
||||
std::string rpath = FileSystem::getRealPath( msg.asJSON()["folder"] );
|
||||
FileSystem::dirAddSlashAtEnd( rpath );
|
||||
mProjectName = FileSystem::fileNameFromPath( rpath );
|
||||
Log::debug("Loaded new workspace: %s ; %s", rpath, mProjectName);
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return PluginRequestHandle::empty();
|
||||
}
|
||||
|
||||
void DiscordRPCplugin::DiscordRPCpluginClient::onDocumentCursorChange( const TextPosition& t) {
|
||||
if (mDoc->isUntitledEmpty()) { return ;}
|
||||
std::string filename = mDoc->getFilename();
|
||||
|
||||
if (filename != mParent->mLastFile) {
|
||||
void DiscordRPCplugin::onRegisterListeners( UICodeEditor* editor, std::vector<Uint32>& listeners ) {
|
||||
listeners.push_back( editor->on( Event::OnFocus, [this, editor]( const Event* ) {
|
||||
// `this` in the scope of the lambda is the parent `DiscordRPCplugin`
|
||||
|
||||
Lock l( mParent->mLastFileMutex );
|
||||
mParent->mLastFile = filename;
|
||||
auto& doc = editor->getDocument();
|
||||
if (!doc.hasFilepath()) { return; }
|
||||
|
||||
Log::debug("Activity in new file. lang = %s", mDoc->getSyntaxDefinition().getLanguageName());
|
||||
auto filename = doc.getFilename();
|
||||
|
||||
Lock ipc( mParent->mIPCmutex );
|
||||
DiscordIPCActivity* a = mParent->mIPC.getActivity();
|
||||
if ( filename != mLastFile ) {
|
||||
this->mLastFile = filename;
|
||||
|
||||
Log::debug( "Activity in new file. lang = %s",
|
||||
doc.getSyntaxDefinition().getLanguageName() );
|
||||
|
||||
DiscordIPCActivity* a = this->mIPC.getActivity();
|
||||
|
||||
if (!mProjectName.empty())
|
||||
a->details = String::format("Working on %s", mProjectName);
|
||||
a->state = String::format("Editing %s, a %s file", filename, doc.getSyntaxDefinition().getLanguageName());
|
||||
a->start = time( nullptr ); // Time spent in this specific file
|
||||
|
||||
this->mIPC.setActivity( *a );
|
||||
}
|
||||
|
||||
a->state = "Editing " + filename + ", a " + mDoc->getSyntaxDefinition().getLanguageName() + " file";
|
||||
a->start = time( nullptr ); // Time spent in this specific file
|
||||
|
||||
mParent->mIPC.setActivity(*a);
|
||||
}
|
||||
|
||||
} ) );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace ecode
|
||||
@@ -46,44 +46,15 @@ class DiscordRPCplugin : public PluginBase {
|
||||
std::string getDescription() override { return Definition().description; }
|
||||
|
||||
protected:
|
||||
class DiscordRPCpluginClient : public TextDocument::Client {
|
||||
public:
|
||||
explicit DiscordRPCpluginClient( DiscordRPCplugin* parent, TextDocument* doc) :
|
||||
mDoc( doc ), mParent( parent ) {}
|
||||
|
||||
// Obligatory definitions, empty behavior
|
||||
virtual void onDocumentTextChanged( const DocumentContentChange& ) override {};
|
||||
virtual void onDocumentUndoRedo( const TextDocument::UndoRedo& ) override {};
|
||||
virtual void onDocumentCursorChange( const TextPosition& ) override;
|
||||
virtual void onDocumentInterestingCursorChange( const TextPosition& ) override {};
|
||||
virtual void onDocumentSelectionChange( const TextRange& ) override {};
|
||||
virtual void onDocumentLineCountChange( const size_t&, const size_t& ) override {};
|
||||
virtual void onDocumentLineChanged( const Int64& ) override {};
|
||||
virtual void onDocumentSaved( TextDocument* ) override {};
|
||||
virtual void onDocumentClosed( TextDocument* doc ) override {};
|
||||
virtual void onDocumentDirtyOnFileSystem( TextDocument* ) override {};
|
||||
virtual void onDocumentMoved( TextDocument* ) override {};
|
||||
virtual void onDocumentReset( TextDocument* ) override {};
|
||||
|
||||
|
||||
protected:
|
||||
TextDocument* mDoc{ nullptr };
|
||||
DiscordRPCplugin* mParent{ nullptr };
|
||||
};
|
||||
DiscordIPC mIPC;
|
||||
Mutex mIPCmutex;
|
||||
|
||||
using ClientsMap = std::unordered_map<TextDocument*, std::unique_ptr<DiscordRPCpluginClient>>;
|
||||
ClientsMap mClients;
|
||||
Mutex mClientsMutex;
|
||||
std::string mLastFile;
|
||||
Mutex mLastFileMutex;
|
||||
|
||||
std::string mProjectName;
|
||||
|
||||
void load ( PluginManager* pluginManager );
|
||||
|
||||
virtual void onRegisterDocument( TextDocument* doc ) override;
|
||||
virtual void onUnregisterDocument( TextDocument* doc ) override;
|
||||
PluginRequestHandle processMessage( const PluginMessage& msg );
|
||||
|
||||
virtual void onRegisterListeners(UICodeEditor* editor, std::vector<Uint32>& listeners ) override;
|
||||
|
||||
DiscordRPCplugin( PluginManager* pluginManager, bool sync );
|
||||
};
|
||||
|
||||
@@ -14,22 +14,22 @@ enum DiscordIPCActivityTypes {
|
||||
};
|
||||
|
||||
struct DiscordIPCActivityButton {
|
||||
std::string label = "";
|
||||
std::string url = "";
|
||||
std::string label;
|
||||
std::string url;
|
||||
};
|
||||
|
||||
struct DiscordIPCActivity {
|
||||
DiscordIPCActivityTypes type = DiscordIPCActivityTypes::Playing;
|
||||
std::string state = "";
|
||||
std::string details = "";
|
||||
std::string state;
|
||||
std::string details;
|
||||
|
||||
time_t start = 0;
|
||||
time_t end = 0;
|
||||
|
||||
std::string largeImage = "";
|
||||
std::string largeText = "";
|
||||
std::string smallImage = "";
|
||||
std::string smallText = "";
|
||||
std::string largeImage;
|
||||
std::string largeText;
|
||||
std::string smallImage;
|
||||
std::string smallText;
|
||||
|
||||
DiscordIPCActivityButton buttons[2];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user