StyleValue.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/ByteBuffer.h>
  27. #include <LibGfx/Palette.h>
  28. #include <LibWeb/CSS/StyleValue.h>
  29. #include <LibWeb/DOM/Document.h>
  30. #include <LibWeb/InProcessWebView.h>
  31. #include <LibWeb/Loader/LoadRequest.h>
  32. #include <LibWeb/Loader/ResourceLoader.h>
  33. #include <LibWeb/Page/Frame.h>
  34. namespace Web::CSS {
  35. StyleValue::StyleValue(Type type)
  36. : m_type(type)
  37. {
  38. }
  39. StyleValue::~StyleValue()
  40. {
  41. }
  42. String IdentifierStyleValue::to_string() const
  43. {
  44. return CSS::string_from_value_id(m_id);
  45. }
  46. Color IdentifierStyleValue::to_color(const DOM::Document& document) const
  47. {
  48. if (id() == CSS::ValueID::LibwebLink)
  49. return document.link_color();
  50. VERIFY(document.page());
  51. auto palette = document.page()->palette();
  52. switch (id()) {
  53. case CSS::ValueID::LibwebPaletteDesktopBackground:
  54. return palette.color(ColorRole::DesktopBackground);
  55. case CSS::ValueID::LibwebPaletteActiveWindowBorder1:
  56. return palette.color(ColorRole::ActiveWindowBorder1);
  57. case CSS::ValueID::LibwebPaletteActiveWindowBorder2:
  58. return palette.color(ColorRole::ActiveWindowBorder2);
  59. case CSS::ValueID::LibwebPaletteActiveWindowTitle:
  60. return palette.color(ColorRole::ActiveWindowTitle);
  61. case CSS::ValueID::LibwebPaletteInactiveWindowBorder1:
  62. return palette.color(ColorRole::InactiveWindowBorder1);
  63. case CSS::ValueID::LibwebPaletteInactiveWindowBorder2:
  64. return palette.color(ColorRole::InactiveWindowBorder2);
  65. case CSS::ValueID::LibwebPaletteInactiveWindowTitle:
  66. return palette.color(ColorRole::InactiveWindowTitle);
  67. case CSS::ValueID::LibwebPaletteMovingWindowBorder1:
  68. return palette.color(ColorRole::MovingWindowBorder1);
  69. case CSS::ValueID::LibwebPaletteMovingWindowBorder2:
  70. return palette.color(ColorRole::MovingWindowBorder2);
  71. case CSS::ValueID::LibwebPaletteMovingWindowTitle:
  72. return palette.color(ColorRole::MovingWindowTitle);
  73. case CSS::ValueID::LibwebPaletteHighlightWindowBorder1:
  74. return palette.color(ColorRole::HighlightWindowBorder1);
  75. case CSS::ValueID::LibwebPaletteHighlightWindowBorder2:
  76. return palette.color(ColorRole::HighlightWindowBorder2);
  77. case CSS::ValueID::LibwebPaletteHighlightWindowTitle:
  78. return palette.color(ColorRole::HighlightWindowTitle);
  79. case CSS::ValueID::LibwebPaletteMenuStripe:
  80. return palette.color(ColorRole::MenuStripe);
  81. case CSS::ValueID::LibwebPaletteMenuBase:
  82. return palette.color(ColorRole::MenuBase);
  83. case CSS::ValueID::LibwebPaletteMenuBaseText:
  84. return palette.color(ColorRole::MenuBaseText);
  85. case CSS::ValueID::LibwebPaletteMenuSelection:
  86. return palette.color(ColorRole::MenuSelection);
  87. case CSS::ValueID::LibwebPaletteMenuSelectionText:
  88. return palette.color(ColorRole::MenuSelectionText);
  89. case CSS::ValueID::LibwebPaletteWindow:
  90. return palette.color(ColorRole::Window);
  91. case CSS::ValueID::LibwebPaletteWindowText:
  92. return palette.color(ColorRole::WindowText);
  93. case CSS::ValueID::LibwebPaletteButton:
  94. return palette.color(ColorRole::Button);
  95. case CSS::ValueID::LibwebPaletteButtonText:
  96. return palette.color(ColorRole::ButtonText);
  97. case CSS::ValueID::LibwebPaletteBase:
  98. return palette.color(ColorRole::Base);
  99. case CSS::ValueID::LibwebPaletteBaseText:
  100. return palette.color(ColorRole::BaseText);
  101. case CSS::ValueID::LibwebPaletteThreedHighlight:
  102. return palette.color(ColorRole::ThreedHighlight);
  103. case CSS::ValueID::LibwebPaletteThreedShadow1:
  104. return palette.color(ColorRole::ThreedShadow1);
  105. case CSS::ValueID::LibwebPaletteThreedShadow2:
  106. return palette.color(ColorRole::ThreedShadow2);
  107. case CSS::ValueID::LibwebPaletteHoverHighlight:
  108. return palette.color(ColorRole::HoverHighlight);
  109. case CSS::ValueID::LibwebPaletteSelection:
  110. return palette.color(ColorRole::Selection);
  111. case CSS::ValueID::LibwebPaletteSelectionText:
  112. return palette.color(ColorRole::SelectionText);
  113. case CSS::ValueID::LibwebPaletteInactiveSelection:
  114. return palette.color(ColorRole::InactiveSelection);
  115. case CSS::ValueID::LibwebPaletteInactiveSelectionText:
  116. return palette.color(ColorRole::InactiveSelectionText);
  117. case CSS::ValueID::LibwebPaletteRubberBandFill:
  118. return palette.color(ColorRole::RubberBandFill);
  119. case CSS::ValueID::LibwebPaletteRubberBandBorder:
  120. return palette.color(ColorRole::RubberBandBorder);
  121. case CSS::ValueID::LibwebPaletteLink:
  122. return palette.color(ColorRole::Link);
  123. case CSS::ValueID::LibwebPaletteActiveLink:
  124. return palette.color(ColorRole::ActiveLink);
  125. case CSS::ValueID::LibwebPaletteVisitedLink:
  126. return palette.color(ColorRole::VisitedLink);
  127. case CSS::ValueID::LibwebPaletteRuler:
  128. return palette.color(ColorRole::Ruler);
  129. case CSS::ValueID::LibwebPaletteRulerBorder:
  130. return palette.color(ColorRole::RulerBorder);
  131. case CSS::ValueID::LibwebPaletteRulerActiveText:
  132. return palette.color(ColorRole::RulerActiveText);
  133. case CSS::ValueID::LibwebPaletteRulerInactiveText:
  134. return palette.color(ColorRole::RulerInactiveText);
  135. case CSS::ValueID::LibwebPaletteTextCursor:
  136. return palette.color(ColorRole::TextCursor);
  137. case CSS::ValueID::LibwebPaletteFocusOutline:
  138. return palette.color(ColorRole::FocusOutline);
  139. case CSS::ValueID::LibwebPaletteSyntaxComment:
  140. return palette.color(ColorRole::SyntaxComment);
  141. case CSS::ValueID::LibwebPaletteSyntaxNumber:
  142. return palette.color(ColorRole::SyntaxNumber);
  143. case CSS::ValueID::LibwebPaletteSyntaxString:
  144. return palette.color(ColorRole::SyntaxString);
  145. case CSS::ValueID::LibwebPaletteSyntaxType:
  146. return palette.color(ColorRole::SyntaxType);
  147. case CSS::ValueID::LibwebPaletteSyntaxPunctuation:
  148. return palette.color(ColorRole::SyntaxPunctuation);
  149. case CSS::ValueID::LibwebPaletteSyntaxOperator:
  150. return palette.color(ColorRole::SyntaxOperator);
  151. case CSS::ValueID::LibwebPaletteSyntaxKeyword:
  152. return palette.color(ColorRole::SyntaxKeyword);
  153. case CSS::ValueID::LibwebPaletteSyntaxControlKeyword:
  154. return palette.color(ColorRole::SyntaxControlKeyword);
  155. case CSS::ValueID::LibwebPaletteSyntaxIdentifier:
  156. return palette.color(ColorRole::SyntaxIdentifier);
  157. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  158. return palette.color(ColorRole::SyntaxPreprocessorStatement);
  159. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorValue:
  160. return palette.color(ColorRole::SyntaxPreprocessorValue);
  161. default:
  162. return {};
  163. }
  164. }
  165. ImageStyleValue::ImageStyleValue(const URL& url, DOM::Document& document)
  166. : StyleValue(Type::Image)
  167. , m_url(url)
  168. , m_document(document)
  169. {
  170. LoadRequest request;
  171. request.set_url(url);
  172. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  173. }
  174. void ImageStyleValue::resource_did_load()
  175. {
  176. if (!m_document)
  177. return;
  178. m_bitmap = resource()->bitmap();
  179. // FIXME: Do less than a full repaint if possible?
  180. if (m_document->frame())
  181. m_document->frame()->set_needs_display({});
  182. }
  183. }