PickerLook.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "ui/PickerLook.h"
  2. #include "ui/Theme.h"
  3. #if JUCE_WINDOWS
  4. #ifndef WIN32_LEAN_AND_MEAN
  5. #define WIN32_LEAN_AND_MEAN
  6. #endif
  7. #ifndef NOMINMAX
  8. #define NOMINMAX
  9. #endif
  10. #include <windows.h>
  11. #endif
  12. namespace lynx::pickerlook
  13. {
  14. // (FL's form is see-through: the desktop and FL show through its veil)
  15. juce::Image captureScreen (juce::Rectangle<int> physical)
  16. {
  17. #if JUCE_WINDOWS
  18. if (physical.isEmpty())
  19. return {};
  20. const auto w = physical.getWidth(), h = physical.getHeight();
  21. auto* screen = GetDC (nullptr);
  22. auto* dc = CreateCompatibleDC (screen);
  23. BITMAPINFO info {};
  24. info.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
  25. info.bmiHeader.biWidth = w;
  26. info.bmiHeader.biHeight = -h;
  27. info.bmiHeader.biPlanes = 1;
  28. info.bmiHeader.biBitCount = 32;
  29. info.bmiHeader.biCompression = BI_RGB;
  30. void* bits = nullptr;
  31. auto* bitmap = CreateDIBSection (dc, &info, DIB_RGB_COLORS, &bits, nullptr, 0);
  32. juce::Image out;
  33. if (bitmap != nullptr && bits != nullptr)
  34. {
  35. auto* old = SelectObject (dc, bitmap);
  36. if (BitBlt (dc, 0, 0, w, h, screen, physical.getX(), physical.getY(), SRCCOPY))
  37. {
  38. GdiFlush();
  39. out = juce::Image (juce::Image::RGB, w, h, false, juce::SoftwareImageType());
  40. juce::Image::BitmapData data (out, juce::Image::BitmapData::writeOnly);
  41. for (int y = 0; y < h; ++y)
  42. {
  43. const auto* row = static_cast<const std::uint8_t*> (bits) + (std::size_t) y * (std::size_t) w * 4;
  44. auto* dest = data.getLinePointer (y);
  45. for (int x = 0; x < w; ++x)
  46. {
  47. // (JUCE's RGB images are B, G, R in memory on little-endian machines, as the DIB is)
  48. dest[x * data.pixelStride + 0] = row[x * 4 + 0];
  49. dest[x * data.pixelStride + 1] = row[x * 4 + 1];
  50. dest[x * data.pixelStride + 2] = row[x * 4 + 2];
  51. }
  52. }
  53. }
  54. SelectObject (dc, old);
  55. DeleteObject (bitmap);
  56. }
  57. DeleteDC (dc);
  58. ReleaseDC (nullptr, screen);
  59. return out;
  60. #else
  61. juce::ignoreUnused (physical);
  62. return {};
  63. #endif
  64. }
  65. void paintVeil (juce::Graphics& g, const juce::Component& owner, const juce::Image& backdrop, juce::Image& backdropShown, double pixelScale)
  66. {
  67. // FL: the screen shows through a veil of 236/256, a vertical blend of two dark tones that also darkens towards
  68. // the top and bottom edges over 96 px (cubic). Composed once per size: a hover repaints only a small area.
  69. const auto w = juce::jmax (1, juce::roundToInt (owner.getWidth() * pixelScale)), h = juce::jmax (1, juce::roundToInt (owner.getHeight() * pixelScale));
  70. if (! backdropShown.isValid() || backdropShown.getWidth() != w || backdropShown.getHeight() != h)
  71. {
  72. backdropShown = juce::Image (juce::Image::RGB, w, h, false);
  73. juce::Graphics b (backdropShown);
  74. b.fillAll (juce::Colours::black);
  75. if (backdrop.isValid())
  76. b.drawImage (backdrop, backdropShown.getBounds().toFloat());
  77. const auto top = juce::Colour (0x23, 0x20, 0x28), bottom = juce::Colour (0x17, 0x22, 0x28); // (FL's two tones)
  78. const auto edge = 96.0 * pixelScale;
  79. for (int y = 0; y < h; ++y)
  80. {
  81. auto c = top.interpolatedWith (bottom, (float) y / (float) juce::jmax (1, h - 1));
  82. double k = 1.0;
  83. if (y < edge)
  84. {
  85. const auto f = 1.0 - y / edge;
  86. k = 1.0 - f * f * f;
  87. }
  88. else if (y > h - edge)
  89. {
  90. const auto f = (y - (h - edge)) / edge;
  91. k = 1.0 - f * f * f;
  92. }
  93. c = juce::Colour::fromFloatRGBA (c.getFloatRed() * (float) k, c.getFloatGreen() * (float) k, c.getFloatBlue() * (float) k, 236.0f / 256.0f);
  94. b.setColour (c);
  95. b.fillRect (0, y, w, 1);
  96. }
  97. }
  98. g.drawImage (backdropShown, owner.getLocalBounds().toFloat());
  99. }
  100. // FL's highlight behind the tile pointed at: the tile ± 3 px and 6 % of its width, the row's height + 14 px above and
  101. // below, rounded; the tab pointed at likewise (± 18 px, no widening)
  102. juce::Rectangle<float> highlight (juce::Rectangle<int> b, int rowHeight, bool isTab)
  103. {
  104. const auto widen = isTab ? 3.0f : 3.0f + juce::roundToInt (b.getWidth() * 0.06f) + 1.0f;
  105. const auto half = rowHeight / 2.0f + (isTab ? 18.0f : 14.0f);
  106. return { (float) b.getX() - widen, (float) b.getCentreY() - half, (float) b.getWidth() + 2.0f * widen, 2.0f * half };
  107. }
  108. void paintHighlight (juce::Graphics& g, juce::Rectangle<float> box, bool pressed)
  109. {
  110. // FL (measured on its hover): a cool tint, strongest under the top edge, a bright glow at the middle of the top
  111. // edge and of each side, and a thin rim. Held down, it takes the warm press colour (LYNX: the accent).
  112. using namespace theme;
  113. const auto base = pressed ? colour::accent : juce::Colour (0xff6cc4e8);
  114. const auto light = pressed ? colour::accent.brighter (0.7f) : juce::Colour (0xffb3ffff);
  115. juce::Graphics::ScopedSaveState keep (g);
  116. juce::Path shape;
  117. shape.addRoundedRectangle (box, 9.0f);
  118. juce::ColourGradient fill (base.withAlpha (pressed ? 0.55f : 0.30f), 0.0f, box.getY(), base.withAlpha (pressed ? 0.30f : 0.13f), 0.0f, box.getBottom(), false);
  119. fill.addColour (0.55, base.withAlpha (pressed ? 0.26f : 0.10f));
  120. g.setGradientFill (fill);
  121. g.fillPath (shape);
  122. g.reduceClipRegion (shape);
  123. const auto glow = [&] (float x, float y, float rx, float a) {
  124. g.setGradientFill (juce::ColourGradient (light.withAlpha (a), x, y, light.withAlpha (0.0f), x + rx, y, true));
  125. g.fillEllipse (x - rx, y - rx, rx * 2.0f, rx * 2.0f);
  126. };
  127. glow (box.getCentreX(), box.getY(), juce::jmin (box.getWidth() * 0.32f, 60.0f), 0.55f);
  128. glow (box.getX(), box.getCentreY(), 14.0f, 0.35f);
  129. glow (box.getRight(), box.getCentreY(), 14.0f, 0.35f);
  130. g.setColour (base.withAlpha (pressed ? 0.9f : 0.6f));
  131. g.strokePath (shape, juce::PathStrokeType (1.2f));
  132. juce::ColourGradient line (light.withAlpha (0.0f), box.getX(), 0.0f, light.withAlpha (0.0f), box.getRight(), 0.0f, false);
  133. line.addColour (0.5, light);
  134. g.setGradientFill (line);
  135. g.fillRect (box.getX(), box.getY(), box.getWidth(), 1.5f);
  136. }
  137. } // namespace lynx::pickerlook