TabWidget.cpp 15 KB

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