Serenity Operating System
at master 279 lines 10 kB view raw
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 8#include <LibGUI/BoxLayout.h> 9#include <LibGUI/Desktop.h> 10#include <LibGUI/Painter.h> 11#include <LibGUI/Splitter.h> 12#include <LibGUI/UIDimensions.h> 13#include <LibGUI/Window.h> 14#include <LibGfx/Palette.h> 15 16REGISTER_WIDGET(GUI, HorizontalSplitter) 17REGISTER_WIDGET(GUI, VerticalSplitter) 18 19namespace GUI { 20 21Splitter::Splitter(Orientation orientation) 22 : m_orientation(orientation) 23{ 24 REGISTER_ENUM_PROPERTY("opportunistic_resizee", opportunisitic_resizee, set_opportunisitic_resizee, OpportunisticResizee, 25 { OpportunisticResizee::First, "First" }, 26 { OpportunisticResizee::Second, "Second" }); 27 28 set_background_role(ColorRole::Button); 29 set_layout<BoxLayout>(orientation); 30 set_fill_with_background_color(true); 31 if (m_orientation == Gfx::Orientation::Horizontal) 32 layout()->set_spacing(3); 33 else 34 layout()->set_spacing(4); 35} 36 37void Splitter::paint_event(PaintEvent& event) 38{ 39 Painter painter(*this); 40 painter.add_clip_rect(event.rect()); 41 42 auto palette = this->palette(); 43 44 auto paint_knurl = [&](int x, int y) { 45 painter.set_pixel(x, y, palette.threed_shadow1()); 46 painter.set_pixel(x + 1, y, palette.threed_shadow1()); 47 painter.set_pixel(x, y + 1, palette.threed_shadow1()); 48 painter.set_pixel(x + 1, y + 1, palette.threed_highlight()); 49 }; 50 51 constexpr size_t knurl_width = 2; 52 constexpr size_t knurl_spacing = 1; 53 constexpr size_t knurl_count = 10; 54 constexpr size_t total_knurling_width = knurl_count * (knurl_width + knurl_spacing); 55 56 if (m_hovered_index.has_value()) 57 painter.fill_rect(m_grabbables[m_hovered_index.value()].paint_rect, palette.hover_highlight()); 58 59 for (auto& grabbable : m_grabbables) { 60 for (size_t i = 0; i < knurl_count; ++i) { 61 auto& rect = grabbable.paint_rect; 62 int primary = rect.center().primary_offset_for_orientation(m_orientation) - 1; 63 int secondary = rect.center().secondary_offset_for_orientation(m_orientation) - (total_knurling_width / 2) + (i * (knurl_width + knurl_spacing)); 64 if (Desktop::the().system_effects().splitter_knurls()) { 65 if (m_orientation == Gfx::Orientation::Vertical) 66 paint_knurl(secondary, primary); 67 else 68 paint_knurl(primary, secondary); 69 } 70 } 71 } 72} 73 74void Splitter::resize_event(ResizeEvent& event) 75{ 76 Widget::resize_event(event); 77 set_hovered_grabbable(nullptr); 78} 79 80void Splitter::set_hovered_grabbable(Grabbable* grabbable) 81{ 82 if (m_hovered_index.has_value()) { 83 if (grabbable && grabbable->index == m_hovered_index.value()) 84 return; 85 update(m_grabbables[m_hovered_index.value()].paint_rect); 86 } 87 88 if (grabbable) { 89 m_hovered_index = grabbable->index; 90 update(grabbable->paint_rect); 91 } else { 92 m_hovered_index = {}; 93 } 94} 95 96void Splitter::override_cursor(bool do_override) 97{ 98 if (do_override) { 99 if (!m_overriding_cursor) { 100 set_override_cursor(m_orientation == Orientation::Horizontal ? Gfx::StandardCursor::ResizeColumn : Gfx::StandardCursor::ResizeRow); 101 m_overriding_cursor = true; 102 } 103 } else { 104 if (m_overriding_cursor) { 105 set_override_cursor(Gfx::StandardCursor::None); 106 m_overriding_cursor = false; 107 } 108 } 109} 110 111void Splitter::leave_event(Core::Event&) 112{ 113 if (!m_resizing) 114 override_cursor(false); 115 set_hovered_grabbable(nullptr); 116} 117 118Splitter::Grabbable* Splitter::grabbable_at(Gfx::IntPoint position) 119{ 120 for (auto& grabbable : m_grabbables) { 121 if (grabbable.grabbable_rect.contains(position)) 122 return &grabbable; 123 } 124 return nullptr; 125} 126 127void Splitter::mousedown_event(MouseEvent& event) 128{ 129 if (event.button() != MouseButton::Primary) 130 return; 131 132 auto* grabbable = grabbable_at(event.position()); 133 if (!grabbable) 134 return; 135 136 m_resizing = true; 137 138 m_first_resizee = *grabbable->first_widget; 139 m_second_resizee = *grabbable->second_widget; 140 m_first_resizee_start_size = m_first_resizee->size(); 141 m_second_resizee_start_size = m_second_resizee->size(); 142 m_resize_origin = event.position(); 143 144 VERIFY(layout()); 145 auto spacer = layout()->spacing(); 146 auto splitter = size().primary_size_for_orientation(m_orientation); 147 m_first_resizee_max_size = splitter - spacer - m_second_resizee->calculated_min_size().value_or({ 0, 0 }).primary_size_for_orientation(m_orientation).as_int(); 148 m_second_resizee_max_size = splitter - spacer - m_first_resizee->calculated_min_size().value_or({ 0, 0 }).primary_size_for_orientation(m_orientation).as_int(); 149} 150 151Gfx::IntRect Splitter::rect_between_widgets(GUI::Widget const& first_widget, GUI::Widget const& second_widget, bool honor_grabbable_margins) const 152{ 153 auto first_widget_rect = honor_grabbable_margins ? first_widget.relative_non_grabbable_rect() : first_widget.relative_rect(); 154 auto second_widget_rect = honor_grabbable_margins ? second_widget.relative_non_grabbable_rect() : second_widget.relative_rect(); 155 156 auto first_edge = first_widget_rect.last_edge_for_orientation(m_orientation); 157 auto second_edge = second_widget_rect.first_edge_for_orientation(m_orientation); 158 Gfx::IntRect rect; 159 rect.set_primary_offset_for_orientation(m_orientation, first_edge + 1); 160 rect.set_primary_size_for_orientation(m_orientation, second_edge - first_edge - 1); 161 rect.set_secondary_offset_for_orientation(m_orientation, 0); 162 rect.set_secondary_size_for_orientation(m_orientation, relative_rect().secondary_size_for_orientation(m_orientation)); 163 return rect; 164} 165 166void Splitter::recompute_grabbables() 167{ 168 auto old_grabbables_count = m_grabbables.size(); 169 m_grabbables.clear(); 170 auto old_hovered_index = m_hovered_index; 171 m_hovered_index = {}; 172 173 auto child_widgets = this->child_widgets(); 174 child_widgets.remove_all_matching([&](auto& widget) { return !widget.is_visible(); }); 175 m_last_child_count = child_widgets.size(); 176 177 if (child_widgets.size() < 2) 178 return; 179 180 size_t start_index = 0; 181 size_t end_index = 1; 182 183 while (end_index < child_widgets.size()) { 184 auto const& first_widget = child_widgets[start_index]; 185 auto const& second_widget = child_widgets[end_index]; 186 m_grabbables.append(Grabbable { 187 .index = m_grabbables.size(), 188 .grabbable_rect = rect_between_widgets(first_widget, second_widget, true), 189 .paint_rect = rect_between_widgets(first_widget, second_widget, false), 190 .first_widget = first_widget, 191 .second_widget = second_widget, 192 }); 193 ++start_index; 194 ++end_index; 195 } 196 197 if (old_hovered_index.has_value() && old_grabbables_count == m_grabbables.size()) 198 set_hovered_grabbable(&m_grabbables[old_hovered_index.value()]); 199} 200 201void Splitter::mousemove_event(MouseEvent& event) 202{ 203 auto* grabbable = grabbable_at(event.position()); 204 set_hovered_grabbable(grabbable); 205 if (!m_resizing) { 206 override_cursor(grabbable != nullptr); 207 return; 208 } 209 if (!m_first_resizee || !m_second_resizee) { 210 m_resizing = false; 211 return; 212 } 213 214 auto delta = (event.position() - m_resize_origin).primary_offset_for_orientation(m_orientation); 215 auto new_first_resizee_size = clamp(m_first_resizee_start_size.primary_size_for_orientation(m_orientation) + delta, 0, m_first_resizee_max_size); 216 auto new_second_resizee_size = clamp(m_second_resizee_start_size.primary_size_for_orientation(m_orientation) - delta, 0, m_second_resizee_max_size); 217 218 if (m_orientation == Orientation::Horizontal) { 219 if (opportunisitic_resizee() == OpportunisticResizee::First) { 220 m_first_resizee->set_preferred_width(SpecialDimension::OpportunisticGrow); 221 m_second_resizee->set_preferred_width(new_second_resizee_size); 222 } else { 223 VERIFY(opportunisitic_resizee() == OpportunisticResizee::Second); 224 m_second_resizee->set_preferred_width(SpecialDimension::OpportunisticGrow); 225 m_first_resizee->set_preferred_width(new_first_resizee_size); 226 } 227 } else { 228 if (opportunisitic_resizee() == OpportunisticResizee::First) { 229 m_first_resizee->set_preferred_height(SpecialDimension::OpportunisticGrow); 230 m_second_resizee->set_preferred_height(new_second_resizee_size); 231 232 } else { 233 VERIFY(opportunisitic_resizee() == OpportunisticResizee::Second); 234 m_second_resizee->set_preferred_height(SpecialDimension::OpportunisticGrow); 235 m_first_resizee->set_preferred_height(new_first_resizee_size); 236 } 237 } 238 239 invalidate_layout(); 240} 241 242void Splitter::did_layout() 243{ 244 recompute_grabbables(); 245} 246 247void Splitter::custom_layout() 248{ 249 auto child_widgets = this->child_widgets(); 250 child_widgets.remove_all_matching([&](auto& widget) { return !widget.is_visible(); }); 251 252 if (!child_widgets.size()) 253 return; 254 255 if (m_last_child_count > child_widgets.size()) { 256 bool has_child_to_fill_space = false; 257 for (auto& child : child_widgets) { 258 if (child.preferred_size().primary_size_for_orientation(m_orientation).is_opportunistic_grow()) { 259 has_child_to_fill_space = true; 260 break; 261 } 262 } 263 if (!has_child_to_fill_space) 264 child_widgets.last().set_preferred_size(SpecialDimension::OpportunisticGrow); 265 } 266} 267 268void Splitter::mouseup_event(MouseEvent& event) 269{ 270 if (event.button() != MouseButton::Primary) 271 return; 272 m_resizing = false; 273 m_first_resizee = nullptr; 274 m_second_resizee = nullptr; 275 if (!rect().contains(event.position())) 276 set_override_cursor(Gfx::StandardCursor::None); 277} 278 279}