44#include < QScrollBar>
55#include < QStyleOption>
66#include < QTextBlock>
7+ #include < QTimer>
78#include < QToolBar>
89#include < QVBoxLayout>
910
@@ -221,6 +222,7 @@ TextPreview::TextPreview(FileViewer* fileViewer_, Window* window_, QWidget* pare
221222
222223void TextPreview::setText (const QString& text, const QString& extension) {
223224 this ->editor ->setText (text, extension);
225+ this ->recomputeToolbarVisibility ();
224226}
225227
226228void TextPreview::setEditing (bool editing) const {
@@ -230,19 +232,33 @@ void TextPreview::setEditing(bool editing) const {
230232 this ->cancelAction ->setVisible (editing);
231233 this ->fileViewer ->getNavBar ()->setDisabled (editing);
232234 this ->window ->freezeActions (editing, true , false );
235+ this ->recomputeToolbarVisibility ();
233236}
234237
235238void TextPreview::setReadOnly (bool readOnly) const {
236239 this ->editAction ->setVisible (!readOnly);
237240 this ->saveAction ->setVisible (false );
238241 this ->cancelAction ->setVisible (false );
242+ this ->recomputeToolbarVisibility ();
239243}
240244
241245void TextPreview::resizeEvent (QResizeEvent* event) {
242246 QWidget::resizeEvent (event);
243247 auto width = this ->width ();
244- if (auto * scrollbar = this ->editor ->verticalScrollBar ()) {
248+ if (const auto * scrollbar = this ->editor ->verticalScrollBar ()) {
245249 width -= scrollbar->sizeHint ().width ();
246250 }
247251 this ->toolbar ->setFixedSize (width, 64 );
252+ this ->recomputeToolbarVisibility ();
253+ }
254+
255+ void TextPreview::recomputeToolbarVisibility () const {
256+ // must be delayed a single frame
257+ QTimer::singleShot (0 , this , [this ] {
258+ QRegion reg;
259+ reg += this ->toolbar ->actionGeometry (this ->editAction );
260+ reg += this ->toolbar ->actionGeometry (this ->saveAction );
261+ reg += this ->toolbar ->actionGeometry (this ->cancelAction );
262+ this ->toolbar ->setMask (reg);
263+ });
248264}
0 commit comments