spelling_highlighter_helper.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 "spellcheck/spelling_highlighter.h"
  8. #include "ui/platform/ui_platform_utility.h"
  9. #include "styles/style_widgets.h"
  10. #include "styles/palette.h"
  11. #include <QtGui/QGuiApplication>
  12. #include <QtGui/QScreen>
  13. namespace Spelling::Helper {
  14. namespace {
  15. constexpr auto kFormattingItem = 1;
  16. constexpr auto kSpellingItem = 1;
  17. } // namespace
  18. bool IsContextMenuTop(not_null<QMenu*> menu, QPoint mousePosition) {
  19. const auto &st = st::defaultMenu;
  20. const auto &stPopup = st::defaultPopupMenu;
  21. const auto itemHeight = st.itemPadding.top()
  22. + st.itemStyle.font->height
  23. + st.itemPadding.bottom();
  24. const auto sepHeight = st.separator.padding.top()
  25. + st.separator.width
  26. + st.separator.padding.bottom();
  27. const auto line = st::lineWidth;
  28. const auto p = Ui::Platform::TranslucentWindowsSupported()
  29. ? stPopup.shadow.extend
  30. : style::margins(line, line, line, line);
  31. const auto additional = kFormattingItem + kSpellingItem;
  32. const auto actions = menu->actions() | ranges::to_vector;
  33. auto sepCount = ranges::count_if(actions, &QAction::isSeparator);
  34. auto itemsCount = actions.size() - sepCount;
  35. sepCount += additional;
  36. itemsCount += additional;
  37. const auto w = mousePosition - QPoint(0, p.top());
  38. const auto screen = QGuiApplication::screenAt(mousePosition);
  39. if (!screen) {
  40. return false;
  41. }
  42. const auto r = screen->availableGeometry();
  43. const auto height = itemHeight * itemsCount
  44. + sepHeight * sepCount
  45. + p.top()
  46. + stPopup.scrollPadding.top()
  47. + stPopup.scrollPadding.bottom()
  48. + p.bottom();
  49. return (w.y() + height - p.bottom() > r.y() + r.height());
  50. }
  51. } // namespace Spelling::Helper