summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/Splitter.cpp
blob: ade4ab746fc33584ebe999e1343f21b569b3a514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibGUI/BoxLayout.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Splitter.h>
#include <LibGUI/UIDimensions.h>
#include <LibGUI/Window.h>
#include <LibGfx/Palette.h>

REGISTER_WIDGET(GUI, HorizontalSplitter)
REGISTER_WIDGET(GUI, VerticalSplitter)

namespace GUI {

Splitter::Splitter(Orientation orientation)
    : m_orientation(orientation)
{
    REGISTER_INT_PROPERTY("first_resizee_minimum_size", first_resizee_minimum_size, set_first_resizee_minimum_size);
    REGISTER_INT_PROPERTY("second_resizee_minimum_size", second_resizee_minimum_size, set_second_resizee_minimum_size);
    REGISTER_ENUM_PROPERTY("fixed_resizee", fixed_resizee, set_fixed_resizee, FixedResizee,
        { FixedResizee::First, "First" },
        { FixedResizee::Second, "Second" });

    set_background_role(ColorRole::Button);
    set_layout<BoxLayout>(orientation);
    set_fill_with_background_color(true);
    if (m_orientation == Gfx::Orientation::Horizontal)
        layout()->set_spacing(3);
    else
        layout()->set_spacing(4);
}

void Splitter::paint_event(PaintEvent& event)
{
    Painter painter(*this);
    painter.add_clip_rect(event.rect());

    auto palette = this->palette();

    auto paint_knurl = [&](int x, int y) {
        painter.set_pixel(x, y, palette.threed_shadow1());
        painter.set_pixel(x + 1, y, palette.threed_shadow1());
        painter.set_pixel(x, y + 1, palette.threed_shadow1());
        painter.set_pixel(x + 1, y + 1, palette.threed_highlight());
    };

    constexpr size_t knurl_width = 2;
    constexpr size_t knurl_spacing = 1;
    constexpr size_t knurl_count = 10;
    constexpr size_t total_knurling_width = knurl_count * (knurl_width + knurl_spacing);

    if (m_hovered_index.has_value())
        painter.fill_rect(m_grabbables[m_hovered_index.value()].paint_rect, palette.hover_highlight());

    for (auto& grabbable : m_grabbables) {
        for (size_t i = 0; i < knurl_count; ++i) {
            auto& rect = grabbable.paint_rect;
            int primary = rect.center().primary_offset_for_orientation(m_orientation) - 1;
            int secondary = rect.center().secondary_offset_for_orientation(m_orientation) - (total_knurling_width / 2) + (i * (knurl_width + knurl_spacing));
            if (m_orientation == Gfx::Orientation::Vertical)
                paint_knurl(secondary, primary);
            else
                paint_knurl(primary, secondary);
        }
    }
}

void Splitter::resize_event(ResizeEvent& event)
{
    Widget::resize_event(event);
    set_hovered_grabbable(nullptr);
}

void Splitter::set_hovered_grabbable(Grabbable* grabbable)
{
    if (m_hovered_index.has_value()) {
        if (grabbable && grabbable->index == m_hovered_index.value())
            return;
        update(m_grabbables[m_hovered_index.value()].paint_rect);
    }

    if (grabbable) {
        m_hovered_index = grabbable->index;
        update(grabbable->paint_rect);
    } else {
        m_hovered_index = {};
    }
}

void Splitter::override_cursor(bool do_override)
{
    if (do_override) {
        if (!m_overriding_cursor) {
            set_override_cursor(m_orientation == Orientation::Horizontal ? Gfx::StandardCursor::ResizeColumn : Gfx::StandardCursor::ResizeRow);
            m_overriding_cursor = true;
        }
    } else {
        if (m_overriding_cursor) {
            set_override_cursor(Gfx::StandardCursor::None);
            m_overriding_cursor = false;
        }
    }
}

void Splitter::leave_event(Core::Event&)
{
    if (!m_resizing)
        override_cursor(false);
    set_hovered_grabbable(nullptr);
}

Splitter::Grabbable* Splitter::grabbable_at(Gfx::IntPoint const& position)
{
    for (auto& grabbable : m_grabbables) {
        if (grabbable.grabbable_rect.contains(position))
            return &grabbable;
    }
    return nullptr;
}

void Splitter::mousedown_event(MouseEvent& event)
{
    if (event.button() != MouseButton::Primary)
        return;

    auto* grabbable = grabbable_at(event.position());
    if (!grabbable)
        return;

    m_resizing = true;

    m_first_resizee = *grabbable->first_widget;
    m_second_resizee = *grabbable->second_widget;
    m_first_resizee_start_size = m_first_resizee->size();
    m_second_resizee_start_size = m_second_resizee->size();
    m_resize_origin = event.position();
}

Gfx::IntRect Splitter::rect_between_widgets(GUI::Widget const& first_widget, GUI::Widget const& second_widget, bool honor_grabbable_margins) const
{
    auto first_widget_rect = honor_grabbable_margins ? first_widget.relative_non_grabbable_rect() : first_widget.relative_rect();
    auto second_widget_rect = honor_grabbable_margins ? second_widget.relative_non_grabbable_rect() : second_widget.relative_rect();

    auto first_edge = first_widget_rect.last_edge_for_orientation(m_orientation);
    auto second_edge = second_widget_rect.first_edge_for_orientation(m_orientation);
    Gfx::IntRect rect;
    rect.set_primary_offset_for_orientation(m_orientation, first_edge + 1);
    rect.set_primary_size_for_orientation(m_orientation, second_edge - first_edge - 1);
    rect.set_secondary_offset_for_orientation(m_orientation, 0);
    rect.set_secondary_size_for_orientation(m_orientation, relative_rect().secondary_size_for_orientation(m_orientation));
    return rect;
}

void Splitter::recompute_grabbables()
{
    auto old_grabbables_count = m_grabbables.size();
    m_grabbables.clear();
    auto old_hovered_index = m_hovered_index;
    m_hovered_index = {};

    auto child_widgets = this->child_widgets();
    child_widgets.remove_all_matching([&](auto& widget) { return !widget.is_visible(); });
    m_last_child_count = child_widgets.size();

    if (child_widgets.size() < 2)
        return;

    size_t start_index = 0;
    size_t end_index = 1;

    while (end_index < child_widgets.size()) {
        auto const& first_widget = child_widgets[start_index];
        auto const& second_widget = child_widgets[end_index];
        m_grabbables.append(Grabbable {
            .index = m_grabbables.size(),
            .grabbable_rect = rect_between_widgets(first_widget, second_widget, true),
            .paint_rect = rect_between_widgets(first_widget, second_widget, false),
            .first_widget = first_widget,
            .second_widget = second_widget,
        });
        ++start_index;
        ++end_index;
    }

    if (old_hovered_index.has_value() && old_grabbables_count == m_grabbables.size())
        set_hovered_grabbable(&m_grabbables[old_hovered_index.value()]);
}

// FIXME: Respect the child widgets min and max sizes
void Splitter::mousemove_event(MouseEvent& event)
{
    auto* grabbable = grabbable_at(event.position());
    set_hovered_grabbable(grabbable);
    if (!m_resizing) {
        override_cursor(grabbable != nullptr);
        return;
    }
    auto delta = event.position() - m_resize_origin;
    if (!m_first_resizee || !m_second_resizee) {
        // One or both of the resizees were deleted during an ongoing resize, screw this.
        m_resizing = false;
        return;
    }
    auto new_first_resizee_size = m_first_resizee_start_size;
    auto new_second_resizee_size = m_second_resizee_start_size;

    new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + delta.primary_offset_for_orientation(m_orientation));
    new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - delta.primary_offset_for_orientation(m_orientation));

    if (new_first_resizee_size.primary_size_for_orientation(m_orientation) < m_first_resizee_minimum_size) {
        int correction = m_first_resizee_minimum_size - new_first_resizee_size.primary_size_for_orientation(m_orientation);
        new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + correction);
        new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - correction);
    }
    if (new_second_resizee_size.primary_size_for_orientation(m_orientation) < m_second_resizee_minimum_size) {
        int correction = m_second_resizee_minimum_size - new_second_resizee_size.primary_size_for_orientation(m_orientation);
        new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) + correction);
        new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) - correction);
    }

    if (m_orientation == Orientation::Horizontal) {
        if (fixed_resizee() == FixedResizee::First) {
            m_first_resizee->set_fixed_width(new_first_resizee_size.width());
            m_second_resizee->set_min_width(SpecialDimension::Shrink);
            m_second_resizee->set_max_width(SpecialDimension::Grow);
        } else {
            VERIFY(fixed_resizee() == FixedResizee::Second);
            m_second_resizee->set_fixed_width(new_second_resizee_size.width());
            m_first_resizee->set_min_width(SpecialDimension::Shrink);
            m_first_resizee->set_max_width(SpecialDimension::Grow);
        }
    } else {
        if (fixed_resizee() == FixedResizee::First) {
            m_first_resizee->set_fixed_height(new_first_resizee_size.height());
            m_second_resizee->set_min_height(SpecialDimension::Shrink);
            m_second_resizee->set_max_height(SpecialDimension::Grow);
        } else {
            VERIFY(fixed_resizee() == FixedResizee::Second);
            m_second_resizee->set_fixed_height(new_second_resizee_size.height());
            m_first_resizee->set_min_height(SpecialDimension::Shrink);
            m_first_resizee->set_max_height(SpecialDimension::Grow);
        }
    }

    invalidate_layout();
}

void Splitter::did_layout()
{
    recompute_grabbables();
}

void Splitter::custom_layout()
{
    auto child_widgets = this->child_widgets();
    child_widgets.remove_all_matching([&](auto& widget) { return !widget.is_visible(); });

    if (!child_widgets.size())
        return;

    if (m_last_child_count > child_widgets.size()) {
        bool has_child_to_fill_space = false;
        for (auto& child : child_widgets) {
            if (child.max_size().primary_size_for_orientation(m_orientation).is_grow()) {
                has_child_to_fill_space = true;
                break;
            }
        }
        if (!has_child_to_fill_space)
            child_widgets.last().set_preferred_size(SpecialDimension::Grow);
    }
}

void Splitter::mouseup_event(MouseEvent& event)
{
    if (event.button() != MouseButton::Primary)
        return;
    m_resizing = false;
    m_first_resizee = nullptr;
    m_second_resizee = nullptr;
    if (!rect().contains(event.position()))
        set_override_cursor(Gfx::StandardCursor::None);
}

}