Splitter.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 OpportunisticResizee {
  14. First,
  15. Second
  16. };
  17. virtual ~Splitter() override = default;
  18. protected:
  19. explicit Splitter(Gfx::Orientation);
  20. virtual void paint_event(PaintEvent&) override;
  21. virtual void resize_event(ResizeEvent&) override;
  22. virtual void mousedown_event(MouseEvent&) override;
  23. virtual void mousemove_event(MouseEvent&) override;
  24. virtual void mouseup_event(MouseEvent&) override;
  25. virtual void leave_event(Core::Event&) override;
  26. virtual void did_layout() override;
  27. virtual void custom_layout() override;
  28. OpportunisticResizee opportunisitic_resizee() const { return m_opportunistic_resizee; }
  29. void set_opportunisitic_resizee(OpportunisticResizee resizee) { m_opportunistic_resizee = resizee; }
  30. private:
  31. void override_cursor(bool do_override);
  32. Gfx::IntRect rect_between_widgets(GUI::Widget const& first_widget, GUI::Widget const& second_widget, bool honor_grabbable_margins) const;
  33. Gfx::Orientation m_orientation;
  34. bool m_resizing { false };
  35. bool m_overriding_cursor { false };
  36. Gfx::IntPoint m_resize_origin;
  37. WeakPtr<Widget> m_first_resizee;
  38. WeakPtr<Widget> m_second_resizee;
  39. Gfx::IntSize m_first_resizee_start_size;
  40. Gfx::IntSize m_second_resizee_start_size;
  41. OpportunisticResizee m_opportunistic_resizee { OpportunisticResizee::Second };
  42. size_t m_last_child_count { 0 };
  43. int m_first_resizee_max_size { 0 };
  44. int m_second_resizee_max_size { 0 };
  45. void recompute_grabbables();
  46. struct Grabbable {
  47. // Index in m_grabbables, for convenience.
  48. size_t index { 0 };
  49. // The full grabbable rect, includes the content margin of adjacent elements.
  50. Gfx::IntRect grabbable_rect;
  51. // The rect used for painting. Does not include content margins.
  52. Gfx::IntRect paint_rect;
  53. WeakPtr<Widget> first_widget;
  54. WeakPtr<Widget> second_widget;
  55. };
  56. Grabbable* grabbable_at(Gfx::IntPoint const&);
  57. void set_hovered_grabbable(Grabbable*);
  58. Vector<Grabbable> m_grabbables;
  59. Optional<size_t> m_hovered_index;
  60. };
  61. class VerticalSplitter final : public Splitter {
  62. C_OBJECT(VerticalSplitter)
  63. public:
  64. virtual ~VerticalSplitter() override = default;
  65. private:
  66. VerticalSplitter()
  67. : Splitter(Gfx::Orientation::Vertical)
  68. {
  69. }
  70. };
  71. class HorizontalSplitter final : public Splitter {
  72. C_OBJECT(HorizontalSplitter)
  73. public:
  74. virtual ~HorizontalSplitter() override = default;
  75. private:
  76. HorizontalSplitter()
  77. : Splitter(Gfx::Orientation::Horizontal)
  78. {
  79. }
  80. };
  81. }