aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/EdgeDecoration.cpp
blob: 58db639121cb8fee03b259d92774ffee0352538b (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: black;
      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_html(const QString &content) {
  html_content = content;
  setup_webview();
}

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

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

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

    webview->setHtml(default_html_layout.replace("{{body}}", html_content));
    if (vertical)
      setFixedWidth(size);
    else
      setFixedHeight(size);
  } else {
    if (webview != nullptr) {
      layout()->removeWidget(webview);
      webview->deleteLater();
      webview = nullptr;
    }
  }
}