ElementLog.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "ElementLog.h"
  29. #include "../../Include/RmlUi/Core/Context.h"
  30. #include "../../Include/RmlUi/Core/ElementText.h"
  31. #include "../../Include/RmlUi/Core/Factory.h"
  32. #include "BeaconSource.h"
  33. #include "CommonSource.h"
  34. #include "LogSource.h"
  35. #include <limits.h>
  36. namespace Rml {
  37. namespace Debugger {
  38. constexpr int MAX_LOG_MESSAGES = 50;
  39. ElementLog::ElementLog(const String& tag) : ElementDebugDocument(tag)
  40. {
  41. // Set up the log type buttons.
  42. log_types[Log::LT_ALWAYS].visible = true;
  43. log_types[Log::LT_ALWAYS].class_name = "error";
  44. log_types[Log::LT_ALWAYS].alert_contents = "A";
  45. log_types[Log::LT_ERROR].visible = true;
  46. log_types[Log::LT_ERROR].class_name = "error";
  47. log_types[Log::LT_ERROR].alert_contents = "!";
  48. log_types[Log::LT_ERROR].button_name = "error_button";
  49. log_types[Log::LT_ASSERT].visible = true;
  50. log_types[Log::LT_ASSERT].class_name = "error";
  51. log_types[Log::LT_ASSERT].alert_contents = "!";
  52. log_types[Log::LT_WARNING].visible = true;
  53. log_types[Log::LT_WARNING].class_name = "warning";
  54. log_types[Log::LT_WARNING].alert_contents = "!";
  55. log_types[Log::LT_WARNING].button_name = "warning_button";
  56. log_types[Log::LT_INFO].visible = false;
  57. log_types[Log::LT_INFO].class_name = "info";
  58. log_types[Log::LT_INFO].alert_contents = "i";
  59. log_types[Log::LT_INFO].button_name = "info_button";
  60. log_types[Log::LT_DEBUG].visible = true;
  61. log_types[Log::LT_DEBUG].class_name = "debug";
  62. log_types[Log::LT_DEBUG].alert_contents = "?";
  63. log_types[Log::LT_DEBUG].button_name = "debug_button";
  64. }
  65. ElementLog::~ElementLog()
  66. {
  67. RemoveEventListener(EventId::Click, this);
  68. if (beacon && beacon->GetFirstChild())
  69. beacon->GetFirstChild()->RemoveEventListener(EventId::Click, this);
  70. if (beacon && beacon->GetParentNode())
  71. beacon->GetParentNode()->RemoveChild(beacon);
  72. if (message_content)
  73. {
  74. message_content->RemoveEventListener(EventId::Resize, this);
  75. }
  76. }
  77. bool ElementLog::Initialise()
  78. {
  79. SetInnerRML(log_rml);
  80. SetId("rmlui-debug-log");
  81. message_content = GetElementById("content");
  82. if (message_content)
  83. {
  84. message_content->AddEventListener(EventId::Resize, this);
  85. }
  86. SharedPtr<StyleSheetContainer> style_sheet = Factory::InstanceStyleSheetString(String(common_rcss) + String(log_rcss));
  87. if (!style_sheet)
  88. return false;
  89. SetStyleSheetContainer(std::move(style_sheet));
  90. AddEventListener(EventId::Click, this);
  91. // Create the log beacon.
  92. beacon = GetContext()->CreateDocument("debug-document");
  93. RMLUI_ASSERT(rmlui_dynamic_cast<ElementDebugDocument*>(beacon));
  94. if (!beacon)
  95. return false;
  96. beacon->SetId("rmlui-debug-log-beacon");
  97. beacon->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  98. beacon->SetInnerRML(beacon_rml);
  99. Element* button = beacon->GetFirstChild();
  100. if (button)
  101. beacon->GetFirstChild()->AddEventListener(EventId::Click, this);
  102. style_sheet = Factory::InstanceStyleSheetString(String(common_rcss) + String(beacon_rcss));
  103. if (!style_sheet)
  104. {
  105. GetContext()->UnloadDocument(beacon);
  106. beacon = nullptr;
  107. return false;
  108. }
  109. beacon->SetStyleSheetContainer(style_sheet);
  110. return true;
  111. }
  112. void ElementLog::AddLogMessage(Log::Type type, const String& message)
  113. {
  114. if (log_messages.size() >= MAX_LOG_MESSAGES)
  115. {
  116. log_messages.pop_front();
  117. }
  118. log_messages.push_back(LogMessage{type, StringUtilities::EncodeRml(message)});
  119. num_new_messages += 1;
  120. // Force a refresh of the RML.
  121. dirty_logs = true;
  122. }
  123. void ElementLog::OnUpdate()
  124. {
  125. ElementDocument::OnUpdate();
  126. if (beacon && num_new_messages > 0)
  127. {
  128. for (int i = Math::Max(0, (int)log_messages.size() - num_new_messages); i < (int)log_messages.size(); i++)
  129. {
  130. const LogMessage& log_message = log_messages[i];
  131. const Log::Type type = log_message.type;
  132. // If this log type is invisible, and there is a button for this log type, then change its text from
  133. // "Off" to "Off*" to signal that there are unread logs.
  134. if (!log_types[type].visible)
  135. {
  136. if (!log_types[type].button_name.empty())
  137. {
  138. if (Element* button = GetElementById(log_types[type].button_name))
  139. {
  140. rmlui_static_cast<ElementText*>(button->GetFirstChild())->SetText("Off*");
  141. }
  142. }
  143. }
  144. // Trigger the beacon if we're hidden. Override any lower-level log type if it is already visible.
  145. else if (!IsVisible() && type < current_beacon_level)
  146. {
  147. beacon->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Visible));
  148. current_beacon_level = type;
  149. Element* beacon_button = beacon->GetFirstChild();
  150. if (beacon_button)
  151. {
  152. beacon_button->SetClassNames(log_types[type].class_name);
  153. beacon_button->SetInnerRML(log_types[type].alert_contents);
  154. }
  155. }
  156. }
  157. num_new_messages = 0;
  158. }
  159. if (dirty_logs && IsVisible())
  160. {
  161. String messages;
  162. if (message_content)
  163. {
  164. for (const LogMessage& log_message : log_messages)
  165. {
  166. const int type = log_message.type;
  167. if (!log_types[type].visible)
  168. continue;
  169. messages += CreateString(R"(<div class="log-entry"><div class="icon %s">%s</div><p class="message">)",
  170. log_types[type].class_name.c_str(), log_types[type].alert_contents.c_str());
  171. messages += log_message.message;
  172. messages += "</p></div>";
  173. }
  174. if (message_content->HasChildNodes())
  175. {
  176. float last_element_top = message_content->GetLastChild()->GetAbsoluteTop();
  177. auto_scroll = message_content->GetAbsoluteTop() + message_content->GetAbsoluteTop() > last_element_top;
  178. }
  179. else
  180. {
  181. auto_scroll = true;
  182. }
  183. message_content->SetInnerRML(messages);
  184. dirty_logs = false;
  185. }
  186. }
  187. }
  188. void ElementLog::ProcessEvent(Event& event)
  189. {
  190. if (beacon)
  191. {
  192. if (event == EventId::Click)
  193. {
  194. if (event.GetTargetElement() == beacon->GetFirstChild())
  195. {
  196. if (!IsVisible())
  197. SetProperty(PropertyId::Visibility, Property(Style::Visibility::Visible));
  198. beacon->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  199. current_beacon_level = Log::LT_MAX;
  200. }
  201. else if (event.GetTargetElement()->GetId() == "close_button")
  202. {
  203. SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  204. }
  205. else if (event.GetTargetElement()->GetId() == "clear_button")
  206. {
  207. for (int i = 0; i < Log::LT_MAX; i++)
  208. {
  209. if (!log_types[i].visible && !log_types[i].button_name.empty())
  210. {
  211. if (Element* button = GetElementById(log_types[i].button_name))
  212. {
  213. rmlui_static_cast<ElementText*>(button->GetFirstChild())->SetText("Off");
  214. }
  215. }
  216. }
  217. log_messages.clear();
  218. dirty_logs = true;
  219. }
  220. else
  221. {
  222. for (int i = 0; i < Log::LT_MAX; i++)
  223. {
  224. if (!log_types[i].button_name.empty() && event.GetTargetElement()->GetId() == log_types[i].button_name)
  225. {
  226. log_types[i].visible = !log_types[i].visible;
  227. rmlui_static_cast<ElementText*>(event.GetTargetElement()->GetFirstChild())->SetText(log_types[i].visible ? "On" : "Off");
  228. dirty_logs = true;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. if (event == EventId::Resize && auto_scroll)
  235. {
  236. if (message_content != nullptr && message_content->HasChildNodes())
  237. message_content->GetLastChild()->ScrollIntoView();
  238. }
  239. }
  240. } // namespace Debugger
  241. } // namespace Rml