TabWidget.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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 <AK/JsonObject.h>
  27. #include <AK/JsonValue.h>
  28. #include <LibGUI/BoxLayout.h>
  29. #include <LibGUI/Painter.h>
  30. #include <LibGUI/TabWidget.h>
  31. #include <LibGUI/Window.h>
  32. #include <LibGfx/Bitmap.h>
  33. #include <LibGfx/Font.h>
  34. #include <LibGfx/Palette.h>
  35. #include <LibGfx/StylePainter.h>
  36. namespace GUI {
  37. TabWidget::TabWidget()
  38. {
  39. set_focus_policy(FocusPolicy::NoFocus);
  40. REGISTER_INT_PROPERTY("container_padding", container_padding, set_container_padding);
  41. REGISTER_BOOL_PROPERTY("uniform_tabs", uniform_tabs, set_uniform_tabs);
  42. register_property(
  43. "text_alignment",
  44. [this] { return Gfx::to_string(text_alignment()); },
  45. [this](auto& value) {
  46. auto alignment = Gfx::text_alignment_from_string(value.to_string());
  47. if (alignment.has_value()) {
  48. set_text_alignment(alignment.value());
  49. return true;
  50. }
  51. return false;
  52. });
  53. }
  54. TabWidget::~TabWidget()
  55. {
  56. }
  57. void TabWidget::add_widget(const StringView& title, Widget& widget)
  58. {
  59. m_tabs.append({ title, nullptr, &widget });
  60. add_child(widget);
  61. update_focus_policy();
  62. }
  63. void TabWidget::remove_widget(Widget& widget)
  64. {
  65. if (active_widget() == &widget)
  66. activate_next_tab();
  67. m_tabs.remove_first_matching([&widget](auto& entry) { return &widget == entry.widget; });
  68. remove_child(widget);
  69. update_focus_policy();
  70. }
  71. void TabWidget::update_focus_policy()
  72. {
  73. FocusPolicy policy;
  74. if (is_bar_visible() && !m_tabs.is_empty())
  75. policy = FocusPolicy::TabFocus;
  76. else
  77. policy = FocusPolicy::NoFocus;
  78. set_focus_policy(policy);
  79. }
  80. void TabWidget::set_active_widget(Widget* widget)
  81. {
  82. if (widget == m_active_widget)
  83. return;
  84. bool active_widget_had_focus = m_active_widget && m_active_widget->has_focus_within();
  85. if (m_active_widget)
  86. m_active_widget->set_visible(false);
  87. m_active_widget = widget;
  88. if (m_active_widget) {
  89. m_active_widget->set_relative_rect(child_rect_for_size(size()));
  90. if (active_widget_had_focus)
  91. m_active_widget->set_focus(true);
  92. m_active_widget->set_visible(true);
  93. deferred_invoke([this](auto&) {
  94. if (on_change)
  95. on_change(*m_active_widget);
  96. });
  97. }
  98. update_bar();
  99. }
  100. void TabWidget::resize_event(ResizeEvent& event)
  101. {
  102. if (!m_active_widget)
  103. return;
  104. m_active_widget->set_relative_rect(child_rect_for_size(event.size()));
  105. }
  106. Gfx::IntRect TabWidget::child_rect_for_size(const Gfx::IntSize& size) const
  107. {
  108. Gfx::IntRect rect;
  109. switch (m_tab_position) {
  110. case TabPosition::Top:
  111. rect = { { container_padding(), bar_height() + container_padding() }, { size.width() - container_padding() * 2, size.height() - bar_height() - container_padding() * 2 } };
  112. break;
  113. case TabPosition::Bottom:
  114. rect = { { container_padding(), container_padding() }, { size.width() - container_padding() * 2, size.height() - bar_height() - container_padding() * 2 } };
  115. break;
  116. }
  117. if (rect.is_empty())
  118. return {};
  119. return rect;
  120. }
  121. void TabWidget::child_event(Core::ChildEvent& event)
  122. {
  123. if (!event.child() || !is<Widget>(*event.child()))
  124. return Widget::child_event(event);
  125. auto& child = downcast<Widget>(*event.child());
  126. if (event.type() == Event::ChildAdded) {
  127. if (!m_active_widget)
  128. set_active_widget(&child);
  129. else if (m_active_widget != &child)
  130. child.set_visible(false);
  131. } else if (event.type() == Event::ChildRemoved) {
  132. if (m_active_widget == &child) {
  133. Widget* new_active_widget = nullptr;
  134. for_each_child_widget([&](auto& new_child) {
  135. new_active_widget = &new_child;
  136. return IterationDecision::Break;
  137. });
  138. set_active_widget(new_active_widget);
  139. }
  140. }
  141. Widget::child_event(event);
  142. }
  143. Gfx::IntRect TabWidget::bar_rect() const
  144. {
  145. switch (m_tab_position) {
  146. case TabPosition::Top:
  147. return { 0, 0, width(), bar_height() };
  148. case TabPosition::Bottom:
  149. return { 0, height() - bar_height(), width(), bar_height() };
  150. }
  151. ASSERT_NOT_REACHED();
  152. }
  153. Gfx::IntRect TabWidget::container_rect() const
  154. {
  155. switch (m_tab_position) {
  156. case TabPosition::Top:
  157. return { 0, bar_height(), width(), height() - bar_height() };
  158. case TabPosition::Bottom:
  159. return { 0, 0, width(), height() - bar_height() };
  160. }
  161. ASSERT_NOT_REACHED();
  162. }
  163. void TabWidget::paint_event(PaintEvent& event)
  164. {
  165. if (!m_bar_visible)
  166. return;
  167. Painter painter(*this);
  168. painter.add_clip_rect(event.rect());
  169. auto container_rect = this->container_rect();
  170. auto padding_rect = container_rect;
  171. for (int i = 0; i < container_padding(); ++i) {
  172. painter.draw_rect(padding_rect, palette().button());
  173. padding_rect.shrink(2, 2);
  174. }
  175. if (container_padding() > 0)
  176. Gfx::StylePainter::paint_frame(painter, container_rect, palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Raised, 2);
  177. auto paint_tab_icon_if_needed = [&](auto& icon, auto& button_rect, auto& text_rect) {
  178. if (!icon)
  179. return;
  180. Gfx::IntRect icon_rect { button_rect.x(), button_rect.y(), 16, 16 };
  181. icon_rect.move_by(4, 3);
  182. painter.draw_scaled_bitmap(icon_rect, *icon, icon->rect());
  183. text_rect.set_x(icon_rect.right() + 1 + 4);
  184. text_rect.intersect(button_rect);
  185. };
  186. for (size_t i = 0; i < m_tabs.size(); ++i) {
  187. if (m_tabs[i].widget == m_active_widget)
  188. continue;
  189. bool hovered = static_cast<int>(i) == m_hovered_tab_index;
  190. auto button_rect = this->button_rect(i);
  191. Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), false, hovered, m_tabs[i].widget->is_enabled(), m_tab_position == TabPosition::Top);
  192. auto tab_button_content_rect = button_rect.translated(0, m_tab_position == TabPosition::Top ? 1 : 0);
  193. paint_tab_icon_if_needed(m_tabs[i].icon, button_rect, tab_button_content_rect);
  194. Gfx::IntRect text_rect { 0, 0, min(tab_button_content_rect.width(), font().width(m_tabs[i].title)), font().glyph_height() };
  195. text_rect.inflate(6, 4);
  196. text_rect.align_within(tab_button_content_rect, m_text_alignment);
  197. text_rect.intersect(tab_button_content_rect);
  198. painter.draw_text(text_rect, m_tabs[i].title, Gfx::TextAlignment::Center, palette().button_text(), Gfx::TextElision::Right);
  199. }
  200. for (size_t i = 0; i < m_tabs.size(); ++i) {
  201. if (m_tabs[i].widget != m_active_widget)
  202. continue;
  203. bool hovered = static_cast<int>(i) == m_hovered_tab_index;
  204. auto button_rect = this->button_rect(i);
  205. Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), true, hovered, m_tabs[i].widget->is_enabled(), m_tab_position == TabPosition::Top);
  206. auto tab_button_content_rect = button_rect.translated(0, m_tab_position == TabPosition::Top ? 1 : 0);
  207. paint_tab_icon_if_needed(m_tabs[i].icon, button_rect, tab_button_content_rect);
  208. Gfx::IntRect text_rect { 0, 0, min(tab_button_content_rect.width(), font().width(m_tabs[i].title)), font().glyph_height() };
  209. text_rect.inflate(6, 4);
  210. text_rect.align_within(tab_button_content_rect, m_text_alignment);
  211. text_rect.intersect(tab_button_content_rect);
  212. painter.draw_text(text_rect, m_tabs[i].title, Gfx::TextAlignment::Center, palette().button_text(), Gfx::TextElision::Right);
  213. if (is_focused()) {
  214. painter.draw_focus_rect(text_rect, palette().focus_outline());
  215. }
  216. if (m_tab_position == TabPosition::Top) {
  217. painter.draw_line(button_rect.bottom_left().translated(1, 1), button_rect.bottom_right().translated(-1, 1), palette().button());
  218. } else if (m_tab_position == TabPosition::Bottom) {
  219. painter.set_pixel(button_rect.top_left().translated(0, -1), palette().threed_highlight());
  220. painter.set_pixel(button_rect.top_right().translated(-1, -1), palette().threed_shadow1());
  221. painter.draw_line(button_rect.top_left().translated(1, -1), button_rect.top_right().translated(-2, -1), palette().button());
  222. painter.draw_line(button_rect.top_left().translated(1, -2), button_rect.top_right().translated(-2, -2), palette().button());
  223. }
  224. break;
  225. }
  226. }
  227. int TabWidget::uniform_tab_width() const
  228. {
  229. int minimum_tab_width = 24;
  230. int maximum_tab_width = 160;
  231. int total_tab_width = m_tabs.size() * maximum_tab_width;
  232. int tab_width = maximum_tab_width;
  233. if (total_tab_width > width())
  234. tab_width = width() / m_tabs.size();
  235. return max(tab_width, minimum_tab_width);
  236. }
  237. void TabWidget::set_bar_visible(bool bar_visible)
  238. {
  239. m_bar_visible = bar_visible;
  240. if (m_active_widget)
  241. m_active_widget->set_relative_rect(child_rect_for_size(size()));
  242. update_bar();
  243. }
  244. Gfx::IntRect TabWidget::button_rect(int index) const
  245. {
  246. int x_offset = 2;
  247. for (int i = 0; i < index; ++i) {
  248. auto tab_width = m_uniform_tabs ? uniform_tab_width() : m_tabs[i].width(font());
  249. x_offset += tab_width;
  250. }
  251. Gfx::IntRect rect { x_offset, 0, m_uniform_tabs ? uniform_tab_width() : m_tabs[index].width(font()), bar_height() };
  252. if (m_tabs[index].widget != m_active_widget) {
  253. rect.move_by(0, m_tab_position == TabPosition::Top ? 2 : 0);
  254. rect.set_height(rect.height() - 2);
  255. } else {
  256. rect.move_by(-2, 0);
  257. rect.set_width(rect.width() + 4);
  258. }
  259. rect.move_by(bar_rect().location());
  260. return rect;
  261. }
  262. int TabWidget::TabData::width(const Gfx::Font& font) const
  263. {
  264. return 16 + font.width(title) + (icon ? (16 + 4) : 0);
  265. }
  266. void TabWidget::mousedown_event(MouseEvent& event)
  267. {
  268. for (size_t i = 0; i < m_tabs.size(); ++i) {
  269. auto button_rect = this->button_rect(i);
  270. if (!button_rect.contains(event.position()))
  271. continue;
  272. if (event.button() == MouseButton::Left) {
  273. set_active_widget(m_tabs[i].widget);
  274. } else if (event.button() == MouseButton::Middle) {
  275. auto* widget = m_tabs[i].widget;
  276. deferred_invoke([this, widget](auto&) {
  277. if (on_middle_click && widget)
  278. on_middle_click(*widget);
  279. });
  280. }
  281. return;
  282. }
  283. }
  284. void TabWidget::mousemove_event(MouseEvent& event)
  285. {
  286. int hovered_tab = -1;
  287. for (size_t i = 0; i < m_tabs.size(); ++i) {
  288. auto button_rect = this->button_rect(i);
  289. if (!button_rect.contains(event.position()))
  290. continue;
  291. hovered_tab = i;
  292. if (m_tabs[i].widget == m_active_widget)
  293. break;
  294. }
  295. if (hovered_tab == m_hovered_tab_index)
  296. return;
  297. m_hovered_tab_index = hovered_tab;
  298. update_bar();
  299. }
  300. void TabWidget::leave_event(Core::Event&)
  301. {
  302. if (m_hovered_tab_index != -1) {
  303. m_hovered_tab_index = -1;
  304. update_bar();
  305. }
  306. }
  307. void TabWidget::update_bar()
  308. {
  309. auto invalidation_rect = bar_rect();
  310. invalidation_rect.set_height(invalidation_rect.height() + 1);
  311. update(invalidation_rect);
  312. }
  313. void TabWidget::set_tab_position(TabPosition tab_position)
  314. {
  315. if (m_tab_position == tab_position)
  316. return;
  317. m_tab_position = tab_position;
  318. if (m_active_widget)
  319. m_active_widget->set_relative_rect(child_rect_for_size(size()));
  320. update();
  321. }
  322. int TabWidget::active_tab_index() const
  323. {
  324. for (size_t i = 0; i < m_tabs.size(); i++) {
  325. if (m_tabs.at(i).widget == m_active_widget)
  326. return i;
  327. }
  328. return -1;
  329. }
  330. void TabWidget::set_tab_title(Widget& tab, const StringView& title)
  331. {
  332. for (auto& t : m_tabs) {
  333. if (t.widget == &tab) {
  334. if (t.title != title) {
  335. t.title = title;
  336. update();
  337. }
  338. return;
  339. }
  340. }
  341. }
  342. void TabWidget::set_tab_icon(Widget& tab, const Gfx::Bitmap* icon)
  343. {
  344. for (auto& t : m_tabs) {
  345. if (t.widget == &tab) {
  346. t.icon = icon;
  347. update();
  348. return;
  349. }
  350. }
  351. }
  352. void TabWidget::activate_next_tab()
  353. {
  354. if (m_tabs.size() <= 1)
  355. return;
  356. int index = active_tab_index();
  357. ++index;
  358. if (index >= (int)m_tabs.size())
  359. index = 0;
  360. set_active_widget(m_tabs.at(index).widget);
  361. }
  362. void TabWidget::activate_previous_tab()
  363. {
  364. if (m_tabs.size() <= 1)
  365. return;
  366. int index = active_tab_index();
  367. --index;
  368. if (index < 0)
  369. index = m_tabs.size() - 1;
  370. set_active_widget(m_tabs.at(index).widget);
  371. }
  372. void TabWidget::keydown_event(KeyEvent& event)
  373. {
  374. if (event.ctrl() && event.key() == Key_Tab) {
  375. if (event.shift())
  376. activate_previous_tab();
  377. else
  378. activate_next_tab();
  379. event.accept();
  380. return;
  381. }
  382. if (is_focused()) {
  383. if (!event.modifiers() && event.key() == Key_Left) {
  384. activate_previous_tab();
  385. event.accept();
  386. return;
  387. }
  388. if (!event.modifiers() && event.key() == Key_Right) {
  389. activate_next_tab();
  390. event.accept();
  391. return;
  392. }
  393. }
  394. Widget::keydown_event(event);
  395. }
  396. void TabWidget::context_menu_event(ContextMenuEvent& context_menu_event)
  397. {
  398. for (size_t i = 0; i < m_tabs.size(); ++i) {
  399. auto button_rect = this->button_rect(i);
  400. if (!button_rect.contains(context_menu_event.position()))
  401. continue;
  402. auto* widget = m_tabs[i].widget;
  403. deferred_invoke([this, widget, context_menu_event](auto&) {
  404. if (on_context_menu_request && widget)
  405. on_context_menu_request(*widget, context_menu_event);
  406. });
  407. return;
  408. }
  409. }
  410. }