BookmarksBarWidget.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "BookmarksBarWidget.h"
  27. #include <LibGUI/Action.h>
  28. #include <LibGUI/BoxLayout.h>
  29. #include <LibGUI/Button.h>
  30. #include <LibGUI/Event.h>
  31. #include <LibGUI/JsonArrayModel.h>
  32. #include <LibGUI/Menu.h>
  33. #include <LibGUI/Model.h>
  34. #include <LibGUI/Widget.h>
  35. #include <LibGUI/Window.h>
  36. #include <LibGfx/Palette.h>
  37. BookmarksBarWidget::BookmarksBarWidget(const String& bookmarks_file, bool enabled)
  38. {
  39. set_layout<GUI::HorizontalBoxLayout>();
  40. layout()->set_spacing(0);
  41. set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  42. set_preferred_size(0, 20);
  43. if (!enabled)
  44. set_visible(false);
  45. m_additional = GUI::Button::construct();
  46. m_additional->set_button_style(Gfx::ButtonStyle::CoolBar);
  47. m_additional->set_text(">");
  48. m_additional->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  49. m_additional->set_preferred_size(14, 20);
  50. m_additional->on_click = [&] {
  51. if (m_additional_menu) {
  52. m_additional_menu->popup(m_additional->relative_position().translated(relative_position().translated(m_additional->window()->position())));
  53. }
  54. };
  55. m_separator = GUI::Widget::construct();
  56. Vector<GUI::JsonArrayModel::FieldSpec> fields;
  57. fields.empend("title", "Title", Gfx::TextAlignment::CenterLeft);
  58. fields.empend("url", "Url", Gfx::TextAlignment::CenterRight);
  59. set_model(GUI::JsonArrayModel::create(bookmarks_file, move(fields)));
  60. model()->update();
  61. }
  62. BookmarksBarWidget::~BookmarksBarWidget()
  63. {
  64. }
  65. void BookmarksBarWidget::set_model(RefPtr<GUI::Model> model)
  66. {
  67. if (model == m_model)
  68. return;
  69. m_model = move(model);
  70. m_model->on_update = [&]() {
  71. did_update_model();
  72. };
  73. }
  74. void BookmarksBarWidget::resize_event(GUI::ResizeEvent& event)
  75. {
  76. Widget::resize_event(event);
  77. update_content_size();
  78. }
  79. void BookmarksBarWidget::did_update_model()
  80. {
  81. for (auto* child : child_widgets()) {
  82. child->remove_from_parent();
  83. }
  84. child_widgets().clear();
  85. m_bookmarks.clear();
  86. int width = 0;
  87. for (int item_index = 0; item_index < model()->row_count(); ++item_index) {
  88. auto title = model()->data(model()->index(item_index, 0)).to_string();
  89. auto url = model()->data(model()->index(item_index, 1)).to_string();
  90. Gfx::Rect rect { width, 0, font().width(title) + 32, height() };
  91. auto& button = add<GUI::Button>();
  92. m_bookmarks.append(button);
  93. button.set_button_style(Gfx::ButtonStyle::CoolBar);
  94. button.set_text(title);
  95. button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  96. button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
  97. button.set_preferred_size(font().width(title) + 32, 20);
  98. button.set_relative_rect(rect);
  99. button.on_click = [title, url, this] {
  100. if (on_bookmark_click)
  101. on_bookmark_click(title, url);
  102. };
  103. width += rect.width();
  104. }
  105. add_child(*m_separator);
  106. add_child(*m_additional);
  107. update_content_size();
  108. update();
  109. }
  110. void BookmarksBarWidget::update_content_size()
  111. {
  112. int x_position = 0;
  113. m_last_visible_index = -1;
  114. for (size_t i = 0; i < m_bookmarks.size(); ++i) {
  115. auto& bookmark = m_bookmarks.at(i);
  116. if (x_position + bookmark.width() > width()) {
  117. m_last_visible_index = i;
  118. break;
  119. }
  120. bookmark.set_x(x_position);
  121. bookmark.set_visible(true);
  122. x_position += bookmark.width();
  123. }
  124. if (m_last_visible_index < 0) {
  125. m_additional->set_visible(false);
  126. } else {
  127. // hide all items > m_last_visible_index and create new bookmarks menu for them
  128. m_additional->set_visible(true);
  129. m_additional_menu = GUI::Menu::construct("Additional Bookmarks");
  130. for (size_t i = m_last_visible_index; i < m_bookmarks.size(); ++i) {
  131. auto& bookmark = m_bookmarks.at(i);
  132. bookmark.set_visible(false);
  133. m_additional_menu->add_action(GUI::Action::create(bookmark.text(),
  134. Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"),
  135. [&](auto&) {
  136. bookmark.on_click();
  137. }));
  138. }
  139. }
  140. }