Skip to content

Commit

Permalink
Add theme contexts to various parts of the editor
Browse files Browse the repository at this point in the history
This change defines additional theme contexts for editor
branches to prevent theme leaking between the default
theme, the project theme, and the editor theme.

- Both editor window and EditorNode define an editor-specific
context with the editor theme and the default theme.
- The 2D viewport defines a project-specific context with
the project theme and the default theme.
- Theme editor preview tabs define the default-only context
with the default theme.

Additionally, the default theme context now only includes
the project theme for running projects (both export and debug).
This prevents the project theme from leaking into the editor.

This commit also does a little clean up on the theming aspects
of the EditorNode.
  • Loading branch information
YuriSizov committed Sep 6, 2023
1 parent 58126e4 commit 512182f
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 150 deletions.
279 changes: 141 additions & 138 deletions editor/editor_node.cpp

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions editor/editor_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ class EditorNode : public Node {
bool is_main_screen_editing = false;

PanelContainer *scene_root_parent = nullptr;
Control *theme_base = nullptr;
Control *gui_base = nullptr;
VBoxContainer *main_vbox = nullptr;
OptionButton *renderer = nullptr;
Expand Down Expand Up @@ -525,6 +524,7 @@ class EditorNode : public Node {
static void _resource_saved(Ref<Resource> p_resource, const String &p_path);
static void _resource_loaded(Ref<Resource> p_resource, const String &p_path);

void _update_theme(bool p_skip_creation = false);
void _build_icon_type_cache();
void _enable_pending_addons();

Expand Down Expand Up @@ -867,7 +867,6 @@ class EditorNode : public Node {
Error export_preset(const String &p_preset, const String &p_path, bool p_debug, bool p_pack_only);

Control *get_gui_base() { return gui_base; }
Control *get_theme_base() { return gui_base->get_parent_control(); }

void save_scene_to_path(String p_file, bool p_with_preview = true) {
if (p_with_preview) {
Expand Down
4 changes: 3 additions & 1 deletion editor/editor_resource_preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "editor/editor_paths.h"
#include "editor/editor_scale.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
#include "scene/resources/image_texture.h"

bool EditorResourcePreviewGenerator::handles(const String &p_type) const {
Expand Down Expand Up @@ -341,7 +342,8 @@ void EditorResourcePreview::_thread() {

void EditorResourcePreview::_update_thumbnail_sizes() {
if (small_thumbnail_size == -1) {
small_thumbnail_size = EditorNode::get_singleton()->get_theme_base()->get_editor_theme_icon(SNAME("Object"))->get_width(); // Kind of a workaround to retrieve the default icon size
// Kind of a workaround to retrieve the default icon size.
small_thumbnail_size = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Object"), EditorStringName(EditorIcons))->get_width();
}
}

Expand Down
10 changes: 6 additions & 4 deletions editor/plugins/script_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,8 @@ void ScriptEditor::_notification(int p_what) {
case NOTIFICATION_TRANSLATION_CHANGED:
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
case NOTIFICATION_THEME_CHANGED: {
tab_container->add_theme_style_override("panel", get_theme_stylebox(SNAME("ScriptEditor"), EditorStringName(EditorStyles)));

help_search->set_icon(get_editor_theme_icon(SNAME("HelpSearch")));
site_search->set_icon(get_editor_theme_icon(SNAME("ExternalLink")));

Expand All @@ -1628,7 +1630,7 @@ void ScriptEditor::_notification(int p_what) {
filter_scripts->set_right_icon(get_editor_theme_icon(SNAME("Search")));
filter_methods->set_right_icon(get_editor_theme_icon(SNAME("Search")));

filename->add_theme_style_override("normal", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("normal"), SNAME("LineEdit")));
filename->add_theme_style_override("normal", get_theme_stylebox(SNAME("normal"), SNAME("LineEdit")));

recent_scripts->reset_size();

Expand All @@ -1639,6 +1641,9 @@ void ScriptEditor::_notification(int p_what) {
} break;

case NOTIFICATION_READY: {
// Can't set own styles in NOTIFICATION_THEME_CHANGED, so for now this will do.
add_theme_style_override("panel", get_theme_stylebox(SNAME("ScriptEditorPanel"), SNAME("EditorStyles")));

get_tree()->connect("tree_changed", callable_mp(this, &ScriptEditor::_tree_changed));
InspectorDock::get_singleton()->connect("request_help", callable_mp(this, &ScriptEditor::_help_class_open));
EditorNode::get_singleton()->connect("request_help_search", callable_mp(this, &ScriptEditor::_help_search));
Expand Down Expand Up @@ -4139,9 +4144,6 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {

ScriptServer::edit_request_func = _open_script_request;

add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("ScriptEditorPanel"), EditorStringName(EditorStyles)));
tab_container->add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("ScriptEditor"), EditorStringName(EditorStyles)));

Ref<EditorJSONSyntaxHighlighter> json_syntax_highlighter;
json_syntax_highlighter.instantiate();
register_syntax_highlighter(json_syntax_highlighter);
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/theme_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ void ThemeItemEditorDialog::_dialog_about_to_show() {
import_default_theme_items->reset_item_tree();

import_editor_theme_items->set_edited_theme(edited_theme);
import_editor_theme_items->set_base_theme(EditorNode::get_singleton()->get_theme_base()->get_theme());
import_editor_theme_items->set_base_theme(EditorNode::get_singleton()->get_editor_theme());
import_editor_theme_items->reset_item_tree();

import_other_theme_items->set_edited_theme(edited_theme);
Expand Down
13 changes: 9 additions & 4 deletions editor/plugins/theme_editor_preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,14 @@ void ThemeEditorPreview::_notification(int p_what) {
}

connect("visibility_changed", callable_mp(this, &ThemeEditorPreview::_preview_visibility_changed));
[[fallthrough]];
}
} break;

case NOTIFICATION_READY: {
List<Ref<Theme>> preview_themes;
preview_themes.push_back(ThemeDB::get_singleton()->get_default_theme());
ThemeDB::get_singleton()->create_theme_context(preview_root, preview_themes);
} break;

case NOTIFICATION_THEME_CHANGED: {
picker_button->set_icon(get_editor_theme_icon(SNAME("ColorPick")));

Expand Down Expand Up @@ -247,9 +253,8 @@ ThemeEditorPreview::ThemeEditorPreview() {
preview_container = memnew(ScrollContainer);
preview_body->add_child(preview_container);

MarginContainer *preview_root = memnew(MarginContainer);
preview_root = memnew(MarginContainer);
preview_container->add_child(preview_root);
preview_root->set_theme(ThemeDB::get_singleton()->get_default_theme());
preview_root->set_clip_contents(true);
preview_root->set_custom_minimum_size(Size2(450, 0) * EDSCALE);
preview_root->set_v_size_flags(SIZE_EXPAND_FILL);
Expand Down
1 change: 1 addition & 0 deletions editor/plugins/theme_editor_preview.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ThemeEditorPreview : public VBoxContainer {
GDCLASS(ThemeEditorPreview, VBoxContainer);

ScrollContainer *preview_container = nullptr;
MarginContainer *preview_root = nullptr;
ColorRect *preview_bg = nullptr;
MarginContainer *preview_overlay = nullptr;
Control *picker_overlay = nullptr;
Expand Down
18 changes: 18 additions & 0 deletions scene/theme/theme_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,17 @@ void ThemeDB::_init_default_theme_context() {
default_theme_context = memnew(ThemeContext);

List<Ref<Theme>> themes;

// Only add the project theme to the default context when running projects.

#ifdef TOOLS_ENABLED
if (!Engine::get_singleton()->is_editor_hint()) {
themes.push_back(project_theme);
}
#else
themes.push_back(project_theme);
#endif

themes.push_back(default_theme);
default_theme_context->set_themes(themes);
}
Expand All @@ -289,6 +299,14 @@ void ThemeDB::_finalize_theme_contexts() {
}
}

ThemeContext *ThemeDB::get_theme_context(Node *p_node) const {
if (!theme_contexts.has(p_node)) {
return nullptr;
}

return theme_contexts[p_node];
}

ThemeContext *ThemeDB::get_default_theme_context() const {
return default_theme_context;
}
Expand Down
1 change: 1 addition & 0 deletions scene/theme/theme_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class ThemeDB : public Object {
ThemeContext *create_theme_context(Node *p_node, List<Ref<Theme>> &p_themes);
void destroy_theme_context(Node *p_node);

ThemeContext *get_theme_context(Node *p_node) const;
ThemeContext *get_default_theme_context() const;
ThemeContext *get_nearest_theme_context(Node *p_for_node) const;

Expand Down

0 comments on commit 512182f

Please sign in to comment.