Skip to content

Commit

Permalink
Improve Glass and OutlineViewer title bar message
Browse files Browse the repository at this point in the history
Detect changes to mode and update based on mode change and connection events.
  • Loading branch information
rzblue committed Aug 4, 2023
1 parent 35a8b12 commit 88cc228
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 28 deletions.
45 changes: 35 additions & 10 deletions glass/src/app/native/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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>(
Expand Down
53 changes: 35 additions & 18 deletions outlineviewer/src/main/native/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,47 @@ static std::unique_ptr<glass::NetworkTablesSettings> 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 = "";
Expand All @@ -79,6 +92,10 @@ static void NtInitialize() {
msg->filename, msg->line));
}
}

if (updateTitle) {
glfwSetWindowTitle(win, MakeTitle(inst, connectionEvent).c_str());
}
});

// NetworkTables table window
Expand Down

0 comments on commit 88cc228

Please sign in to comment.