aboutsummaryrefslogtreecommitdiff
path: root/src/WindowActionRouter.cpp
blob: 8c8029078f3ce24fcb1b397c484a20fd093fa68f (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <QWidget>
#include <QtCore>
#include <unordered_map>

#include "LuaRuntime.hpp"
#include "WindowActionRouter.hpp"
#include "WindowMediator.hpp"
#include "keymap/KeymapEvaluator.hpp"
#include "widgets/BrowserWindow.hpp"
#include "widgets/WebViewStack.hpp"

void WindowActionRouter::initialize() {
  auto &runtime = LuaRuntime::instance();

  connect(&runtime, &LuaRuntime::keymap_added, this,
          &WindowActionRouter::add_keymap);

  connect(&runtime, &LuaRuntime::history_back_requested, this,
          [this](WebViewId webview_id, qsizetype history_index) {
            WITH_WEBVIEW_WINDOW(webview_id, window, {
              emit window->mediator()->history_back_requested(webview_id,
                                                              history_index);
            });
          });
  connect(&runtime, &LuaRuntime::history_forward_requested, this,
          [this](WebViewId webview_id, qsizetype history_index) {
            WITH_WEBVIEW_WINDOW(webview_id, window, {
              emit window->mediator()->history_forward_requested(webview_id,
                                                                 history_index);
            });
          });
  connect(&runtime, &LuaRuntime::url_opened, this,
          [this](const QString &url, OpenType open_type, WebViewId webview_id) {
            WITH_WEBVIEW_WINDOW(webview_id, window, {
              emit window->mediator()->url_opened(url, open_type, webview_id);
            });
          });
  connect(&runtime, &LuaRuntime::webview_closed, this,
          [this](WebViewId webview_id) {
            WITH_WEBVIEW_WINDOW(webview_id, window, {
              emit window->mediator()->webview_closed(webview_id);
            });
          });
  connect(&runtime, &LuaRuntime::webview_selected, this,
          [this](WebViewId webview_id) {
            WITH_WEBVIEW_WINDOW(webview_id, window, {
              emit window->mediator()->webview_selected(webview_id);
            });
          });
}

void WindowActionRouter::add_window(BrowserWindow *window) {
  auto win_id = last_id;
  last_id++;

  window_map.insert({win_id, window});
  window->set_id(win_id);
  connect(window, &BrowserWindow::closed, this,
          [this, win_id]() { window_map.erase(win_id); });
  connect(window->mediator(), &WindowMediator::new_window_requested, this,
          &WindowActionRouter::new_window_requested);
}

const WindowMap &WindowActionRouter::windows() { return window_map; }

void WindowActionRouter::add_keymap(const QString &mode_string,
                                    const QString &keyseq,
                                    std::function<void()> action) {
  auto &keymap_evaluator = KeymapEvaluator::instance();
  const KeyMode mode = keymap_evaluator.mode_from_string(mode_string);
  keymap_evaluator.add_keymap(mode, keyseq, std::move(action));
}

WebViewId WindowActionRouter::fetch_current_tab_id(WindowId win_id) {
  for (auto &pair : window_map) {
    auto *win = pair.second;
    auto is_current_window =
        win_id == win->get_id() || (win_id == 0 && win->isActiveWindow());
    if (is_current_window) {
      return win->mediator()->current_webview_id();
    }
  }
  return 0;
}

QList<WebViewData>
WindowActionRouter::fetch_webview_data_list(WindowId win_id) {
  for (auto &pair : window_map) {
    auto *win = pair.second;
    auto is_current_window =
        win_id == win->get_id() || (win_id == 0 && win->isActiveWindow());
    if (is_current_window) {
      return win->mediator()->get_webview_list();
    }
  }
  return {};
}