aboutsummaryrefslogtreecommitdiff
path: root/src/WindowActionRouter.hpp
blob: f7cb2b4bacdcf0be9f6c9decbfbdd90a44280532 (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
#pragma once

#include <QWidget>
#include <QtCore>
#include <cstdint>
#include <functional>
#include <mutex>
#include <string>
#include <unordered_map>

#include "events.hpp"
#include "widgets/BrowserWindow.hpp"
#include "widgets/WebViewStack.hpp"

#define WITH_WEBVIEW_WINDOW(WEBVIEW_ID, IDENT, BLOCK)                          \
  for (auto &win_match : window_map) {                                         \
    auto *IDENT = win_match.second;                                            \
    if (IDENT->mediator()->has_webview(WEBVIEW_ID)) {                          \
      BLOCK;                                                                   \
    }                                                                          \
  }

class WindowActionRouter : public QWidget {
  Q_OBJECT

public:
  static WindowActionRouter &instance() {
    static WindowActionRouter router;
    return router;
  }

  void initialize();

  void add_window(BrowserWindow *window);
  const WindowMap &windows();

  WebViewId fetch_current_tab_id(WindowId win_id = 0);
  QList<WebViewData> fetch_webview_data_list(WindowId win_id = 0);

  void dispatch_event(BrowserEvent *event);
  void register_event(const EventHandlerRequest &event);

protected:
  WindowActionRouter() = default;

  void add_keymap(const QString &mode_string, const QString &keyseq,
                  std::function<void()> action);

signals:
  void new_window_requested(const QUrl &url);

private:
  WindowMap window_map;
  uint64_t last_id = 1;
  Configuration *configuration;

  // TODO: Split event handling to its own class
  std::mutex events_mutex;
  std::unordered_map<QString, std::vector<EventHandlerRequest>> events;
};