From 159f90a42c689ca63ef45067c7b9a4483362f1e7 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 24 May 2019 21:46:52 +0300 Subject: [PATCH] Added background analysis in main menu for better readability. --- Telegram/Resources/icons/menu_shadow.png | Bin 0 -> 1182 bytes Telegram/Resources/icons/menu_shadow@2x.png | Bin 0 -> 1603 bytes Telegram/Resources/icons/menu_shadow@3x.png | Bin 0 -> 2240 bytes Telegram/SourceFiles/window/window.style | 1 + .../SourceFiles/window/window_main_menu.cpp | 93 +++++++++++++++--- .../SourceFiles/window/window_main_menu.h | 3 + 6 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 Telegram/Resources/icons/menu_shadow.png create mode 100644 Telegram/Resources/icons/menu_shadow@2x.png create mode 100644 Telegram/Resources/icons/menu_shadow@3x.png diff --git a/Telegram/Resources/icons/menu_shadow.png b/Telegram/Resources/icons/menu_shadow.png new file mode 100644 index 0000000000000000000000000000000000000000..fabf8b6e74fa7548a0f0527455c02d8853cb3fa1 GIT binary patch literal 1182 zcmaJ=O=uHA6y6vNT8K#TU|SEvmWo1?&8A73U7EH@Hrk+DN*ZWNrFOeJY1ejl*4?Q| zi-({m4~jpah{cm2_=gsXf}qu+R1gFYf(3gK1VPY?=Q@+W7BTK&_PyQj``-6v-dq?O zJnC&~ZDJV4n;wud4C87hv9@amS>KEvIzV16IF-Z0D31$@4Vk!xCLu^0$`s5%MVmkU z4)!pN`=Fl9;hcP2RFUCVD8^qjEW&1(p1z``s520QNjRmOG4|*4mn_h=7&{V{1G1Hb z)B3=I4Tl#7v+BZ(Drjt9FX$K%GDnuhwCP~QRAE|*u|&nM>zWwh zp#A{@|M8fRQ&q#=JhLk_MZ1#z1ci4-fKVESH3-T$+e4jnc;iwrO_M9uFs{} ziPhOtzvtbbJP*9Nw#(jUcU&}F^PKp&vd%0~M1KUd^+$e*x8CEX34>;r$^ZTNuN__r z)Z8x&0;WQ_mbH?g(a@o@RuzLlrmvx^r`ZoGalR&p^do1V_tYB)#xO{WH>M~R7Ze*lBb Bd~E;# literal 0 HcmV?d00001 diff --git a/Telegram/Resources/icons/menu_shadow@2x.png b/Telegram/Resources/icons/menu_shadow@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..f1f8e9e4cfb078332c333f6a69243d1efe27ef9a GIT binary patch literal 1603 zcmbtTO=uit82&bC*HXLUNz$Z;aUvy!?9R;oW`?Zk?oQS&y6duCND#!G%zW7$vh(B2 z#NDJIg9oWnu|m~@P$S|&1w)!c1rMfzi$if<9wEnCL?&1Bu znSGw;ecqpWrdW6&aB%n_LMV_sA(jvtxDW0h9TFFMtaH+scN&s~QA#o9 zFqPA!DlTD3S$h0qJc-cYQMEkl%;p~wWJ3!{1QTj%Ca@8joNAhqynr2Q4p&t@N$*}R#-HTA@@g-;xBULCuw7m< zF;l{}v1rLKxcO0%$`mpdmK?(>8%Dj`#bV8H47+BSR3<~Z##3W6s;(Ff`|kxlc7OghSO(>%|`qUmrX!=@7vE)z+0xuPL2YFKx=T;&!w z)t5`wK{FwOwZE4{U%d`j>F;HtFP8x`3>ofi!|uL<4IcT^B z`LPZgZ^3H2E6?75?`%CMrpnDPzq^{c>jU=C^rfSlH&>4wpL_0w(=Q629(nDR)o;?P zcX}VtzXkW@z3pS@>|3Yyp`j6UCV<4J8*wx?g3bjxvE@fketpL8Uhr*fA$QGJncAtN zVw}Ja@UQz;>WGb_yRGct@hhlON7S~5z2>fc`{-2!|Be;DAO|oYzI$ow=h!g1G9Eyi zeuM|x5wzjniy#E|Yxl_4-~No4*j$hP!4WXQT{pU;t=7fQiT=5&N)BMOBY?(-Mt)oO zfnf3c8{{xIwwhIvX=U<3ai}zESw)eZz^8>VDR2*6zzV}TJ;*xtNhnW{g)pPyY&mC$1pSZY-E^d zb_mcIm}>g6R|Cu(M!EOH<^O=~Ek)ZghyWWR=sq@N7asP;2Chbm^6rP;LguCm;ydZeQ@;b63*ONH literal 0 HcmV?d00001 diff --git a/Telegram/Resources/icons/menu_shadow@3x.png b/Telegram/Resources/icons/menu_shadow@3x.png new file mode 100644 index 0000000000000000000000000000000000000000..15cf662619e9c7704cc96867b889aa80aa4b63c0 GIT binary patch literal 2240 zcmd5+U1%It6h6tukQO3BQL!vKOd+);yEC)dzZtSNo0&GH*f*^1bDJbVxUMoXu(3P3CiFfcwcNIh@e{oqr2dyMEcat1Gwrr{$fT6h6* zCC6Ao6=YZ|=f6Ue6g6g7Yot~_t(e$J8Z0N-ay*I#m^{()409O~ZUHUXZh`;p^2a=9 zTLu1XrYx2{71ixit3EojI$JeYmrdE?PdvpAYyPW=bRfTtpXqND)gDT1)d92 z$Z~<-ZmL$E<5cV;E|bhAOevk=a&l72PNZddoJ)yPUJ&zwG?9?9N?KG>DXx9-w3=@% zDiv*}U5maI_&On;A_&c9GufO-Vt+}HWLXx(l#og#XhtGfaf#7NxWSQ#K|_J*+a9s8 z%P~e{0XIm2r=4z(;CLNcH)yYkE=*_{o**SfHd2&WF7Kb}IGyZ(R8Y6yJ&A+riid;> z3UI?W>A@`?VWm7p^^rlaU&Z)bw8gnPCOD{Lk5g5)HJN+ztnFI385|2S<+4(817f%) zDrp6tvLtQWQgSnDPS5DsoI0JBBwe0P6;nk?)iTpLP1B`pgw?RwaF9zPtkuQldSaOz z9FJDkkZ(VatQj9WTsXF3_wFU#ldp}ndiRp)i4|xw0z2FthaI`7g4kn6y7Z+Ze&kZc z`&8Pzegja8(~wU?Yw?Fi zx7}YpP5u45c60sAhLvX)zj~p) zrF+cI!mI|fjhp(8Da51K8^ z;}7oG#{VD`3L@+-)CoJMuB+dMyNLZrmF`p`{%-R`1sWu7h~axvJ^&q_tqtVscWx748})B U;`vwBSJ)4+q|a*aP0zph4+?5m1poj5 literal 0 HcmV?d00001 diff --git a/Telegram/SourceFiles/window/window.style b/Telegram/SourceFiles/window/window.style index bb046e10ac..0ffe156e52 100644 --- a/Telegram/SourceFiles/window/window.style +++ b/Telegram/SourceFiles/window/window.style @@ -128,6 +128,7 @@ mainMenu: Menu(defaultMenu) { } itemToggleShift: 11px; } +mainMenuShadow: icon {{ "menu_shadow", windowShadowFg }}; mainMenuNewGroup: icon {{ "menu_new_group", menuIconFg }}; mainMenuNewGroupOver: icon {{ "menu_new_group", menuIconFgOver }}; mainMenuNewChannel: icon {{ "menu_new_channel", menuIconFg }}; diff --git a/Telegram/SourceFiles/window/window_main_menu.cpp b/Telegram/SourceFiles/window/window_main_menu.cpp index b85dbe0517..8add93a948 100644 --- a/Telegram/SourceFiles/window/window_main_menu.cpp +++ b/Telegram/SourceFiles/window/window_main_menu.cpp @@ -33,6 +33,30 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_settings.h" #include "styles/style_boxes.h" +namespace { + +constexpr auto kMinDiffIntensity = 0.25; + +float64 IntensityOfColor(QColor color) { + return (0.299 * color.red() + + 0.587 * color.green() + + 0.114 * color.blue()) / 255.0; +} + +bool IsShadowShown(const QImage &img, const QRect r, float64 intensityText) { + for (auto x = r.x(); x < r.x() + r.width(); x++) { + for (auto y = r.y(); y < r.y() + r.height(); y++) { + const auto intensity = IntensityOfColor(QColor(img.pixel(x, y))); + if ((std::abs(intensity - intensityText)) < kMinDiffIntensity) { + return true; + } + } + } + return false; +} + +} + namespace Window { class MainMenu::ResetScaleButton : public Ui::AbstractButton { @@ -122,6 +146,7 @@ MainMenu::MainMenu( Window::Theme::ToggleNightMode(); Window::Theme::KeepApplied(); } + refreshBackground(); } }); @@ -130,6 +155,7 @@ MainMenu::MainMenu( emit action->triggered(); }); refreshMenu(); + refreshBackground(); _telegram->setRichText(textcmdLink(1, qsl("Telegram Desktop"))); _telegram->setLink(1, std::make_shared(qsl("https://desktop.telegram.org"))); @@ -148,6 +174,7 @@ MainMenu::MainMenu( subscribe(Window::Theme::Background(), [this](const Window::Theme::BackgroundUpdate &update) { if (update.type == Window::Theme::BackgroundUpdate::Type::ApplyingTheme) { refreshMenu(); + refreshBackground(); } }); updatePhone(); @@ -220,6 +247,45 @@ void MainMenu::refreshMenu() { updatePhone(); } +void MainMenu::refreshBackground() { + const auto fill = QRect(0, 0, width(), st::mainMenuCoverHeight); + const auto intensityText = IntensityOfColor(st::mainMenuCoverFg->c); + QImage backgroundImage( + st::mainMenuWidth * cIntRetinaFactor(), + st::mainMenuCoverHeight * cIntRetinaFactor(), + QImage::Format_ARGB32_Premultiplied); + QPainter p(&backgroundImage); + + // Solid color. + if (const auto color = Window::Theme::Background()->colorForFill()) { + const auto intensity = IntensityOfColor(*color); + _isShadowShown = + (std::abs(intensity - intensityText) < kMinDiffIntensity); + p.fillRect(fill, *color); + _background = backgroundImage; + return; + } + + // Background image. + const auto &pixmap = Window::Theme::Background()->pixmap(); + QRect to, from; + Window::Theme::ComputeBackgroundRects(fill, pixmap.size(), to, from); + + p.drawPixmap(to, pixmap, from); + _background = backgroundImage; + + // Cut off the part of the background that is under text. + const QRect underText( + st::mainMenuCoverTextLeft, + st::mainMenuCoverNameTop, + std::max( + st::semiboldFont->width(Auth().user()->nameText.toString()), + st::normalFont->width(_phoneText)), + st::semiboldFont->height * 2); + + _isShadowShown = IsShadowShown(backgroundImage, underText, intensityText); +} + void MainMenu::resizeEvent(QResizeEvent *e) { _menu->setForceWidth(width()); updateControlsGeometry(); @@ -248,25 +314,30 @@ void MainMenu::updatePhone() { void MainMenu::paintEvent(QPaintEvent *e) { Painter p(this); const auto clip = e->rect(); - const auto fill = QRect(0, 0, width(), st::mainMenuCoverHeight); - const auto cover = fill.intersected(clip); + const auto cover = QRect(0, 0, width(), st::mainMenuCoverHeight) + .intersected(e->rect()); - if (const auto color = Window::Theme::Background()->colorForFill()) { - p.fillRect(fill, *color); - } else { + if (!_background.isNull()) { PainterHighQualityEnabler hq(p); - - const auto &pix = Window::Theme::Background()->pixmap(); - QRect to, from; - Window::Theme::ComputeBackgroundRects(fill, pix.size(), to, from); - p.drawPixmap(to, pix, from); + p.drawImage(0, 0, _background); } if (!cover.isEmpty()) { const auto widthText = _cloudButton ? _cloudButton->x() - st::mainMenuCloudSize : width() - 2 * st::mainMenuCoverTextLeft; - p.fillRect(cover, QColor(0, 0, 0, 51)); // 20% opacity. + + if (_isShadowShown) { + st::mainMenuShadow.paint( + p, + 0, + st::mainMenuCoverHeight - st::mainMenuShadow.height(), + st::mainMenuWidth, + IntensityOfColor(st::mainMenuCoverFg->c) < 0.5 + ? Qt::white + : Qt::black); + } + p.setPen(st::mainMenuCoverFg); p.setFont(st::semiboldFont); Auth().user()->nameText.drawLeftElided( diff --git a/Telegram/SourceFiles/window/window_main_menu.h b/Telegram/SourceFiles/window/window_main_menu.h index 8f182e9700..65a1487050 100644 --- a/Telegram/SourceFiles/window/window_main_menu.h +++ b/Telegram/SourceFiles/window/window_main_menu.h @@ -38,6 +38,7 @@ private: void updatePhone(); void initResetScaleButton(); void refreshMenu(); + void refreshBackground(); class ResetScaleButton; not_null _controller; @@ -51,6 +52,8 @@ private: base::Timer _nightThemeSwitch; QString _phoneText; + QImage _background; + bool _isShadowShown = false; };