aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/terminal/terminalwidget.h
blob: acba039ba96eaba4cb8d2cd09e08280693be4238 (plain)
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
113
114
115
116
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#pragma once

#include "shellintegration.h"
#include "shortcutmap.h"

#include <aggregation/aggregate.h>

#include <coreplugin/actionmanager/command.h>
#include <coreplugin/icontext.h>
#include <coreplugin/terminal/searchableterminal.h>

#include <utils/link.h>
#include <utils/process.h>
#include <utils/terminalhooks.h>

#include <QFutureWatcher>

#include <memory>

namespace Terminal {

using RegisteredAction = std::unique_ptr<QAction, std::function<void(QAction *)>>;

class TerminalWidget : public Core::SearchableTerminal
{
    Q_OBJECT
public:
    TerminalWidget(QWidget *parent = nullptr,
                   const Utils::Terminal::OpenTerminalParameters &openParameters = {});

    void closeTerminal();

    void setShellName(const QString &shellName);
    QString shellName() const;
    QString title() const;

    Utils::FilePath cwd() const;
    Utils::CommandLine currentCommand() const;
    std::optional<Utils::Id> identifier() const;
    QProcess::ProcessState processState() const;

    void restart(const Utils::Terminal::OpenTerminalParameters &openParameters);

    static void initActions(QObject *parent);

    void unlockGlobalAction(const Utils::Id &commandId);

signals:
    void started(qint64 pid);
    void finished(int exitCode);
    void cwdChanged(const Utils::FilePath &cwd);
    void commandChanged(const Utils::CommandLine &cmd);
    void titleChanged();

protected:
    void dragEnterEvent(QDragEnterEvent *event) override;
    void dropEvent(QDropEvent *event) override;
    void showEvent(QShowEvent *event) override;
    void focusInEvent(QFocusEvent *event) override;
    bool event(QEvent *event) override;

    void onReadyRead(bool forceFlush);
    void setupFont();
    void setupPty();
    void setupColors();
    void setupActions();

    void handleEscKey(QKeyEvent *event);

    void surfaceChanged() override;

    void selectionChanged(const std::optional<Selection> &newSelection) override;
    void linkActivated(const Link &link) override;
    void contextMenuRequested(const QPoint &pos) override;

    qint64 writeToPty(const QByteArray &data) override;
    void resizePty(QSize newSize) override;
    void setClipboard(const QString &text) override;
    std::optional<TerminalView::Link> toLink(const QString &text) override;

    void registerShortcut(Core::Command *command);

    void updateCopyState();

private:
    Core::Context m_context;
    std::unique_ptr<Utils::Process> m_process;
    std::unique_ptr<ShellIntegration> m_shellIntegration;

    QString m_shellName;
    QString m_title;

    Utils::Id m_identifier;

    Utils::Terminal::OpenTerminalParameters m_openParameters;

    Utils::FilePath m_cwd;
    Utils::CommandLine m_currentCommand;

    RegisteredAction m_copy;
    RegisteredAction m_paste;
    RegisteredAction m_clearSelection;
    RegisteredAction m_clearTerminal;
    RegisteredAction m_selectAll;
    RegisteredAction m_moveCursorWordLeft;
    RegisteredAction m_moveCursorWordRight;

    Internal::ShortcutMap m_shortcutMap;

    std::unique_ptr<QFutureWatcher<Utils::expected_str<Utils::FilePath>>> m_findShellWatcher;
};

} // namespace Terminal