Repository: microsoft/terminal (language: cpp)

Issue:
Add support for Acrylic and Opacity in "Unfocused Appearance"
Please add "Acrylic" setting support to unfocused appearance options being introduced in PR #10317 

I'd really like to make my unfocused windows to have transparency instead of opaque. Ideally, I could make my unfocused windows more transparent than my focused window.

Right now, since unfocused windows are opaque, they appear more predominant on the desktop than the focused window appears when configured with acrylic enabled and semi-transparent.





Discussion / hints:
You know, we don't have an issue tracking this specific task, so congratulations, _this is now that thread_.

We need to first do #7158 to allow unfocused windows to have acrylic at all. Once that's done, we can add it back to the `Appearance` object and the `unfocusedAppearance` settings.
This is the only reason why I keep using the old-school cmd and powershell, they don't offer acrylic transparency but their transparency is better because it's always enabled.
I really hope this feature will be added, thanks everyone.

![immagine](https://user-images.githubusercontent.com/27242001/141379571-45c5c150-fd1f-4d84-8883-7600da0d4d7d.png)

@LoneDev6 you're probably interested in https://github.com/microsoft/terminal/issues/1753#issuecomment-950277817.
And just #603, #11180 in general
For testing purposes:

settings:
```jsonc

            {
                "commandline": "cmd.exe",
                "hidden": false,
                "name": "unfocused acrylic, 20%",
                "useAcrylic": true,
                "opacity": 20,
                "colorScheme": "Campbell",
                "background": "#00ff00",
                "unfocusedAppearance":
                {
                    "background": "#0000ff"
                }
            },
            {
                "commandline": "cmd.exe",
                "hidden": false,
                "name": "focused only acrylic, 20%",
                "useAcrylic": true,
                "opacity": 20,
                "colorScheme": "Campbell",
                "background": "#00ff00",
                "unfocusedAppearance":
                {
                    "background": "#0000ff",
                    "useAcrylic": false,
                }
            },
            {
                "commandline": "cmd.exe",
                "hidden": false,
                "name": "focused only 100%",
                "useAcrylic": true,
                "opacity": 20,
                "colorScheme": "Campbell",
                "background": "#00ff00",
                "unfocusedAppearance":
                {
                    "background": "#0000ff",
                    "opacity": 100,
                }
            }
```


State:
```jsonc

				{
					"action" : "newTab",
					"commandline" : "cmd.exe",
					"profile" : "unfocused acrylic",
					"startingDirectory" : null,
					"suppressApplicationTitle" : false,
					"tabTitle" : "unfocused acrylic"
				},
				{
					"action" : "splitPane",
					"commandline" : "cmd.exe",
					"profile" : "focused acrylic",
					"size" : 0.5,
					"split" : "right",
					"splitMode" : "manual",
					"startingDirectory" : null,
					"suppressApplicationTitle" : false,
					"tabTitle" : "focused acrylic"
				},
				{
					"action" : "splitPane",
					"commandline" : "cmd.exe",
					"profile" : "focused only 100%",
					"size" : 0.5,
					"split" : "down",
					"splitMode" : "manual",
					"startingDirectory" : null,
					"suppressApplicationTitle" : false,
					"tabTitle" : "focused only 100%"
				},
				{
					"action" : "focusPane",
					"id" : 0
				}
```
Yeah, I much agree with this issue. I love the transparency functionality, but seems to have been implemented the wrong way around. When the window loses focus, then I would expect transparency to become effective, becoming opaque when the window gains focus so you can see text more clearly. And perhaps we could define different opacity levels depending on focus state; so typically one would set a higher opacity level when the window gains focus.


> **Note**
> ## Walkthrough

x-linking https://github.com/microsoft/terminal/issues/7158#issuecomment-1162080143. That comment basically describes how to do both of these
This is also not working for normal transparency (not acrylic) 
![Focussed-Unfocussed-Opacity-NotAcrylic](https://github.com/microsoft/terminal/assets/15957528/55342018-c2b4-412e-8531-ec16977fd717)

![image](https://github.com/microsoft/terminal/assets/15957528/2fe1833c-471b-4c57-bfaf-5535e1b24ed1)

A hero has arrived 😎
![image](https://github.com/microsoft/terminal/assets/15957528/3f6868f1-d261-4b84-8066-1b81ea6c2808)

For:
![image](https://github.com/microsoft/terminal/assets/15957528/fc751bc2-0036-4ed2-a9c6-9b78e8cd6834)



Will keep you up to date with the progress!
Yes!

# 🥳

Fix patch:
diff --git a/doc/cascadia/profiles.schema.json b/doc/cascadia/profiles.schema.json
index 6f3d62fa297..f8e71e1dc51 100644
--- a/doc/cascadia/profiles.schema.json
+++ b/doc/cascadia/profiles.schema.json
@@ -273,6 +273,13 @@
         "useAcrylic":{
           "description": "When set to true, the window will have an acrylic material background when unfocused. When set to false, the window will have a plain, untextured background when unfocused.",
           "type": "boolean"
+        },
+        "opacity": {
+          "default": 100,
+          "description": "Sets the opacity of the window for the profile when unfocused. Accepts values from 0-100.",
+          "maximum": 100,
+          "minimum": 0,
+          "type": "number"
         }
       },
       "type": "object"
diff --git a/src/cascadia/TerminalControl/ControlCore.cpp b/src/cascadia/TerminalControl/ControlCore.cpp
index a7609d29747..7925fc16eb8 100644
--- a/src/cascadia/TerminalControl/ControlCore.cpp
+++ b/src/cascadia/TerminalControl/ControlCore.cpp
@@ -680,7 +680,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
         _setOpacity(Opacity() + adjustment);
     }
 
-    void ControlCore::_setOpacity(const double opacity)
+    // Method Description:
+    // - Updates the opacity of the terminal
+    // Arguments:
+    // - opacity: The new opacity to set.
+    // - focused (default == true): Whether the window is focused or unfocused.
+    // Return Value:
+    // - <none>
+    void ControlCore::_setOpacity(const double opacity, bool focused)
     {
         const auto newOpacity = std::clamp(opacity,
                                            0.0,
@@ -694,6 +701,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation
         // Update our runtime opacity value
         _runtimeOpacity = newOpacity;
 
+        //Stores the focused runtime opacity separately from unfocused opacity
+        //to transition smoothly between the two.
+        _runtimeFocusedOpacity = focused ? newOpacity : _runtimeFocusedOpacity;
+
         // Manually turn off acrylic if they turn off transparency.
         _runtimeUseAcrylic = newOpacity < 1.0 && _settings->UseAcrylic();
 
@@ -824,6 +835,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
         _cellWidth = CSSLengthPercentage::FromString(_settings->CellWidth().c_str());
         _cellHeight = CSSLengthPercentage::FromString(_settings->CellHeight().c_str());
         _runtimeOpacity = std::nullopt;
+        _runtimeFocusedOpacity = std::nullopt;
 
         // Manually turn off acrylic if they turn off transparency.
         _runtimeUseAcrylic = _settings->Opacity() < 1.0 && _settings->UseAcrylic();
@@ -874,21 +886,29 @@ namespace winrt::Microsoft::Terminal::Control::implementation
             _renderEngine->SetRetroTerminalEffect(newAppearance->RetroTerminalEffect());
             _renderEngine->SetPixelShaderPath(newAppearance->PixelShaderPath());
 
+            // Incase EnableUnfocusedAcrylic is disabled and Focused Acrylic is set to true,
+            // the terminal should ignore the unfocused opacity from settings.
+            // The Focused Opacity from settings should be ignored if overridden at runtime.
+            bool useFocusedRuntimeOpacity = focused || (!_settings->EnableUnfocusedAcrylic() && UseAcrylic());
+            double newOpacity = useFocusedRuntimeOpacity ?
+                                    FocusedOpacity() :
+                                    newAppearance->Opacity();
+            _setOpacity(newOpacity, focused);
+
             // No need to update Acrylic if UnfocusedAcrylic is disabled
             if (_settings->EnableUnfocusedAcrylic())
             {
                 // Manually turn off acrylic if they turn off transparency.
                 _runtimeUseAcrylic = Opacity() < 1.0 && newAppearance->UseAcrylic();
+            }
 
-                // Update the renderer as well. It might need to fall back from
-                // cleartype -> grayscale if the BG is transparent / acrylic.
-                _renderEngine->EnableTransparentBackground(_isBackgroundTransparent());
-                _renderer->NotifyPaintFrame();
-
-                auto eventArgs = winrt::make_self<TransparencyChangedEventArgs>(Opacity());
+            // Update the renderer as well. It might need to fall back from
+            // cleartype -> grayscale if the BG is transparent / acrylic.
+            _renderEngine->EnableTransparentBackground(_isBackgroundTransparent());
+            _renderer->NotifyPaintFrame();
 
-                _TransparencyChangedHandlers(*this, *eventArgs);
-            }
+            auto eventArgs = winrt::make_self<TransparencyChangedEventArgs>(Opacity());
+            _TransparencyChangedHandlers(*this, *eventArgs);
 
             _renderer->TriggerRedrawAll(true, true);
         }
diff --git a/src/cascadia/TerminalControl/ControlCore.h b/src/cascadia/TerminalControl/ControlCore.h
index c21730f4789..90cc5fca4d4 100644
--- a/src/cascadia/TerminalControl/ControlCore.h
+++ b/src/cascadia/TerminalControl/ControlCore.h
@@ -246,6 +246,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
         bool ShouldShowSelectOutput();
 
         RUNTIME_SETTING(double, Opacity, _settings->Opacity());
+        RUNTIME_SETTING(double, FocusedOpacity, FocusedAppearance().Opacity());
         RUNTIME_SETTING(bool, UseAcrylic, _settings->UseAcrylic());
 
         // -------------------------------- WinRT Events ---------------------------------
@@ -386,7 +387,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
         void _updateAntiAliasingMode();
         void _connectionOutputHandler(const hstring& hstr);
         void _updateHoveredCell(const std::optional<til::point> terminalPosition);
-        void _setOpacity(const double opacity);
+        void _setOpacity(const double opacity, const bool focused = true);
 
         bool _isBackgroundTransparent();
         void _focusChanged(bool focused);


Test patch: