TabWidget.cpp 15 KB

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