GIcon.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <LibGUI/GIcon.h>
  2. GIcon::GIcon()
  3. : m_impl(GIconImpl::create())
  4. {
  5. }
  6. GIcon::GIcon(const GIconImpl& impl)
  7. : m_impl(const_cast<GIconImpl&>(impl))
  8. {
  9. }
  10. GIcon::GIcon(const GIcon& other)
  11. : m_impl(other.m_impl)
  12. {
  13. }
  14. GIcon::GIcon(RefPtr<GraphicsBitmap>&& bitmap)
  15. : GIcon()
  16. {
  17. if (bitmap) {
  18. ASSERT(bitmap->width() == bitmap->height());
  19. int size = bitmap->width();
  20. set_bitmap_for_size(size, move(bitmap));
  21. }
  22. }
  23. GIcon::GIcon(RefPtr<GraphicsBitmap>&& bitmap1, RefPtr<GraphicsBitmap>&& bitmap2)
  24. : GIcon(move(bitmap1))
  25. {
  26. if (bitmap2) {
  27. ASSERT(bitmap2->width() == bitmap2->height());
  28. int size = bitmap2->width();
  29. set_bitmap_for_size(size, move(bitmap2));
  30. }
  31. }
  32. const GraphicsBitmap* GIconImpl::bitmap_for_size(int size) const
  33. {
  34. auto it = m_bitmaps.find(size);
  35. if (it != m_bitmaps.end())
  36. return it->value.ptr();
  37. int best_diff_so_far = INT32_MAX;
  38. const GraphicsBitmap* best_fit = nullptr;
  39. for (auto& it : m_bitmaps) {
  40. int abs_diff = abs(it.key - size);
  41. if (abs_diff < best_diff_so_far) {
  42. best_diff_so_far = abs_diff;
  43. best_fit = it.value.ptr();
  44. }
  45. }
  46. return best_fit;
  47. }
  48. void GIconImpl::set_bitmap_for_size(int size, RefPtr<GraphicsBitmap>&& bitmap)
  49. {
  50. if (!bitmap) {
  51. m_bitmaps.remove(size);
  52. return;
  53. }
  54. m_bitmaps.set(size, move(bitmap));
  55. }
  56. GIcon GIcon::default_icon(const StringView& name)
  57. {
  58. auto bitmap16 = GraphicsBitmap::load_from_file(String::format("/res/icons/16x16/%s.png", String(name).characters()));
  59. auto bitmap32 = GraphicsBitmap::load_from_file(String::format("/res/icons/32x32/%s.png", String(name).characters()));
  60. return GIcon(move(bitmap16), move(bitmap32));
  61. }