Serenity Operating System
1/*
2 * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2021, Julius Heijmen <julius.heijmen@gmail.com>
4 * Copyright (c) 2022, the SerenityOS developers.
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 */
8
9#pragma once
10
11#include <AK/HashMap.h>
12#include <AK/NonnullRefPtr.h>
13#include <AK/RefCounted.h>
14#include <LibGfx/Bitmap.h>
15
16namespace GUI {
17
18class IconImpl : public RefCounted<IconImpl> {
19public:
20 static NonnullRefPtr<IconImpl> create() { return adopt_ref(*new IconImpl); }
21 ~IconImpl() = default;
22
23 Gfx::Bitmap const* bitmap_for_size(int) const;
24 void set_bitmap_for_size(int, RefPtr<Gfx::Bitmap const>&&);
25
26 Vector<int> sizes() const
27 {
28 Vector<int> sizes;
29 for (auto& it : m_bitmaps)
30 sizes.append(it.key);
31 return sizes;
32 }
33
34private:
35 IconImpl() = default;
36 HashMap<int, RefPtr<Gfx::Bitmap const>> m_bitmaps;
37};
38
39class Icon {
40public:
41 Icon();
42 explicit Icon(RefPtr<Gfx::Bitmap const>&&);
43 explicit Icon(RefPtr<Gfx::Bitmap const>&&, RefPtr<Gfx::Bitmap const>&&);
44 explicit Icon(IconImpl const&);
45 Icon(Icon const&);
46 ~Icon() = default;
47
48 static Icon default_icon(StringView);
49 static ErrorOr<Icon> try_create_default_icon(StringView);
50
51 Icon& operator=(Icon const& other)
52 {
53 if (this != &other)
54 m_impl = other.m_impl;
55 return *this;
56 }
57
58 Gfx::Bitmap const* bitmap_for_size(int size) const { return m_impl->bitmap_for_size(size); }
59 void set_bitmap_for_size(int size, RefPtr<Gfx::Bitmap const>&& bitmap) { m_impl->set_bitmap_for_size(size, move(bitmap)); }
60
61 IconImpl const& impl() const { return *m_impl; }
62
63 Vector<int> sizes() const { return m_impl->sizes(); }
64
65private:
66 NonnullRefPtr<IconImpl> m_impl;
67};
68
69}