diff --git a/src/EditProfileDialog.h b/EditProfileDialog.h --- a/src/EditProfileDialog.h +++ b/EditProfileDialog.h @@ -120,6 +120,7 @@ void terminalColumnsEntryChanged(int); void terminalRowsEntryChanged(int); void showTerminalSizeHint(bool); + void setDimWhenInactive(bool); void showEnvironmentEditor(); void silenceSecondsChanged(int); diff --git a/src/EditProfileDialog.cpp b/EditProfileDialog.cpp --- a/src/EditProfileDialog.cpp +++ b/EditProfileDialog.cpp @@ -359,6 +359,7 @@ // window options _ui->showTerminalSizeHintButton->setChecked(profile->showTerminalSizeHint()); + _ui->dimWhenInactiveCheckbox->setChecked(profile->dimWhenInactive()); // signals and slots connect(_ui->dirSelectButton, &QToolButton::clicked, this, @@ -384,6 +385,9 @@ connect(_ui->showTerminalSizeHintButton, &QCheckBox::toggled, this, &Konsole::EditProfileDialog::showTerminalSizeHint); + + connect(_ui->dimWhenInactiveCheckbox, &QCheckBox::toggled, this, + &Konsole::EditProfileDialog::setDimWhenInactive); } void EditProfileDialog::showEnvironmentEditor() @@ -457,6 +461,11 @@ updateTempProfileProperty(Profile::ShowTerminalSizeHint, value); } +void EditProfileDialog::setDimWhenInactive(bool value) +{ + updateTempProfileProperty(Profile::DimWhenInactive, value); +} + void EditProfileDialog::tabTitleFormatChanged(const QString &format) { updateTempProfileProperty(Profile::LocalTabTitleFormat, format); diff --git a/src/EditProfileDialog.ui b/EditProfileDialog.ui --- a/src/EditProfileDialog.ui +++ b/EditProfileDialog.ui @@ -343,6 +343,16 @@ + + + + Indicate whether the window is active by dimming the colors + + + Dim the colors when the window loses focus + + + diff --git a/src/Profile.h b/Profile.h --- a/src/Profile.h +++ b/Profile.h @@ -114,6 +114,9 @@ * resizing the application window. */ ShowTerminalSizeHint, + /** (bool) If the background color should change to indicate if the window is active + */ + DimWhenInactive, /** (QFont) The font to use in terminal displays using this profile. */ Font, /** (QString) The name of the color scheme to use in terminal @@ -439,6 +442,12 @@ return property(Profile::ShowTerminalSizeHint); } + /** Convenience method for property(Profile::DimWhenInactive) */ + bool dimWhenInactive() const + { + return property(Profile::DimWhenInactive); + } + /** Convenience method for property(Profile::Font) */ QFont font() const { diff --git a/src/Profile.cpp b/Profile.cpp --- a/src/Profile.cpp +++ b/Profile.cpp @@ -67,6 +67,7 @@ , { LocalTabTitleFormat , "tabtitle" , nullptr , QVariant::String } , { RemoteTabTitleFormat , "RemoteTabTitleFormat" , GENERAL_GROUP , QVariant::String } , { ShowTerminalSizeHint , "ShowTerminalSizeHint" , GENERAL_GROUP , QVariant::Bool } + , { DimWhenInactive , "DimWhenInactive" , GENERAL_GROUP , QVariant::Bool } , { StartInCurrentSessionDir , "StartInCurrentSessionDir" , GENERAL_GROUP , QVariant::Bool } , { SilenceSeconds, "SilenceSeconds" , GENERAL_GROUP , QVariant::Int } , { TerminalColumns, "TerminalColumns" , GENERAL_GROUP , QVariant::Int } @@ -166,6 +167,7 @@ setProperty(LocalTabTitleFormat, QStringLiteral("%d : %n")); setProperty(RemoteTabTitleFormat, QStringLiteral("(%u) %H")); setProperty(ShowTerminalSizeHint, true); + setProperty(DimWhenInactive, false); setProperty(StartInCurrentSessionDir, true); setProperty(MenuIndex, QStringLiteral("0")); setProperty(SilenceSeconds, 10); diff --git a/src/TerminalDisplay.h b/TerminalDisplay.h --- a/src/TerminalDisplay.h +++ b/TerminalDisplay.h @@ -696,6 +696,13 @@ */ void setCenterContents(bool enable); + /** + * Sets whether the background should change when the window loses focus + */ + void setDimWhenInactive(bool shouldDim) { + _dimWhenInactive = shouldDim; + } + // Used to show/hide the message widget void updateReadOnlyState(bool readonly); IncrementalSearchBar *searchBar() const; @@ -798,7 +805,8 @@ void inputMethodEvent(QInputMethodEvent *event) Q_DECL_OVERRIDE; QVariant inputMethodQuery(Qt::InputMethodQuery query) const Q_DECL_OVERRIDE; - void updateScrollBarPalette(); + void onColorsChanged(); + protected Q_SLOTS: void scrollBarPositionChanged(int value); @@ -954,6 +962,9 @@ QVector _lineProperties; ColorEntry _colorTable[TABLE_COLORS]; + ColorEntry _dimColorTable[TABLE_COLORS]; + ColorEntry *_currentColorTable = _colorTable; + uint _randomSeed; bool _resizing; @@ -1064,6 +1075,8 @@ qreal _opacity; + bool _dimWhenInactive; + ScrollState _scrollWheelState; IncrementalSearchBar *_searchBar; diff --git a/src/TerminalDisplay.cpp b/TerminalDisplay.cpp --- a/src/TerminalDisplay.cpp +++ b/TerminalDisplay.cpp @@ -137,48 +137,75 @@ return _colorTable; } -void TerminalDisplay::updateScrollBarPalette() +static void setPaletteColors(QPalette *palette, const QPalette::ColorGroup group, const QColor &fg, const QColor &bg) { - QColor backgroundColor = _colorTable[DEFAULT_BACK_COLOR]; + palette->setColor(group, QPalette::Window, bg); + + // Fix the scrollbar + // this is a workaround to add some readability to old themes like Fusion + // changing the light value for button a bit makes themes like fusion, windows and oxygen way more readable and pleasing + QColor button = bg.toHsv(); + if (button.valueF() < 0.5) { + button = button.lighter(); + } else { + button = button.darker(); + } + palette->setColor(group, QPalette::Button, button); + palette->setColor(group, QPalette::WindowText, fg); + palette->setColor(group, QPalette::ButtonText, fg); +} + +void TerminalDisplay::onColorsChanged() +{ + // Create a dimmed version for indicating unfocused window + for (int i = 0; i < TABLE_COLORS; i++) { + _dimColorTable[i] = _colorTable[i].darker(); + } + + // Update the normal widget palette + QPalette p = QApplication::palette(); + + QColor backgroundColor, buttonColor, buttonTextColor; + + // First set the normal active colors + buttonTextColor = _colorTable[DEFAULT_FORE_COLOR]; + backgroundColor = _colorTable[DEFAULT_BACK_COLOR]; backgroundColor.setAlphaF(_opacity); - QPalette p = palette(); - p.setColor(QPalette::Window, backgroundColor); + setPaletteColors(&p, QPalette::Active, buttonTextColor, backgroundColor); - //this is a workaround to add some readability to old themes like Fusion - //changing the light value for button a bit makes themes like fusion, windows and oxygen way more readable and pleasing - QColor buttonColor; - buttonColor.setHsvF(backgroundColor.hueF(), backgroundColor.saturationF(), backgroundColor.valueF() + (backgroundColor.valueF() < 0.5 ? 0.2 : -0.2)); - p.setColor(QPalette::Button, buttonColor); + // Now set the inactive color palette + buttonTextColor = _dimColorTable[DEFAULT_FORE_COLOR]; + backgroundColor = _dimColorTable[DEFAULT_BACK_COLOR]; + backgroundColor.setAlphaF(_opacity); + setPaletteColors(&p, QPalette::Inactive, buttonTextColor, backgroundColor); - p.setColor(QPalette::WindowText, _colorTable[DEFAULT_FORE_COLOR]); - p.setColor(QPalette::ButtonText, _colorTable[DEFAULT_FORE_COLOR]); + setPalette(p); + + // As explained above, we need to fix the scrollbar palette as well to make fusion look nice _scrollBar->setPalette(p); + update(); } void TerminalDisplay::setBackgroundColor(const QColor& color) { - _colorTable[DEFAULT_BACK_COLOR] = color; + _currentColorTable[DEFAULT_BACK_COLOR] = color; - QPalette p = palette(); - p.setColor(backgroundRole(), color); - setPalette(p); - - updateScrollBarPalette(); - update(); + onColorsChanged(); } + QColor TerminalDisplay::getBackgroundColor() const { - QPalette p = palette(); - return p.color(backgroundRole()); + return _currentColorTable[DEFAULT_BACK_COLOR]; } + void TerminalDisplay::setForegroundColor(const QColor& color) { _colorTable[DEFAULT_FORE_COLOR] = color; - updateScrollBarPalette(); - update(); + onColorsChanged(); } + void TerminalDisplay::setColorTable(const ColorEntry table[]) { for (int i = 0; i < TABLE_COLORS; i++) { @@ -186,6 +213,8 @@ } setBackgroundColor(_colorTable[DEFAULT_BACK_COLOR]); + + onColorsChanged(); } /* ------------------------------------------------------------------------- */ @@ -470,6 +499,7 @@ , _readOnlyMessageWidget(nullptr) , _readOnly(false) , _opacity(1.0) + , _dimWhenInactive(false) , _scrollWheelState(ScrollState()) , _searchBar(new IncrementalSearchBar(this)) { @@ -533,6 +563,19 @@ _verticalLayout->setContentsMargins(0, 0, scrollBarWidth, 0); }); + // To redraw when focused window changes + connect(qApp, &QApplication::focusChanged, this, [=](QWidget *old, QWidget *now) { + if (!_dimWhenInactive) { + return; + } + // If old or now is a nullptr, it means that either the old or new widget is not in this application + if (!old || !now) { + update(); + } + }); + + + new AutoScrollHandler(this); @@ -872,7 +915,7 @@ }*/ _blendColor = color.rgba(); - updateScrollBarPalette(); + onColorsChanged(); } void TerminalDisplay::setWallpaper(ColorSchemeWallpaper::Ptr p) @@ -1001,7 +1044,7 @@ // setup pen const CharacterColor& textColor = (invertCharacterColor ? style->backgroundColor : style->foregroundColor); - const QColor color = textColor.color(_colorTable); + const QColor color = textColor.color(_currentColorTable); QPen pen = painter.pen(); if (pen.color() != color) { pen.setColor(color); @@ -1036,11 +1079,11 @@ painter.save(); // setup painter - const QColor foregroundColor = style->foregroundColor.color(_colorTable); - const QColor backgroundColor = style->backgroundColor.color(_colorTable); + const QColor foregroundColor = style->foregroundColor.color(_currentColorTable); + const QColor backgroundColor = style->backgroundColor.color(_currentColorTable); // draw background if different from the display's background color - if (backgroundColor != palette().background().color()) { + if (backgroundColor != getBackgroundColor()) { drawBackground(painter, rect, backgroundColor, false /* do not use transparency */); } @@ -1475,8 +1518,14 @@ { QPainter paint(this); + if (_dimWhenInactive && !isActiveWindow()) { + _currentColorTable = _dimColorTable; + } else { + _currentColorTable = _colorTable; + } + foreach(const QRect & rect, (pe->region() & contentsRect()).rects()) { - drawBackground(paint, rect, palette().background().color(), + drawBackground(paint, rect, getBackgroundColor(), true /* use opacity setting */); drawContents(paint, rect); } @@ -1541,7 +1590,7 @@ getCharacterPosition(cursorPos, cursorLine, cursorColumn, false); Character cursorCharacter = _image[loc(qMin(cursorColumn, _columns - 1), cursorLine)]; - painter.setPen(QPen(cursorCharacter.foregroundColor.color(colorTable()))); + painter.setPen(QPen(cursorCharacter.foregroundColor.color(_currentColorTable))); // iterate over hotspots identified by the display's currently active filters // and draw appropriate visuals to indicate the presence of the hotspot @@ -3473,8 +3522,8 @@ const QPoint cursorPos = cursorPosition(); bool invertColors = false; - const QColor background = _colorTable[DEFAULT_BACK_COLOR]; - const QColor foreground = _colorTable[DEFAULT_FORE_COLOR]; + const QColor background = _currentColorTable[DEFAULT_BACK_COLOR]; + const QColor foreground = _currentColorTable[DEFAULT_FORE_COLOR]; const Character* style = &_image[loc(cursorPos.x(), cursorPos.y())]; drawBackground(painter, rect, background, true); @@ -3695,7 +3744,7 @@ break; case QEvent::PaletteChange: case QEvent::ApplicationPaletteChange: - _scrollBar->setPalette(QApplication::palette()); + onColorsChanged(); break; default: break; @@ -3774,7 +3823,7 @@ _colorTable[DEFAULT_BACK_COLOR] = _colorTable[DEFAULT_FORE_COLOR]; _colorTable[DEFAULT_FORE_COLOR] = color; - update(); + onColorsChanged(); } /* --------------------------------------------------------------------- */ diff --git a/src/ViewManager.cpp b/ViewManager.cpp --- a/src/ViewManager.cpp +++ b/ViewManager.cpp @@ -811,6 +811,7 @@ // show hint about terminal size after resizing view->setShowTerminalSizeHint(profile->showTerminalSizeHint()); + view->setDimWhenInactive(profile->dimWhenInactive()); // terminal features view->setBlinkingCursorEnabled(profile->blinkingCursorEnabled());