diff --git a/CMakeLists.txt b/CMakeLists.txt index 56dc8a2..7cf7c74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ set(SRCS src/main.cpp src/processhelper.h src/processhelper.cpp + src/utils.cpp ) set(RESOURCES diff --git a/qmltermwidget/lib/TerminalDisplay.cpp b/qmltermwidget/lib/TerminalDisplay.cpp index 0438269..e038f39 100644 --- a/qmltermwidget/lib/TerminalDisplay.cpp +++ b/qmltermwidget/lib/TerminalDisplay.cpp @@ -2674,7 +2674,12 @@ void TerminalDisplay::bracketText(QString& text) void TerminalDisplay::setSelection(const QString& t) { - QApplication::clipboard()->setText(t, QClipboard::Selection); + QApplication::clipboard()->setText(t, QClipboard::Selection); +} + +bool TerminalDisplay::selectedText() +{ + return !_screenWindow->selectedText(false).isEmpty(); } void TerminalDisplay::copyClipboard() diff --git a/qmltermwidget/lib/TerminalDisplay.h b/qmltermwidget/lib/TerminalDisplay.h index 4fbba9b..8f1ae6d 100644 --- a/qmltermwidget/lib/TerminalDisplay.h +++ b/qmltermwidget/lib/TerminalDisplay.h @@ -103,6 +103,8 @@ class KONSOLEPRIVATE_EXPORT TerminalDisplay : public QQuickPaintedItem Q_PROPERTY(bool antialiasText READ antialias WRITE setAntialias) Q_PROPERTY(QStringList availableColorSchemes READ availableColorSchemes NOTIFY availableColorSchemesChanged) + Q_PROPERTY(bool selectedText READ selectedText CONSTANT) + public: /** Constructs a new terminal display widget with the specified parent. */ TerminalDisplay(QQuickItem *parent=0); @@ -362,6 +364,7 @@ public: }; void setSelection(const QString &t); + bool selectedText(); /** * Reimplemented. Has no effect. Use setVTFont() to change the font diff --git a/src/main.cpp b/src/main.cpp index c3c1071..64ad079 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,7 @@ #include #include "processhelper.h" +#include "utils.h" int main(int argc, char *argv[]) { @@ -47,6 +48,8 @@ int main(int argc, char *argv[]) } engine.rootContext()->setContextProperty("Process", new ProcessHelper); + engine.rootContext()->setContextProperty("Utils", new Utils); + engine.addImportPath(QStringLiteral("qrc:/")); engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); diff --git a/src/qml/Terminal.qml b/src/qml/Terminal.qml index a5e45c6..551bf46 100644 --- a/src/qml/Terminal.qml +++ b/src/qml/Terminal.qml @@ -122,6 +122,7 @@ Page { onPressed: { if ((!_terminal.terminalUsesMouse || mouse.modifiers & Qt.ShiftModifier) && mouse.button == Qt.RightButton) { + updateMenu() terminalMenu.open() } else { var coord = correctDistortion(mouse.x, mouse.y) @@ -141,8 +142,8 @@ Page { onClicked: { if (mouse.button === Qt.RightButton) { + updateMenu() terminalMenu.open() - } else if(mouse.button === Qt.LeftButton) { _terminal.forceActiveFocus() } @@ -216,4 +217,9 @@ Page { return Qt.point((x - cc.width * (1 + distortion) * distortion) * _terminal.width, (y - cc.height * (1 + distortion) * distortion) * _terminal.height) } + + function updateMenu() { + pasteAction.visible = Utils.text() + copyAction.visible = _terminal.selectedText + } } diff --git a/src/qml/main.qml b/src/qml/main.qml index 8ea65f2..27700a4 100644 --- a/src/qml/main.qml +++ b/src/qml/main.qml @@ -38,7 +38,7 @@ FishUI.Window { property int currentIndex: -1 property alias currentItem: _view.currentItem - readonly property QMLTermWidget currentTerminal: currentItem.terminal + readonly property QMLTermWidget currentTerminal: currentItem ? currentItem.terminal : null GlobalSettings { id: settings } ObjectModel { id: tabsModel } @@ -71,7 +71,7 @@ FishUI.Window { anchors.leftMargin: FishUI.Units.smallSpacing anchors.rightMargin: FishUI.Units.smallSpacing anchors.topMargin: FishUI.Units.smallSpacing - anchors.bottomMargin: FishUI.Units.smallSpacing + anchors.bottomMargin: FishUI.Units.smallSpacing / 2 spacing: FishUI.Units.smallSpacing ListView { @@ -90,7 +90,7 @@ FishUI.Window { delegate: Item { id: _tabItem - height: root.header.height - FishUI.Units.largeSpacing + height: root.header.height - FishUI.Units.largeSpacing + FishUI.Units.smallSpacing / 2 width: Math.min(_layout.implicitWidth + FishUI.Units.largeSpacing, _tabView.width / _tabView.count - FishUI.Units.smallSpacing) @@ -106,7 +106,8 @@ FishUI.Window { Rectangle { anchors.fill: parent - color: isCurrent ? FishUI.Theme.secondBackgroundColor : "transparent" + color: isCurrent ? FishUI.Theme.highlightColor : "transparent" + opacity: 0.1 border.width: 0 radius: FishUI.Theme.smallRadius } @@ -152,6 +153,7 @@ FishUI.Window { } } + ListView { id: _view anchors.fill: parent @@ -202,7 +204,6 @@ FishUI.Window { _view.currentIndex = index object.terminalClosed.connect(() => closeTab(index)) } - } function closeTab(index) { diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..d0927f5 --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2021 CutefishOS Team. + * + * Author: Reion Wong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "utils.h" +#include +#include + +static Utils *SELF = nullptr; + +Utils *Utils::self() +{ + if (!SELF) + SELF = new Utils; + + return SELF; +} + +Utils::Utils(QObject *parent) + : QObject(parent) +{ + +} + +QString Utils::text() const +{ + return qApp->clipboard()->text(); +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..eecdf32 --- /dev/null +++ b/src/utils.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2021 CutefishOS Team. + * + * Author: Reion Wong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef UTILS_H +#define UTILS_H + +#include + +class Utils : public QObject +{ + Q_OBJECT + +public: + static Utils *self(); + explicit Utils(QObject *parent = nullptr); + + Q_INVOKABLE QString text() const; +}; + +#endif // UTILS_H