From 88cc22871ca1007326cddbce92783efd182fe363 Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Fri, 4 Aug 2023 01:23:48 -0400 Subject: [PATCH] Improve Glass and OutlineViewer title bar message Detect changes to mode and update based on mode change and connection events. --- glass/src/app/native/cpp/main.cpp | 45 ++++++++++++++---- outlineviewer/src/main/native/cpp/main.cpp | 53 ++++++++++++++-------- 2 files changed, 70 insertions(+), 28 deletions(-) diff --git a/glass/src/app/native/cpp/main.cpp b/glass/src/app/native/cpp/main.cpp index 609e0555d86..66e48417025 100644 --- a/glass/src/app/native/cpp/main.cpp +++ b/glass/src/app/native/cpp/main.cpp @@ -53,6 +53,7 @@ static bool gKeyEdit = false; static int* gEnterKey; static void (*gPrevKeyCallback)(GLFWwindow*, int, int, int, int); static bool gNetworkTablesDebugLog = false; +static unsigned int gPrevMode = NT_NET_MODE_NONE; static void RemapEnterKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { @@ -70,26 +71,46 @@ static void RemapEnterKeyCallback(GLFWwindow* window, int key, int scancode, } } +/** + * Generates the proper title bar title based on current instance state and + * event. + */ +static std::string MakeTitle(NT_Inst inst, nt::Event event) { + auto mode = nt::GetNetworkMode(inst); + if (mode & NT_NET_MODE_SERVER) { + auto numClients = nt::GetConnections(inst).size(); + return fmt::format("Glass - {} Client{} Connected", numClients, + (numClients == 1 ? "" : "s")); + } else if (mode & NT_NET_MODE_CLIENT3 || mode & NT_NET_MODE_CLIENT4) { + if (event.Is(NT_EVENT_CONNECTED)) { + return fmt::format("Glass - Connected ({})", + event.GetConnectionInfo()->remote_ip); + } + } + return "Glass - DISCONNECTED"; +} + static void NtInitialize() { auto inst = nt::GetDefaultInstance(); auto poller = nt::CreateListenerPoller(inst); nt::AddPolledListener(poller, inst, NT_EVENT_CONNECTION | NT_EVENT_IMMEDIATE); nt::AddPolledLogger(poller, 0, 100); - gui::AddEarlyExecute([poller] { + gui::AddEarlyExecute([inst, poller] { auto win = gui::GetSystemWindow(); if (!win) { return; } + bool updateTitle = false; + nt::Event connectionEvent; + if (nt::GetNetworkMode(inst) != gPrevMode) { + gPrevMode = nt::GetNetworkMode(inst); + updateTitle = true; + } + for (auto&& event : nt::ReadListenerQueue(poller)) { - if (auto connInfo = event.GetConnectionInfo()) { - // update window title when connection status changes - if ((event.flags & NT_EVENT_CONNECTED) != 0) { - glfwSetWindowTitle( - win, fmt::format("Glass - Connected ({})", connInfo->remote_ip) - .c_str()); - } else { - glfwSetWindowTitle(win, "Glass - DISCONNECTED"); - } + if (event.Is(NT_EVENT_CONNECTION)) { + updateTitle = true; + connectionEvent = event; } else if (auto msg = event.GetLogMessage()) { const char* level = ""; if (msg->level >= NT_LOG_CRITICAL) { @@ -105,6 +126,10 @@ static void NtInitialize() { "{}{} ({}:{})\n", level, msg->message, msg->filename, msg->line)); } } + + if (updateTitle) { + glfwSetWindowTitle(win, MakeTitle(inst, connectionEvent).c_str()); + } }); gNetworkTablesLogWindow = std::make_unique( diff --git a/outlineviewer/src/main/native/cpp/main.cpp b/outlineviewer/src/main/native/cpp/main.cpp index 97ac9fd034a..cf78c9b0aff 100644 --- a/outlineviewer/src/main/native/cpp/main.cpp +++ b/outlineviewer/src/main/native/cpp/main.cpp @@ -37,34 +37,47 @@ static std::unique_ptr gSettings; static glass::LogData gLog; static glass::NetworkTablesFlagsSettings gFlagsSettings; static glass::MainMenuBar gMainMenu; +static unsigned int gPrevMode = NT_NET_MODE_NONE; + +/** + * Generates the proper title bar title based on current instance state and + * event. + */ +static std::string MakeTitle(NT_Inst inst, nt::Event event) { + auto mode = nt::GetNetworkMode(inst); + if (mode & NT_NET_MODE_SERVER) { + auto numClients = nt::GetConnections(inst).size(); + return fmt::format("OutlineViewer - {} Client{} Connected", numClients, + (numClients == 1 ? "" : "s")); + } else if (mode & NT_NET_MODE_CLIENT3 || mode & NT_NET_MODE_CLIENT4) { + if (event.Is(NT_EVENT_CONNECTED)) { + return fmt::format("OutlineViewer - Connected ({})", + event.GetConnectionInfo()->remote_ip); + } + } + return "OutlineViewer - DISCONNECTED"; +} static void NtInitialize() { auto inst = nt::GetDefaultInstance(); auto poller = nt::CreateListenerPoller(inst); - nt::AddPolledListener( - poller, inst, - NT_EVENT_CONNECTION | NT_EVENT_IMMEDIATE | NT_EVENT_LOGMESSAGE); + nt::AddPolledListener(poller, inst, NT_EVENT_CONNECTION | NT_EVENT_IMMEDIATE); + nt::AddPolledLogger(poller, 0, 100); gui::AddEarlyExecute([inst, poller] { auto win = gui::GetSystemWindow(); if (!win) { return; } + bool updateTitle = false; + nt::Event connectionEvent; + if (nt::GetNetworkMode(inst) != gPrevMode) { + gPrevMode = nt::GetNetworkMode(inst); + updateTitle = true; + } for (auto&& event : nt::ReadListenerQueue(poller)) { - if (auto connInfo = event.GetConnectionInfo()) { - // update window title when connection status changes - if ((nt::GetNetworkMode(inst) & NT_NET_MODE_SERVER) != 0) { - // for server mode, just print number of clients connected - glfwSetWindowTitle(win, - fmt::format("OutlineViewer - {} Clients Connected", - nt::GetConnections(inst).size()) - .c_str()); - } else if ((event.flags & NT_EVENT_CONNECTED) != 0) { - glfwSetWindowTitle(win, fmt::format("OutlineViewer - Connected ({})", - connInfo->remote_ip) - .c_str()); - } else { - glfwSetWindowTitle(win, "OutlineViewer - DISCONNECTED"); - } + if (event.Is(NT_EVENT_CONNECTION)) { + updateTitle = true; + connectionEvent = event; } else if (auto msg = event.GetLogMessage()) { // handle NetworkTables log messages const char* level = ""; @@ -79,6 +92,10 @@ static void NtInitialize() { msg->filename, msg->line)); } } + + if (updateTitle) { + glfwSetWindowTitle(win, MakeTitle(inst, connectionEvent).c_str()); + } }); // NetworkTables table window