2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
2020-02-11 11:33:01 +01:00
|
|
|
#include <LibGUI/Painter.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Splitter.h>
|
|
|
|
|
#include <LibGUI/Window.h>
|
2020-02-11 11:33:01 +01:00
|
|
|
#include <LibGfx/Palette.h>
|
2019-03-30 13:53:30 +01:00
|
|
|
|
2021-01-02 16:30:13 -07:00
|
|
|
REGISTER_WIDGET(GUI, HorizontalSplitter)
|
|
|
|
|
REGISTER_WIDGET(GUI, VerticalSplitter)
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2020-02-23 12:07:13 +01:00
|
|
|
Splitter::Splitter(Orientation orientation)
|
|
|
|
|
: m_orientation(orientation)
|
2019-03-30 13:53:30 +01:00
|
|
|
{
|
2019-12-24 20:57:54 +01:00
|
|
|
set_background_role(ColorRole::Button);
|
2020-03-05 09:21:46 +01:00
|
|
|
set_layout<BoxLayout>(orientation);
|
2019-03-30 13:53:30 +01:00
|
|
|
set_fill_with_background_color(true);
|
2020-04-24 19:03:51 +02:00
|
|
|
layout()->set_spacing(3);
|
2019-03-30 13:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Splitter::~Splitter()
|
2019-03-30 13:53:30 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 11:33:01 +01:00
|
|
|
void Splitter::paint_event(PaintEvent& event)
|
|
|
|
|
{
|
|
|
|
|
Painter painter(*this);
|
|
|
|
|
painter.add_clip_rect(event.rect());
|
|
|
|
|
painter.fill_rect(m_grabbable_rect, palette().hover_highlight());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Splitter::resize_event(ResizeEvent& event)
|
|
|
|
|
{
|
2020-08-26 00:06:48 +02:00
|
|
|
Widget::resize_event(event);
|
2020-02-11 11:33:01 +01:00
|
|
|
m_grabbable_rect = {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 22:24:19 -06:00
|
|
|
void Splitter::override_cursor(bool do_override)
|
2019-03-30 14:04:53 +01:00
|
|
|
{
|
2020-08-26 22:24:19 -06:00
|
|
|
if (do_override) {
|
|
|
|
|
if (!m_overriding_cursor) {
|
2020-09-11 14:22:27 +02:00
|
|
|
set_override_cursor(m_orientation == Orientation::Horizontal ? Gfx::StandardCursor::ResizeColumn : Gfx::StandardCursor::ResizeRow);
|
2020-08-26 22:24:19 -06:00
|
|
|
m_overriding_cursor = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (m_overriding_cursor) {
|
2020-09-11 14:22:27 +02:00
|
|
|
set_override_cursor(Gfx::StandardCursor::None);
|
2020-08-26 22:24:19 -06:00
|
|
|
m_overriding_cursor = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-30 14:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Splitter::leave_event(Core::Event&)
|
2019-03-30 14:04:53 +01:00
|
|
|
{
|
2019-04-02 02:34:09 +02:00
|
|
|
if (!m_resizing)
|
2020-08-26 22:24:19 -06:00
|
|
|
override_cursor(false);
|
2020-02-11 11:33:01 +01:00
|
|
|
if (!m_grabbable_rect.is_empty()) {
|
|
|
|
|
m_grabbable_rect = {};
|
|
|
|
|
update();
|
|
|
|
|
}
|
2019-03-30 14:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
bool Splitter::get_resize_candidates_at(const Gfx::IntPoint& position, Widget*& first, Widget*& second)
|
2019-03-30 13:53:30 +01:00
|
|
|
{
|
2020-02-11 10:56:26 +01:00
|
|
|
int x_or_y = position.primary_offset_for_orientation(m_orientation);
|
2020-10-24 15:22:43 -06:00
|
|
|
Widget* previous_widget = nullptr;
|
|
|
|
|
bool found_candidates = false;
|
|
|
|
|
for_each_child_widget([&](auto& child_widget) {
|
|
|
|
|
if (!child_widget.is_visible()) {
|
|
|
|
|
// We need to skip over widgets that are not visible as they
|
|
|
|
|
// are not necessarily in the correct location (anymore)
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
}
|
|
|
|
|
if (!previous_widget) {
|
|
|
|
|
previous_widget = &child_widget;
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
}
|
2020-04-24 18:55:29 +02:00
|
|
|
|
2020-10-24 15:22:43 -06:00
|
|
|
if (x_or_y > previous_widget->content_rect().last_edge_for_orientation(m_orientation)
|
|
|
|
|
&& x_or_y <= child_widget.content_rect().first_edge_for_orientation(m_orientation)) {
|
|
|
|
|
first = previous_widget;
|
|
|
|
|
second = &child_widget;
|
|
|
|
|
found_candidates = true;
|
|
|
|
|
return IterationDecision::Break;
|
2020-04-24 18:55:29 +02:00
|
|
|
}
|
2020-10-24 15:22:43 -06:00
|
|
|
|
2020-10-24 19:00:27 -06:00
|
|
|
previous_widget = &child_widget;
|
2020-10-24 15:22:43 -06:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
return found_candidates;
|
2020-02-11 10:56:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Splitter::mousedown_event(MouseEvent& event)
|
|
|
|
|
{
|
|
|
|
|
if (event.button() != MouseButton::Left)
|
|
|
|
|
return;
|
|
|
|
|
m_resizing = true;
|
|
|
|
|
|
|
|
|
|
Widget* first { nullptr };
|
|
|
|
|
Widget* second { nullptr };
|
2020-02-13 20:00:01 +01:00
|
|
|
if (!get_resize_candidates_at(event.position(), first, second))
|
|
|
|
|
return;
|
2020-02-11 10:56:26 +01:00
|
|
|
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 16:26:13 -06:00
|
|
|
m_first_resizee = *first;
|
|
|
|
|
m_second_resizee = *second;
|
2020-02-11 10:56:26 +01:00
|
|
|
m_first_resizee_start_size = first->size();
|
|
|
|
|
m_second_resizee_start_size = second->size();
|
2019-03-30 13:53:30 +01:00
|
|
|
m_resize_origin = event.position();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 11:33:01 +01:00
|
|
|
void Splitter::recompute_grabbable_rect(const Widget& first, const Widget& second)
|
|
|
|
|
{
|
2020-04-24 18:55:29 +02:00
|
|
|
auto first_edge = first.content_rect().primary_offset_for_orientation(m_orientation) + first.content_rect().primary_size_for_orientation(m_orientation);
|
|
|
|
|
auto second_edge = second.content_rect().primary_offset_for_orientation(m_orientation);
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntRect rect;
|
2020-02-11 11:33:01 +01:00
|
|
|
rect.set_primary_offset_for_orientation(m_orientation, first_edge);
|
|
|
|
|
rect.set_primary_size_for_orientation(m_orientation, second_edge - first_edge);
|
2020-04-24 18:55:29 +02:00
|
|
|
rect.set_secondary_offset_for_orientation(m_orientation, first.content_rect().secondary_offset_for_orientation(m_orientation));
|
|
|
|
|
rect.set_secondary_size_for_orientation(m_orientation, first.content_rect().secondary_size_for_orientation(m_orientation));
|
|
|
|
|
|
2020-02-11 11:33:01 +01:00
|
|
|
if (m_grabbable_rect != rect) {
|
|
|
|
|
m_grabbable_rect = rect;
|
|
|
|
|
update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Splitter::mousemove_event(MouseEvent& event)
|
2019-03-30 13:53:30 +01:00
|
|
|
{
|
2020-02-11 11:33:01 +01:00
|
|
|
if (!m_resizing) {
|
|
|
|
|
Widget* first { nullptr };
|
|
|
|
|
Widget* second { nullptr };
|
2020-08-26 22:24:19 -06:00
|
|
|
if (!get_resize_candidates_at(event.position(), first, second)) {
|
|
|
|
|
override_cursor(false);
|
2020-02-13 20:00:01 +01:00
|
|
|
return;
|
2020-08-26 22:24:19 -06:00
|
|
|
}
|
2020-02-11 11:33:01 +01:00
|
|
|
recompute_grabbable_rect(*first, *second);
|
2020-08-26 22:24:19 -06:00
|
|
|
override_cursor(m_grabbable_rect.contains(event.position()));
|
2019-03-30 13:53:30 +01:00
|
|
|
return;
|
2020-02-11 11:33:01 +01:00
|
|
|
}
|
2019-03-30 13:53:30 +01:00
|
|
|
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;
|
2019-06-07 11:46:02 +02:00
|
|
|
return;
|
2019-03-30 13:53:30 +01:00
|
|
|
}
|
|
|
|
|
int minimum_size = 0;
|
|
|
|
|
auto new_first_resizee_size = m_first_resizee_start_size;
|
|
|
|
|
auto new_second_resizee_size = m_second_resizee_start_size;
|
|
|
|
|
|
2019-07-20 22:17:46 +02:00
|
|
|
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));
|
2019-03-30 13:53:30 +01:00
|
|
|
|
2019-07-20 22:17:46 +02:00
|
|
|
if (new_first_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
|
|
|
|
|
int correction = 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) < minimum_size) {
|
|
|
|
|
int correction = 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);
|
2019-03-30 13:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
if (m_orientation == Orientation::Horizontal) {
|
|
|
|
|
m_first_resizee->set_fixed_width(new_first_resizee_size.width());
|
|
|
|
|
m_second_resizee->set_fixed_width(-1);
|
|
|
|
|
} else {
|
|
|
|
|
m_first_resizee->set_fixed_height(new_first_resizee_size.height());
|
|
|
|
|
m_second_resizee->set_fixed_height(-1);
|
|
|
|
|
}
|
2019-07-27 09:36:21 +02:00
|
|
|
|
2019-03-30 13:53:30 +01:00
|
|
|
invalidate_layout();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 11:33:01 +01:00
|
|
|
void Splitter::did_layout()
|
|
|
|
|
{
|
|
|
|
|
if (m_first_resizee && m_second_resizee)
|
|
|
|
|
recompute_grabbable_rect(*m_first_resizee, *m_second_resizee);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Splitter::mouseup_event(MouseEvent& event)
|
2019-03-30 13:53:30 +01:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
if (event.button() != MouseButton::Left)
|
2019-03-30 13:53:30 +01:00
|
|
|
return;
|
|
|
|
|
m_resizing = false;
|
2020-02-11 11:52:34 +01:00
|
|
|
m_first_resizee = nullptr;
|
|
|
|
|
m_second_resizee = nullptr;
|
2019-04-02 02:34:09 +02:00
|
|
|
if (!rect().contains(event.position()))
|
2020-09-11 14:22:27 +02:00
|
|
|
set_override_cursor(Gfx::StandardCursor::None);
|
2020-02-02 15:07:41 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-30 13:53:30 +01:00
|
|
|
}
|