1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:57:35 +00:00

DisplaySettings: Add theme selection and preview

This commit is contained in:
MacDue 2022-03-27 20:32:32 +01:00 committed by Andreas Kling
parent 8fa0409ae1
commit 04b6a060ca
7 changed files with 208 additions and 0 deletions

View file

@ -8,6 +8,7 @@ compile_gml(MonitorSettings.gml MonitorSettingsGML.h monitor_settings_window_gml
compile_gml(BackgroundSettings.gml BackgroundSettingsGML.h background_settings_gml)
compile_gml(DesktopSettings.gml DesktopSettingsGML.h desktop_settings_gml)
compile_gml(FontSettings.gml FontSettingsGML.h font_settings_gml)
compile_gml(ThemesSettings.gml ThemesSettingsGML.h themes_settings_gml)
set(SOURCES
BackgroundSettingsGML.h
@ -19,6 +20,11 @@ set(SOURCES
MonitorSettingsWidget.cpp
MonitorSettingsGML.h
MonitorWidget.cpp
ThemePreviewWidget.h
ThemePreviewWidget.cpp
ThemesSettingsWidget.h
ThemesSettingsWidget.cpp
ThemesSettingsGML.h
main.cpp
)

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ThemePreviewWidget.h"
#include <AK/Array.h>
#include <LibCore/File.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Painter.h>
#include <LibGfx/StylePainter.h>
namespace DisplaySettings {
ThemePreviewWidget::ThemePreviewWidget(Gfx::Palette const& palette)
: GUI::AbstractThemePreview(palette)
{
set_fixed_size(304, 201);
}
void ThemePreviewWidget::set_theme(String path)
{
set_theme_from_file(*Core::File::open(path, Core::OpenMode::ReadOnly).release_value_but_fixme_should_propagate_errors());
}
void ThemePreviewWidget::paint_preview(GUI::PaintEvent&)
{
GUI::Painter painter(*this);
auto active_window_rect = rect().shrunken(48, 100).translated(4, 26);
auto inactive_window_rect = active_window_rect.translated(-8, -32);
auto message_box = active_window_rect.shrunken(100, 60);
paint_window("Inactive Window", inactive_window_rect, Gfx::WindowTheme::WindowState::Inactive, inactive_window_icon());
paint_window("Active Window", active_window_rect, Gfx::WindowTheme::WindowState::Active, active_window_icon());
paint_window("Alert", message_box, Gfx::WindowTheme::WindowState::Highlighted, active_window_icon(), 1);
auto draw_centered_button = [&](auto window_rect, auto text, int button_width, int button_height) {
auto box_center = window_rect.center();
Gfx::IntRect button_rect { box_center.x() - button_width / 2, box_center.y() - button_height / 2, button_width, button_height };
Gfx::StylePainter::paint_button(
painter, button_rect, preview_palette(), Gfx::ButtonStyle::Normal, false, false, false, true, false, false);
painter.draw_text(button_rect, text, Gfx::TextAlignment::Center, preview_palette().color(foreground_role()), Gfx::TextElision::Right, Gfx::TextWrapping::DontWrap);
};
draw_centered_button(message_box, "Ok", 32, 16);
}
}

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGUI/AbstractThemePreview.h>
#include <LibGUI/Widget.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Palette.h>
#include <LibGfx/WindowTheme.h>
namespace DisplaySettings {
class ThemePreviewWidget final : public GUI::AbstractThemePreview {
C_OBJECT(ThemePreviewWidget);
public:
void set_theme(String path);
private:
explicit ThemePreviewWidget(Gfx::Palette const& palette);
void paint_preview(GUI::PaintEvent& event) override;
};
}

View file

@ -0,0 +1,32 @@
@GUI::Widget {
fill_with_background_color: true
layout: @GUI::VerticalBoxLayout {
margins: [8]
}
@GUI::Frame {
layout: @GUI::HorizontalBoxLayout {}
name: "preview_frame"
fixed_width: 304
fixed_height: 201
}
@GUI::Widget {
fixed_height: 20
}
@GUI::Widget {
shrink_to_fit: true
layout: @GUI::HorizontalBoxLayout {}
@GUI::Label {
text: "Theme:"
text_alignment: "CenterLeft"
fixed_width: 95
}
@GUI::ComboBox {
name: "themes_combo"
}
}
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ThemesSettingsWidget.h"
#include <AK/LexicalPath.h>
#include <AK/QuickSort.h>
#include <Applications/DisplaySettings/ThemesSettingsGML.h>
#include <LibCore/DirIterator.h>
#include <LibGUI/ConnectionToWindowServer.h>
#include <LibGUI/ItemListModel.h>
namespace DisplaySettings {
ThemesSettingsWidget::ThemesSettingsWidget()
{
load_from_gml(themes_settings_gml);
m_themes = Gfx::list_installed_system_themes();
auto current_theme_name = GUI::ConnectionToWindowServer::the().get_system_theme();
size_t current_theme_index;
m_theme_names.ensure_capacity(m_themes.size());
for (auto& theme_meta : m_themes) {
m_theme_names.append(theme_meta.name);
if (current_theme_name == theme_meta.name) {
m_selected_theme = &theme_meta;
current_theme_index = m_theme_names.size() - 1;
}
}
VERIFY(m_selected_theme);
m_theme_preview = find_descendant_of_type_named<GUI::Frame>("preview_frame")->add<ThemePreviewWidget>(palette());
m_themes_combo = *find_descendant_of_type_named<GUI::ComboBox>("themes_combo");
m_themes_combo->set_only_allow_values_from_model(true);
m_themes_combo->set_model(*GUI::ItemListModel<String>::create(m_theme_names));
m_themes_combo->on_change = [this](auto&, const GUI::ModelIndex& index) {
m_selected_theme = &m_themes.at(index.row());
m_theme_preview->set_theme(m_selected_theme->path);
};
m_themes_combo->set_selected_index(current_theme_index);
}
void ThemesSettingsWidget::apply_settings()
{
if (m_selected_theme)
VERIFY(GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name));
}
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibGUI/ComboBox.h>
#include <LibGUI/SettingsWindow.h>
#include <LibGfx/SystemTheme.h>
#include "ThemePreviewWidget.h"
namespace DisplaySettings {
class ThemesSettingsWidget final : public GUI::SettingsWindow::Tab {
C_OBJECT(ThemesSettingsWidget);
public:
virtual void apply_settings() override;
private:
Vector<Gfx::SystemThemeMetaData> m_themes;
Vector<String> m_theme_names;
RefPtr<GUI::ComboBox> m_themes_combo;
RefPtr<ThemePreviewWidget> m_theme_preview;
Gfx::SystemThemeMetaData const* m_selected_theme { nullptr };
ThemesSettingsWidget();
};
}

View file

@ -10,6 +10,7 @@
#include "DesktopSettingsWidget.h"
#include "FontSettingsWidget.h"
#include "MonitorSettingsWidget.h"
#include "ThemesSettingsWidget.h"
#include <LibConfig/Client.h>
#include <LibCore/System.h>
#include <LibGUI/Application.h>
@ -28,6 +29,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto window = TRY(GUI::SettingsWindow::create("Display Settings"));
(void)TRY(window->add_tab<DisplaySettings::BackgroundSettingsWidget>("Background"));
(void)TRY(window->add_tab<DisplaySettings::ThemesSettingsWidget>("Themes"));
(void)TRY(window->add_tab<DisplaySettings::FontSettingsWidget>("Fonts"));
(void)TRY(window->add_tab<DisplaySettings::MonitorSettingsWidget>("Monitor"));
(void)TRY(window->add_tab<DisplaySettings::DesktopSettingsWidget>("Workspaces"));