Fix tab dragging issues

This commit is contained in:
kit 2023-10-18 22:12:50 -04:00
parent 8c25a98fdf
commit 701c8d8323
6 changed files with 120 additions and 181 deletions

View file

@ -243,7 +243,7 @@
<signal name="tab_selected">
<param index="0" name="tab" type="int" />
<description>
Emitted when a tab is selected, even if it is the current tab.
Emitted when a tab is selected via click or script, even if it is the current tab.
</description>
</signal>
</signals>

View file

@ -4770,21 +4770,15 @@ void EditorNode::_dock_select_input(const Ref<InputEvent> &p_input) {
Ref<InputEventMouseButton> mb = me;
if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed() && dock_popup_selected_idx != nrect) {
Control *dock = dock_slot[dock_popup_selected_idx]->get_current_tab_control();
if (dock) {
dock_slot[dock_popup_selected_idx]->remove_child(dock);
}
dock_slot[nrect]->move_tab_from_tab_container(dock_slot[dock_popup_selected_idx], dock_slot[dock_popup_selected_idx]->get_current_tab(), dock_slot[nrect]->get_tab_count());
if (dock_slot[dock_popup_selected_idx]->get_tab_count() == 0) {
dock_slot[dock_popup_selected_idx]->hide();
} else {
dock_slot[dock_popup_selected_idx]->set_current_tab(0);
}
dock_slot[nrect]->add_child(dock);
dock_popup_selected_idx = nrect;
dock_slot[nrect]->set_current_tab(dock_slot[nrect]->get_tab_count() - 1);
dock_slot[nrect]->set_tab_title(dock_slot[nrect]->get_tab_count() - 1, TTRGET(dock->get_name()));
dock_slot[nrect]->show();
dock_select->queue_redraw();

View file

@ -321,7 +321,6 @@ void TabBar::gui_input(const Ref<InputEvent> &p_event) {
}
void TabBar::_shape(int p_tab) {
tabs.write[p_tab].xl_text = atr(tabs[p_tab].text);
tabs.write[p_tab].text_buf->clear();
tabs.write[p_tab].text_buf->set_width(-1);
if (tabs[p_tab].text_direction == Control::TEXT_DIRECTION_INHERITED) {
@ -330,7 +329,7 @@ void TabBar::_shape(int p_tab) {
tabs.write[p_tab].text_buf->set_direction((TextServer::Direction)tabs[p_tab].text_direction);
}
tabs.write[p_tab].text_buf->add_string(tabs[p_tab].xl_text, theme_cache.font, theme_cache.font_size, tabs[p_tab].language);
tabs.write[p_tab].text_buf->add_string(atr(tabs[p_tab].text), theme_cache.font, theme_cache.font_size, tabs[p_tab].language);
}
void TabBar::_notification(int p_what) {
@ -1118,7 +1117,26 @@ Variant TabBar::get_drag_data(const Point2 &p_point) {
if (!drag_to_rearrange_enabled) {
return Control::get_drag_data(p_point); // Allow stuff like TabContainer to override it.
}
return _handle_get_drag_data("tab_bar_tab", p_point);
}
bool TabBar::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
if (!drag_to_rearrange_enabled) {
return Control::can_drop_data(p_point, p_data); // Allow stuff like TabContainer to override it.
}
return _handle_can_drop_data("tab_bar_tab", p_point, p_data);
}
void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) {
if (!drag_to_rearrange_enabled) {
Control::drop_data(p_point, p_data); // Allow stuff like TabContainer to override it.
return;
}
_handle_drop_data("tab_bar_tab", p_point, p_data, callable_mp(this, &TabBar::move_tab), callable_mp(this, &TabBar::_move_tab_from));
}
Variant TabBar::_handle_get_drag_data(const String &p_type, const Point2 &p_point) {
int tab_over = get_tab_idx_at_point(p_point);
if (tab_over < 0) {
return Variant();
@ -1138,30 +1156,26 @@ Variant TabBar::get_drag_data(const Point2 &p_point) {
drag_preview->add_child(tf);
}
Label *label = memnew(Label(tabs[tab_over].xl_text));
Label *label = memnew(Label(get_tab_title(tab_over)));
drag_preview->add_child(label);
set_drag_preview(drag_preview);
Dictionary drag_data;
drag_data["type"] = "tab_element";
drag_data["tab_element"] = tab_over;
drag_data["type"] = p_type;
drag_data["tab_index"] = tab_over;
drag_data["from_path"] = get_path();
return drag_data;
}
bool TabBar::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
if (!drag_to_rearrange_enabled) {
return Control::can_drop_data(p_point, p_data); // Allow stuff like TabContainer to override it.
}
bool TabBar::_handle_can_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data) const {
Dictionary d = p_data;
if (!d.has("type")) {
return false;
}
if (String(d["type"]) == "tab_element") {
if (String(d["type"]) == p_type) {
NodePath from_path = d["from_path"];
NodePath to_path = get_path();
if (from_path == to_path) {
@ -1179,19 +1193,14 @@ bool TabBar::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
return false;
}
void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) {
if (!drag_to_rearrange_enabled) {
Control::drop_data(p_point, p_data); // Allow stuff like TabContainer to override it.
return;
}
void TabBar::_handle_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data, const Callable &p_move_tab_callback, const Callable &p_move_tab_from_other_callback) {
Dictionary d = p_data;
if (!d.has("type")) {
return;
}
if (String(d["type"]) == "tab_element") {
int tab_from_id = d["tab_element"];
if (String(d["type"]) == p_type) {
int tab_from_id = d["tab_index"];
int hover_now = get_tab_idx_at_point(p_point);
NodePath from_path = d["from_path"];
NodePath to_path = get_path();
@ -1216,7 +1225,7 @@ void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) {
hover_now = is_layout_rtl() ^ (p_point.x < x) ? 0 : get_tab_count() - 1;
}
move_tab(tab_from_id, hover_now);
p_move_tab_callback.call(tab_from_id, hover_now);
if (!is_tab_disabled(hover_now)) {
emit_signal(SNAME("active_tab_rearranged"), hover_now);
set_current_tab(hover_now);
@ -1242,37 +1251,44 @@ void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) {
hover_now = tabs.is_empty() || (is_layout_rtl() ^ (p_point.x < get_tab_rect(0).position.x)) ? 0 : get_tab_count();
}
Tab moving_tab = from_tabs->tabs[tab_from_id];
from_tabs->remove_tab(tab_from_id);
tabs.insert(hover_now, moving_tab);
if (tabs.size() > 1) {
if (current >= hover_now) {
current++;
}
if (previous >= hover_now) {
previous++;
}
}
if (!is_tab_disabled(hover_now)) {
set_current_tab(hover_now);
} else {
_update_cache();
queue_redraw();
}
update_minimum_size();
if (tabs.size() == 1) {
emit_signal(SNAME("tab_selected"), 0);
emit_signal(SNAME("tab_changed"), 0);
}
p_move_tab_from_other_callback.call(from_tabs, tab_from_id, hover_now);
}
}
}
}
void TabBar::_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index) {
Tab moving_tab = p_from_tabbar->tabs[p_from_index];
p_from_tabbar->remove_tab(p_from_index);
tabs.insert(p_to_index, moving_tab);
if (tabs.size() > 1) {
if (current >= p_to_index) {
current++;
}
if (previous >= p_to_index) {
previous++;
}
}
if (!is_tab_disabled(p_to_index)) {
set_current_tab(p_to_index);
if (tabs.size() == 1) {
_update_cache();
queue_redraw();
emit_signal(SNAME("tab_changed"), 0);
}
} else {
_update_cache();
queue_redraw();
if (tabs.size() == 1) {
emit_signal(SNAME("tab_changed"), 0);
}
}
update_minimum_size();
}
int TabBar::get_tab_idx_at_point(const Point2 &p_point) const {
int hover_now = -1;

View file

@ -55,7 +55,6 @@ public:
private:
struct Tab {
String text;
String xl_text;
String language;
Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED;
@ -167,8 +166,13 @@ protected:
Variant get_drag_data(const Point2 &p_point) override;
bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
void drop_data(const Point2 &p_point, const Variant &p_data) override;
void _move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index);
public:
Variant _handle_get_drag_data(const String &p_type, const Point2 &p_point);
bool _handle_can_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data) const;
void _handle_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data, const Callable &p_move_tab_callback, const Callable &p_move_tab_from_other_callback);
void add_tab(const String &p_str = "", const Ref<Texture2D> &p_icon = Ref<Texture2D>());
void set_tab_title(int p_tab, const String &p_title);

