aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/EdgeDecoration.cpp
blob: 8880294df8210e073762b90130e31e7eca88d698 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <QWidget>
#include <QtCore>
#include <qboxlayout.h>
#include <qlabel.h>
#include <qwebengineview.h>

#include "widgets/WebView.hpp"
#include "widgets/WebViewStack.hpp"

#include "widgets/EdgeDecoration.hpp"

QString default_html_layout = R"HTML(
  {{body}}
  <style>
    :where(html, body) {
      margin: 0;
      padding: 0;
      background: #333;
      color: white;
      overflow: hidden;
    }
  </style>
)HTML";

EdgeDecoration::EdgeDecoration(bool vertical, QWebEngineProfile *profile, QWidget *parent)
    : QWidget(parent), vertical(vertical), profile(profile) {
  auto *vbox = new QVBoxLayout();
  vbox->setContentsMargins(0, 0, 0, 0);
  vbox->setSpacing(0);
  setLayout(vbox);
}

void EdgeDecoration::set_enabled(bool enabled_value) {
  enabled = enabled_value;
  setup_webview();
}

void EdgeDecoration::set_size(u_int16_t size_value) {
  size = size_value;
  setup_webview();
}

void EdgeDecoration::set_html(const QString &content) {
  html_content = content;
  setup_webview();
}

void EdgeDecoration::setup_webview() {
  if (enabled) {
    if (!webview.has_value()) {
      webview = new WebView(WebViewStack::next_webview_id++, profile, this);
      layout()->addWidget(webview.value());
    }

    webview.value()->setHtml(QString(default_html_layout).replace("{{body}}", html_content));
    if (vertical)
      webview.value()->setFixedWidth(size);
    else
      webview.value()->setFixedHeight(size);
  } else {
    if (webview.has_value()) {
      layout()->removeWidget(webview.value());
      webview.value()->deleteLater();
      webview = std::nullopt;
    }
  }
}