Palette.cpp 2.3 KB

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