History.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWebView/History.h>
  7. namespace WebView {
  8. void History::dump() const
  9. {
  10. dbgln("Dump {} items(s)", m_items.size());
  11. int i = 0;
  12. for (auto& item : m_items) {
  13. dbgln("[{}] {} '{}' {}", i, item.url, item.title, m_current == i ? '*' : ' ');
  14. ++i;
  15. }
  16. }
  17. Vector<History::URLTitlePair> History::get_all_history_entries()
  18. {
  19. return m_items;
  20. }
  21. void History::push(const URL& url, DeprecatedString const& title)
  22. {
  23. if (!m_items.is_empty() && m_items[m_current].url == url)
  24. return;
  25. m_items.shrink(m_current + 1);
  26. m_items.append(URLTitlePair {
  27. .url = url,
  28. .title = title,
  29. });
  30. m_current++;
  31. }
  32. void History::replace_current(const URL& url, DeprecatedString const& title)
  33. {
  34. if (m_current == -1)
  35. return;
  36. m_items.remove(m_current);
  37. m_current--;
  38. push(url, title);
  39. }
  40. History::URLTitlePair History::current() const
  41. {
  42. if (m_current == -1)
  43. return {};
  44. return m_items[m_current];
  45. }
  46. void History::go_back(int steps)
  47. {
  48. VERIFY(can_go_back(steps));
  49. m_current -= steps;
  50. }
  51. void History::go_forward(int steps)
  52. {
  53. VERIFY(can_go_forward(steps));
  54. m_current += steps;
  55. }
  56. void History::clear()
  57. {
  58. m_items = {};
  59. m_current = -1;
  60. }
  61. void History::update_title(DeprecatedString const& title)
  62. {
  63. if (m_current == -1)
  64. return;
  65. m_items[m_current].title = title;
  66. }
  67. Vector<StringView> const History::get_back_title_history()
  68. {
  69. Vector<StringView> back_title_history;
  70. for (int i = m_current - 1; i >= 0; i--) {
  71. back_title_history.append(m_items[i].title);
  72. }
  73. return back_title_history;
  74. }
  75. Vector<StringView> const History::get_forward_title_history()
  76. {
  77. Vector<StringView> forward_title_history;
  78. for (int i = m_current + 1; i < static_cast<int>(m_items.size()); i++) {
  79. forward_title_history.append(m_items[i].title);
  80. }
  81. return forward_title_history;
  82. }
  83. }