Refactor Bottom Panel to be a TabContainer

This commit is contained in:
Logan Detrick 2025-07-07 17:09:23 -07:00
parent 250ef8dc32
commit e2caff9a06
18 changed files with 272 additions and 358 deletions

View file

@ -31,6 +31,7 @@
#include "editor_bottom_panel.h"
#include "editor/debugger/editor_debugger_node.h"
#include "editor/docks/editor_dock_manager.h"
#include "editor/editor_node.h"
#include "editor/editor_string_names.h"
#include "editor/gui/editor_toaster.h"
@ -39,7 +40,6 @@
#include "editor/themes/editor_scale.h"
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
#include "scene/gui/scroll_container.h"
#include "scene/gui/split_container.h"
void EditorBottomPanel::_notification(int p_what) {
@ -47,297 +47,167 @@ void EditorBottomPanel::_notification(int p_what) {
case NOTIFICATION_THEME_CHANGED: {
pin_button->set_button_icon(get_editor_theme_icon(SNAME("Pin")));
expand_button->set_button_icon(get_editor_theme_icon(SNAME("ExpandBottomDock")));
left_button->set_button_icon(get_editor_theme_icon(SNAME("Back")));
right_button->set_button_icon(get_editor_theme_icon(SNAME("Forward")));
} break;
case NOTIFICATION_TRANSLATION_CHANGED:
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
if (is_layout_rtl()) {
bottom_hbox->move_child(left_button, button_scroll->get_index() + 1);
bottom_hbox->move_child(right_button, 0);
} else {
bottom_hbox->move_child(right_button, button_scroll->get_index() + 1);
bottom_hbox->move_child(left_button, 0);
}
} break;
}
}
void EditorBottomPanel::_switch_by_control(bool p_visible, Control *p_control, bool p_ignore_lock) {
for (int i = 0; i < items.size(); i++) {
if (items[i].control == p_control) {
_switch_to_item(p_visible, i, p_ignore_lock);
return;
}
}
void EditorBottomPanel::_on_tab_changed(int p_idx) {
callable_mp(this, &EditorBottomPanel::_repaint).call_deferred();
}
void EditorBottomPanel::_scroll(bool p_right) {
HScrollBar *h_scroll = button_scroll->get_h_scroll_bar();
if (Input::get_singleton()->is_key_pressed(Key::CTRL)) {
h_scroll->set_value(p_right ? h_scroll->get_max() : 0);
} else if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * (p_right ? 1 : -1));
void EditorBottomPanel::_theme_changed() {
Ref<StyleBox> bottom_tabbar_style = get_theme_stylebox("tabbar_background", "BottomPanel")->duplicate();
bottom_tabbar_style->set_content_margin(SIDE_RIGHT, bottom_hbox->get_minimum_size().x + bottom_tabbar_style->get_content_margin(SIDE_LEFT));
add_theme_style_override("tabbar_background", bottom_tabbar_style);
if (get_current_tab() == -1) {
// Hide panel when not showing anything.
remove_theme_style_override(SceneStringName(panel));
} else {
h_scroll->set_value(h_scroll->get_value() + (h_scroll->get_page() * 0.5) * (p_right ? 1 : -1));
add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("BottomPanel"), EditorStringName(EditorStyles)));
}
}
void EditorBottomPanel::_update_scroll_buttons() {
bool show_arrows = button_hbox->get_size().width > button_scroll->get_size().width;
left_button->set_visible(show_arrows);
right_button->set_visible(show_arrows);
if (show_arrows) {
_update_disabled_buttons();
}
}
void EditorBottomPanel::_update_disabled_buttons() {
HScrollBar *h_scroll = button_scroll->get_h_scroll_bar();
left_button->set_disabled(h_scroll->get_value() == 0);
right_button->set_disabled(h_scroll->get_value() + h_scroll->get_page() == h_scroll->get_max());
}
void EditorBottomPanel::_ensure_control_visible(ObjectID p_id) {
Control *c = ObjectDB::get_instance<Control>(p_id);
if (!c) {
return;
}
button_scroll->ensure_control_visible(c);
}
void EditorBottomPanel::_switch_to_item(bool p_visible, int p_idx, bool p_ignore_lock) {
ERR_FAIL_INDEX(p_idx, items.size());
if (items[p_idx].control->is_visible() == p_visible) {
void EditorBottomPanel::_repaint() {
bool panel_collapsed = get_current_tab() == -1;
if (panel_collapsed == (get_previous_tab() == -1)) {
return;
}
SplitContainer *center_split = Object::cast_to<SplitContainer>(get_parent());
ERR_FAIL_NULL(center_split);
if (p_visible) {
if (!p_ignore_lock && lock_panel_switching && pin_button->is_visible()) {
return;
}
center_split->set_dragger_visibility(panel_collapsed ? SplitContainer::DRAGGER_HIDDEN : SplitContainer::DRAGGER_VISIBLE);
center_split->set_collapsed(panel_collapsed);
for (int i = 0; i < items.size(); i++) {
items[i].button->set_pressed_no_signal(i == p_idx);
items[i].control->set_visible(i == p_idx);
}
pin_button->set_visible(!panel_collapsed);
expand_button->set_visible(!panel_collapsed);
if (expand_button->is_pressed()) {
EditorNode::get_top_split()->set_visible(panel_collapsed);
}
center_split->set_dragger_visibility(SplitContainer::DRAGGER_VISIBLE);
center_split->set_collapsed(false);
pin_button->show();
_theme_changed();
}
expand_button->show();
if (expand_button->is_pressed()) {
EditorNode::get_top_split()->hide();
}
callable_mp(this, &EditorBottomPanel::_ensure_control_visible).call_deferred(items[p_idx].button->get_instance_id());
} else {
items[p_idx].button->set_pressed_no_signal(false);
items[p_idx].control->set_visible(false);
center_split->set_dragger_visibility(SplitContainer::DRAGGER_HIDDEN);
center_split->set_collapsed(true);
pin_button->hide();
void EditorBottomPanel::save_layout_to_config(Ref<ConfigFile> p_config_file, const String &p_section) const {
p_config_file->set_value(p_section, "selected_bottom_panel_item", get_current_tab() != -1 ? Variant(get_current_tab()) : Variant());
}
expand_button->hide();
if (expand_button->is_pressed()) {
EditorNode::get_top_split()->show();
void EditorBottomPanel::load_layout_from_config(Ref<ConfigFile> p_config_file, const String &p_section) {
if (p_config_file->has_section_key(p_section, "selected_bottom_panel_item")) {
int stored_current_tab = p_config_file->get_value(p_section, "selected_bottom_panel_item");
if (stored_current_tab >= 0 && stored_current_tab < get_tab_bar()->get_tab_count()) {
// Make sure we don't try to open contextual editors which are not enabled in the current context.
if (!get_tab_bar()->is_tab_hidden(stored_current_tab)) {
set_current_tab(stored_current_tab);
return;
}
}
}
last_opened_control = items[p_idx].control;
// If there is no active tab we need to collapse the panel.
set_current_tab(-1);
}
void EditorBottomPanel::make_item_visible(Control *p_item, bool p_visible, bool p_ignore_lock) {
// Don't allow changing tabs involuntarily when tabs are locked.
if (!p_ignore_lock && lock_panel_switching && pin_button->is_visible()) {
return;
}
p_item->set_visible(p_visible);
}
void EditorBottomPanel::move_item_to_end(Control *p_item) {
move_child(p_item, -1);
}
void EditorBottomPanel::hide_bottom_panel() {
set_current_tab(-1);
}
void EditorBottomPanel::toggle_last_opened_bottom_panel() {
set_current_tab(get_current_tab() == -1 ? get_previous_tab() : -1);
}
void EditorBottomPanel::shortcut_input(const Ref<InputEvent> &p_event) {
if (p_event.is_null() || !p_event->is_pressed() || p_event->is_echo()) {
return;
}
for (uint32_t i = 0; i < dock_shortcuts.size(); i++) {
if (dock_shortcuts[i].is_valid() && dock_shortcuts[i]->matches_event(p_event)) {
bottom_docks[i]->set_visible(!bottom_docks[i]->is_visible());
break;
}
}
}
void EditorBottomPanel::_pin_button_toggled(bool p_pressed) {
lock_panel_switching = p_pressed;
}
void EditorBottomPanel::_expand_button_toggled(bool p_pressed) {
EditorNode::get_top_split()->set_visible(!p_pressed);
}
bool EditorBottomPanel::_button_drag_hover(const Vector2 &, const Variant &, Button *p_button, Control *p_control) {
if (!p_button->is_pressed()) {
_switch_by_control(true, p_control, true);
}
return false;
}
void EditorBottomPanel::save_layout_to_config(Ref<ConfigFile> p_config_file, const String &p_section) const {
int selected_item_idx = -1;
for (int i = 0; i < items.size(); i++) {
if (items[i].button->is_pressed()) {
selected_item_idx = i;
break;
}
}
if (selected_item_idx != -1) {
p_config_file->set_value(p_section, "selected_bottom_panel_item", selected_item_idx);
} else {
p_config_file->set_value(p_section, "selected_bottom_panel_item", Variant());
}
}
void EditorBottomPanel::load_layout_from_config(Ref<ConfigFile> p_config_file, const String &p_section) {
bool has_active_tab = false;
if (p_config_file->has_section_key(p_section, "selected_bottom_panel_item")) {
int selected_item_idx = p_config_file->get_value(p_section, "selected_bottom_panel_item");
if (selected_item_idx >= 0 && selected_item_idx < items.size()) {
// Make sure we don't try to open contextual editors which are not enabled in the current context.
if (items[selected_item_idx].button->is_visible()) {
_switch_to_item(true, selected_item_idx);
has_active_tab = true;
}
}
}
// If there is no active tab we need to collapse the panel.
if (!has_active_tab) {
items[0].control->show(); // _switch_to_item() can collapse only visible tabs.
_switch_to_item(false, 0);
}
}
Button *EditorBottomPanel::add_item(String p_text, Control *p_item, const Ref<Shortcut> &p_shortcut, bool p_at_front) {
Button *tb = memnew(Button);
tb->set_theme_type_variation("BottomPanelButton");
tb->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_switch_by_control).bind(p_item, true));
tb->set_drag_forwarding(Callable(), callable_mp(this, &EditorBottomPanel::_button_drag_hover).bind(tb, p_item), Callable());
tb->set_text(p_text);
tb->set_shortcut(p_shortcut);
tb->set_toggle_mode(true);
tb->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
item_vbox->add_child(p_item);
bottom_hbox->move_to_front();
button_hbox->add_child(tb);
if (p_at_front) {
button_hbox->move_child(tb, 0);
}
p_item->set_v_size_flags(Control::SIZE_EXPAND_FILL);
p_item->hide();
BottomPanelItem bpi;
bpi.button = tb;
bpi.control = p_item;
bpi.name = p_text;
if (p_at_front) {
items.insert(0, bpi);
} else {
items.push_back(bpi);
}
return tb;
}
void EditorBottomPanel::remove_item(Control *p_item) {
bool was_visible = false;
for (int i = 0; i < items.size(); i++) {
if (items[i].control == p_item) {
if (p_item->is_visible_in_tree()) {
was_visible = true;
}
item_vbox->remove_child(items[i].control);
button_hbox->remove_child(items[i].button);
memdelete(items[i].button);
items.remove_at(i);
break;
}
}
if (was_visible) {
// Open the first panel to ensure that if the removed dock was visible, the bottom
// panel will not collapse.
_switch_to_item(true, 0, true);
} else if (last_opened_control == p_item) {
// When a dock is removed by plugins, it might not have been visible, and it
// might have been the last_opened_control. We need to make sure to reset the last opened control.
last_opened_control = items[0].control;
}
}
void EditorBottomPanel::make_item_visible(Control *p_item, bool p_visible, bool p_ignore_lock) {
_switch_by_control(p_visible, p_item, p_ignore_lock);
}
void EditorBottomPanel::move_item_to_end(Control *p_item) {
for (int i = 0; i < items.size(); i++) {
if (items[i].control == p_item) {
items[i].button->move_to_front();
SWAP(items.write[i], items.write[items.size() - 1]);
break;
}
}
}
void EditorBottomPanel::hide_bottom_panel() {
for (int i = 0; i < items.size(); i++) {
if (items[i].control->is_visible()) {
_switch_to_item(false, i);
break;
}
}
}
void EditorBottomPanel::toggle_last_opened_bottom_panel() {
// Select by control instead of index, so that the last bottom panel is opened correctly
// if it's been reordered since.
if (last_opened_control) {
_switch_by_control(!last_opened_control->is_visible(), last_opened_control, true);
} else {
// Open the first panel in the list if no panel was opened this session.
_switch_to_item(true, 0, true);
}
}
void EditorBottomPanel::set_expanded(bool p_expanded) {
expand_button->set_pressed(p_expanded);
}
void EditorBottomPanel::_expand_button_toggled(bool p_pressed) {
EditorNode::get_top_split()->set_visible(!p_pressed);
}
Button *EditorBottomPanel::add_item(String p_text, Control *p_item, const Ref<Shortcut> &p_shortcut, bool p_at_front) {
p_item->set_name(p_text);
add_child(p_item);
if (p_at_front) {
move_child(p_item, 0);
}
bottom_docks.push_back(p_item);
dock_shortcuts.push_back(p_shortcut);
set_process_shortcut_input(is_processing_shortcut_input() || p_shortcut.is_valid());
// Still return a dummy button for compatibility reasons.
Button *tb = memnew(Button);
tb->set_toggle_mode(true);
tb->connect(SceneStringName(visibility_changed), callable_mp(this, &EditorBottomPanel::_on_button_visibility_changed).bind(tb, p_item));
legacy_buttons.push_back(tb);
return tb;
}
void EditorBottomPanel::remove_item(Control *p_item) {
int item_idx = bottom_docks.find(p_item);
ERR_FAIL_COND_MSG(item_idx == -1, vformat("Cannot remove unknown dock \"%s\" from the bottom panel.", p_item->get_name()));
bottom_docks.remove_at(item_idx);
dock_shortcuts.remove_at(item_idx);
legacy_buttons[item_idx]->queue_free();
legacy_buttons.remove_at(item_idx);
remove_child(p_item);
}
void EditorBottomPanel::_on_button_visibility_changed(Button *p_button, Control *p_control) {
int tab_index = get_tab_idx_from_control(p_control);
if (tab_index == -1) {
return;
}
// Ignore the tab if the button is hidden.
get_tab_bar()->set_tab_hidden(tab_index, !p_button->is_visible());
}
EditorBottomPanel::EditorBottomPanel() {
item_vbox = memnew(VBoxContainer);
add_child(item_vbox);
get_tab_bar()->connect(SceneStringName(gui_input), callable_mp(EditorDockManager::get_singleton(), &EditorDockManager::_dock_container_gui_input).bind(this));
get_tab_bar()->connect("tab_changed", callable_mp(this, &EditorBottomPanel::_on_tab_changed));
set_tabs_position(TabPosition::POSITION_BOTTOM);
set_deselect_enabled(true);
bottom_hbox = memnew(HBoxContainer);
bottom_hbox->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the "Expand Bottom Dock" icon.
item_vbox->add_child(bottom_hbox);
left_button = memnew(Button);
left_button->set_tooltip_text(TTRC("Scroll Left\nHold Ctrl to scroll to the begin.\nHold Shift to scroll one page."));
left_button->set_accessibility_name(TTRC("Scroll Left"));
left_button->set_theme_type_variation("BottomPanelButton");
left_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
left_button->connect(SceneStringName(pressed), callable_mp(this, &EditorBottomPanel::_scroll).bind(false));
bottom_hbox->add_child(left_button);
left_button->hide();
button_scroll = memnew(ScrollContainer);
button_scroll->set_h_size_flags(Control::SIZE_EXPAND_FILL);
button_scroll->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_SHOW_NEVER);
button_scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
button_scroll->get_h_scroll_bar()->connect(CoreStringName(changed), callable_mp(this, &EditorBottomPanel::_update_scroll_buttons), CONNECT_DEFERRED);
button_scroll->get_h_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(this, &EditorBottomPanel::_update_disabled_buttons).unbind(1), CONNECT_DEFERRED);
bottom_hbox->add_child(button_scroll);
right_button = memnew(Button);
right_button->set_tooltip_text(TTRC("Scroll Right\nHold Ctrl to scroll to the end.\nHold Shift to scroll one page."));
right_button->set_accessibility_name(TTRC("Scroll Right"));
right_button->set_theme_type_variation("BottomPanelButton");
right_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
right_button->connect(SceneStringName(pressed), callable_mp(this, &EditorBottomPanel::_scroll).bind(true));
bottom_hbox->add_child(right_button);
right_button->hide();
callable_mp(this, &EditorBottomPanel::_update_scroll_buttons).call_deferred();
button_hbox = memnew(HBoxContainer);
button_hbox->set_h_size_flags(Control::SIZE_EXPAND | Control::SIZE_SHRINK_BEGIN);
button_scroll->add_child(button_hbox);
bottom_hbox->set_anchors_and_offsets_preset(Control::PRESET_RIGHT_WIDE);
bottom_hbox->set_h_grow_direction(Control::GROW_DIRECTION_END);
get_tab_bar()->add_child(bottom_hbox);
editor_toaster = memnew(EditorToaster);
bottom_hbox->add_child(editor_toaster);
@ -355,7 +225,7 @@ EditorBottomPanel::EditorBottomPanel() {
pin_button = memnew(Button);
bottom_hbox->add_child(pin_button);
pin_button->hide();
pin_button->set_theme_type_variation("FlatMenuButton");
pin_button->set_theme_type_variation("BottomPanelButton");
pin_button->set_toggle_mode(true);
pin_button->set_tooltip_text(TTRC("Pin Bottom Panel Switching"));
pin_button->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_pin_button_toggled));
@ -363,9 +233,17 @@ EditorBottomPanel::EditorBottomPanel() {
expand_button = memnew(Button);
bottom_hbox->add_child(expand_button);
expand_button->hide();
expand_button->set_theme_type_variation("FlatMenuButton");
expand_button->set_theme_type_variation("BottomPanelButton");
expand_button->set_toggle_mode(true);
expand_button->set_accessibility_name(TTRC("Expand Bottom Panel"));
expand_button->set_shortcut(ED_SHORTCUT_AND_COMMAND("editor/bottom_panel_expand", TTRC("Expand Bottom Panel"), KeyModifierMask::SHIFT | Key::F12));
expand_button->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_expand_button_toggled));
callable_mp(this, &EditorBottomPanel::_repaint).call_deferred();
}
EditorBottomPanel::~EditorBottomPanel() {
for (Button *b : legacy_buttons) {
memdelete(b);
}
}

