FlexFormattingContext.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2021, 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 <LibWeb/Layout/BlockBox.h>
  27. #include <LibWeb/Layout/Box.h>
  28. #include <LibWeb/Layout/FlexFormattingContext.h>
  29. namespace Web::Layout {
  30. FlexFormattingContext::FlexFormattingContext(Box& context_box, FormattingContext* parent)
  31. : FormattingContext(context_box, parent)
  32. {
  33. }
  34. FlexFormattingContext::~FlexFormattingContext()
  35. {
  36. }
  37. void FlexFormattingContext::run(Box& box, LayoutMode layout_mode)
  38. {
  39. // FIXME: This is *extremely* naive and only supports flex items laid out on a single line.
  40. auto flex_direction = box.computed_values().flex_direction();
  41. bool horizontal = flex_direction == CSS::FlexDirection::Row || flex_direction == CSS::FlexDirection::RowReverse;
  42. auto available_width = box.containing_block()->width();
  43. // First, compute the size of each flex item.
  44. box.for_each_child_of_type<Box>([&](Box& child_box) {
  45. auto shrink_to_fit_result = calculate_shrink_to_fit_widths(child_box);
  46. auto shrink_to_fit_width = min(max(shrink_to_fit_result.preferred_minimum_width, available_width), shrink_to_fit_result.preferred_width);
  47. child_box.set_width(shrink_to_fit_width);
  48. layout_inside(child_box, layout_mode);
  49. });
  50. // Then, place the items on a vertical or horizontal line.
  51. float x = 0;
  52. float y = 0;
  53. float tallest_child_height = 0;
  54. float widest_child_width = 0;
  55. box.for_each_child_of_type<Box>([&](Box& child_box) {
  56. child_box.set_offset(x, y);
  57. tallest_child_height = max(tallest_child_height, child_box.height());
  58. widest_child_width = max(widest_child_width, child_box.width());
  59. if (horizontal)
  60. x += child_box.margin_box_width();
  61. else
  62. y += child_box.margin_box_height();
  63. });
  64. // Then, compute the height of the entire flex container.
  65. // FIXME: This is not correct height calculation..
  66. if (horizontal) {
  67. box.set_height(tallest_child_height);
  68. box.set_width(x);
  69. } else {
  70. box.set_width(widest_child_width);
  71. box.set_height(y);
  72. }
  73. }
  74. }