aboutsummaryrefslogtreecommitdiff
path: root/src/InputMediator.cpp
blob: ee7a5522c5d5325722041bda6524033ebfa37e4b (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <QWidget>
#include <QtCore>

#include "CommandParser.hpp"
#include "InputMediator.hpp"
#include "LuaRuntime.hpp"
#include "completion/CommandsAdapter.hpp"
#include "completion/TabsAdapter.hpp"
#include "completion/UrlAdapter.hpp"
#include "widgets/WebViewStack.hpp"

InputMediator::InputMediator(InputLine *inputLine, WebViewStack *webViewStack,
                             LuaRuntime *luaRuntime)
    : QObject(), inputLine(inputLine), webViewStack(webViewStack),
      luaRuntime(luaRuntime) {
  // Inputline
  inputLine->setHidden(true);
  connect(inputLine, &InputLine::submitted, this,
          &InputMediator::onInputSubmit);
  connect(inputLine, &InputLine::cancelled, this,
          &InputMediator::hideInputLine);

  // Lua runtime
  connect(luaRuntime, &LuaRuntime::urlOpened, webViewStack,
          &WebViewStack::openUrl);
}

void InputMediator::onInputSubmit(QString input) {
  hideInputLine();

  if (dynamic_cast<CommandEval *>(currentEvaluationType))
    return evaluateCommand(input);

  if (auto urlEval = dynamic_cast<UrlEval *>(currentEvaluationType))
    return webViewStack->openUrl(input, urlEval->type());

  if (dynamic_cast<BufferEval *>(currentEvaluationType))
    return webViewStack->focusWebView(1);
}

void InputMediator::hideInputLine() {
  inputLine->setInputFocus(false);
  inputLine->setHidden(true);
}

void InputMediator::showInputLine() {
  inputLine->setHidden(false);
  inputLine->raise();
  inputLine->setInputFocus(true);
}

void InputMediator::showCommandInput(QString cmd) {
  setEvaluationType(new CommandEval());
  inputLine->setAdapter(new CommandsAdapter);
  inputLine->setInputText(cmd);
  showInputLine();
}

void InputMediator::showBufferInput(QString url) {
  setEvaluationType(new CommandEval());
  inputLine->setAdapter(new TabsAdapter);
  inputLine->setInputText(url);
  showInputLine();
}

void InputMediator::setEvaluationType(EvaluationType *evalType) {
  if (currentEvaluationType)
    delete currentEvaluationType;
  currentEvaluationType = evalType;
}

void InputMediator::showURLInput(QString url, OpenType openType) {
  setEvaluationType(new UrlEval(openType));
  inputLine->setAdapter(new UrlAdapter);
  inputLine->setInputText(url);
  showInputLine();
}

void InputMediator::evaluateCommand(QString command) {
  CommandParser parser;
  auto cmd = parser.parse(command);

  switch (cmd.command) {
  case CommandType::LuaEval:
    luaRuntime->evaluate(cmd.argsString);
    break;
  case CommandType::Open:
    if (cmd.argsString.trimmed().isEmpty())
      showURLInput("", OpenType::OpenUrl);
    else
      openUrl(cmd.argsString, OpenType::OpenUrl);
    break;
  case CommandType::TabOpen:
    if (cmd.argsString.trimmed().isEmpty())
      showURLInput("", OpenType::OpenUrlInTab);
    else
      openUrl(cmd.argsString, OpenType::OpenUrlInTab);
    break;
  case CommandType::TabNext:
    nextWebView();
    break;
  case CommandType::TabPrev:
    previousWebView();
    break;
  case CommandType::TabSelect:
    showBufferInput();
    // focusWebView(long index) // TODO: parse index and select
    break;
  case CommandType::Noop:
    break;
  }
}