aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/Decorations.hpp
blob: 8eac41921fbd20b35773701deb98fe1578774520 (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
68
69
70
71
#pragma once

#include "widgets/EdgeDecoration.hpp"
#include "widgets/WebViewStack.hpp"
#include <QWidget>
#include <QtCore>
#include <optional>
#include <qwebengineprofile.h>

enum DecorationType : uint8_t {
  DecorationTop = 1,
  DecorationBottom = 2,
  DecorationLeft = 3,
  DecorationRight = 4,
};

class Decorations : public QWidget {
  Q_OBJECT

public:
  Decorations(QWidget *content_widget, QWebEngineProfile *profile, QWidget *parent = nullptr);

  void set_enabled(DecorationType type, bool enabled);
  bool get_enabled(DecorationType type);

  std::optional<WebViewId> get_view_id(DecorationType type) {
    auto decoration = get_decoration_widget(type);
    return decoration.has_value() ? decoration.value()->get_view_id() : std::nullopt;
  }

  bool has_webview(WebViewId view_id) {
    for (auto *decoration : decorations())
      if (decoration->get_view_id() == view_id)
        return true;
    return false;
  }

  void open_url(const QUrl &url, WebViewId view_id) {
    for (auto *decoration : decorations()) {
      if (decoration->get_view_id() == view_id) {
        decoration->set_url(url);
        return;
      }
    }
  }

  void set_html(const QString &html, WebViewId view_id) {
    for (auto *decoration : decorations()) {
      if (decoration->get_view_id() == view_id) {
        decoration->set_html(html);
        return;
      }
    }
  }

private:
  EdgeDecoration *decoration_top;
  EdgeDecoration *decoration_bottom;
  EdgeDecoration *decoration_left;
  EdgeDecoration *decoration_right;

  std::vector<EdgeDecoration *> decorations() {
    return {
        decoration_top,
        decoration_bottom,
        decoration_left,
        decoration_right,
    };
  }
  std::optional<EdgeDecoration *> get_decoration_widget(DecorationType type);
};