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 #pragma once
8 
9 #include "base/unique_qptr.h"
10 #include "base/flags.h"
11 #include "ui/layers/layer_widget.h"
12 #include "ui/layers/box_content.h"
13 #include "ui/wrap/padding_wrap.h"
14 #include "ui/widgets/labels.h"
15 #include "ui/effects/animation_value.h"
16 #include "ui/text/text_entity.h"
17 #include "ui/round_rect.h"
18 #include "ui/rp_widget.h"
19 
20 class Painter;
21 
22 namespace style {
23 struct RoundButton;
24 struct IconButton;
25 struct ScrollArea;
26 struct Box;
27 } // namespace style
28 
29 namespace Ui {
30 
31 class RoundButton;
32 class IconButton;
33 class ScrollArea;
34 class FlatLabel;
35 class FadeShadow;
36 
37 class BoxLayerWidget : public LayerWidget, public BoxContentDelegate {
38 public:
39 	BoxLayerWidget(
40 		not_null<LayerStackWidget*> layer,
41 		object_ptr<BoxContent> content);
42 	~BoxLayerWidget();
43 
44 	void parentResized() override;
45 
46 	void setLayerType(bool layerType) override;
47 	void setStyle(const style::Box &st) override;
48 	const style::Box &style() override;
49 	void setTitle(rpl::producer<TextWithEntities> title) override;
50 	void setAdditionalTitle(rpl::producer<QString> additional) override;
51 	void showBox(
52 		object_ptr<BoxContent> box,
53 		LayerOptions options,
54 		anim::type animated) override;
55 
showFinished()56 	void showFinished() override {
57 		_content->showFinished();
58 	}
59 
60 	void clearButtons() override;
61 	QPointer<RoundButton> addButton(
62 		rpl::producer<QString> text,
63 		Fn<void()> clickCallback,
64 		const style::RoundButton &st) override;
65 	QPointer<RoundButton> addLeftButton(
66 		rpl::producer<QString> text,
67 		Fn<void()> clickCallback,
68 		const style::RoundButton &st) override;
69 	QPointer<IconButton> addTopButton(
70 		const style::IconButton &st,
71 		Fn<void()> clickCallback) override;
72 	void showLoading(bool show) override;
73 	void updateButtonsPositions() override;
74 	QPointer<QWidget> outerContainer() override;
75 
76 	void setDimensions(
77 		int newWidth,
78 		int maxHeight,
79 		bool forceCenterPosition = false) override;
80 
setNoContentMargin(bool noContentMargin)81 	void setNoContentMargin(bool noContentMargin) override {
82 		if (_noContentMargin != noContentMargin) {
83 			_noContentMargin = noContentMargin;
84 			updateSize();
85 		}
86 	}
87 
isBoxShown()88 	bool isBoxShown() const override {
89 		return !isHidden();
90 	}
closeBox()91 	void closeBox() override {
92 		closeLayer();
93 	}
94 	void triggerButton(int index) override;
95 
96 	void setCloseByOutsideClick(bool close) override;
97 	bool closeByOutsideClick() const override;
98 
99 protected:
100 	void keyPressEvent(QKeyEvent *e) override;
101 	void resizeEvent(QResizeEvent *e) override;
102 	void paintEvent(QPaintEvent *e) override;
103 
doSetInnerFocus()104 	void doSetInnerFocus() override {
105 		_content->setInnerFocus();
106 	}
closeHook()107 	void closeHook() override {
108 		_content->notifyBoxClosing();
109 	}
110 
111 private:
112 	struct LoadingProgress;
113 
114 	void paintAdditionalTitle(Painter &p);
115 	void updateTitlePosition();
116 
117 	[[nodiscard]] const style::Box &st() const;
118 	[[nodiscard]] bool hasTitle() const;
119 	[[nodiscard]] int titleHeight() const;
120 	[[nodiscard]] int buttonsHeight() const;
121 	[[nodiscard]] int buttonsTop() const;
122 	[[nodiscard]] int contentTop() const;
123 	[[nodiscard]] int countFullHeight() const;
124 	[[nodiscard]] int countRealHeight() const;
125 	[[nodiscard]] QRect loadingRect() const;
126 	void updateSize();
127 
128 	const style::Box *_st = nullptr;
129 	not_null<LayerStackWidget*> _layer;
130 	int _fullHeight = 0;
131 
132 	bool _noContentMargin = false;
133 	int _maxContentHeight = 0;
134 	object_ptr<BoxContent> _content;
135 
136 	RoundRect _roundRect;
137 	object_ptr<FlatLabel> _title = { nullptr };
138 	Fn<TextWithEntities()> _titleFactory;
139 	rpl::variable<QString> _additionalTitle;
140 	int _titleLeft = 0;
141 	int _titleTop = 0;
142 	bool _layerType = false;
143 	bool _closeByOutsideClick = true;
144 
145 	std::vector<object_ptr<RoundButton>> _buttons;
146 	object_ptr<RoundButton> _leftButton = { nullptr };
147 	base::unique_qptr<IconButton> _topButton = { nullptr };
148 	std::unique_ptr<LoadingProgress> _loadingProgress;
149 
150 };
151 
152 } // namespace Ui
153