Palette.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Badge.h>
  7. #include <LibGfx/Palette.h>
  8. #include <string.h>
  9. namespace Gfx {
  10. NonnullRefPtr<PaletteImpl> PaletteImpl::create_with_anonymous_buffer(Core::AnonymousBuffer buffer)
  11. {
  12. return adopt_ref(*new PaletteImpl(move(buffer)));
  13. }
  14. PaletteImpl::PaletteImpl(Core::AnonymousBuffer buffer)
  15. : m_theme_buffer(move(buffer))
  16. {
  17. }
  18. Palette::Palette(const PaletteImpl& impl)
  19. : m_impl(impl)
  20. {
  21. }
  22. Palette::~Palette()
  23. {
  24. }
  25. int PaletteImpl::metric(MetricRole role) const
  26. {
  27. VERIFY((int)role < (int)MetricRole::__Count);
  28. return theme().metric[(int)role];
  29. }
  30. String PaletteImpl::path(PathRole role) const
  31. {
  32. VERIFY((int)role < (int)PathRole::__Count);
  33. return theme().path[(int)role];
  34. }
  35. NonnullRefPtr<PaletteImpl> PaletteImpl::clone() const
  36. {
  37. auto new_theme_buffer = Core::AnonymousBuffer::create_with_size(m_theme_buffer.size());
  38. memcpy(new_theme_buffer.data<SystemTheme>(), &theme(), m_theme_buffer.size());
  39. return adopt_ref(*new PaletteImpl(move(new_theme_buffer)));
  40. }
  41. void Palette::set_color(ColorRole role, Color color)
  42. {
  43. if (m_impl->ref_count() != 1)
  44. m_impl = m_impl->clone();
  45. auto& theme = const_cast<SystemTheme&>(impl().theme());
  46. theme.color[(int)role] = color.value();
  47. }
  48. void Palette::set_metric(MetricRole role, int value)
  49. {
  50. if (m_impl->ref_count() != 1)
  51. m_impl = m_impl->clone();
  52. auto& theme = const_cast<SystemTheme&>(impl().theme());
  53. theme.metric[(int)role] = value;
  54. }
  55. void Palette::set_path(PathRole role, String path)
  56. {
  57. if (m_impl->ref_count() != 1)
  58. m_impl = m_impl->clone();
  59. auto& theme = const_cast<SystemTheme&>(impl().theme());
  60. memcpy(theme.path[(int)role], path.characters(), min(path.length() + 1, sizeof(theme.path[(int)role])));
  61. theme.path[(int)role][sizeof(theme.path[(int)role]) - 1] = '\0';
  62. }
  63. PaletteImpl::~PaletteImpl()
  64. {
  65. }
  66. void PaletteImpl::replace_internal_buffer(Badge<GUI::Application>, Core::AnonymousBuffer buffer)
  67. {
  68. m_theme_buffer = move(buffer);
  69. }
  70. }