View file

@ -330,143 +330,59 @@ Vector<Control *> TabContainer::_get_tab_controls() const {
}
Variant TabContainer::_get_drag_data_fw(const Point2 &p_point, Control *p_from_control) {
if (!drag_to_rearrange_enabled) {
return Variant();
}
int tab_over = get_tab_idx_at_point(p_point);
if (tab_over < 0) {
return Variant();
}
HBoxContainer *drag_preview = memnew(HBoxContainer);
Ref<Texture2D> icon = get_tab_icon(tab_over);
if (!icon.is_null()) {
TextureRect *tf = memnew(TextureRect);
tf->set_texture(icon);
drag_preview->add_child(tf);
}
Label *label = memnew(Label(get_tab_title(tab_over)));
set_drag_preview(drag_preview);
drag_preview->add_child(label);
Dictionary drag_data;
drag_data["type"] = "tabc_element";
drag_data["tabc_element"] = tab_over;
drag_data["from_path"] = get_path();
return drag_data;
return tab_bar->_handle_get_drag_data("tab_container_tab", p_point);
}
bool TabContainer::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) const {
if (!drag_to_rearrange_enabled) {
return false;
}
Dictionary d = p_data;
if (!d.has("type")) {
return false;
}
if (String(d["type"]) == "tabc_element") {
NodePath from_path = d["from_path"];
NodePath to_path = get_path();
if (from_path == to_path) {
return true;
} else if (get_tabs_rearrange_group() != -1) {
// Drag and drop between other TabContainers.
Node *from_node = get_node(from_path);
TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
return true;
}
}
}
return false;
return tab_bar->_handle_can_drop_data("tab_container_tab", p_point, p_data);
}
void TabContainer::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) {
if (!drag_to_rearrange_enabled) {
return tab_bar->_handle_drop_data("tab_container_tab", p_point, p_data, callable_mp(this, &TabContainer::_drag_move_tab), callable_mp(this, &TabContainer::_drag_move_tab_from));
}
void TabContainer::_drag_move_tab(int p_from_index, int p_to_index) {
move_child(get_tab_control(p_from_index), get_tab_control(p_to_index)->get_index(false));
}
void TabContainer::_drag_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index) {
Node *parent = p_from_tabbar->get_parent();
if (!parent) {
return;
}
Dictionary d = p_data;
if (!d.has("type")) {
TabContainer *from_tab_container = Object::cast_to<TabContainer>(parent);
if (!from_tab_container) {
return;
}
move_tab_from_tab_container(from_tab_container, p_from_index, p_to_index);
}
if (String(d["type"]) == "tabc_element") {
int tab_from_id = d["tabc_element"];
int hover_now = get_tab_idx_at_point(p_point);
NodePath from_path = d["from_path"];
NodePath to_path = get_path();
void TabContainer::move_tab_from_tab_container(TabContainer *p_from, int p_from_index, int p_to_index) {
ERR_FAIL_NULL(p_from);
ERR_FAIL_INDEX(p_from_index, p_from->get_tab_count());
ERR_FAIL_INDEX(p_to_index, get_tab_count() + 1);
if (from_path == to_path) {
if (tab_from_id == hover_now) {
return;
}
// Get the tab properties before they get erased by the child removal.
String tab_title = p_from->get_tab_title(p_from_index);
Ref<Texture2D> tab_icon = p_from->get_tab_icon(p_from_index);
bool tab_disabled = p_from->is_tab_disabled(p_from_index);
Variant tab_metadata = p_from->get_tab_metadata(p_from_index);
// Drop the new tab to the left or right depending on where the target tab is being hovered.
if (hover_now != -1) {
Rect2 tab_rect = tab_bar->get_tab_rect(hover_now);
if (is_layout_rtl() ^ (p_point.x <= tab_rect.position.x + tab_rect.size.width / 2)) {
if (hover_now > tab_from_id) {
hover_now -= 1;
}
} else if (tab_from_id > hover_now) {
hover_now += 1;
}
} else {
hover_now = is_layout_rtl() ^ (p_point.x < tab_bar->get_tab_rect(0).position.x) ? 0 : get_tab_count() - 1;
}
Control *moving_tabc = p_from->get_tab_control(p_from_index);
p_from->remove_child(moving_tabc);
add_child(moving_tabc, true);
move_child(get_tab_control(tab_from_id), get_tab_control(hover_now)->get_index(false));
if (!is_tab_disabled(hover_now)) {
emit_signal(SNAME("active_tab_rearranged"), hover_now);
set_current_tab(hover_now);
}
set_tab_title(get_tab_count() - 1, tab_title);
set_tab_icon(get_tab_count() - 1, tab_icon);
set_tab_disabled(get_tab_count() - 1, tab_disabled);
set_tab_metadata(get_tab_count() - 1, tab_metadata);
} else if (get_tabs_rearrange_group() != -1) {
// Drag and drop between TabContainers.
Node *from_node = get_node(from_path);
TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
// Get the tab properties before they get erased by the child removal.
String tab_title = from_tabc->get_tab_title(tab_from_id);
Ref<Texture2D> tab_icon = from_tabc->get_tab_icon(tab_from_id);
bool tab_disabled = from_tabc->is_tab_disabled(tab_from_id);
Variant tab_metadata = from_tabc->get_tab_metadata(tab_from_id);
// Drop the new tab to the left or right depending on where the target tab is being hovered.
if (hover_now != -1) {
Rect2 tab_rect = tab_bar->get_tab_rect(hover_now);
if (is_layout_rtl() ^ (p_point.x > tab_rect.position.x + tab_rect.size.width / 2)) {
hover_now += 1;
}
} else {
hover_now = is_layout_rtl() ^ (p_point.x < tab_bar->get_tab_rect(0).position.x) ? 0 : get_tab_count();
}
Control *moving_tabc = from_tabc->get_tab_control(tab_from_id);
from_tabc->remove_child(moving_tabc);
add_child(moving_tabc, true);
set_tab_title(get_tab_count() - 1, tab_title);
set_tab_icon(get_tab_count() - 1, tab_icon);
set_tab_disabled(get_tab_count() - 1, tab_disabled);
set_tab_metadata(get_tab_count() - 1, tab_metadata);
move_child(moving_tabc, get_tab_control(hover_now)->get_index(false));
if (!is_tab_disabled(hover_now)) {
set_current_tab(hover_now);
}
}
}
if (p_to_index < 0 || p_to_index > get_tab_count() - 1) {
p_to_index = get_tab_count() - 1;
}
move_child(moving_tabc, get_tab_control(p_to_index)->get_index(false));
if (!is_tab_disabled(p_to_index)) {
set_current_tab(p_to_index);
}
}
@ -496,6 +412,10 @@ void TabContainer::_on_tab_button_pressed(int p_tab) {
emit_signal(SNAME("tab_button_pressed"), p_tab);
}
void TabContainer::_on_active_tab_rearranged(int p_tab) {
emit_signal(SNAME("active_tab_rearranged"), p_tab);
}
void TabContainer::_refresh_tab_names() {
Vector<Control *> controls = _get_tab_controls();
for (int i = 0; i < controls.size(); i++) {
@ -895,11 +815,11 @@ Popup *TabContainer::get_popup() const {
}
void TabContainer::set_drag_to_rearrange_enabled(bool p_enabled) {
drag_to_rearrange_enabled = p_enabled;
tab_bar->set_drag_to_rearrange_enabled(p_enabled);
}
bool TabContainer::get_drag_to_rearrange_enabled() const {
return drag_to_rearrange_enabled;
return tab_bar->get_drag_to_rearrange_enabled();
}
void TabContainer::set_tabs_rearrange_group(int p_group_id) {
@ -1038,6 +958,7 @@ TabContainer::TabContainer() {
tab_bar->connect("tab_hovered", callable_mp(this, &TabContainer::_on_tab_hovered));
tab_bar->connect("tab_selected", callable_mp(this, &TabContainer::_on_tab_selected));
tab_bar->connect("tab_button_pressed", callable_mp(this, &TabContainer::_on_tab_button_pressed));
tab_bar->connect("active_tab_rearranged", callable_mp(this, &TabContainer::_on_active_tab_rearranged));
connect("mouse_exited", callable_mp(this, &TabContainer::_on_mouse_exited));
}

View file

@ -43,7 +43,6 @@ class TabContainer : public Container {
bool all_tabs_in_front = false;
bool menu_hovered = false;
mutable ObjectID popup_obj_id;
bool drag_to_rearrange_enabled = false;
bool use_hidden_tabs_for_min_size = false;
bool theme_changing = false;
Vector<Control *> children_removing;
@ -97,10 +96,13 @@ class TabContainer : public Container {
void _on_tab_hovered(int p_tab);
void _on_tab_selected(int p_tab);
void _on_tab_button_pressed(int p_tab);
void _on_active_tab_rearranged(int p_tab);
Variant _get_drag_data_fw(const Point2 &p_point, Control *p_from_control);
bool _can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) const;
void _drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control);
void _drag_move_tab(int p_from_index, int p_to_index);
void _drag_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index);
protected:
virtual void gui_input(const Ref<InputEvent> &p_event) override;
@ -166,6 +168,8 @@ public:
void set_popup(Node *p_popup);
Popup *get_popup() const;
void move_tab_from_tab_container(TabContainer *p_from, int p_from_index, int p_to_index = -1);
void set_drag_to_rearrange_enabled(bool p_enabled);
bool get_drag_to_rearrange_enabled() const;
void set_tabs_rearrange_group(int p_group_id);