Allow theme inheritance

Terminal colors and fonts will be inherited from ancestor nodes if not
defined.
This commit is contained in:
Leroy Hopson 2021-07-23 08:33:05 +07:00
parent c81da3820b
commit 5f399ed46e
No known key found for this signature in database
GPG key ID: D2747312A6DB51AA
6 changed files with 70 additions and 40 deletions

View file

@ -398,25 +398,34 @@ void Terminal::_draw() {
void Terminal::update_theme() {
ResourceLoader *rl = ResourceLoader::get_singleton();
Ref<Theme> default_theme;
/* Load the default theme if it exists and no theme is set */
// Having an actual theme resource set allows things like like font resizing.
// Don't actually set the theme to default (to allow inheritence of themes),
// but do load default values from it.
const char *default_theme_path =
"res://addons/godot_xterm/themes/default.tres";
if (!get_theme().is_valid() && rl->exists(default_theme_path)) {
set_theme(rl->load(default_theme_path));
default_theme = rl->load(default_theme_path);
}
/* Generate color palette based on theme */
auto set_pallete_color = [this](tsm_vte_color color, String theme_color,
Color default_color) -> void {
auto set_pallete_color = [this, default_theme](tsm_vte_color color,
String theme_color,
Color default_color) -> void {
Color c;
c = has_color(theme_color, "Terminal") ? get_color(theme_color, "Terminal")
: default_color;
c = has_color(theme_color, "Terminal")
? get_color(theme_color, "Terminal")
: has_color_override(theme_color)
? get_color(theme_color, "")
: (default_theme != nullptr &&
default_theme->has_color(theme_color, "Terminal"))
? default_theme->get_color(theme_color, "Terminal")
: default_color;
color_palette[color][0] = c.get_r8();
color_palette[color][1] = c.get_g8();
@ -475,13 +484,18 @@ void Terminal::update_theme() {
/* Load fonts into the fontmap from theme */
auto set_font = [this, rl](String font_style) -> void {
auto load_font = [this, default_theme](String font_style) -> void {
Ref<Font> fontref;
if (has_font(font_style, "Terminal")) {
fontref = get_font(font_style, "Terminal");
} else if (has_font_override(font_style)) {
fontref = get_font(font_style, "");
} else if (has_font("Regular", "Terminal")) {
fontref = get_font("Regular", "Terminal");
} else if (default_theme != nullptr &&
default_theme->has_font("Regular", "Terminal")) {
fontref = default_theme->get_font("Regular", "Terminal");
} else {
fontref = get_font("");
}
@ -489,12 +503,12 @@ void Terminal::update_theme() {
fontmap.insert(std::pair<String, Ref<Font>>(font_style, fontref));
};
set_font("Bold Italic");
set_font("Bold");
set_font("Italic");
set_font("Regular");
load_font("Bold Italic");
load_font("Bold");
load_font("Italic");
load_font("Regular");
update_size();
// update_size();
}
void Terminal::draw_background(int row, int col, Color bgcolor, int width = 1) {

View file

@ -7,8 +7,6 @@
tool
extends Control
const DefaultTheme = preload("../../themes/default.tres")
signal data_sent(data)
signal key_pressed(data, event)
signal size_changed(new_size)
@ -46,6 +44,7 @@ export var bell_cooldown: float = 0.1
export var blink_on_time: float = 0.6
export var blink_off_time: float = 0.3
var _default_theme: Theme = preload("../../themes/default.tres")
var _viewport: Viewport = preload("./viewport.tscn").instance()
var _native_terminal: Control = _viewport.get_node("Terminal")
var _screen := TextureRect.new()
@ -103,8 +102,7 @@ func copy_all() -> String:
func _ready():
if theme:
_native_terminal.theme = theme
_update_theme()
_native_terminal.update_mode = update_mode
_native_terminal.connect("data_sent", self, "_on_data_sent")
@ -131,6 +129,30 @@ func _ready():
_refresh()
func _update_theme():
# Themes are not propagated through the Viewport, so in order for theme
# inheritance to work we can pass through the theme variables manually.
for color in _default_theme.get_color_list("Terminal"):
var c: Color
if has_color(color, "Terminal"):
c = get_color(color, "Terminal")
else:
c = _default_theme.get_color(color, "Terminal")
_native_terminal.add_color_override(color, c)
for font in _default_theme.get_font_list("Terminal"):
var f: Font
if has_font(font, "Terminal"):
f = get_font(font, "Terminal")
else:
if _default_theme.has_font(font, "Terminal"):
f = _default_theme.get_font(font, "Terminal")
else:
f = _default_theme.get_font(font, "Regular")
_native_terminal.add_font_override(font, f)
_native_terminal._update_theme()
_native_terminal._update_size()
func _refresh():
_screen.update()
if update_mode == UpdateMode.AUTO:
@ -212,7 +234,7 @@ func _notification(what: int) -> void:
_viewport.size = rect_size
_refresh()
NOTIFICATION_THEME_CHANGED:
_native_terminal.theme = theme
_update_theme()
_refresh()