@@ -7,6 +7,7 @@ inline std::chrono::seconds displayDuration = std::chrono::seconds(7);
77
88inline ImVec2 notificationSize = { 600 , 100 };
99inline float notificationSpacing = 10 .0f ;
10+ inline float notificationTextScale = 1 .5f ;
1011
1112class Notifications
1213{
@@ -15,16 +16,17 @@ class Notifications
1516 {
1617 std::string message;
1718 std::chrono::time_point<std::chrono::steady_clock> timestamp;
19+ int minDisplaySeconds;
1820 };
1921
2022 std::deque<Notification> notifications;
2123 std::mutex notificationsMutex;
2224
2325public:
24- void add (const std::string& message)
26+ void add (const std::string& message, int minDisplaySeconds = 0 )
2527 {
2628 std::lock_guard<std::mutex> lock (notificationsMutex);
27- notifications.push_back ({ message, std::chrono::steady_clock::now () });
29+ notifications.push_back ({ message, std::chrono::steady_clock::now (), minDisplaySeconds });
2830
2931 if (notifications.size () > maxNotifications)
3032 notifications.pop_front ();
@@ -36,8 +38,20 @@ class Notifications
3638 auto now = std::chrono::steady_clock::now ();
3739 {
3840 std::lock_guard<std::mutex> lock (notificationsMutex);
39- while (!notifications.empty () && now - notifications.front ().timestamp > displayDuration)
41+
42+ while (!notifications.empty ())
43+ {
44+ auto & front = notifications.front ();
45+
46+ auto duration = displayDuration;
47+ if (front.minDisplaySeconds > 0 )
48+ duration = std::chrono::seconds (front.minDisplaySeconds );
49+
50+ if (now - front.timestamp <= duration) // notif time hasn't elapsed yet?
51+ break ;
52+
4053 notifications.pop_front ();
54+ }
4155 }
4256
4357 ImVec2 screenSize = ImGui::GetIO ().DisplaySize ;
@@ -50,39 +64,43 @@ class Notifications
5064 borderWidth = 0 ;
5165
5266 float startX = screenSize.x - notificationSize.x - borderWidth - 10 .f ; // 10px padding from the right
53- float startY = (screenSize.y / 4 .0f ) - (notifications.size () * (notificationSize.y + notificationSpacing) / 2 .0f );
67+ float curY = (screenSize.y / 4 .0f ) - (notifications.size () * (notificationSize.y + notificationSpacing) / 2 .0f );
5468
5569 std::lock_guard<std::mutex> lock (notificationsMutex);
5670
5771 for (size_t i = 0 ; i < notifications.size (); ++i)
5872 {
73+ auto windowSize = notificationSize;
5974 const auto & notification = notifications[i];
6075
61- float posY = startY + i * (notificationSize. y + notificationSpacing );
62- ImGui::SetNextWindowPos ( ImVec2 (startX, posY));
63- ImGui::SetNextWindowSize (notificationSize) ;
76+ ImGui::SetNextWindowPos ( ImVec2 (startX, curY) );
77+
78+ curY += windowSize. y + notificationSpacing ;
6479
6580 std::string windowName = " Notification " + std::to_string (i);
6681 ImGui::Begin (windowName.c_str (), nullptr , ImGuiWindowFlags_NoDecoration |
6782 ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoInputs |
6883 ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing);
6984
70- ImGui::SetWindowFontScale (1 .5f );
85+ ImGui::SetWindowFontScale (notificationTextScale);
86+ ImVec2 textSize = ImGui::CalcTextSize (notification.message .c_str (), nullptr , true , windowSize.x - 20 .0f ); // Account for padding
87+
88+ // Adjust window height if text is larger than current window height
89+ if (textSize.y + 40 .0f > windowSize.y )
90+ windowSize.y = textSize.y + 40 .0f ;
7191
72- // Calculate offsets to center text
73- ImVec2 textSize = ImGui::CalcTextSize (notification.message .c_str ());
74- float offsetX = (notificationSize.x - textSize.x ) / 2 .0f ;
75- float offsetY = (notificationSize.y - textSize.y ) / 2 .0f ;
92+ ImGui::SetWindowSize (windowSize);
7693
77- // Padding
78- float paddingX = 10 .0f ;
79- float paddingY = 5 .0f ;
94+ // Center text with padding
95+ float paddingX = 10 .0f , paddingY = 5 .0f ;
96+ float offsetX = (windowSize.x - textSize.x ) / 2 .0f ;
97+ float offsetY = (windowSize.y - textSize.y ) / 2 .0f ;
8098 offsetX = max (offsetX, paddingX);
8199 offsetY = max (offsetY, paddingY);
82100
83- // Position cursor for centered text
84101 ImGui::SetCursorPos (ImVec2 (offsetX, offsetY));
85102 ImGui::TextWrapped (" %s" , notification.message .c_str ());
103+
86104 ImGui::End ();
87105 }
88106 }
0 commit comments