mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 14:47:46 +00:00
Libraries: Make CharacterBitmap instances at compile-time
`CharacterBitmap` instances are generated at run-time and put on the heap, but they can be created in a `constexpr` context and stored in static memory. Also, remove additional `width` and `height` `static` values in favor of using the `constexpr` member functions of `CharacterBitmap`. These changes also include the removal of some initialization code which tests if the `CharacterBitmap` is created since it is always created and removes function-local `static` values which cause run-time branches to ensure it is initialized each time the function is called.
This commit is contained in:
parent
dc518404ce
commit
d5fdc6096c
9 changed files with 97 additions and 135 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
|
||||
* Copyright (c) 2022, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -13,7 +14,7 @@
|
|||
|
||||
namespace GUI {
|
||||
|
||||
static const char* s_arrow_bitmap_data = {
|
||||
static constexpr Gfx::CharacterBitmap s_arrow_bitmap {
|
||||
" "
|
||||
" # "
|
||||
" ## "
|
||||
|
@ -22,10 +23,9 @@ static const char* s_arrow_bitmap_data = {
|
|||
" ### "
|
||||
" ## "
|
||||
" # "
|
||||
" "
|
||||
" ",
|
||||
9, 9
|
||||
};
|
||||
static const int s_arrow_bitmap_width = 9;
|
||||
static const int s_arrow_bitmap_height = 9;
|
||||
|
||||
ColumnsView::ColumnsView()
|
||||
{
|
||||
|
@ -128,7 +128,7 @@ void ColumnsView::paint_event(PaintEvent& event)
|
|||
|
||||
Gfx::IntRect text_rect = {
|
||||
icon_rect.right() + 1 + icon_spacing(), row * item_height(),
|
||||
column.width - icon_spacing() - icon_size() - icon_spacing() - icon_spacing() - s_arrow_bitmap_width - icon_spacing(), item_height()
|
||||
column.width - icon_spacing() - icon_size() - icon_spacing() - icon_spacing() - static_cast<int>(s_arrow_bitmap.width()) - icon_spacing(), item_height()
|
||||
};
|
||||
draw_item_text(painter, index, is_selected_row, text_rect, index.data().to_string(), font_for_index(index), Gfx::TextAlignment::CenterLeft, Gfx::TextElision::None);
|
||||
|
||||
|
@ -145,11 +145,10 @@ void ColumnsView::paint_event(PaintEvent& event)
|
|||
if (expandable) {
|
||||
Gfx::IntRect arrow_rect = {
|
||||
text_rect.right() + 1 + icon_spacing(), 0,
|
||||
s_arrow_bitmap_width, s_arrow_bitmap_height
|
||||
s_arrow_bitmap.width(), s_arrow_bitmap.height()
|
||||
};
|
||||
arrow_rect.center_vertically_within(row_rect);
|
||||
static auto& arrow_bitmap = Gfx::CharacterBitmap::create_from_ascii(s_arrow_bitmap_data, s_arrow_bitmap_width, s_arrow_bitmap_height).leak_ref();
|
||||
painter.draw_bitmap(arrow_rect.location(), arrow_bitmap, text_color);
|
||||
painter.draw_bitmap(arrow_rect.location(), s_arrow_bitmap, text_color);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -201,7 +200,7 @@ void ColumnsView::update_column_sizes()
|
|||
ModelIndex index = model()->index(row, m_model_column, column.parent_index);
|
||||
VERIFY(index.is_valid());
|
||||
auto text = index.data().to_string();
|
||||
int row_width = icon_spacing() + icon_size() + icon_spacing() + font().width(text) + icon_spacing() + s_arrow_bitmap_width + icon_spacing();
|
||||
int row_width = icon_spacing() + icon_size() + icon_spacing() + font().width(text) + icon_spacing() + s_arrow_bitmap.width() + icon_spacing();
|
||||
if (row_width > column.width)
|
||||
column.width = row_width;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2022, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -12,7 +13,8 @@
|
|||
|
||||
namespace GUI {
|
||||
|
||||
static const char* s_resize_corner_shadows_data = {
|
||||
static constexpr Gfx::CharacterBitmap s_resize_corner_shadows_bitmap {
|
||||
|
||||
" "
|
||||
" ## "
|
||||
" # "
|
||||
|
@ -28,10 +30,11 @@ static const char* s_resize_corner_shadows_data = {
|
|||
" "
|
||||
" ## ## ## ## ## "
|
||||
" # # # # # "
|
||||
" "
|
||||
" ",
|
||||
16, 16
|
||||
};
|
||||
|
||||
static const char* s_resize_corner_highlights_data = {
|
||||
static constexpr Gfx::CharacterBitmap s_resize_corner_highlights_bitmap {
|
||||
" "
|
||||
" "
|
||||
" # "
|
||||
|
@ -47,14 +50,10 @@ static const char* s_resize_corner_highlights_data = {
|
|||
" "
|
||||
" "
|
||||
" # # # # # "
|
||||
" "
|
||||
" ",
|
||||
16, 16
|
||||
};
|
||||
|
||||
static Gfx::CharacterBitmap* s_resize_corner_shadows_bitmap;
|
||||
static Gfx::CharacterBitmap* s_resize_corner_highlights_bitmap;
|
||||
static const int s_resize_corner_bitmap_width = 16;
|
||||
static const int s_resize_corner_bitmap_height = 16;
|
||||
|
||||
ResizeCorner::ResizeCorner()
|
||||
{
|
||||
set_override_cursor(Gfx::StandardCursor::ResizeDiagonalTLBR);
|
||||
|
@ -72,13 +71,8 @@ void ResizeCorner::paint_event(PaintEvent& event)
|
|||
painter.add_clip_rect(event.rect());
|
||||
painter.fill_rect(rect(), palette().color(background_role()));
|
||||
|
||||
if (!s_resize_corner_shadows_bitmap)
|
||||
s_resize_corner_shadows_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_resize_corner_shadows_data, s_resize_corner_bitmap_width, s_resize_corner_bitmap_height).leak_ref();
|
||||
painter.draw_bitmap({ 0, 2 }, *s_resize_corner_shadows_bitmap, palette().threed_shadow1());
|
||||
|
||||
if (!s_resize_corner_highlights_bitmap)
|
||||
s_resize_corner_highlights_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_resize_corner_highlights_data, s_resize_corner_bitmap_width, s_resize_corner_bitmap_height).leak_ref();
|
||||
painter.draw_bitmap({ 0, 2 }, *s_resize_corner_highlights_bitmap, palette().threed_highlight());
|
||||
painter.draw_bitmap({ 0, 2 }, s_resize_corner_shadows_bitmap, palette().threed_shadow1());
|
||||
painter.draw_bitmap({ 0, 2 }, s_resize_corner_highlights_bitmap, palette().threed_highlight());
|
||||
|
||||
Widget::paint_event(event);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2022, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -15,7 +16,7 @@ REGISTER_WIDGET(GUI, Scrollbar)
|
|||
|
||||
namespace GUI {
|
||||
|
||||
static const char* s_up_arrow_bitmap_data = {
|
||||
static constexpr Gfx::CharacterBitmap s_up_arrow_bitmap {
|
||||
" "
|
||||
" "
|
||||
" "
|
||||
|
@ -24,10 +25,11 @@ static const char* s_up_arrow_bitmap_data = {
|
|||
" ##### "
|
||||
" ####### "
|
||||
" "
|
||||
" "
|
||||
" ",
|
||||
9, 9
|
||||
};
|
||||
|
||||
static const char* s_down_arrow_bitmap_data = {
|
||||
static constexpr Gfx::CharacterBitmap s_down_arrow_bitmap {
|
||||
" "
|
||||
" "
|
||||
" "
|
||||
|
@ -36,10 +38,11 @@ static const char* s_down_arrow_bitmap_data = {
|
|||
" ### "
|
||||
" # "
|
||||
" "
|
||||
" "
|
||||
" ",
|
||||
9, 9
|
||||
};
|
||||
|
||||
static const char* s_left_arrow_bitmap_data = {
|
||||
static constexpr Gfx::CharacterBitmap s_left_arrow_bitmap {
|
||||
" "
|
||||
" # "
|
||||
" ## "
|
||||
|
@ -48,10 +51,11 @@ static const char* s_left_arrow_bitmap_data = {
|
|||
" ### "
|
||||
" ## "
|
||||
" # "
|
||||
" "
|
||||
" ",
|
||||
9, 9
|
||||
};
|
||||
|
||||
static const char* s_right_arrow_bitmap_data = {
|
||||
static constexpr Gfx::CharacterBitmap s_right_arrow_bitmap {
|
||||
" "
|
||||
" # "
|
||||
" ## "
|
||||
|
@ -60,26 +64,14 @@ static const char* s_right_arrow_bitmap_data = {
|
|||
" ### "
|
||||
" ## "
|
||||
" # "
|
||||
" "
|
||||
" ",
|
||||
9, 9
|
||||
};
|
||||
|
||||
static Gfx::CharacterBitmap* s_up_arrow_bitmap;
|
||||
static Gfx::CharacterBitmap* s_down_arrow_bitmap;
|
||||
static Gfx::CharacterBitmap* s_left_arrow_bitmap;
|
||||
static Gfx::CharacterBitmap* s_right_arrow_bitmap;
|
||||
|
||||
Scrollbar::Scrollbar(Orientation orientation)
|
||||
: AbstractSlider(orientation)
|
||||
{
|
||||
m_automatic_scrolling_timer = add<Core::Timer>();
|
||||
if (!s_up_arrow_bitmap)
|
||||
s_up_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_up_arrow_bitmap_data, 9, 9).leak_ref();
|
||||
if (!s_down_arrow_bitmap)
|
||||
s_down_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_down_arrow_bitmap_data, 9, 9).leak_ref();
|
||||
if (!s_left_arrow_bitmap)
|
||||
s_left_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_left_arrow_bitmap_data, 9, 9).leak_ref();
|
||||
if (!s_right_arrow_bitmap)
|
||||
s_right_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_right_arrow_bitmap_data, 9, 9).leak_ref();
|
||||
|
||||
if (orientation == Orientation::Vertical) {
|
||||
set_fixed_width(16);
|
||||
|
@ -210,15 +202,15 @@ void Scrollbar::paint_event(PaintEvent& event)
|
|||
if (decrement_pressed)
|
||||
decrement_location.translate_by(1, 1);
|
||||
if (!has_scrubber() || !is_enabled())
|
||||
painter.draw_bitmap(decrement_location.translated(1, 1), orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, palette().threed_highlight());
|
||||
painter.draw_bitmap(decrement_location, orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, (has_scrubber() && is_enabled()) ? palette().button_text() : palette().threed_shadow1());
|
||||
painter.draw_bitmap(decrement_location.translated(1, 1), orientation() == Orientation::Vertical ? s_up_arrow_bitmap : s_left_arrow_bitmap, palette().threed_highlight());
|
||||
painter.draw_bitmap(decrement_location, orientation() == Orientation::Vertical ? s_up_arrow_bitmap : s_left_arrow_bitmap, (has_scrubber() && is_enabled()) ? palette().button_text() : palette().threed_shadow1());
|
||||
|
||||
auto increment_location = increment_button_rect().location().translated(3, 3);
|
||||
if (increment_pressed)
|
||||
increment_location.translate_by(1, 1);
|
||||
if (!has_scrubber() || !is_enabled())
|
||||
painter.draw_bitmap(increment_location.translated(1, 1), orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, palette().threed_highlight());
|
||||
painter.draw_bitmap(increment_location, orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, (has_scrubber() && is_enabled()) ? palette().button_text() : palette().threed_shadow1());
|
||||
painter.draw_bitmap(increment_location.translated(1, 1), orientation() == Orientation::Vertical ? s_down_arrow_bitmap : s_right_arrow_bitmap, palette().threed_highlight());
|
||||
painter.draw_bitmap(increment_location, orientation() == Orientation::Vertical ? s_down_arrow_bitmap : s_right_arrow_bitmap, (has_scrubber() && is_enabled()) ? palette().button_text() : palette().threed_shadow1());
|
||||
}
|
||||
|
||||
if (has_scrubber() && !scrubber_rect().is_null())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue