1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 14:07:45 +00:00

Terminal+LibVT: Get the color scheme from the system theme

This commit is contained in:
implicitfield 2022-12-02 18:16:58 +02:00 committed by Andrew Kaster
parent 8eb402f8e5
commit 800c292be8
5 changed files with 38 additions and 107 deletions

View file

@ -72,8 +72,7 @@ public:
{ {
VERIFY(domain == "Terminal"); VERIFY(domain == "Terminal");
if (group == "Window") { if (group == "Window" && key == "Bell") {
if (key == "Bell") {
auto bell_mode = VT::TerminalWidget::BellMode::Visible; auto bell_mode = VT::TerminalWidget::BellMode::Visible;
if (value == "AudibleBeep") if (value == "AudibleBeep")
bell_mode = VT::TerminalWidget::BellMode::AudibleBeep; bell_mode = VT::TerminalWidget::BellMode::AudibleBeep;
@ -82,9 +81,6 @@ public:
if (value == "Disabled") if (value == "Disabled")
bell_mode = VT::TerminalWidget::BellMode::Disabled; bell_mode = VT::TerminalWidget::BellMode::Disabled;
m_parent_terminal.set_bell_mode(bell_mode); m_parent_terminal.set_bell_mode(bell_mode);
} else if (key == "ColorScheme") {
m_parent_terminal.set_color_scheme(value);
}
} else if (group == "Text" && key == "Font") { } else if (group == "Text" && key == "Font") {
auto font = Gfx::FontDatabase::the().get_by_name(value); auto font = Gfx::FontDatabase::the().get_by_name(value);
if (font.is_null()) if (font.is_null())

View file

@ -85,17 +85,4 @@
text: "Blinking cursor" text: "Blinking cursor"
} }
} }
@GUI::GroupBox {
title: "Color Scheme"
preferred_height: "fit"
layout: @GUI::VerticalBoxLayout {
margins: [16, 8, 8]
spacing: 16
}
@GUI::ComboBox {
name: "color_scheme_combo"
}
}
} }

View file

