Splitter.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibGUI/Widget.h>
  9. namespace GUI {
  10. class Splitter : public Widget {
  11. C_OBJECT(Splitter);
  12. public:
  13. enum class FixedResizee {
  14. First,
  15. Second
  16. };
  17. virtual ~Splitter() override = default;
  18. int first_resizee_minimum_size() { return m_first_resizee_minimum_size; }
  19. void set_first_resizee_minimum_size(int minimum_size) { m_first_resizee_minimum_size = minimum_size; }
  20. int second_resizee_minimum_size() { return m_second_resizee_minimum_size; }
  21. void set_second_resizee_minimum_size(int minimum_size) { m_second_resizee_minimum_size = minimum_size; }
  22. protected:
  23. explicit Splitter(Gfx::Orientation);
  24. virtual void paint_event(PaintEvent&) override;
  25. virtual void resize_event(ResizeEvent&) override;
  26. virtual void mousedown_event(MouseEvent&) override;
  27. virtual void mousemove_event(MouseEvent&) override;
  28. virtual void mouseup_event(MouseEvent&) override;
  29. virtual void leave_event(Core::Event&) override;
  30. virtual void did_layout() override;
  31. virtual void custom_layout() override;
  32. FixedResizee fixed_resizee() const { return m_fixed_resizee; }
  33. void set_fixed_resizee(FixedResizee resizee) { m_fixed_resizee = resizee; }
  34. private:
  35. void override_cursor(bool do_override);
  36. Gfx::IntRect rect_between_widgets(GUI::Widget const& first_widget, GUI::Widget const& second_widget, bool honor_grabbable_margins) const;
  37. Gfx::Orientation m_orientation;
  38. bool m_resizing { false };
  39. bool m_overriding_cursor { false };
  40. Gfx::IntPoint m_resize_origin;
  41. WeakPtr<Widget> m_first_resizee;
  42. WeakPtr<Widget> m_second_resizee;
  43. Gfx::IntSize m_first_resizee_start_size;
  44. Gfx::IntSize m_second_resizee_start_size;
  45. int m_first_resizee_minimum_size { 0 };
  46. int m_second_resizee_minimum_size { 0 };
  47. FixedResizee m_fixed_resizee { FixedResizee::First };
  48. size_t m_last_child_count { 0 };
  49. void recompute_grabbables();
  50. struct Grabbable {
  51. // Index in m_grabbables, for convenience.
  52. size_t index { 0 };
  53. // The full grabbable rect, includes the content margin of adjacent elements.
  54. Gfx::IntRect grabbable_rect;
  55. // The rect used for painting. Does not include content margins.
  56. Gfx::IntRect paint_rect;
  57. WeakPtr<Widget> first_widget;
  58. WeakPtr<Widget> second_widget;
  59. };
  60. Grabbable* grabbable_at(Gfx::IntPoint const&);
  61. void set_hovered_grabbable(Grabbable*);
  62. Vector<Grabbable> m_grabbables;
  63. Optional<size_t> m_hovered_index;
  64. };
  65. class VerticalSplitter final : public Splitter {
  66. C_OBJECT(VerticalSplitter)
  67. public:
  68. virtual ~VerticalSplitter() override = default;
  69. private:
  70. VerticalSplitter()
  71. : Splitter(Gfx::Orientation::Vertical)
  72. {
  73. }
  74. };
  75. class HorizontalSplitter final : public Splitter {
  76. C_OBJECT(HorizontalSplitter)
  77. public:
  78. virtual ~HorizontalSplitter() override = default;
  79. private:
  80. HorizontalSplitter()
  81. : Splitter(Gfx::Orientation::Horizontal)
  82. {
  83. }
  84. };
  85. }