View file

@ -30,52 +30,38 @@
#pragma once
#include "scene/gui/panel_container.h"
#include "scene/gui/tab_container.h"
class Button;
class ConfigFile;
class EditorToaster;
class HBoxContainer;
class VBoxContainer;
class ScrollContainer;
class EditorBottomPanel : public PanelContainer {
GDCLASS(EditorBottomPanel, PanelContainer);
class EditorBottomPanel : public TabContainer {
GDCLASS(EditorBottomPanel, TabContainer);
struct BottomPanelItem {
String name;
Control *control = nullptr;
Button *button = nullptr;
};
Vector<BottomPanelItem> items;
bool lock_panel_switching = false;
VBoxContainer *item_vbox = nullptr;
HBoxContainer *bottom_hbox = nullptr;
Button *left_button = nullptr;
Button *right_button = nullptr;
ScrollContainer *button_scroll = nullptr;
HBoxContainer *button_hbox = nullptr;
EditorToaster *editor_toaster = nullptr;
Button *pin_button = nullptr;
Button *expand_button = nullptr;
Control *last_opened_control = nullptr;
void _switch_by_control(bool p_visible, Control *p_control, bool p_ignore_lock = false);
void _switch_to_item(bool p_visible, int p_idx, bool p_ignore_lock = false);
bool lock_panel_switching = false;
LocalVector<Control *> bottom_docks;
LocalVector<Ref<Shortcut>> dock_shortcuts;
LocalVector<Button *> legacy_buttons;
void _on_button_visibility_changed(Button *p_button, Control *p_control);
void _repaint();
void _on_tab_changed(int p_idx);
void _pin_button_toggled(bool p_pressed);
void _expand_button_toggled(bool p_pressed);
void _scroll(bool p_right);
void _update_scroll_buttons();
void _update_disabled_buttons();
void _ensure_control_visible(ObjectID p_id);
bool _button_drag_hover(const Vector2 &, const Variant &, Button *p_button, Control *p_control);
protected:
void _notification(int p_what);
virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
public:
void save_layout_to_config(Ref<ConfigFile> p_config_file, const String &p_section) const;
void load_layout_from_config(Ref<ConfigFile> p_config_file, const String &p_section);
@ -87,6 +73,8 @@ public:
void hide_bottom_panel();
void toggle_last_opened_bottom_panel();
void set_expanded(bool p_expanded);
void _theme_changed();
EditorBottomPanel();
~EditorBottomPanel();
};