padding_wrap.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/wrap/padding_wrap.h"
  8. #include "ui/ui_utility.h"
  9. namespace Ui {
  10. PaddingWrap<RpWidget>::PaddingWrap(
  11. QWidget *parent,
  12. object_ptr<RpWidget> &&child,
  13. const style::margins &padding)
  14. : Parent(parent, std::move(child)) {
  15. setPadding(padding);
  16. }
  17. void PaddingWrap<RpWidget>::setPadding(const style::margins &padding) {
  18. if (_padding != padding) {
  19. auto oldWidth = width() - _padding.left() - _padding.top();
  20. _padding = padding;
  21. if (auto weak = wrapped()) {
  22. wrappedSizeUpdated(weak->size());
  23. auto margins = weak->getMargins();
  24. weak->moveToLeft(
  25. _padding.left() + margins.left(),
  26. _padding.top() + margins.top());
  27. } else {
  28. resize(QSize(
  29. _padding.left() + oldWidth + _padding.right(),
  30. _padding.top() + _padding.bottom()));
  31. }
  32. }
  33. }
  34. void PaddingWrap<RpWidget>::wrappedSizeUpdated(QSize size) {
  35. resize(QRect(QPoint(), size).marginsAdded(_padding).size());
  36. }
  37. int PaddingWrap<RpWidget>::naturalWidth() const {
  38. auto inner = [this] {
  39. if (auto weak = wrapped()) {
  40. return weak->naturalWidth();
  41. }
  42. return RpWidget::naturalWidth();
  43. }();
  44. return (inner < 0)
  45. ? inner
  46. : (_padding.left() + inner + _padding.right());
  47. }
  48. int PaddingWrap<RpWidget>::resizeGetHeight(int newWidth) {
  49. if (auto weak = wrapped()) {
  50. weak->resizeToWidth(newWidth
  51. - _padding.left()
  52. - _padding.right());
  53. SendPendingMoveResizeEvents(weak);
  54. } else {
  55. resize(QSize(
  56. _padding.left() + newWidth + _padding.right(),
  57. _padding.top() + _padding.bottom()));
  58. }
  59. return heightNoMargins();
  60. }
  61. CenterWrap<RpWidget>::CenterWrap(
  62. QWidget *parent,
  63. object_ptr<RpWidget> &&child)
  64. : Parent(parent, std::move(child)) {
  65. if (const auto weak = wrapped()) {
  66. wrappedSizeUpdated(weak->size());
  67. }
  68. }
  69. int CenterWrap<RpWidget>::naturalWidth() const {
  70. return -1;
  71. }
  72. int CenterWrap<RpWidget>::resizeGetHeight(int newWidth) {
  73. updateWrappedPosition(newWidth);
  74. return heightNoMargins();
  75. }
  76. void CenterWrap<RpWidget>::wrappedSizeUpdated(QSize size) {
  77. resize(width(), size.height());
  78. updateWrappedPosition(width());
  79. }
  80. void CenterWrap<RpWidget>::updateWrappedPosition(int forWidth) {
  81. if (const auto weak = wrapped()) {
  82. const auto margins = weak->getMargins();
  83. weak->moveToLeft(
  84. (forWidth - weak->width()) / 2 + margins.left(),
  85. margins.top());
  86. }
  87. }
  88. } // namespace Ui