mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[glass,ov] Improve Glass and OutlineViewer title bar message (#5502)
Detect changes to mode and update based on mode change and connection events.
This commit is contained in:
@@ -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<glass::Window>(
|
||||
|
||||
Reference in New Issue
Block a user