separate_panel.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "ui/widgets/separate_panel.h"
  8. #include "ui/effects/ripple_animation.h"
  9. #include "ui/widgets/fields/input_field.h"
  10. #include "ui/widgets/menu/menu_add_action_callback.h"
  11. #include "ui/widgets/menu/menu_add_action_callback_factory.h"
  12. #include "ui/widgets/shadow.h"
  13. #include "ui/widgets/buttons.h"
  14. #include "ui/widgets/labels.h"
  15. #include "ui/widgets/tooltip.h"
  16. #include "ui/widgets/popup_menu.h"
  17. #include "ui/wrap/padding_wrap.h"
  18. #include "ui/wrap/fade_wrap.h"
  19. #include "ui/platform/ui_platform_utility.h"
  20. #include "ui/platform/ui_platform_window.h"
  21. #include "ui/layers/box_content.h"
  22. #include "ui/layers/layer_widget.h"
  23. #include "ui/layers/show.h"
  24. #include "ui/style/style_core_palette.h"
  25. #include "ui/painter.h"
  26. #include "ui/rect.h"
  27. #include "ui/qt_object_factory.h"
  28. #include "ui/qt_weak_factory.h"
  29. #include "ui/ui_utility.h"
  30. #include "base/platform/base_platform_info.h"
  31. #include "base/debug_log.h"
  32. #include "base/invoke_queued.h"
  33. #include "styles/style_widgets.h"
  34. #include "styles/style_layers.h"
  35. #include "styles/palette.h"
  36. #include <QtGui/QWindow>
  37. #include <QtGui/QScreen>
  38. #include <QtWidgets/QApplication>
  39. namespace Ui {
  40. namespace {
  41. void OverlayWidgetCache(QPainter &p, Ui::RpWidget *widget) {
  42. if (widget) {
  43. widget->show();
  44. p.drawPixmap(widget->pos(), GrabWidget(widget));
  45. widget->hide();
  46. }
  47. }
  48. class PanelShow final : public Show {
  49. public:
  50. explicit PanelShow(not_null<SeparatePanel*> panel);
  51. ~PanelShow();
  52. void showOrHideBoxOrLayer(
  53. std::variant<
  54. v::null_t,
  55. object_ptr<BoxContent>,
  56. std::unique_ptr<LayerWidget>> &&layer,
  57. LayerOptions options,
  58. anim::type animated) const override;
  59. [[nodiscard]] not_null<QWidget*> toastParent() const override;
  60. [[nodiscard]] bool valid() const override;
  61. operator bool() const override;
  62. private:
  63. const QPointer<SeparatePanel> _panel;
  64. };
  65. [[nodiscard]] std::unique_ptr<style::palette> MakeAdjustedPalette(
  66. QColor color) {
  67. auto result = std::make_unique<style::palette>();
  68. *result = *style::main_palette::get();
  69. const auto set = [](const style::color &color, QColor value) {
  70. color.set(
  71. uchar(value.red()),
  72. uchar(value.green()),
  73. uchar(value.blue()),
  74. uchar(value.alpha()));
  75. };
  76. const auto contrast = 2.5;
  77. const auto luminance = 0.2126 * color.redF()
  78. + 0.7152 * color.greenF()
  79. + 0.0722 * color.blueF();
  80. const auto textColor = (luminance > 0.5)
  81. ? QColor(0, 0, 0)
  82. : QColor(255, 255, 255);
  83. const auto textLuminance = (luminance > 0.5) ? 0 : 1;
  84. const auto adaptiveOpacity = (luminance - textLuminance + contrast)
  85. / contrast;
  86. const auto opacity = std::clamp(adaptiveOpacity, 0.5, 0.64);
  87. auto buttonColor = textColor;
  88. buttonColor.setAlphaF(opacity);
  89. auto rippleColor = textColor;
  90. rippleColor.setAlphaF(opacity * 0.1);
  91. set(result->windowFg(), textColor);
  92. set(result->boxTitleCloseFg(), buttonColor);
  93. set(result->boxTitleCloseFgOver(), buttonColor);
  94. set(result->windowBgOver(), rippleColor);
  95. result->finalize();
  96. return result;
  97. }
  98. PanelShow::PanelShow(not_null<SeparatePanel*> panel)
  99. : _panel(panel.get()) {
  100. }
  101. PanelShow::~PanelShow() = default;
  102. void PanelShow::showOrHideBoxOrLayer(
  103. std::variant<
  104. v::null_t,
  105. object_ptr<BoxContent>,
  106. std::unique_ptr<LayerWidget>> &&layer,
  107. LayerOptions options,
  108. anim::type animated) const {
  109. using UniqueLayer = std::unique_ptr<LayerWidget>;
  110. using ObjectBox = object_ptr<BoxContent>;
  111. if (auto layerWidget = std::get_if<UniqueLayer>(&layer)) {
  112. if (const auto panel = _panel.data()) {
  113. panel->showLayer(std::move(*layerWidget), options, animated);
  114. }
  115. } else if (auto box = std::get_if<ObjectBox>(&layer)) {
  116. if (const auto panel = _panel.data()) {
  117. panel->showBox(std::move(*box), options, animated);
  118. }
  119. } else if (const auto panel = _panel.data()) {
  120. panel->hideLayer(animated);
  121. }
  122. }
  123. not_null<QWidget*> PanelShow::toastParent() const {
  124. const auto panel = _panel.data();
  125. Ensures(panel != nullptr);
  126. return panel;
  127. }
  128. bool PanelShow::valid() const {
  129. return (_panel.data() != nullptr);
  130. }
  131. PanelShow::operator bool() const {
  132. return valid();
  133. }
  134. } // namespace
  135. class SeparatePanel::FullScreenButton : public RippleButton {
  136. public:
  137. FullScreenButton(QWidget *parent, const style::IconButton &st);
  138. void init(not_null<QWidget*> parentWindow);
  139. protected:
  140. void paintEvent(QPaintEvent *e) override;
  141. QImage prepareRippleMask() const override;
  142. QPoint prepareRippleStartPosition() const override;
  143. private:
  144. const style::IconButton &_st;
  145. };
  146. class SeparatePanel::ResizeEdge final : public RpWidget {
  147. public:
  148. ResizeEdge(not_null<QWidget*> parent, Qt::Edges edges);
  149. void updateSize();
  150. void setParentPadding(QMargins padding);
  151. private:
  152. void mousePressEvent(QMouseEvent *e) override;
  153. void mouseReleaseEvent(QMouseEvent *e) override;
  154. void mouseMoveEvent(QMouseEvent *e) override;
  155. void updateFromResize(QPoint delta);
  156. const Qt::Edges _edges;
  157. QMargins _extent;
  158. QRect _startGeometry;
  159. QPoint _startPosition;
  160. bool _press = false;
  161. bool _resizing = false;
  162. };
  163. SeparatePanel::FullScreenButton::FullScreenButton(
  164. QWidget *parent,
  165. const style::IconButton &st)
  166. : RippleButton(parent, st.ripple)
  167. , _st(st) {
  168. resize(_st.width, _st.height);
  169. }
  170. void SeparatePanel::FullScreenButton::paintEvent(QPaintEvent *e) {
  171. Painter p(this);
  172. auto hq = PainterHighQualityEnabler(p);
  173. p.setBrush(st::radialBg);
  174. p.setPen(Qt::NoPen);
  175. p.drawEllipse(rect());
  176. paintRipple(p, _st.rippleAreaPosition);
  177. const auto icon = &_st.icon;
  178. auto position = _st.iconPosition;
  179. if (position.x() < 0) {
  180. position.setX((width() - icon->width()) / 2);
  181. }
  182. if (position.y() < 0) {
  183. position.setY((height() - icon->height()) / 2);
  184. }
  185. icon->paint(p, position, width());
  186. }
  187. QPoint SeparatePanel::FullScreenButton::prepareRippleStartPosition() const {
  188. auto result = mapFromGlobal(QCursor::pos())
  189. - _st.rippleAreaPosition;
  190. auto rect = QRect(0, 0, _st.rippleAreaSize, _st.rippleAreaSize);
  191. return rect.contains(result)
  192. ? result
  193. : DisabledRippleStartPosition();
  194. }
  195. QImage SeparatePanel::FullScreenButton::prepareRippleMask() const {
  196. return RippleAnimation::EllipseMask(
  197. QSize(_st.rippleAreaSize, _st.rippleAreaSize));
  198. }
  199. SeparatePanel::ResizeEdge::ResizeEdge(
  200. not_null<QWidget*> parent,
  201. Qt::Edges edges)
  202. : RpWidget(parent)
  203. , _edges(edges) {
  204. show();
  205. setCursor([&] {
  206. if ((_edges == (Qt::LeftEdge | Qt::TopEdge))
  207. || (_edges == (Qt::RightEdge | Qt::BottomEdge))) {
  208. return Qt::SizeFDiagCursor;
  209. } else if (_edges == Qt::TopEdge || _edges == Qt::BottomEdge) {
  210. return Qt::SizeVerCursor;
  211. } else if ((_edges == (Qt::RightEdge | Qt::TopEdge))
  212. || (_edges == (Qt::LeftEdge | Qt::BottomEdge))) {
  213. return Qt::SizeBDiagCursor;
  214. } else if (_edges == Qt::RightEdge || _edges == Qt::LeftEdge) {
  215. return Qt::SizeHorCursor;
  216. } else {
  217. Unexpected("Bad edges in SeparatePanel::ResizeEdge.");
  218. }
  219. }());
  220. }
  221. void SeparatePanel::ResizeEdge::updateSize() {
  222. const auto parent = parentWidget()->rect();
  223. if ((_extent.left() + _extent.right() >= parent.width())
  224. || (_extent.top() + _extent.bottom() >= parent.height())) {
  225. return;
  226. }
  227. if (_edges == (Qt::LeftEdge | Qt::TopEdge)) {
  228. setGeometry(0, 0, _extent.left(), _extent.top());
  229. } else if (_edges == Qt::TopEdge) {
  230. setGeometry(
  231. _extent.left(),
  232. 0,
  233. parent.width() - _extent.left() - _extent.right(),
  234. _extent.top());
  235. } else if (_edges == (Qt::RightEdge | Qt::TopEdge)) {
  236. setGeometry(
  237. parent.width() - _extent.right(),
  238. 0,
  239. _extent.right(),
  240. _extent.top());
  241. } else if (_edges == Qt::RightEdge) {
  242. setGeometry(
  243. parent.width() - _extent.right(),
  244. _extent.top(),
  245. _extent.right(),
  246. parent.height() - _extent.top() - _extent.bottom());
  247. } else if (_edges == (Qt::RightEdge | Qt::BottomEdge)) {
  248. setGeometry(
  249. parent.width() - _extent.right(),
  250. parent.height() - _extent.bottom(),
  251. _extent.right(),
  252. _extent.bottom());
  253. } else if (_edges == Qt::BottomEdge) {
  254. setGeometry(
  255. _extent.left(),
  256. parent.height() - _extent.bottom(),
  257. parent.width() - _extent.left() - _extent.right(),
  258. _extent.bottom());
  259. } else if (_edges == (Qt::LeftEdge | Qt::BottomEdge)) {
  260. setGeometry(
  261. 0,
  262. parent.height() - _extent.bottom(),
  263. _extent.left(),
  264. _extent.bottom());
  265. } else if (_edges == Qt::LeftEdge) {
  266. setGeometry(
  267. 0,
  268. _extent.top(),
  269. _extent.left(),
  270. parent.height() - _extent.top() - _extent.bottom());
  271. } else {
  272. Unexpected("Corrupt edges in SeparatePanel::ResizeEdge.");
  273. }
  274. }
  275. void SeparatePanel::ResizeEdge::setParentPadding(QMargins padding) {
  276. if (_extent != padding) {
  277. _extent = padding;
  278. updateSize();
  279. }
  280. }
  281. void SeparatePanel::ResizeEdge::mousePressEvent(QMouseEvent *e) {
  282. if (e->button() == Qt::LeftButton) {
  283. _press = true;
  284. _startPosition = e->globalPos();
  285. _startGeometry = window()->geometry();
  286. }
  287. }
  288. void SeparatePanel::ResizeEdge::mouseReleaseEvent(QMouseEvent *e) {
  289. if (e->button() == Qt::LeftButton) {
  290. _press = false;
  291. _resizing = false;
  292. }
  293. }
  294. void SeparatePanel::ResizeEdge::mouseMoveEvent(QMouseEvent *e) {
  295. if (base::take(_press)) {
  296. if (const auto handle = window()->windowHandle()) {
  297. if (handle->startSystemResize(_edges)) {
  298. SendSynteticMouseEvent(
  299. this,
  300. QEvent::MouseButtonRelease,
  301. Qt::LeftButton);
  302. } else {
  303. _resizing = true;
  304. }
  305. }
  306. }
  307. if (_resizing) {
  308. updateFromResize(e->globalPos() - _startPosition);
  309. }
  310. }
  311. void SeparatePanel::ResizeEdge::updateFromResize(QPoint delta) {
  312. auto geometry = _startGeometry;
  313. const auto min = window()->minimumSize();
  314. const auto minw = std::max(min.width(), 80);
  315. const auto minh = std::max(min.height(), 40);
  316. const auto updateLeft = [&](int left) {
  317. geometry.setX(std::min(
  318. left,
  319. geometry.x() + geometry.width() - minw));
  320. };
  321. const auto updateRight = [&](int right) {
  322. geometry.setWidth(std::max(right - geometry.x(), minw));
  323. };
  324. const auto updateTop = [&](int top) {
  325. geometry.setY(std::min(
  326. top,
  327. geometry.y() + geometry.height() - minh));
  328. };
  329. const auto updateBottom = [&](int bottom) {
  330. geometry.setHeight(std::max(bottom - geometry.y(), minh));
  331. };
  332. if (_edges & Qt::LeftEdge) {
  333. updateLeft(geometry.x() + delta.x());
  334. } else if (_edges & Qt::RightEdge) {
  335. updateRight(geometry.x() + geometry.width() + delta.x());
  336. }
  337. if (_edges & Qt::TopEdge) {
  338. updateTop(geometry.y() + delta.y());
  339. } else if (_edges & Qt::BottomEdge) {
  340. updateBottom(geometry.y() + geometry.height() + delta.y());
  341. }
  342. window()->setGeometry(geometry);
  343. }
  344. SeparatePanel::SeparatePanel(SeparatePanelArgs &&args)
  345. : RpWidget(args.parent)
  346. , _menuSt(args.menuSt ? *args.menuSt : st::popupMenuWithIcons)
  347. , _close(this, st::separatePanelClose)
  348. , _back(this, object_ptr<IconButton>(this, st::separatePanelBack))
  349. , _body(this)
  350. , _titleHeight(st::separatePanelTitleHeight) {
  351. setMouseTracking(true);
  352. setWindowIcon(QGuiApplication::windowIcon());
  353. initControls();
  354. initLayout(args);
  355. rpl::combine(
  356. shownValue(),
  357. _fullscreen.value()
  358. ) | rpl::filter([=](bool shown, bool) {
  359. return shown;
  360. }) | rpl::start_with_next([=](bool, bool fullscreen) {
  361. updateControlsVisibility(fullscreen);
  362. Platform::SetWindowMargins(
  363. this,
  364. _useTransparency ? computePadding() : QMargins());
  365. }, lifetime());
  366. Platform::FullScreenEvents(
  367. this
  368. ) | rpl::start_with_next([=](Platform::FullScreenEvent event) {
  369. if (event == Platform::FullScreenEvent::DidEnter) {
  370. createFullScreenButtons();
  371. } else if (event == Platform::FullScreenEvent::WillExit) {
  372. _fullscreen = false;
  373. }
  374. }, lifetime());
  375. }
  376. SeparatePanel::~SeparatePanel() = default;
  377. void SeparatePanel::setTitle(rpl::producer<QString> title) {
  378. _title.create(this, std::move(title), st::separatePanelTitle);
  379. updateTitleColors();
  380. _title->setAttribute(Qt::WA_TransparentForMouseEvents);
  381. _title->setVisible(!_fullscreen.current());
  382. updateTitleGeometry(width());
  383. }
  384. void SeparatePanel::setTitleHeight(int height) {
  385. _titleHeight = height;
  386. updateControlsGeometry();
  387. }
  388. void SeparatePanel::setTitleBadge(object_ptr<RpWidget> badge) {
  389. if (badge) {
  390. badge->setParent(this);
  391. }
  392. _titleBadge = std::move(badge);
  393. updateTitleGeometry(width());
  394. }
  395. void SeparatePanel::initControls() {
  396. _back->toggledValue(
  397. ) | rpl::start_with_next([=](bool toggled) {
  398. _titleLeft.start(
  399. [=] { updateTitleGeometry(width()); },
  400. toggled ? 0. : 1.,
  401. toggled ? 1. : 0.,
  402. st::fadeWrapDuration);
  403. }, _back->lifetime());
  404. _back->hide(anim::type::instant);
  405. if (_fsBack) {
  406. _fsBack->hide(anim::type::instant);
  407. }
  408. _titleLeft.stop();
  409. _fullscreen.value(
  410. ) | rpl::start_with_next([=](bool fullscreen) {
  411. if (!fullscreen) {
  412. _fsClose = nullptr;
  413. _fsMenuToggle = nullptr;
  414. _fsBack = nullptr;
  415. } else if (!_fsClose) {
  416. createFullScreenButtons();
  417. }
  418. }, lifetime());
  419. rpl::combine(
  420. widthValue(),
  421. _fullscreen.value()
  422. ) | rpl::start_with_next([=](int width, bool fullscreen) {
  423. const auto padding = computePadding();
  424. _back->moveToLeft(padding.left(), padding.top());
  425. _close->moveToRight(padding.right(), padding.top());
  426. updateTitleGeometry(width);
  427. }, lifetime());
  428. _back->raise();
  429. _close->raise();
  430. }
  431. void SeparatePanel::createFullScreenButtons() {
  432. _fsClose = std::make_unique<FullScreenButton>(
  433. this,
  434. st::fullScreenPanelClose);
  435. initFullScreenButton(_fsClose.get());
  436. _fsClose->clicks() | rpl::to_empty | rpl::start_to_stream(
  437. _userCloseRequests,
  438. _fsClose->lifetime());
  439. _fsBack = std::make_unique<FadeWrapScaled<FullScreenButton>>(
  440. this,
  441. object_ptr<FullScreenButton>(this, st::fullScreenPanelBack));
  442. initFullScreenButton(_fsBack.get());
  443. _fsBack->toggle(_back->toggled(), anim::type::instant);
  444. if (_back->toggled()) {
  445. _fsBack->raise();
  446. }
  447. _fsBack->entity()->clicks() | rpl::to_empty | rpl::start_to_stream(
  448. _synteticBackRequests,
  449. _fsBack->lifetime());
  450. if (_menuToggle) {
  451. _fsMenuToggle = std::make_unique<FullScreenButton>(
  452. this,
  453. st::fullScreenPanelMenu);
  454. initFullScreenButton(_fsMenuToggle.get());
  455. if (const auto onstack = _menuToggleCreated) {
  456. onstack(_fsMenuToggle.get(), true);
  457. }
  458. _fsMenuToggle->setClickedCallback([=] {
  459. _menuToggle->clicked(
  460. _fsMenuToggle->clickModifiers(),
  461. Qt::LeftButton);
  462. });
  463. } else {
  464. _fsMenuToggle = nullptr;
  465. }
  466. geometryValue() | rpl::start_with_next([=](QRect geometry) {
  467. if (_fsAllowChildControls) {
  468. geometry = QRect(QPoint(), size());
  469. }
  470. const auto shift = st::separatePanelClose.rippleAreaPosition;
  471. _fsBack->move(geometry.topLeft() + shift);
  472. _fsBack->resize(st::fullScreenPanelBack.width, st::fullScreenPanelBack.height);
  473. _fsClose->move(geometry.topLeft() + QPoint(geometry.width() - _fsClose->width() - shift.x(), shift.y()));
  474. _fsClose->resize(st::fullScreenPanelClose.width, st::fullScreenPanelClose.height);
  475. if (_fsMenuToggle) {
  476. _fsMenuToggle->move(_fsClose->pos()
  477. - QPoint(_fsMenuToggle->width() + shift.x(), 0));
  478. _fsMenuToggle->resize(st::fullScreenPanelMenu.width, st::fullScreenPanelMenu.height);
  479. }
  480. }, _fsClose->lifetime());
  481. }
  482. void SeparatePanel::initFullScreenButton(not_null<QWidget*> button) {
  483. if (_fsAllowChildControls) {
  484. button->show();
  485. return;
  486. }
  487. button->setWindowFlags(Qt::WindowFlags(Qt::FramelessWindowHint)
  488. | Qt::BypassWindowManagerHint
  489. | Qt::NoDropShadowWindowHint
  490. | Qt::Tool);
  491. button->setAttribute(Qt::WA_MacAlwaysShowToolWindow);
  492. button->setAttribute(Qt::WA_OpaquePaintEvent, false);
  493. button->setAttribute(Qt::WA_TranslucentBackground, true);
  494. button->setAttribute(Qt::WA_NoSystemBackground, true);
  495. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  496. button->setScreen(screen());
  497. #else // Qt >= 6.0.0
  498. button->createWinId();
  499. button->windowHandle()->setScreen(windowHandle()->screen());
  500. #endif
  501. button->show();
  502. }
  503. void SeparatePanel::updateTitleButtonColors(not_null<IconButton*> button) {
  504. if (!_titleOverridePalette) {
  505. _titleOverrideStyles.remove(button);
  506. button->setIconOverride(nullptr, nullptr);
  507. button->setRippleColorOverride(nullptr);
  508. return;
  509. }
  510. const auto &st = button->st();
  511. auto &updated = _titleOverrideStyles[button];
  512. updated = std::make_unique<style::IconButton>(st);
  513. updated->icon = st.icon.withPalette(*_titleOverridePalette);
  514. updated->iconOver = st.iconOver.withPalette(*_titleOverridePalette);
  515. updated->ripple.color = _titleOverridePalette->windowBgOver();
  516. button->setIconOverride(&updated->icon, &updated->iconOver);
  517. button->setRippleColorOverride(&updated->ripple.color);
  518. }
  519. void SeparatePanel::updateTitleColors() {
  520. _title->setTextColorOverride(_titleOverridePalette
  521. ? _titleOverridePalette->windowFg()->c
  522. : std::optional<QColor>());
  523. }
  524. void SeparatePanel::overrideTitleColor(std::optional<QColor> color) {
  525. if (_titleOverrideColor == color) {
  526. return;
  527. }
  528. _titleOverrideColor = color;
  529. _titleOverrideBorderParts = _titleOverrideColor
  530. ? createBorderImage(*_titleOverrideColor)
  531. : QPixmap();
  532. _titleOverridePalette = color
  533. ? MakeAdjustedPalette(*color)
  534. : nullptr;
  535. updateTitleButtonColors(_back->entity());
  536. updateTitleButtonColors(_close.data());
  537. if (_menuToggle) {
  538. updateTitleButtonColors(_menuToggle.data());
  539. }
  540. if (_title) {
  541. updateTitleColors();
  542. }
  543. if (!_titleOverridePalette) {
  544. _titleOverrideStyles.clear();
  545. }
  546. update();
  547. }
  548. void SeparatePanel::overrideBodyColor(std::optional<QColor> color) {
  549. if (_bodyOverrideColor == color) {
  550. return;
  551. }
  552. _bodyOverrideColor = color;
  553. _bodyOverrideBorderParts = _bodyOverrideColor
  554. ? createBorderImage(*_bodyOverrideColor)
  555. : QPixmap();
  556. update();
  557. }
  558. void SeparatePanel::overrideBottomBarColor(std::optional<QColor> color) {
  559. if (_bottomBarOverrideColor == color) {
  560. return;
  561. }
  562. _bottomBarOverrideColor = color;
  563. _bottomBarOverrideBorderParts = _bottomBarOverrideColor
  564. ? createBorderImage(*_bottomBarOverrideColor)
  565. : QPixmap();
  566. update();
  567. }
  568. void SeparatePanel::setBottomBarHeight(int height) {
  569. Expects(!height || height >= st::callRadius);
  570. if (_bottomBarHeight == height) {
  571. return;
  572. }
  573. _bottomBarHeight = height;
  574. update();
  575. }
  576. style::palette *SeparatePanel::titleOverridePalette() const {
  577. return _titleOverridePalette.get();
  578. }
  579. void SeparatePanel::updateTitleGeometry(int newWidth) const {
  580. if (!_title && !_searchWrap) {
  581. return;
  582. }
  583. const auto progress = _titleLeft.value(_back->toggled() ? 1. : 0.);
  584. const auto left = anim::interpolate(
  585. st::separatePanelTitleLeft,
  586. _back->width() + st::separatePanelTitleSkip,
  587. progress);
  588. const auto padding = computePadding();
  589. const auto available = newWidth
  590. - rect::m::sum::h(padding)
  591. - left
  592. - _close->width();
  593. if (_title) {
  594. _title->resizeToWidth(
  595. std::min(
  596. available
  597. - (_menuToggle ? _menuToggle->width() : 0)
  598. - (_searchToggle ? _searchToggle->width() : 0)
  599. - (_titleBadge
  600. ? (_titleBadge->width()
  601. + st::separatePanelTitleBadgeSkip * 2)
  602. : 0),
  603. _title->textMaxWidth()));
  604. _title->moveToLeft(
  605. padding.left() + left,
  606. padding.top() + st::separatePanelTitleTop);
  607. if (_titleBadge) {
  608. _titleBadge->moveToLeft(
  609. rect::right(_title) + st::separatePanelTitleBadgeSkip,
  610. _title->y() + st::separatePanelTitleBadgeTop);
  611. }
  612. }
  613. if (_searchWrap) {
  614. _searchWrap->entity()->resize(available, _close->height());
  615. _searchWrap->move(padding.left() + left, padding.top());
  616. if (_searchField) {
  617. _searchField->resizeToWidth(available);
  618. _searchField->move(
  619. 0,
  620. (_close->height() - _searchField->height()) / 2);
  621. }
  622. }
  623. }
  624. rpl::producer<> SeparatePanel::allBackRequests() const {
  625. return rpl::merge(
  626. _back->entity()->clicks() | rpl::to_empty,
  627. _synteticBackRequests.events());
  628. }
  629. rpl::producer<> SeparatePanel::backRequests() const {
  630. return allBackRequests(
  631. ) | rpl::filter([=] {
  632. return !_searchField;
  633. });
  634. }
  635. rpl::producer<> SeparatePanel::allCloseRequests() const {
  636. return rpl::merge(
  637. _close->clicks() | rpl::to_empty,
  638. _userCloseRequests.events());
  639. }
  640. rpl::producer<> SeparatePanel::closeRequests() const {
  641. return allCloseRequests(
  642. ) | rpl::filter([=] {
  643. return !_searchField;
  644. });
  645. }
  646. rpl::producer<> SeparatePanel::closeEvents() const {
  647. return _closeEvents.events();
  648. }
  649. void SeparatePanel::setBackAllowed(bool allowed) {
  650. _backAllowed = allowed;
  651. updateBackToggled();
  652. }
  653. void SeparatePanel::updateBackToggled() {
  654. const auto toggled = _backAllowed || (_searchField != nullptr);
  655. if (_back->toggled() != toggled) {
  656. _back->toggle(toggled, anim::type::normal);
  657. if (_fsBack) {
  658. _fsBack->toggle(toggled, anim::type::normal);
  659. if (toggled) {
  660. _fsBack->raise();
  661. }
  662. }
  663. }
  664. }
  665. void SeparatePanel::setMenuAllowed(
  666. Fn<void(const Menu::MenuCallback&)> fill,
  667. Fn<void(not_null<RpWidget*>, bool fullscreen)> created) {
  668. _menuToggle.create(this, st::separatePanelMenu);
  669. updateTitleButtonColors(_menuToggle.data());
  670. _menuToggle->show();
  671. _menuToggle->setClickedCallback([=] { showMenu(fill); });
  672. rpl::combine(
  673. widthValue(),
  674. _fullscreen.value()
  675. ) | rpl::start_with_next([=](int width, bool) {
  676. const auto padding = computePadding();
  677. _menuToggle->moveToRight(
  678. padding.right() + _close->width(),
  679. padding.top());
  680. }, _menuToggle->lifetime());
  681. updateTitleGeometry(width());
  682. if (_fullscreen.current()) {
  683. createFullScreenButtons();
  684. }
  685. _menuToggleCreated = std::move(created);
  686. if (const auto onstack = _menuToggleCreated) {
  687. onstack(_menuToggle.data(), false);
  688. }
  689. if (!_animationCache.isNull()) {
  690. const auto rect = _menuToggle->geometry()
  691. | (_title ? _title->geometry() : QRect())
  692. | (_titleBadge ? _titleBadge->geometry() : QRect());
  693. auto p = QPainter(&_animationCache);
  694. p.fillRect(rect, st::windowBg);
  695. OverlayWidgetCache(p, _title);
  696. OverlayWidgetCache(p, _titleBadge);
  697. OverlayWidgetCache(p, _menuToggle);
  698. }
  699. }
  700. void SeparatePanel::setSearchAllowed(
  701. rpl::producer<QString> placeholder,
  702. Fn<void(std::optional<QString>)> queryChanged) {
  703. _searchPlaceholder = std::move(placeholder);
  704. _searchQueryChanged = std::move(queryChanged);
  705. _searchToggle.create(
  706. this,
  707. object_ptr<IconButton>(this, st::separatePanelSearch));
  708. const auto button = _searchToggle->entity();
  709. updateTitleButtonColors(button);
  710. _searchToggle->show(anim::type::instant);
  711. button->setClickedCallback([=] { toggleSearch(true); });
  712. rpl::combine(
  713. widthValue(),
  714. _fullscreen.value()
  715. ) | rpl::start_with_next([=](int width, bool) {
  716. const auto padding = computePadding();
  717. _searchToggle->moveToRight(
  718. padding.right() + _close->width(),
  719. padding.top());
  720. }, _searchToggle->lifetime());
  721. updateTitleGeometry(width());
  722. }
  723. bool SeparatePanel::closeSearch() {
  724. if (!_searchField) {
  725. return false;
  726. }
  727. toggleSearch(false);
  728. return true;
  729. }
  730. void SeparatePanel::toggleSearch(bool shown) {
  731. const auto weak = Ui::MakeWeak(this);
  732. if (shown) {
  733. if (_searchWrap && _searchWrap->toggled()) {
  734. return;
  735. }
  736. _searchWrap.create(this, object_ptr<RpWidget>(this));
  737. const auto inner = _searchWrap->entity();
  738. inner->paintRequest() | rpl::start_with_next([=](QRect clip) {
  739. QPainter(inner).fillRect(clip, st::windowBg);
  740. }, inner->lifetime());
  741. _searchField = CreateChild<InputField>(
  742. inner,
  743. st::defaultMultiSelectSearchField,
  744. InputField::Mode::SingleLine,
  745. _searchPlaceholder.value());
  746. _searchField->show();
  747. _searchField->setFocusFast();
  748. const auto field = _searchField;
  749. field->changes() | rpl::filter([=] {
  750. return (_searchField == field);
  751. }) | rpl::start_with_next([=] {
  752. if (const auto onstack = _searchQueryChanged) {
  753. onstack(field->getLastText());
  754. }
  755. }, field->lifetime());
  756. rpl::merge(
  757. allBackRequests(),
  758. allCloseRequests()
  759. ) | rpl::filter([=] {
  760. return (_searchField == field);
  761. }) | rpl::start_with_next([=] {
  762. toggleSearch(false);
  763. }, field->lifetime());
  764. if (const auto onstack = _searchQueryChanged) {
  765. onstack(QString());
  766. if (!weak) {
  767. return;
  768. }
  769. }
  770. updateTitleGeometry(width());
  771. _searchWrap->show(anim::type::normal);
  772. updateBackToggled();
  773. inner->shownValue(
  774. ) | rpl::filter([=](bool active) {
  775. return active && (_searchField == field);
  776. }) | rpl::take(1) | rpl::start_with_next([=] {
  777. InvokeQueued(field, [=] {
  778. if (_searchField == field && window()->isActiveWindow()) {
  779. // In case focus is somewhat in a native child window,
  780. // like a webview, Qt glitches here with field showing
  781. // focused state, but not receiving any keyboard input:
  782. //
  783. // window()->windowHandle()->isActive() == false.
  784. //
  785. // Steps were: SeparatePanel with a WebView2 child,
  786. // some interaction with mouse inside the WebView2,
  787. // so that WebView2 gets focus and active window state,
  788. // then we call setSearchAllowed() and after animation
  789. // is finished try typing -> nothing happens.
  790. //
  791. // With this workaround it works fine.
  792. activateWindow();
  793. }
  794. });
  795. }, inner->lifetime());
  796. _searchWrap->shownValue(
  797. ) | rpl::filter(
  798. !rpl::mappers::_1
  799. ) | rpl::start_with_next([=] {
  800. _searchWrap.destroy();
  801. }, _searchWrap->lifetime());
  802. } else if (_searchField) {
  803. _searchField = nullptr;
  804. if (const auto onstack = _searchQueryChanged) {
  805. onstack(std::nullopt);
  806. if (!weak) {
  807. return;
  808. }
  809. }
  810. _searchWrap->hide(anim::type::normal);
  811. updateBackToggled();
  812. }
  813. }
  814. void SeparatePanel::showMenu(Fn<void(const Menu::MenuCallback&)> fill) {
  815. const auto created = createMenu(_menuToggle);
  816. if (!created) {
  817. return;
  818. }
  819. fill(Menu::CreateAddActionCallback(_menu));
  820. if (_menu->empty()) {
  821. _menu = nullptr;
  822. } else {
  823. _menu->setForcedOrigin(PanelAnimation::Origin::TopRight);
  824. _menu->popup(mapToGlobal(QPoint(
  825. (width()
  826. - computePadding().right()
  827. - _close->width()
  828. + st::separatePanelMenuPosition.x()),
  829. st::separatePanelMenuPosition.y())));
  830. }
  831. }
  832. bool SeparatePanel::createMenu(not_null<IconButton*> button) {
  833. if (_menu) {
  834. return false;
  835. }
  836. _menu = base::make_unique_q<PopupMenu>(this, _menuSt);
  837. _menu->setDestroyedCallback([
  838. weak = MakeWeak(this),
  839. weakButton = MakeWeak(button),
  840. menu = _menu.get()]{
  841. if (weak && weak->_menu == menu) {
  842. if (weakButton) {
  843. weakButton->setForceRippled(false);
  844. }
  845. }
  846. });
  847. button->setForceRippled(true);
  848. return true;
  849. }
  850. void SeparatePanel::setHideOnDeactivate(bool hideOnDeactivate) {
  851. _hideOnDeactivate = hideOnDeactivate;
  852. if (!_hideOnDeactivate) {
  853. showAndActivate();
  854. } else if (!isActiveWindow()) {
  855. LOG(("Export Info: Panel Hide On Inactive Change."));
  856. hideGetDuration();
  857. }
  858. }
  859. void SeparatePanel::showAndActivate() {
  860. if (isHidden()) {
  861. while (const auto widget = QApplication::activePopupWidget()) {
  862. if (!widget->close()) {
  863. break;
  864. }
  865. }
  866. }
  867. toggleOpacityAnimation(true);
  868. raise();
  869. setWindowState(windowState() | Qt::WindowActive);
  870. activateWindow();
  871. setFocus();
  872. }
  873. void SeparatePanel::keyPressEvent(QKeyEvent *e) {
  874. if (e->key() == Qt::Key_Escape) {
  875. crl::on_main(this, [=] {
  876. const auto searchQuery = _searchField
  877. ? _searchField->getLastText().trimmed()
  878. : QString();
  879. if (!searchQuery.isEmpty()) {
  880. _searchField->clear();
  881. _searchField->setFocus();
  882. } else if (_back->toggled()) {
  883. _synteticBackRequests.fire({});
  884. } else {
  885. _userCloseRequests.fire({});
  886. }
  887. });
  888. }
  889. return RpWidget::keyPressEvent(e);
  890. }
  891. bool SeparatePanel::eventHook(QEvent *e) {
  892. if (e->type() == QEvent::WindowDeactivate && _hideOnDeactivate) {
  893. LOG(("Export Info: Panel Hide On Inactive Window."));
  894. hideGetDuration();
  895. }
  896. return RpWidget::eventHook(e);
  897. }
  898. void SeparatePanel::initLayout(const SeparatePanelArgs &args) {
  899. setWindowFlags(Qt::WindowFlags(Qt::FramelessWindowHint)
  900. | Qt::WindowStaysOnTopHint
  901. | Qt::NoDropShadowWindowHint
  902. | Qt::Dialog);
  903. setAttribute(Qt::WA_MacAlwaysShowToolWindow);
  904. setAttribute(Qt::WA_NoSystemBackground, true);
  905. setAttribute(Qt::WA_TranslucentBackground, true);
  906. validateBorderImage();
  907. style::PaletteChanged(
  908. ) | rpl::start_with_next([=] {
  909. validateBorderImage();
  910. ForceFullRepaint(this);
  911. }, lifetime());
  912. if (args.onAllSpaces) {
  913. Platform::InitOnTopPanel(this);
  914. }
  915. }
  916. void SeparatePanel::validateBorderImage() {
  917. _borderParts = createBorderImage(st::windowBg->c);
  918. }
  919. QPixmap SeparatePanel::createBorderImage(QColor color) const {
  920. const auto shadowPadding = st::callShadow.extend;
  921. const auto cacheSize = st::separatePanelBorderCacheSize;
  922. auto cache = QImage(
  923. cacheSize * style::DevicePixelRatio(),
  924. cacheSize * style::DevicePixelRatio(),
  925. QImage::Format_ARGB32_Premultiplied);
  926. cache.setDevicePixelRatio(style::DevicePixelRatio());
  927. cache.fill(Qt::transparent);
  928. {
  929. auto p = QPainter(&cache);
  930. auto inner = QRect(0, 0, cacheSize, cacheSize).marginsRemoved(
  931. shadowPadding);
  932. Shadow::paint(p, inner, cacheSize, st::callShadow);
  933. p.setCompositionMode(QPainter::CompositionMode_Source);
  934. p.setBrush(color);
  935. p.setPen(Qt::NoPen);
  936. PainterHighQualityEnabler hq(p);
  937. p.drawRoundedRect(
  938. myrtlrect(inner),
  939. st::callRadius,
  940. st::callRadius);
  941. }
  942. return PixmapFromImage(std::move(cache));
  943. }
  944. void SeparatePanel::toggleOpacityAnimation(bool visible) {
  945. if (_visible == visible) {
  946. return;
  947. }
  948. _visible = visible;
  949. if (_useTransparency) {
  950. if (_animationCache.isNull()) {
  951. showControls();
  952. _animationCache = GrabWidget(this);
  953. hideChildren();
  954. }
  955. _opacityAnimation.start(
  956. [this] { opacityCallback(); },
  957. _visible ? 0. : 1.,
  958. _visible ? 1. : 0.,
  959. st::separatePanelDuration,
  960. _visible ? anim::easeOutCirc : anim::easeInCirc);
  961. }
  962. if (isHidden() && _visible) {
  963. show();
  964. }
  965. }
  966. void SeparatePanel::opacityCallback() {
  967. update();
  968. if (!_visible && !_opacityAnimation.animating()) {
  969. finishAnimating();
  970. }
  971. }
  972. void SeparatePanel::finishAnimating() {
  973. _animationCache = QPixmap();
  974. if (_visible) {
  975. showControls();
  976. if (_inner) {
  977. _inner->setFocus();
  978. }
  979. } else {
  980. finishClose();
  981. }
  982. }
  983. void SeparatePanel::showControls() {
  984. showChildren();
  985. updateControlsVisibility(_fullscreen.current());
  986. }
  987. void SeparatePanel::updateControlsVisibility(bool fullscreen) {
  988. if (_title) {
  989. _title->setVisible(!fullscreen);
  990. }
  991. if (_titleBadge) {
  992. _titleBadge->setVisible(!fullscreen);
  993. }
  994. _close->setVisible(!fullscreen);
  995. if (_menuToggle) {
  996. _menuToggle->setVisible(!fullscreen);
  997. }
  998. if (fullscreen) {
  999. _back->lower();
  1000. } else {
  1001. _back->raise();
  1002. }
  1003. if (!_back->toggled()) {
  1004. _back->setVisible(false);
  1005. if (_fsBack) {
  1006. _fsBack->setVisible(false);
  1007. }
  1008. }
  1009. }
  1010. void SeparatePanel::finishClose() {
  1011. hide();
  1012. crl::on_main(this, [=] {
  1013. if (isHidden() && !_visible && !_opacityAnimation.animating()) {
  1014. LOG(("Export Info: Panel Closed."));
  1015. _closeEvents.fire({});
  1016. }
  1017. });
  1018. }
  1019. int SeparatePanel::hideGetDuration() {
  1020. LOG(("Export Info: Panel Hide Requested."));
  1021. toggleOpacityAnimation(false);
  1022. if (_animationCache.isNull()) {
  1023. finishClose();
  1024. return 0;
  1025. }
  1026. return st::separatePanelDuration;
  1027. }
  1028. void SeparatePanel::showBox(
  1029. object_ptr<BoxContent> box,
  1030. LayerOptions options,
  1031. anim::type animated) {
  1032. Expects(box != nullptr);
  1033. ensureLayerCreated();
  1034. _layer->showBox(std::move(box), options, animated);
  1035. }
  1036. void SeparatePanel::showLayer(
  1037. std::unique_ptr<LayerWidget> layer,
  1038. LayerOptions options,
  1039. anim::type animated) {
  1040. Expects(layer != nullptr);
  1041. ensureLayerCreated();
  1042. _layer->showLayer(std::move(layer), options, animated);
  1043. }
  1044. void SeparatePanel::hideLayer(anim::type animated) {
  1045. if (_layer) {
  1046. _layer->hideAll(animated);
  1047. }
  1048. }
  1049. base::weak_ptr<Toast::Instance> SeparatePanel::showToast(
  1050. Toast::Config &&config) {
  1051. return PanelShow(this).showToast(std::move(config));
  1052. }
  1053. base::weak_ptr<Toast::Instance> SeparatePanel::showToast(
  1054. TextWithEntities &&text,
  1055. crl::time duration) {
  1056. return PanelShow(this).showToast(std::move(text), duration);
  1057. }
  1058. base::weak_ptr<Toast::Instance> SeparatePanel::showToast(
  1059. const QString &text,
  1060. crl::time duration) {
  1061. return PanelShow(this).showToast(text, duration);
  1062. }
  1063. std::shared_ptr<Show> SeparatePanel::uiShow() {
  1064. return std::make_shared<PanelShow>(this);
  1065. }
  1066. void SeparatePanel::ensureLayerCreated() {
  1067. if (_layer) {
  1068. return;
  1069. }
  1070. _layer = base::make_unique_q<LayerStackWidget>(
  1071. _body,
  1072. crl::guard(this, [=] { return std::make_shared<PanelShow>(this); }));
  1073. _layer->setHideByBackgroundClick(false);
  1074. _layer->move(0, 0);
  1075. _body->sizeValue(
  1076. ) | rpl::start_with_next([=](QSize size) {
  1077. _layer->resize(size);
  1078. }, _layer->lifetime());
  1079. _layer->hideFinishEvents(
  1080. ) | rpl::filter([=] {
  1081. return _layer != nullptr; // Last hide finish is sent from destructor.
  1082. }) | rpl::start_with_next([=] {
  1083. destroyLayer();
  1084. }, _layer->lifetime());
  1085. }
  1086. void SeparatePanel::destroyLayer() {
  1087. if (!_layer) {
  1088. return;
  1089. }
  1090. auto layer = base::take(_layer);
  1091. const auto resetFocus = InFocusChain(layer);
  1092. if (resetFocus) {
  1093. setFocus();
  1094. }
  1095. layer = nullptr;
  1096. }
  1097. RpWidget *SeparatePanel::inner() const {
  1098. return _inner.get();
  1099. }
  1100. void SeparatePanel::showInner(base::unique_qptr<RpWidget> inner) {
  1101. Expects(!size().isEmpty());
  1102. auto old = base::take(_inner);
  1103. _inner = std::move(inner);
  1104. old = nullptr; // Make sure in old destructor inner() != old.
  1105. _inner->setParent(_body);
  1106. _inner->move(0, 0);
  1107. _body->sizeValue(
  1108. ) | rpl::start_with_next([=](QSize size) {
  1109. _inner->resize(size);
  1110. }, _inner->lifetime());
  1111. _inner->show();
  1112. if (_layer) {
  1113. _layer->raise();
  1114. }
  1115. showAndActivate();
  1116. }
  1117. void SeparatePanel::focusInEvent(QFocusEvent *e) {
  1118. crl::on_main(this, [=] {
  1119. if (_layer) {
  1120. _layer->setInnerFocus();
  1121. } else if (_inner && !_inner->isHidden()) {
  1122. _inner->setFocus();
  1123. }
  1124. });
  1125. }
  1126. void SeparatePanel::setInnerSize(QSize size, bool allowResize) {
  1127. Expects(!size.isEmpty());
  1128. if (_allowResize != allowResize) {
  1129. _allowResize = allowResize;
  1130. if (!_allowResize) {
  1131. _resizeEdges.clear();
  1132. } else if (_resizeEdges.empty()) {
  1133. const auto areas = std::array<Qt::Edges, 8>{ {
  1134. Qt::LeftEdge | Qt::TopEdge,
  1135. Qt::TopEdge,
  1136. Qt::RightEdge | Qt::TopEdge,
  1137. Qt::RightEdge,
  1138. Qt::RightEdge | Qt::BottomEdge,
  1139. Qt::BottomEdge,
  1140. Qt::LeftEdge | Qt::BottomEdge,
  1141. Qt::LeftEdge
  1142. } };
  1143. for (const auto area : areas) {
  1144. _resizeEdges.push_back(
  1145. std::make_unique<ResizeEdge>(this, area));
  1146. _resizeEdges.back()->showOn(
  1147. _fullscreen.value() | rpl::map(!rpl::mappers::_1));
  1148. }
  1149. }
  1150. }
  1151. if (rect().isEmpty()) {
  1152. initGeometry(size);
  1153. } else {
  1154. updateGeometry(size);
  1155. }
  1156. }
  1157. QRect SeparatePanel::innerGeometry() const {
  1158. return _body->geometry();
  1159. }
  1160. void SeparatePanel::toggleFullScreen(bool fullscreen) {
  1161. _fullscreen = fullscreen;
  1162. if (fullscreen) {
  1163. showFullScreen();
  1164. } else {
  1165. showNormal();
  1166. }
  1167. }
  1168. void SeparatePanel::allowChildFullScreenControls(bool allow) {
  1169. if (_fsAllowChildControls == allow) {
  1170. return;
  1171. }
  1172. _fsAllowChildControls = allow;
  1173. if (_fullscreen.current()) {
  1174. createFullScreenButtons();
  1175. }
  1176. }
  1177. rpl::producer<bool> SeparatePanel::fullScreenValue() const {
  1178. return _fullscreen.value();
  1179. }
  1180. QMargins SeparatePanel::computePadding() const {
  1181. return _fullscreen.current() ? QMargins() : _padding;
  1182. }
  1183. void SeparatePanel::initGeometry(QSize size) {
  1184. const auto active = QApplication::activeWindow();
  1185. const auto available = !active
  1186. ? QGuiApplication::primaryScreen()->availableGeometry()
  1187. : active->screen()->availableGeometry();
  1188. const auto parentGeometry = (active
  1189. && active->isVisible()
  1190. && active->isActiveWindow())
  1191. ? active->geometry()
  1192. : available;
  1193. auto center = parentGeometry.center();
  1194. if (size.height() > available.height()) {
  1195. size = QSize(size.width(), available.height());
  1196. }
  1197. if (center.x() + size.width() / 2
  1198. > available.x() + available.width()) {
  1199. center.setX(
  1200. available.x() + available.width() - size.width() / 2);
  1201. }
  1202. if (center.x() - size.width() / 2 < available.x()) {
  1203. center.setX(available.x() + size.width() / 2);
  1204. }
  1205. if (center.y() + size.height() / 2
  1206. > available.y() + available.height()) {
  1207. center.setY(
  1208. available.y() + available.height() - size.height() / 2);
  1209. }
  1210. if (center.y() - size.height() / 2 < available.y()) {
  1211. center.setY(available.y() + size.height() / 2);
  1212. }
  1213. _useTransparency = Platform::TranslucentWindowsSupported();
  1214. _padding = _useTransparency
  1215. ? st::callShadow.extend
  1216. : style::margins(
  1217. st::lineWidth,
  1218. st::lineWidth,
  1219. st::lineWidth,
  1220. st::lineWidth);
  1221. for (const auto &edge : _resizeEdges) {
  1222. edge->setParentPadding(_padding);
  1223. }
  1224. setAttribute(Qt::WA_OpaquePaintEvent, !_useTransparency);
  1225. if (!_fullscreen.current()) {
  1226. const auto rect = [&] {
  1227. const auto initRect = QRect(QPoint(), size);
  1228. const auto shift = center - initRect.center();
  1229. return initRect.translated(shift).marginsAdded(_padding);
  1230. }();
  1231. move(rect.topLeft());
  1232. if (_allowResize) {
  1233. setMinimumSize(rect.size());
  1234. } else {
  1235. setFixedSize(rect.size());
  1236. }
  1237. updateControlsGeometry();
  1238. }
  1239. }
  1240. void SeparatePanel::updateGeometry(QSize size) {
  1241. if (!_fullscreen.current()) {
  1242. size = QRect(QPoint(), size).marginsAdded(_padding).size();
  1243. if (_allowResize) {
  1244. setMinimumSize(size);
  1245. } else {
  1246. setFixedSize(size);
  1247. }
  1248. updateControlsGeometry();
  1249. }
  1250. update();
  1251. }
  1252. void SeparatePanel::resizeEvent(QResizeEvent *e) {
  1253. updateControlsGeometry();
  1254. for (const auto &edge : _resizeEdges) {
  1255. edge->updateSize();
  1256. }
  1257. }
  1258. void SeparatePanel::updateControlsGeometry() {
  1259. const auto padding = computePadding();
  1260. const auto top = padding.top()
  1261. + (_fullscreen.current() ? 0 : _titleHeight);
  1262. _body->setGeometry(
  1263. padding.left(),
  1264. top,
  1265. width() - padding.left() - padding.right(),
  1266. height() - top - padding.bottom());
  1267. }
  1268. void SeparatePanel::paintEvent(QPaintEvent *e) {
  1269. auto p = QPainter(this);
  1270. if (!_animationCache.isNull()) {
  1271. auto opacity = _opacityAnimation.value(_visible ? 1. : 0.);
  1272. if (!_opacityAnimation.animating()) {
  1273. finishAnimating();
  1274. if (isHidden()) return;
  1275. } else {
  1276. p.setOpacity(opacity);
  1277. PainterHighQualityEnabler hq(p);
  1278. auto marginRatio = (1. - opacity) / 5;
  1279. auto marginWidth = qRound(width() * marginRatio);
  1280. auto marginHeight = qRound(height() * marginRatio);
  1281. p.drawPixmap(
  1282. rect().marginsRemoved(
  1283. QMargins(
  1284. marginWidth,
  1285. marginHeight,
  1286. marginWidth,
  1287. marginHeight)),
  1288. _animationCache,
  1289. QRect(QPoint(0, 0), _animationCache.size()));
  1290. return;
  1291. }
  1292. }
  1293. if (_useTransparency && !_fullscreen.current()) {
  1294. paintShadowBorder(p);
  1295. } else {
  1296. paintOpaqueBorder(p);
  1297. }
  1298. }
  1299. void SeparatePanel::paintShadowBorder(QPainter &p) const {
  1300. const auto factor = style::DevicePixelRatio();
  1301. const auto size = st::separatePanelBorderCacheSize;
  1302. const auto part1 = size / 3;
  1303. const auto part2 = size - part1;
  1304. const auto corner = QSize(part1, part1) * factor;
  1305. const auto radius = st::callRadius;
  1306. const auto &header = (_titleHeight
  1307. && !_fullscreen.current()
  1308. && _titleOverrideColor)
  1309. ? _titleOverrideBorderParts
  1310. : _bodyOverrideColor
  1311. ? _bodyOverrideBorderParts
  1312. : _borderParts;
  1313. const auto &footer = (_bottomBarHeight && _bottomBarOverrideColor)
  1314. ? _bottomBarOverrideBorderParts
  1315. : _bodyOverrideColor
  1316. ? _bodyOverrideBorderParts
  1317. : _borderParts;
  1318. const auto topleft = QRect(QPoint(0, 0), corner);
  1319. p.drawPixmap(QRect(0, 0, part1, part1), header, topleft);
  1320. const auto topright = QRect(QPoint(part2, 0) * factor, corner);
  1321. p.drawPixmap(QRect(width() - part1, 0, part1, part1), header, topright);
  1322. const auto top = QRect(
  1323. QPoint(part1, 0) * factor,
  1324. QSize(part2 - part1, _padding.top() + radius) * factor);
  1325. p.drawPixmap(
  1326. QRect(part1, 0, width() - 2 * part1, _padding.top() + radius),
  1327. header,
  1328. top);
  1329. const auto bottomleft = QRect(QPoint(0, part2) * factor, corner);
  1330. p.drawPixmap(
  1331. QRect(0, height() - part1, part1, part1),
  1332. footer,
  1333. bottomleft);
  1334. const auto bottomright = QRect(QPoint(part2, part2) * factor, corner);
  1335. p.drawPixmap(
  1336. QRect(width() - part1, height() - part1, part1, part1),
  1337. footer,
  1338. bottomright);
  1339. const auto bottom = QRect(
  1340. QPoint(part1, size - _padding.bottom() - radius) * factor,
  1341. QSize(part2 - part1, _padding.bottom() + radius) * factor);
  1342. p.drawPixmap(
  1343. QRect(
  1344. part1,
  1345. height() - _padding.bottom() - radius,
  1346. width() - 2 * part1,
  1347. _padding.bottom() + radius),
  1348. footer,
  1349. bottom);
  1350. const auto fillLeft = [&](int from, int till, const auto &parts) {
  1351. const auto left = QRect(
  1352. QPoint(0, part1) * factor,
  1353. QSize(_padding.left(), part2 - part1) * factor);
  1354. p.drawPixmap(
  1355. QRect(0, from, _padding.left(), till - from),
  1356. parts,
  1357. left);
  1358. };
  1359. const auto fillRight = [&](int from, int till, const auto &parts) {
  1360. const auto right = QRect(
  1361. QPoint(size - _padding.right(), part1) * factor,
  1362. QSize(_padding.right(), part2 - part1) * factor);
  1363. p.drawPixmap(
  1364. QRect(
  1365. width() - _padding.right(),
  1366. from,
  1367. _padding.right(),
  1368. till - from),
  1369. parts,
  1370. right);
  1371. };
  1372. fillLeft(part1, height() - part1, _borderParts);
  1373. fillRight(part1, height() - part1, _borderParts);
  1374. paintBodyBg(p, radius);
  1375. }
  1376. void SeparatePanel::paintBodyBg(QPainter &p, int radius) const {
  1377. const auto padding = computePadding();
  1378. const auto fillBody = [&](int from, int till, QColor color) {
  1379. if (till <= from) {
  1380. return;
  1381. }
  1382. p.fillRect(
  1383. padding.left(),
  1384. from,
  1385. width() - padding.left() - padding.right(),
  1386. till - from,
  1387. color);
  1388. };
  1389. const auto bg = _bodyOverrideColor.value_or(st::windowBg->c);
  1390. const auto chosenFooter = (_bottomBarHeight && _bottomBarOverrideColor)
  1391. ? _bottomBarOverrideColor
  1392. : _bodyOverrideColor;
  1393. const auto footerColor = chosenFooter.value_or(st::windowBg->c);
  1394. const auto chosenHeader = (_titleHeight
  1395. && !_fullscreen.current()
  1396. && _titleOverrideColor)
  1397. ? _titleOverrideColor
  1398. : _bodyOverrideColor;
  1399. const auto titleColor = chosenHeader.value_or(st::windowBg->c);
  1400. const auto niceOverscroll = !_layer && ::Platform::IsMac();
  1401. if ((niceOverscroll && titleColor == footerColor)
  1402. || (titleColor == footerColor && titleColor == bg)) {
  1403. fillBody(
  1404. padding.top() + radius,
  1405. height() - padding.bottom() - radius,
  1406. titleColor);
  1407. } else if (niceOverscroll || titleColor == bg || footerColor == bg) {
  1408. const auto top = niceOverscroll
  1409. ? (height() / 2)
  1410. : (titleColor != bg)
  1411. ? (padding.top() + _titleHeight)
  1412. : (height() - padding.bottom() - _bottomBarHeight);
  1413. fillBody(padding.top() + radius, top, titleColor);
  1414. fillBody(top, height() - padding.bottom() - radius, footerColor);
  1415. } else {
  1416. const auto one = padding.top() + _titleHeight;
  1417. const auto two = height() - padding.bottom() - _bottomBarHeight;
  1418. fillBody(padding.top() + radius, one, titleColor);
  1419. fillBody(one, two, bg);
  1420. fillBody(two, height() - padding.bottom() - radius, footerColor);
  1421. }
  1422. }
  1423. void SeparatePanel::paintOpaqueBorder(QPainter &p) const {
  1424. const auto border = st::windowShadowFgFallback;
  1425. const auto padding = computePadding();
  1426. if (!_fullscreen.current()) {
  1427. p.fillRect(0, 0, width(), padding.top(), border);
  1428. p.fillRect(
  1429. myrtlrect(
  1430. 0,
  1431. padding.top(),
  1432. padding.left(),
  1433. height() - padding.top()),
  1434. border);
  1435. p.fillRect(
  1436. myrtlrect(
  1437. width() - padding.right(),
  1438. padding.top(),
  1439. padding.right(),
  1440. height() - padding.top()),
  1441. border);
  1442. p.fillRect(
  1443. padding.left(),
  1444. height() - padding.bottom(),
  1445. width() - padding.left() - padding.right(),
  1446. padding.bottom(),
  1447. border);
  1448. }
  1449. paintBodyBg(p);
  1450. }
  1451. void SeparatePanel::closeEvent(QCloseEvent *e) {
  1452. e->ignore();
  1453. _userCloseRequests.fire({});
  1454. }
  1455. void SeparatePanel::mousePressEvent(QMouseEvent *e) {
  1456. if (_fullscreen.current()) {
  1457. return;
  1458. }
  1459. auto dragArea = myrtlrect(
  1460. _padding.left(),
  1461. _padding.top(),
  1462. width() - _padding.left() - _padding.right(),
  1463. _titleHeight);
  1464. if (e->button() == Qt::LeftButton) {
  1465. if (dragArea.contains(e->pos())) {
  1466. const auto dragViaSystem = [&] {
  1467. if (windowHandle()->startSystemMove()) {
  1468. SendSynteticMouseEvent(
  1469. this,
  1470. QEvent::MouseButtonRelease,
  1471. Qt::LeftButton);
  1472. return true;
  1473. }
  1474. return false;
  1475. }();
  1476. if (!dragViaSystem) {
  1477. _dragging = true;
  1478. _dragStartMousePosition = e->globalPos();
  1479. _dragStartMyPosition = QPoint(x(), y());
  1480. }
  1481. } else if (!rect().contains(e->pos()) && _hideOnDeactivate) {
  1482. LOG(("Export Info: Panel Hide On Click."));
  1483. hideGetDuration();
  1484. }
  1485. }
  1486. }
  1487. void SeparatePanel::mouseMoveEvent(QMouseEvent *e) {
  1488. if (_fullscreen.current()) {
  1489. return;
  1490. } else if (_dragging) {
  1491. if (!(e->buttons() & Qt::LeftButton)) {
  1492. _dragging = false;
  1493. } else {
  1494. move(_dragStartMyPosition
  1495. + (e->globalPos() - _dragStartMousePosition));
  1496. }
  1497. }
  1498. }
  1499. void SeparatePanel::mouseReleaseEvent(QMouseEvent *e) {
  1500. if (_fullscreen.current()) {
  1501. return;
  1502. } else if (e->button() == Qt::LeftButton && _dragging) {
  1503. _dragging = false;
  1504. }
  1505. }
  1506. void SeparatePanel::leaveEventHook(QEvent *e) {
  1507. Tooltip::Hide();
  1508. }
  1509. void SeparatePanel::leaveToChildEvent(QEvent *e, QWidget *child) {
  1510. Tooltip::Hide();
  1511. }
  1512. } // namespace Ui