@ -114,30 +114,6 @@ TerminalSettingsViewWidget::TerminalSettingsViewWidget()
set_modified(true); set_modified(true);
}; };
m_color_scheme = Config::read_string("Terminal"sv, "Window"sv, "ColorScheme"sv);
m_original_color_scheme = m_color_scheme;
// The settings window takes a reference to this vector, so it needs to outlive this scope.
// As long as we ensure that only one settings window may be open at a time (which we do),
// this should cause no problems.
static Vector<DeprecatedString> color_scheme_names;
color_scheme_names.clear();
Core::DirIterator iterator("/res/color-schemes", Core::DirIterator::SkipParentAndBaseDir);
while (iterator.has_next()) {
auto path = iterator.next_path();
color_scheme_names.append(path.replace(".ini"sv, ""sv, ReplaceMode::FirstOnly));
}
quick_sort(color_scheme_names);
auto& color_scheme_combo = *find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo");
color_scheme_combo.set_only_allow_values_from_model(true);
color_scheme_combo.set_model(*GUI::ItemListModel<DeprecatedString>::create(color_scheme_names));
color_scheme_combo.set_selected_index(color_scheme_names.find_first_index(m_color_scheme).value());
color_scheme_combo.set_enabled(color_scheme_names.size() > 1);
color_scheme_combo.on_change = [&](auto&, const GUI::ModelIndex& index) {
m_color_scheme = index.data().as_string();
Config::write_string("Terminal"sv, "Window"sv, "ColorScheme"sv, m_color_scheme);
set_modified(true);
};
auto& font_button = *find_descendant_of_type_named<GUI::Button>("terminal_font_button"); auto& font_button = *find_descendant_of_type_named<GUI::Button>("terminal_font_button");
auto& font_text = *find_descendant_of_type_named<GUI::Label>("terminal_font_label"); auto& font_text = *find_descendant_of_type_named<GUI::Label>("terminal_font_label");
auto font_name = Config::read_string("Terminal"sv, "Text"sv, "Font"sv); auto font_name = Config::read_string("Terminal"sv, "Text"sv, "Font"sv);
@ -281,7 +257,6 @@ void TerminalSettingsViewWidget::apply_settings()
{ {
m_original_opacity = m_opacity; m_original_opacity = m_opacity;
m_original_font = m_font; m_original_font = m_font;
m_original_color_scheme = m_color_scheme;
m_original_cursor_shape = m_cursor_shape; m_original_cursor_shape = m_cursor_shape;
m_original_cursor_is_blinking_set = m_cursor_is_blinking_set; m_original_cursor_is_blinking_set = m_cursor_is_blinking_set;
write_back_settings(); write_back_settings();
@ -291,7 +266,6 @@ void TerminalSettingsViewWidget::write_back_settings() const
{ {
Config::write_i32("Terminal"sv, "Window"sv, "Opacity"sv, static_cast<i32>(m_original_opacity)); Config::write_i32("Terminal"sv, "Window"sv, "Opacity"sv, static_cast<i32>(m_original_opacity));
Config::write_string("Terminal"sv, "Text"sv, "Font"sv, m_original_font->qualified_name()); Config::write_string("Terminal"sv, "Text"sv, "Font"sv, m_original_font->qualified_name());
Config::write_string("Terminal"sv, "Window"sv, "ColorScheme"sv, m_original_color_scheme);
Config::write_string("Terminal"sv, "Cursor"sv, "Shape"sv, VT::TerminalWidget::stringify_cursor_shape(m_original_cursor_shape)); Config::write_string("Terminal"sv, "Cursor"sv, "Shape"sv, VT::TerminalWidget::stringify_cursor_shape(m_original_cursor_shape));
Config::write_bool("Terminal"sv, "Cursor"sv, "Blinking"sv, m_original_cursor_is_blinking_set); Config::write_bool("Terminal"sv, "Cursor"sv, "Blinking"sv, m_original_cursor_is_blinking_set);
} }

View file

@ -148,8 +148,7 @@ TerminalWidget::TerminalWidget(int ptm_fd, bool automatic_size_policy)
update_copy_action(); update_copy_action();
update_paste_action(); update_paste_action();
update_color_scheme();
set_color_scheme(Config::read_string("Terminal"sv, "Window"sv, "ColorScheme"sv, "Default"sv));
} }
Gfx::IntRect TerminalWidget::glyph_rect(u16 row, u16 column) Gfx::IntRect TerminalWidget::glyph_rect(u16 row, u16 column)
@ -197,7 +196,9 @@ void TerminalWidget::focusout_event(GUI::FocusEvent& event)
void TerminalWidget::event(Core::Event& event) void TerminalWidget::event(Core::Event& event)
{ {
if (event.type() == GUI::Event::WindowBecameActive) if (event.type() == GUI::Event::ThemeChange)
update_color_scheme();
else if (event.type() == GUI::Event::WindowBecameActive)
set_logical_focus(true); set_logical_focus(true);
else if (event.type() == GUI::Event::WindowBecameInactive) else if (event.type() == GUI::Event::WindowBecameInactive)
set_logical_focus(false); set_logical_focus(false);
@ -1198,59 +1199,32 @@ void TerminalWidget::update_paste_action()
m_paste_action->set_enabled(mime_type.starts_with("text/"sv) && !data.is_empty()); m_paste_action->set_enabled(mime_type.starts_with("text/"sv) && !data.is_empty());
} }
void TerminalWidget::set_color_scheme(StringView name) void TerminalWidget::update_color_scheme()
{ {
if (name.contains('/')) { auto const& palette = GUI::Widget::palette();
dbgln("Shenanigans! Color scheme names can't contain slashes.");
return;
}
m_color_scheme_name = name; m_show_bold_text_as_bright = palette.bold_text_as_bright();
constexpr StringView color_names[] = { m_default_background_color = palette.background();
"Black"sv, m_default_foreground_color = palette.foreground();
"Red"sv,
"Green"sv,
"Yellow"sv,
"Blue"sv,
"Magenta"sv,
"Cyan"sv,
"White"sv
};
auto path = DeprecatedString::formatted("/res/color-schemes/{}.ini", name); m_colors[0] = palette.black();
auto color_config_or_error = Core::ConfigFile::open(path); m_colors[1] = palette.red();
if (color_config_or_error.is_error()) { m_colors[2] = palette.green();
dbgln("Unable to read color scheme file '{}': {}", path, color_config_or_error.error()); m_colors[3] = palette.yellow();
return; m_colors[4] = palette.blue();
} m_colors[5] = palette.magenta();
auto color_config = color_config_or_error.release_value(); m_colors[6] = palette.cyan();
m_colors[7] = palette.white();
m_colors[8] = palette.bright_black();
m_colors[9] = palette.bright_red();
m_colors[10] = palette.bright_green();
m_colors[11] = palette.bright_yellow();
m_colors[12] = palette.bright_blue();
m_colors[13] = palette.bright_magenta();
m_colors[14] = palette.bright_cyan();
m_colors[15] = palette.bright_white();
m_show_bold_text_as_bright = color_config->read_bool_entry("Options", "ShowBoldTextAsBright", true);
auto default_background = Gfx::Color::from_string(color_config->read_entry("Primary", "Background"));
if (default_background.has_value())
m_default_background_color = default_background.value();
else
m_default_background_color = Gfx::Color::from_rgb(m_colors[(u8)VT::Color::ANSIColor::Black]);
auto default_foreground = Gfx::Color::from_string(color_config->read_entry("Primary", "Foreground"));
if (default_foreground.has_value())
m_default_foreground_color = default_foreground.value();
else
m_default_foreground_color = Gfx::Color::from_rgb(m_colors[(u8)VT::Color::ANSIColor::White]);
for (u8 color_idx = 0; color_idx < 8; ++color_idx) {
auto rgb = Gfx::Color::from_string(color_config->read_entry("Normal", color_names[color_idx]));
if (rgb.has_value())
m_colors[color_idx] = rgb.value().value();
}
for (u8 color_idx = 0; color_idx < 8; ++color_idx) {
auto rgb = Gfx::Color::from_string(color_config->read_entry("Bright", color_names[color_idx]));
if (rgb.has_value())
m_colors[color_idx + 8] = rgb.value().value();
}
update(); update();
} }
@ -1268,11 +1242,11 @@ constexpr Gfx::Color TerminalWidget::terminal_color_to_rgb(VT::Color color) cons
case VT::Color::Kind::RGB: case VT::Color::Kind::RGB:
return Gfx::Color::from_rgb(color.as_rgb()); return Gfx::Color::from_rgb(color.as_rgb());
case VT::Color::Kind::Indexed: case VT::Color::Kind::Indexed:
return Gfx::Color::from_rgb(m_colors[color.as_indexed()]); return m_colors[color.as_indexed()];
case VT::Color::Kind::Named: { case VT::Color::Kind::Named: {
auto ansi = color.as_named(); auto ansi = color.as_named();
if ((u16)ansi < 256) if ((u16)ansi < 256)
return Gfx::Color::from_rgb(m_colors[(u16)ansi]); return m_colors[(u16)ansi];
else if (ansi == VT::Color::ANSIColor::DefaultForeground) else if (ansi == VT::Color::ANSIColor::DefaultForeground)
return m_default_foreground_color; return m_default_foreground_color;
else if (ansi == VT::Color::ANSIColor::DefaultBackground) else if (ansi == VT::Color::ANSIColor::DefaultBackground)

View file

@ -94,7 +94,7 @@ public:
void set_font_and_resize_to_fit(Gfx::Font const&); void set_font_and_resize_to_fit(Gfx::Font const&);
void set_color_scheme(StringView); void update_color_scheme();
void set_logical_focus(bool); void set_logical_focus(bool);
@ -174,7 +174,7 @@ private:
// Snapshot of m_hovered_href when opening a context menu for a hyperlink. // Snapshot of m_hovered_href when opening a context menu for a hyperlink.
DeprecatedString m_context_menu_href; DeprecatedString m_context_menu_href;
unsigned m_colors[256]; Gfx::Color m_colors[256];
Gfx::Color m_default_foreground_color; Gfx::Color m_default_foreground_color;
Gfx::Color m_default_background_color; Gfx::Color m_default_background_color;
bool m_show_bold_text_as_bright { true }; bool m_show_bold_text_as_bright { true };