mirror of
https://github.com/RGBCube/serenity
synced 2025-07-10 06:37:36 +00:00
LibGUI+FileManager: Add a GIcon class to support multi-size icons.
A GIcon can contain any number of bitmaps internally, and will give you the best fitting icon when you call bitmap_for_size().
This commit is contained in:
parent
7e54fdce99
commit
86413a6f5a
12 changed files with 165 additions and 23 deletions
63
LibGUI/GIcon.cpp
Normal file
63
LibGUI/GIcon.cpp
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <LibGUI/GIcon.h>
|
||||
|
||||
GIcon::GIcon()
|
||||
: m_impl(GIconImpl::create())
|
||||
{
|
||||
}
|
||||
|
||||
GIcon::GIcon(const GIconImpl& impl)
|
||||
: m_impl(const_cast<GIconImpl&>(impl))
|
||||
{
|
||||
}
|
||||
|
||||
GIcon::GIcon(const GIcon& other)
|
||||
: m_impl(other.m_impl.copy_ref())
|
||||
{
|
||||
}
|
||||
|
||||
GIcon::GIcon(RetainPtr<GraphicsBitmap>&& bitmap)
|
||||
: GIcon()
|
||||
{
|
||||
if (bitmap) {
|
||||
ASSERT(bitmap->width() == bitmap->height());
|
||||
int size = bitmap->width();
|
||||
set_bitmap_for_size(size, move(bitmap));
|
||||
}
|
||||
}
|
||||
|
||||
GIcon::GIcon(RetainPtr<GraphicsBitmap>&& bitmap1, RetainPtr<GraphicsBitmap>&& bitmap2)
|
||||
: GIcon(move(bitmap1))
|
||||
{
|
||||
if (bitmap2) {
|
||||
ASSERT(bitmap2->width() == bitmap2->height());
|
||||
int size = bitmap2->width();
|
||||
set_bitmap_for_size(size, move(bitmap2));
|
||||
}
|
||||
}
|
||||
|
||||
const GraphicsBitmap* GIconImpl::bitmap_for_size(int size) const
|
||||
{
|
||||
auto it = m_bitmaps.find(size);
|
||||
if (it != m_bitmaps.end())
|
||||
return it->value.ptr();
|
||||
|
||||
int best_diff_so_far = INT32_MAX;
|
||||
const GraphicsBitmap* best_fit = nullptr;
|
||||
for (auto& it : m_bitmaps) {
|
||||
int abs_diff = abs(it.key - size);
|
||||
if (abs_diff < best_diff_so_far) {
|
||||
best_diff_so_far = abs_diff;
|
||||
best_fit = it.value.ptr();
|
||||
}
|
||||
}
|
||||
return best_fit;
|
||||
}
|
||||
|
||||
void GIconImpl::set_bitmap_for_size(int size, RetainPtr<GraphicsBitmap>&& bitmap)
|
||||
{
|
||||
if (!bitmap) {
|
||||
m_bitmaps.remove(size);
|
||||
return;
|
||||
}
|
||||
m_bitmaps.set(size, move(bitmap));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue