Skip to content

Commit 6108bb2

Browse files
committed
Notifications: center each line individually
1 parent 1713ce4 commit 6108bb2

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

src/overlay/notifications.hpp

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,52 @@ class Notifications
9191
ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing);
9292

9393
ImGui::SetWindowFontScale(notificationTextScale);
94-
ImVec2 textSize = ImGui::CalcTextSize(notification.message.c_str(), nullptr, true, windowSize.x - 20.0f); // Account for padding
9594

96-
// Adjust window height if text is larger than current window height
97-
if (textSize.y + 40.0f > windowSize.y)
98-
windowSize.y = textSize.y + 40.0f;
95+
// Split the message into lines
96+
std::istringstream messageStream(notification.message);
97+
std::vector<std::string> lines;
98+
std::string line;
99+
while (std::getline(messageStream, line)) {
100+
lines.push_back(line);
101+
}
102+
103+
// Calculate total height for all lines
104+
float totalTextHeight = 0.0f;
105+
for (const auto& singleLine : lines) {
106+
ImVec2 lineSize = ImGui::CalcTextSize(singleLine.c_str(), nullptr, true, windowSize.x - 20.0f);
107+
totalTextHeight += lineSize.y;
108+
}
109+
totalTextHeight += (lines.size() - 1) * ImGui::GetStyle().ItemSpacing.y;
110+
111+
// Adjust window height if necessary
112+
if (totalTextHeight + 40.0f > windowSize.y)
113+
windowSize.y = totalTextHeight + 40.0f;
99114

100115
ImGui::SetWindowSize(windowSize);
101116

102117
curY += windowSize.y + notificationSpacing;
103118

104-
// Center text with padding
105-
float paddingX = 10.0f, paddingY = 5.0f;
106-
float offsetX = (windowSize.x - textSize.x) / 2.0f;
107-
float offsetY = (windowSize.y - textSize.y) / 2.0f;
108-
offsetX = max(offsetX, paddingX);
109-
offsetY = max(offsetY, paddingY);
119+
// Center text block vertically
120+
float paddingY = 5.0f;
121+
float currentYOffset = (windowSize.y - totalTextHeight) / 2.0f;
122+
currentYOffset = max(currentYOffset, paddingY);
123+
124+
for (const auto& singleLine : lines) {
125+
// Calculate individual line size
126+
ImVec2 lineSize = ImGui::CalcTextSize(singleLine.c_str(), nullptr, true, windowSize.x - 20.0f);
110127

111-
ImGui::SetCursorPos(ImVec2(offsetX, offsetY));
112-
ImGui::TextWrapped("%s", notification.message.c_str());
128+
// Center line horizontally
129+
float paddingX = 10.0f;
130+
float offsetX = (windowSize.x - lineSize.x) / 2.0f;
131+
offsetX = max(offsetX, paddingX);
132+
133+
ImGui::SetCursorPos(ImVec2(offsetX, currentYOffset));
134+
135+
ImGui::TextWrapped("%s", singleLine.c_str());
136+
137+
// Move down for the next line, including spacing
138+
currentYOffset += lineSize.y + ImGui::GetStyle().ItemSpacing.y;
139+
}
113140

114141
ImGui::End();
115142
}

0 commit comments

Comments
 (0)