DefaultTapHighlight.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2012, 2013 Research In Motion Limited. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #if USE(ACCELERATED_COMPOSITING)
  20. #include "DefaultTapHighlight.h"
  21. #include "GraphicsContext.h"
  22. #include "Path.h"
  23. #include "WebAnimation.h"
  24. #include "WebPage_p.h"
  25. #include <BlackBerryPlatformGraphicsContext.h>
  26. #include <BlackBerryPlatformMessageClient.h>
  27. #include <BlackBerryPlatformPath.h>
  28. using namespace WebCore;
  29. namespace BlackBerry {
  30. namespace WebKit {
  31. const double ActiveTextFadeAnimationDuration = 0.3;
  32. const double OverlayShrinkAnimationDuration = 0.5;
  33. const double OverlayInitialScale = 2.0;
  34. STATIC_LOCAL_STRING(s_fadeAnimationName, "fade");
  35. STATIC_LOCAL_STRING(s_shrinkAnimationName, "shrink");
  36. DefaultTapHighlight::DefaultTapHighlight(WebPagePrivate* page)
  37. : m_page(page)
  38. , m_visible(false)
  39. , m_shouldHideAfterScroll(false)
  40. {
  41. }
  42. DefaultTapHighlight::~DefaultTapHighlight()
  43. {
  44. }
  45. void DefaultTapHighlight::draw(const Platform::IntRectRegion& region, int red, int green, int blue, int alpha, bool hideAfterScroll, bool isStartOfSelection)
  46. {
  47. ASSERT(BlackBerry::Platform::webKitThreadMessageClient()->isCurrentThread());
  48. m_region = region;
  49. m_color = Color(red, green, blue, std::min(128, alpha));
  50. m_shouldHideAfterScroll = hideAfterScroll;
  51. FloatRect rect = IntRect(m_region.extents());
  52. if (rect.isEmpty())
  53. return;
  54. // Transparent color means disable the tap highlight.
  55. if (!m_color.alpha()) {
  56. hide();
  57. return;
  58. }
  59. {
  60. MutexLocker lock(m_mutex);
  61. m_visible = true;
  62. }
  63. if (!m_overlay) {
  64. m_overlay = adoptPtr(new WebOverlay(this));
  65. m_page->m_webPage->addOverlay(m_overlay.get());
  66. }
  67. m_overlay->removeAnimation(s_shrinkAnimationName);
  68. m_overlay->resetOverrides();
  69. m_overlay->setPosition(rect.location());
  70. m_overlay->setSize(rect.size());
  71. m_overlay->setDrawsContent(true);
  72. m_overlay->removeAnimation(s_fadeAnimationName);
  73. m_overlay->setOpacity(1.0);
  74. m_overlay->invalidate();
  75. // Animate overlay scale to indicate selection is started.
  76. if (isStartOfSelection) {
  77. WebAnimation shrinkAnimation = WebAnimation::shrinkAnimation(s_shrinkAnimationName, OverlayInitialScale, 1, OverlayShrinkAnimationDuration);
  78. m_overlay->addAnimation(shrinkAnimation);
  79. }
  80. }
  81. void DefaultTapHighlight::hide()
  82. {
  83. if (!m_overlay)
  84. return;
  85. {
  86. MutexLocker lock(m_mutex);
  87. if (!m_visible)
  88. return;
  89. m_visible = false;
  90. }
  91. // Since WebAnimation is not thread safe, we create a new one each time instead of reusing the same object on different
  92. // threads (that would introduce race conditions).
  93. WebAnimation fadeAnimation = WebAnimation::fadeAnimation(s_fadeAnimationName, 1.0, 0.0, ActiveTextFadeAnimationDuration);
  94. // Normally, this method is called on the WebKit thread, but it can also be
  95. // called from the compositing thread.
  96. if (BlackBerry::Platform::webKitThreadMessageClient()->isCurrentThread())
  97. m_overlay->addAnimation(fadeAnimation);
  98. else if (BlackBerry::Platform::userInterfaceThreadMessageClient()->isCurrentThread())
  99. m_overlay->override()->addAnimation(fadeAnimation);
  100. }
  101. void DefaultTapHighlight::notifyFlushRequired(const GraphicsLayer* layer)
  102. {
  103. m_page->notifyFlushRequired(layer);
  104. }
  105. void DefaultTapHighlight::paintContents(const GraphicsLayer*, GraphicsContext& c, GraphicsLayerPaintingPhase, const IntRect& /*inClip*/)
  106. {
  107. if (!m_region.numRects())
  108. return;
  109. Path path(m_region.boundaryPath());
  110. c.save();
  111. const Platform::IntRect& rect = m_region.extents();
  112. c.translate(-rect.x(), -rect.y());
  113. // Draw tap highlight
  114. c.setFillColor(m_color, ColorSpaceDeviceRGB);
  115. c.fillPath(path);
  116. Color darker = Color(m_color.red(), m_color.green(), m_color.blue()); // Get rid of alpha.
  117. c.setStrokeColor(darker, ColorSpaceDeviceRGB);
  118. c.setStrokeThickness(1);
  119. c.strokePath(path);
  120. c.restore();
  121. }
  122. } // namespace WebKit
  123. } // namespace BlackBerry
  124. #endif // USE(ACCELERATED_COMPOSITING)