diff options
author | Filiph Sandström <filiph.sandstrom@filfatstudios.com> | 2022-01-01 18:26:19 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-01-01 22:51:53 +0100 |
commit | 35dac843b4d6c252f37bf77fc410d8fce55f6cdb (patch) | |
tree | 60c074c8a04b09c4a610fa575d956f23a8e8f594 /Userland/Libraries/LibGfx | |
parent | 2bb32a87f9b942c90afef871603e3d5e3b851255 (diff) | |
download | serenity-35dac843b4d6c252f37bf77fc410d8fce55f6cdb.zip |
Theming: Add alignment section
This commit removes the IsTitleCenter property and replaces it with
the TitleAlignment property that supports "Left", "Right" & "Center".
Diffstat (limited to 'Userland/Libraries/LibGfx')
-rw-r--r-- | Userland/Libraries/LibGfx/ClassicWindowTheme.cpp | 41 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Palette.cpp | 9 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Palette.h | 12 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/SystemTheme.cpp | 30 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/SystemTheme.h | 36 |
5 files changed, 110 insertions, 18 deletions
diff --git a/Userland/Libraries/LibGfx/ClassicWindowTheme.cpp b/Userland/Libraries/LibGfx/ClassicWindowTheme.cpp index c67616b015..5b2c35f874 100644 --- a/Userland/Libraries/LibGfx/ClassicWindowTheme.cpp +++ b/Userland/Libraries/LibGfx/ClassicWindowTheme.cpp @@ -1,6 +1,6 @@ /* * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> - * Copyright (c) 2021, Filiph Sandström <filiph.sandstrom@filfatstudios.com> + * Copyright (c) 2021-2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -74,16 +74,12 @@ void ClassicWindowTheme::paint_normal_frame(Painter& painter, WindowState window painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2); + auto title_alignment = palette.title_alignment(); + int stripe_right = leftmost_button_rect.left() - 3; if (stripes_color.alpha() > 0) { - if (palette.is_title_center()) { - auto stripe_width = (leftmost_button_rect.left() / 2 - titlebar_title_rect.width() / 2) - titlebar_icon_rect.width() - 3; - - for (int i = 2; i <= titlebar_inner_rect.height() - 2; i += 2) { - painter.draw_line({ titlebar_inner_rect.left(), titlebar_inner_rect.y() + i }, { titlebar_inner_rect.left() + stripe_width, titlebar_inner_rect.y() + i }, stripes_color); - painter.draw_line({ stripe_right - stripe_width, titlebar_inner_rect.y() + i }, { stripe_right, titlebar_inner_rect.y() + i }, stripes_color); - } - } else { + switch (title_alignment) { + case Gfx::TextAlignment::CenterLeft: { int stripe_left = titlebar_title_rect.right() + 5; if (stripe_left && stripe_right && stripe_left < stripe_right) { @@ -91,19 +87,34 @@ void ClassicWindowTheme::paint_normal_frame(Painter& painter, WindowState window painter.draw_line({ stripe_left, titlebar_inner_rect.y() + i }, { stripe_right, titlebar_inner_rect.y() + i }, stripes_color); } } + break; + } + case Gfx::TextAlignment::CenterRight: { + for (int i = 2; i <= titlebar_inner_rect.height() - 2; i += 2) { + painter.draw_line({ titlebar_inner_rect.left(), titlebar_inner_rect.y() + i }, { stripe_right - titlebar_title_rect.width() - 3, titlebar_inner_rect.y() + i }, stripes_color); + } + break; + } + case Gfx::TextAlignment::Center: { + auto stripe_width = (leftmost_button_rect.left() / 2 - titlebar_title_rect.width() / 2) - titlebar_icon_rect.width() - 3; + + for (int i = 2; i <= titlebar_inner_rect.height() - 2; i += 2) { + painter.draw_line({ titlebar_inner_rect.left(), titlebar_inner_rect.y() + i }, { titlebar_inner_rect.left() + stripe_width, titlebar_inner_rect.y() + i }, stripes_color); + painter.draw_line({ stripe_right - stripe_width, titlebar_inner_rect.y() + i }, { stripe_right, titlebar_inner_rect.y() + i }, stripes_color); + } + break; + } + default: + dbgln("Unhandled title alignment!"); } } auto clipped_title_rect = titlebar_title_rect; clipped_title_rect.set_width(stripe_right - clipped_title_rect.x()); if (!clipped_title_rect.is_empty()) { - auto align = Gfx::TextAlignment::CenterLeft; - if (palette.is_title_center()) - align = Gfx::TextAlignment::Center; - - painter.draw_text(clipped_title_rect.translated(1, 2), window_title, title_font, align, shadow_color, Gfx::TextElision::Right); + painter.draw_text(clipped_title_rect.translated(1, 2), window_title, title_font, title_alignment, shadow_color, Gfx::TextElision::Right); // FIXME: The translated(0, 1) wouldn't be necessary if we could center text based on its baseline. - painter.draw_text(clipped_title_rect.translated(0, 1), window_title, title_font, align, title_color, Gfx::TextElision::Right); + painter.draw_text(clipped_title_rect.translated(0, 1), window_title, title_font, title_alignment, title_color, Gfx::TextElision::Right); } painter.draw_scaled_bitmap(titlebar_icon_rect, icon, icon.rect()); diff --git a/Userland/Libraries/LibGfx/Palette.cpp b/Userland/Libraries/LibGfx/Palette.cpp index c8e5e24675..e0c1984b6c 100644 --- a/Userland/Libraries/LibGfx/Palette.cpp +++ b/Userland/Libraries/LibGfx/Palette.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> + * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -57,6 +58,14 @@ void Palette::set_color(ColorRole role, Color color) theme.color[(int)role] = color.value(); } +void Palette::set_alignment(AlignmentRole role, Gfx::TextAlignment value) +{ + if (m_impl->ref_count() != 1) + m_impl = m_impl->clone(); + auto& theme = const_cast<SystemTheme&>(impl().theme()); + theme.alignment[(int)role] = value; +} + void Palette::set_flag(FlagRole role, bool value) { if (m_impl->ref_count() != 1) diff --git a/Userland/Libraries/LibGfx/Palette.h b/Userland/Libraries/LibGfx/Palette.h index 980921612b..c968330237 100644 --- a/Userland/Libraries/LibGfx/Palette.h +++ b/Userland/Libraries/LibGfx/Palette.h @@ -1,6 +1,7 @@ /* * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> + * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -31,6 +32,12 @@ public: return Color::from_rgba(theme().color[(int)role]); } + Gfx::TextAlignment alignment(AlignmentRole role) const + { + VERIFY((int)role < (int)AlignmentRole::__Count); + return theme().alignment[(int)role]; + } + bool flag(FlagRole role) const { VERIFY((int)role < (int)FlagRole::__Count); @@ -125,8 +132,9 @@ public: Color syntax_preprocessor_statement() const { return color(ColorRole::SyntaxPreprocessorStatement); } Color syntax_preprocessor_value() const { return color(ColorRole::SyntaxPreprocessorValue); } + Gfx::TextAlignment title_alignment() const { return alignment(AlignmentRole::TitleAlignment); } + bool is_dark() const { return flag(FlagRole::IsDark); } - bool is_title_center() const { return flag(FlagRole::IsTitleCenter); } int window_border_thickness() const { return metric(MetricRole::BorderThickness); } int window_border_radius() const { return metric(MetricRole::BorderRadius); } @@ -142,11 +150,13 @@ public: String tooltip_shadow_path() const { return path(PathRole::TooltipShadow); } Color color(ColorRole role) const { return m_impl->color(role); } + Gfx::TextAlignment alignment(AlignmentRole role) const { return m_impl->alignment(role); } bool flag(FlagRole role) const { return m_impl->flag(role); } int metric(MetricRole role) const { return m_impl->metric(role); } String path(PathRole role) const { return m_impl->path(role); } void set_color(ColorRole, Color); + void set_alignment(AlignmentRole, Gfx::TextAlignment); void set_flag(FlagRole, bool); void set_metric(MetricRole, int); void set_path(PathRole, String); diff --git a/Userland/Libraries/LibGfx/SystemTheme.cpp b/Userland/Libraries/LibGfx/SystemTheme.cpp index e4e9666d41..d6e7d16846 100644 --- a/Userland/Libraries/LibGfx/SystemTheme.cpp +++ b/Userland/Libraries/LibGfx/SystemTheme.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> + * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -45,6 +46,29 @@ Core::AnonymousBuffer load_system_theme(Core::ConfigFile const& file) return file.read_bool_entry("Flags", name, false); }; + auto get_alignment = [&](auto& name, auto role) { + auto alignment = file.read_entry("Alignments", name).to_lowercase(); + if (alignment.is_empty()) { + switch (role) { + case (int)AlignmentRole::TitleAlignment: + return Gfx::TextAlignment::CenterLeft; + default: + dbgln("Alignment {} has no fallback value!", name); + return Gfx::TextAlignment::CenterLeft; + } + } + + if (alignment == "left" || alignment == "centerleft") + return Gfx::TextAlignment::CenterLeft; + else if (alignment == "right" || alignment == "centerright") + return Gfx::TextAlignment::CenterRight; + else if (alignment == "center") + return Gfx::TextAlignment::Center; + + dbgln("Alignment {} has an invalid value!", name); + return Gfx::TextAlignment::CenterLeft; + }; + auto get_metric = [&](auto& name, auto role) { int metric = file.read_num_entry("Metrics", name, -1); if (metric == -1) { @@ -86,6 +110,12 @@ Core::AnonymousBuffer load_system_theme(Core::ConfigFile const& file) ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE) #undef __ENUMERATE_COLOR_ROLE +#undef __ENUMERATE_ALIGNMENT_ROLE +#define __ENUMERATE_ALIGNMENT_ROLE(role) \ + data->alignment[(int)AlignmentRole::role] = get_alignment(#role, (int)AlignmentRole::role); + ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE) +#undef __ENUMERATE_ALIGNMENT_ROLE + #undef __ENUMERATE_FLAG_ROLE #define __ENUMERATE_FLAG_ROLE(role) \ data->flag[(int)FlagRole::role] = get_flag(#role); diff --git a/Userland/Libraries/LibGfx/SystemTheme.h b/Userland/Libraries/LibGfx/SystemTheme.h index 0b7ac2d1a2..ec1c00f6a4 100644 --- a/Userland/Libraries/LibGfx/SystemTheme.h +++ b/Userland/Libraries/LibGfx/SystemTheme.h @@ -1,6 +1,7 @@ /* * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> + * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -13,6 +14,7 @@ #include <LibCore/AnonymousBuffer.h> #include <LibCore/ConfigFile.h> #include <LibGfx/Color.h> +#include <LibGfx/TextAlignment.h> namespace Gfx { @@ -90,9 +92,11 @@ namespace Gfx { C(Window) \ C(WindowText) +#define ENUMERATE_ALIGNMENT_ROLES(C) \ + C(TitleAlignment) + #define ENUMERATE_FLAG_ROLES(C) \ - C(IsDark) \ - C(IsTitleCenter) + C(IsDark) #define ENUMERATE_METRIC_ROLES(C) \ C(BorderThickness) \ @@ -139,6 +143,33 @@ inline const char* to_string(ColorRole role) } } +enum class AlignmentRole { + NoRole, + +#undef __ENUMERATE_ALIGNMENT_ROLE +#define __ENUMERATE_ALIGNMENT_ROLE(role) role, + ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE) +#undef __ENUMERATE_ALIGNMENT_ROLE + + __Count, +}; + +inline const char* to_string(AlignmentRole role) +{ + switch (role) { + case AlignmentRole::NoRole: + return "NoRole"; +#undef __ENUMERATE_ALIGNMENT_ROLE +#define __ENUMERATE_ALIGNMENT_ROLE(role) \ + case AlignmentRole::role: \ + return #role; + ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE) +#undef __ENUMERATE_ALIGNMENT_ROLE + default: + VERIFY_NOT_REACHED(); + } +} + enum class FlagRole { NoRole, @@ -222,6 +253,7 @@ inline const char* to_string(PathRole role) struct SystemTheme { RGBA32 color[(int)ColorRole::__Count]; + Gfx::TextAlignment alignment[(int)AlignmentRole::__Count]; bool flag[(int)FlagRole::__Count]; int metric[(int)MetricRole::__Count]; char path[(int)PathRole::__Count][256]; // TODO: PATH_MAX? |