Commit 67508680 authored by Jonah Brüchert's avatar Jonah Brüchert 🌳 Committed by Tomaz Canabrava
Browse files

Move TerminalBell into its own class

parent 1a2867c8
......@@ -185,6 +185,7 @@ set(konsoleprivate_SRCS ${windowadaptors_SRCS}
terminalDisplay/TerminalColor.cpp
terminalDisplay/TerminalFonts.cpp
terminalDisplay/TerminalClipboard.cpp
terminalDisplay/TerminalBell.cpp
widgets/TerminalDisplayAccessible.cpp
widgets/TerminalHeaderBar.cpp
......
/*
SPDX-FileCopyrightText: 2006-2008 Robert Knight <robertknight@gmail.com>
SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de>
SPDX-FileCopyrightText: 2021 Jonah Brüchert <jbb@kaidan.im>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "TerminalBell.h"
#include <QTimer>
#include <KNotification>
#include <chrono>
using namespace std::literals::chrono_literals;
namespace Konsole {
constexpr auto MASK_TIMEOUT = 500ms;
TerminalBell::TerminalBell(Enum::BellModeEnum bellMode)
: _bellMode(bellMode)
{
}
void TerminalBell::bell(const QString &message, bool terminalHasFocus)
{
switch (_bellMode) {
case Enum::SystemBeepBell:
KNotification::beep();
break;
case Enum::NotifyBell:
// STABLE API:
// Please note that these event names, "BellVisible" and "BellInvisible",
// should not change and should be kept stable, because other applications
// that use this code via KPart rely on these names for notifications.
KNotification::event(terminalHasFocus ? QStringLiteral("BellVisible") : QStringLiteral("BellInvisible"),
message, QPixmap());
break;
case Enum::VisualBell:
Q_EMIT visualBell();
break;
default:
break;
}
// limit the rate at which bells can occur.
// ...mainly for sound effects where rapid bells in sequence
// produce a horrible noise.
_bellMasked = true;
QTimer::singleShot(MASK_TIMEOUT, this, [this]() {
_bellMasked = false;
});
}
void TerminalBell::setBellMode(Enum::BellModeEnum mode)
{
_bellMode = mode;
}
}
/*
SPDX-FileCopyrightText: 2006-2008 Robert Knight <robertknight@gmail.com>
SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de>
SPDX-FileCopyrightText: 2021 Jonah Brüchert <jbb@kaidan.im>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <QObject>
#include <Enumeration.h>
namespace Konsole
{
class TerminalBell : public QObject {
Q_OBJECT
public:
TerminalBell(Enum::BellModeEnum bellMode);
void bell(const QString &message, bool terminalHasFocus);
/**
* Sets the type of effect used to alert the user when a 'bell' occurs in the
* terminal session.
*
* The terminal session can trigger the bell effect by calling bell() with
* the alert message.
*/
void setBellMode(Enum::BellModeEnum mode);
Q_SIGNALS:
void visualBell();
private:
Enum::BellModeEnum _bellMode;
bool _bellMasked = false;
};
}
......@@ -178,7 +178,6 @@ QAccessibleInterface* accessibleInterfaceFactory(const QString &key, QObject *ob
TerminalDisplay::TerminalDisplay(QWidget* parent)
: QWidget(parent)
, _screenWindow(nullptr)
, _bellMasked(false)
, _verticalLayout(new QVBoxLayout(this))
, _lines(1)
, _columns(1)
......@@ -208,7 +207,7 @@ TerminalDisplay::TerminalDisplay(QWidget* parent)
, _copyTextAsHTML(true)
, _middleClickPasteMode(Enum::PasteFromX11Selection)
, _wordCharacters(QStringLiteral(":@-./_~"))
, _bellMode(Enum::NotifyBell)
, _bell(Enum::NotifyBell)
, _allowBlinkingText(true)
, _allowBlinkingCursor(false)
, _textBlinking(false)
......@@ -310,6 +309,10 @@ TerminalDisplay::TerminalDisplay(QWidget* parent)
connect(focusWatcher, &CompositeWidgetFocusWatcher::compositeFocusChanged,
_headerBar, &TerminalHeaderBar::setFocusIndicatorState);
connect(&_bell, &TerminalBell::visualBell, this, [this] {
_terminalColor->visualBell();
});
#ifndef QT_NO_ACCESSIBILITY
QAccessible::installFactory(Konsole::accessibleInterfaceFactory);
#endif
......@@ -2505,48 +2508,9 @@ void TerminalDisplay::contextMenuEvent(QContextMenuEvent* event)
/* */
/* --------------------------------------------------------------------- */
void TerminalDisplay::setBellMode(int mode)
{
_bellMode = mode;
}
int TerminalDisplay::bellMode() const
{
return _bellMode;
}
void TerminalDisplay::bell(const QString& message)
{
if (_bellMasked) {
return;
}
switch (_bellMode) {
case Enum::SystemBeepBell:
KNotification::beep();
break;
case Enum::NotifyBell:
// STABLE API:
// Please note that these event names, "BellVisible" and "BellInvisible",
// should not change and should be kept stable, because other applications
// that use this code via KPart rely on these names for notifications.
KNotification::event(hasFocus() ? QStringLiteral("BellVisible") : QStringLiteral("BellInvisible"),
message, QPixmap(), this);
break;
case Enum::VisualBell:
_terminalColor->visualBell();
break;
default:
break;
}
// limit the rate at which bells can occur.
// ...mainly for sound effects where rapid bells in sequence
// produce a horrible noise.
_bellMasked = true;
QTimer::singleShot(500, this, [this]() {
_bellMasked = false;
});
_bell.bell(message, hasFocus());
}
/* --------------------------------------------------------------------- */
......@@ -2755,7 +2719,7 @@ void TerminalDisplay::applyProfile(const Profile::Ptr &profile)
setWordCharacters(profile->wordCharacters());
// bell mode
setBellMode(profile->property<int>(Profile::BellMode));
_bell.setBellMode(Enum::BellModeEnum(profile->property<int>(Profile::BellMode)));
// mouse wheel zoom
_mouseWheelZoom = profile->mouseWheelZoomEnabled();
......
......@@ -24,6 +24,8 @@
#include "ScrollState.h"
#include "widgets/TerminalHeaderBar.h"
#include "TerminalBell.h"
class QDrag;
class QDragEnterEvent;
class QDropEvent;
......@@ -208,22 +210,6 @@ public:
*/
void setWordCharacters(const QString &wc);
/**
* Sets the type of effect used to alert the user when a 'bell' occurs in the
* terminal session.
*
* The terminal session can trigger the bell effect by calling bell() with
* the alert message.
*/
void setBellMode(int mode);
/**
* Returns the type of effect used to alert the user when a 'bell' occurs in
* the terminal session.
*
* See setBellMode()
*/
int bellMode() const;
TerminalHeaderBar *headerBar() const
{
return _headerBar;
......@@ -659,7 +645,7 @@ private:
Enum::MiddleClickPasteModeEnum _middleClickPasteMode;
QString _wordCharacters;
int _bellMode;
TerminalBell _bell;
bool _allowBlinkingText; // allow text to blink
bool _allowBlinkingCursor; // allow cursor to blink
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment