From c3e40967ad41f2d31955d37606c2ff408131f6ac Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 21 May 2018 01:22:57 +0200 Subject: Focus when displaying new message --- src/main.cpp | 66 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 91f99af..a6dfb5f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -143,11 +143,12 @@ int main(int argc, char **argv) recursive_mutex channelMessageMutex; bool waitingToJoin = false; bool loggedIn = false; + sf::Clock lastFocusedTimer; odhtdb::Database *database = nullptr; odhtdb::DatabaseCallbackFuncs callbackFuncs; - callbackFuncs.createNodeCallbackFunc = [&waitingToJoinChannels, &database, &channels, &channelMessageMutex, &waitingToJoin, &localNodeUsers](const odhtdb::DatabaseCreateNodeRequest &request) + callbackFuncs.createNodeCallbackFunc = [&waitingToJoinChannels, &database, &channels, &channelMessageMutex, &waitingToJoin, &localNodeUsers, &lastFocusedTimer](const odhtdb::DatabaseCreateNodeRequest &request) { lock_guard lock(channelMessageMutex); //printf("Create node callback func %s\n", request.nodeHash->toString().c_str()); @@ -186,7 +187,7 @@ int main(int argc, char **argv) } }; - callbackFuncs.addNodeCallbackFunc = [&channels, &channelMessageMutex](const odhtdb::DatabaseAddNodeRequest &request) + callbackFuncs.addNodeCallbackFunc = [&channels, &channelMessageMutex, &lastFocusedTimer](const odhtdb::DatabaseAddNodeRequest &request) { lock_guard lock(channelMessageMutex); //printf("Add node callback func %s\n", request.requestHash->toString().c_str()); @@ -195,12 +196,13 @@ int main(int argc, char **argv) if(*request.nodeHash == *channel->getNodeInfo().getRequestHash()) { channelAddStoredMessage(channel, *request.requestHash, *request.creatorPublicKey, StringView((const char*)request.decryptedData.data, request.decryptedData.size), request.timestamp); + lastFocusedTimer.restart(); return; } } }; - callbackFuncs.addUserCallbackFunc = [&channels, &channelMessageMutex, &waitingToJoin, &localNodeUsers](const odhtdb::DatabaseAddUserRequest &request) + callbackFuncs.addUserCallbackFunc = [&channels, &channelMessageMutex, &waitingToJoin, &localNodeUsers, &lastFocusedTimer](const odhtdb::DatabaseAddUserRequest &request) { lock_guard lock(channelMessageMutex); printf("Add user callback. Channel to add user to: %s\n", request.nodeHash->toString().c_str()); @@ -219,6 +221,8 @@ int main(int argc, char **argv) return; } + lastFocusedTimer.restart(); + if(*request.userToAddPublicKey == nodeUserData->second.userKeyPair->getPublicKey()) { printf("You were added to channel %s by %s\n", request.nodeHash->toString().c_str(), request.creatorPublicKey->toString().c_str()); @@ -235,6 +239,12 @@ int main(int argc, char **argv) }; database = new odhtdb::Database("bootstrap.ring.cx", 4222, Cache::getDchatDir(), callbackFuncs); + auto addSystemMessage = [&lastFocusedTimer](const std::string &msg) + { + Channel::getCurrent()->addLocalMessage(msg, Channel::getCurrent()->getSystemUser()); + lastFocusedTimer.restart(); + }; + // Login to account Command::add("login", [&localNodeUsers, &database, &channels, &loggedIn, &channelMessageMutex, ¤tUsername, ¤tPassword](const vector &args) { @@ -311,7 +321,7 @@ int main(int argc, char **argv) // TODO: Use database->addData to change channel name // Create channel - Command::add("cc", [&database, &channels, &channelMessageMutex, &loggedIn, &localNodeUsers, ¤tUsername, ¤tPassword](const vector &args) + Command::add("cc", [&database, &channels, &channelMessageMutex, &loggedIn, &localNodeUsers, ¤tUsername, ¤tPassword, &lastFocusedTimer, addSystemMessage](const vector &args) { lock_guard lock(channelMessageMutex); if(args.size() != 1) @@ -335,14 +345,15 @@ int main(int argc, char **argv) ChannelSidePanel::addChannel(channel); channels.push_back(channel); Channel::setCurrent(channel); + lastFocusedTimer.restart(); localNodeUsers[*createResponse->getRequestHash()] = { createResponse->getNodeEncryptionKey(), createResponse->getNodeAdminKeyPair() }; database->storeNodeInfoForUserEncrypted(databaseNode, currentUsername, currentPassword, *createResponse->getNodeAdminKeyPair()); - Channel::getCurrent()->addLocalMessage("Channel created and stored in database", Channel::getCurrent()->getSystemUser()); + addSystemMessage("Channel created and stored in database"); }); // Create invite key - Command::add("invite", [&channelMessageMutex, &offlineChannel, &database](const vector &args) + Command::add("invite", [&channelMessageMutex, &offlineChannel, &database, addSystemMessage](const vector &args) { lock_guard lock(channelMessageMutex); if(args.size() != 0) @@ -354,7 +365,7 @@ int main(int argc, char **argv) Channel *currentChannel = Channel::getCurrent(); if(currentChannel == &offlineChannel) { - Channel::getCurrent()->addLocalMessage("You need to be in a channel to create an invite key", Channel::getCurrent()->getSystemUser()); + addSystemMessage("You need to be in a channel to create an invite key"); return; } @@ -372,7 +383,7 @@ int main(int argc, char **argv) string msg = "You are now listening for users to join the channel using the key: "; msg += inviteKey; - Channel::getCurrent()->addLocalMessage(msg, Channel::getCurrent()->getSystemUser()); + addSystemMessage(msg); printf("%s\n", msg.c_str()); sibs::SafeSerializer keySerializer; @@ -542,14 +553,14 @@ int main(int argc, char **argv) printf("UI scaling set to %f\n", scaling); }); - Command::add("addbind", [](const vector &args) + Command::add("addbind", [addSystemMessage](const vector &args) { if(args.size() != 2) { string errMsg = "Expected 2 arguments for command addbind, got "; errMsg += to_string(args.size()); errMsg += " argument(s)"; - Channel::getCurrent()->addLocalMessage(errMsg, Channel::getCurrent()->getSystemUser()); + addSystemMessage(errMsg); return; } @@ -560,25 +571,25 @@ int main(int argc, char **argv) if(key.size() > 255) { // 253 = bind + two colons - Channel::getCurrent()->addLocalMessage("Bind is too long. Max size is 253 bytes", Channel::getCurrent()->getSystemUser()); + addSystemMessage("Bind is too long. Max size is 253 bytes"); return; } bool bindAdded = Chatbar::addBind(key, args[1]); if(bindAdded) - Channel::getCurrent()->addLocalMessage("Bind added", Channel::getCurrent()->getSystemUser()); + addSystemMessage("Bind added"); else - Channel::getCurrent()->addLocalMessage("Bind already exists. Remove it first if you want to replace it", Channel::getCurrent()->getSystemUser()); + addSystemMessage("Bind already exists. Remove it first if you want to replace it"); }); - Command::add("removebind", [](const vector &args) + Command::add("removebind", [addSystemMessage](const vector &args) { if(args.size() != 1) { string errMsg = "Expected 1 argument for command removebind, got "; errMsg += to_string(args.size()); errMsg += " argument(s)"; - Channel::getCurrent()->addLocalMessage(errMsg, Channel::getCurrent()->getSystemUser()); + addSystemMessage(errMsg); return; } @@ -589,25 +600,25 @@ int main(int argc, char **argv) if(key.size() > 255) { // 253 = bind + two colons - Channel::getCurrent()->addLocalMessage("Bind is too long. Max size is 253 bytes", Channel::getCurrent()->getSystemUser()); + addSystemMessage("Bind is too long. Max size is 253 bytes"); return; } bool bindRemoved = Chatbar::removeBind(key); if(bindRemoved) - Channel::getCurrent()->addLocalMessage("Bind removed", Channel::getCurrent()->getSystemUser()); + addSystemMessage("Bind removed"); else - Channel::getCurrent()->addLocalMessage("Bind doesn't exist, nothing was removed", Channel::getCurrent()->getSystemUser()); + addSystemMessage("Bind doesn't exist, nothing was removed"); }); - Command::add("binds", [](const vector &args) + Command::add("binds", [addSystemMessage](const vector &args) { if(args.size() != 0) { string errMsg = "Expected 0 arguments for command removebind, got "; errMsg += to_string(args.size()); errMsg += " argument(s)"; - Channel::getCurrent()->addLocalMessage(errMsg, Channel::getCurrent()->getSystemUser()); + addSystemMessage(errMsg); return; } @@ -620,43 +631,43 @@ int main(int argc, char **argv) msg += " "; msg += bind.second; } - Channel::getCurrent()->addLocalMessage(msg, Channel::getCurrent()->getSystemUser()); + addSystemMessage(msg); }); // Change nick of current user in current channel - Command::add("nick", [&loggedIn, &offlineChannel](const vector &args) + Command::add("nick", [&loggedIn, &offlineChannel, addSystemMessage](const vector &args) { if(args.size() != 1) { string errMsg = "Expected 1 argument for command nick, got "; errMsg += to_string(args.size()); errMsg += " argument(s)"; - Channel::getCurrent()->addLocalMessage(errMsg, Channel::getCurrent()->getSystemUser()); + addSystemMessage(errMsg); return; } if(!loggedIn) { - Channel::getCurrent()->addLocalMessage("You need to be logged in to change your nickname", Channel::getCurrent()->getSystemUser()); + addSystemMessage("You need to be logged in to change your nickname"); return; } if(Channel::getCurrent() == &offlineChannel) { - Channel::getCurrent()->addLocalMessage("You need to be in a channel to change your nickname", Channel::getCurrent()->getSystemUser()); + addSystemMessage("You need to be in a channel to change your nickname"); return; } if(args[0].size() == 0 || args[0].size() > 255) { - Channel::getCurrent()->addLocalMessage("Invalid nickname. Nickname has to be between 1 and 255 characters", Channel::getCurrent()->getSystemUser()); + addSystemMessage("Invalid nickname. Nickname has to be between 1 and 255 characters"); return; } Channel::getCurrent()->changeNick(args[0]); string msg = "Your nickname was changed to "; msg += args[0]; - Channel::getCurrent()->addLocalMessage(msg, Channel::getCurrent()->getSystemUser()); + addSystemMessage(msg); }); Command::add("clearcache", [&database](const vector &args) @@ -664,7 +675,6 @@ int main(int argc, char **argv) printf("Cleared cache (%d bytes)\n", database->clearCache()); }); - sf::Clock lastFocusedTimer; sf::Clock frameTimer; while (window.isOpen()) -- cgit v1.2.3