2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-02-15 01:03:37 +01:00
|
|
|
#include <AK/String.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Icon.h>
|
2020-02-16 09:17:49 +01:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2019-03-24 04:28:36 +01:00
|
|
|
|
2020-03-07 12:02:21 +13:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
|
|
Icon::Icon()
|
|
|
|
|
: m_impl(IconImpl::create())
|
2019-03-24 04:28:36 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 12:02:21 +13:00
|
|
|
Icon::Icon(const IconImpl& impl)
|
|
|
|
|
: m_impl(const_cast<IconImpl&>(impl))
|
2019-03-24 04:28:36 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 12:02:21 +13:00
|
|
|
Icon::Icon(const Icon& other)
|
2019-07-11 15:49:47 +02:00
|
|
|
: m_impl(other.m_impl)
|
2019-03-24 04:28:36 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 12:02:21 +13:00
|
|
|
Icon::Icon(RefPtr<Gfx::Bitmap>&& bitmap)
|
|
|
|
|
: Icon()
|
2019-03-24 04:28:36 +01:00
|
|
|
{
|
|
|
|
|
if (bitmap) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(bitmap->width() == bitmap->height());
|
2019-03-24 04:28:36 +01:00
|
|
|
int size = bitmap->width();
|
|
|
|
|
set_bitmap_for_size(size, move(bitmap));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 12:02:21 +13:00
|
|
|
Icon::Icon(RefPtr<Gfx::Bitmap>&& bitmap1, RefPtr<Gfx::Bitmap>&& bitmap2)
|
|
|
|
|
: Icon(move(bitmap1))
|
2019-03-24 04:28:36 +01:00
|
|
|
{
|
|
|
|
|
if (bitmap2) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(bitmap2->width() == bitmap2->height());
|
2019-03-24 04:28:36 +01:00
|
|
|
int size = bitmap2->width();
|
|
|
|
|
set_bitmap_for_size(size, move(bitmap2));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 12:02:21 +13:00
|
|
|
const Gfx::Bitmap* IconImpl::bitmap_for_size(int size) const
|
2019-03-24 04:28:36 +01:00
|
|
|
{
|
|
|
|
|
auto it = m_bitmaps.find(size);
|
|
|
|
|
if (it != m_bitmaps.end())
|
|
|
|
|
return it->value.ptr();
|
|
|
|
|
|
|
|
|
|
int best_diff_so_far = INT32_MAX;
|
2020-02-06 11:56:38 +01:00
|
|
|
const Gfx::Bitmap* best_fit = nullptr;
|
2019-03-24 04:28:36 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 12:02:21 +13:00
|
|
|
void IconImpl::set_bitmap_for_size(int size, RefPtr<Gfx::Bitmap>&& bitmap)
|
2019-03-24 04:28:36 +01:00
|
|
|
{
|
|
|
|
|
if (!bitmap) {
|
|
|
|
|
m_bitmaps.remove(size);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_bitmaps.set(size, move(bitmap));
|
|
|
|
|
}
|
2019-03-25 14:46:37 +01:00
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
Icon Icon::default_icon(StringView name)
|
2019-03-25 14:46:37 +01:00
|
|
|
{
|
2021-11-06 16:25:29 +01:00
|
|
|
RefPtr<Gfx::Bitmap> bitmap16;
|
|
|
|
|
RefPtr<Gfx::Bitmap> bitmap32;
|
|
|
|
|
if (auto bitmap_or_error = Gfx::Bitmap::try_load_from_file(String::formatted("/res/icons/16x16/{}.png", name)); !bitmap_or_error.is_error())
|
|
|
|
|
bitmap16 = bitmap_or_error.release_value();
|
|
|
|
|
if (auto bitmap_or_error = Gfx::Bitmap::try_load_from_file(String::formatted("/res/icons/32x32/{}.png", name)); !bitmap_or_error.is_error())
|
|
|
|
|
bitmap32 = bitmap_or_error.release_value();
|
2020-03-07 12:02:21 +13:00
|
|
|
return Icon(move(bitmap16), move(bitmap32));
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-25 14:46:37 +01:00
|
|
|
}
|