Repository: Textualize/textual

Issue:
When TextArea is on a non-default screen, the text of the active line is hidden
This was reported [on Discord](https://discord.com/channels/1026214085173461072/1033754296224841768/1211142660786618438).

![image](https://github.com/Textualize/textual/assets/5740731/6010e9be-ac09-4e58-b305-a8389c0fe02b)

There is text behind the grey "active line" highlight.

```python
from textual.app import App
from textual.screen import Screen
from textual.widgets import TextArea

class AltScreen(Screen):
    def compose(self):
        yield TextArea()

class TABug(App):
    def on_mount(self):
        self.push_screen(AltScreen())

if __name__ == "__main__":
    app = TABug()
    app.run()
```


Discussion:

Resolving patch:
diff --git a/src/textual/_text_area_theme.py b/src/textual/_text_area_theme.py
--- a/src/textual/_text_area_theme.py
+++ b/src/textual/_text_area_theme.py
@@ -1,6 +1,6 @@
 from __future__ import annotations
 
-from dataclasses import dataclass, field
+from dataclasses import dataclass, field, fields
 from typing import TYPE_CHECKING
 
 from rich.style import Style
@@ -67,6 +67,15 @@ class TextAreaTheme:
     syntax_styles: dict[str, Style] = field(default_factory=dict)
     """The mapping of tree-sitter names from the `highlight_query` to Rich styles."""
 
+    _theme_configured_attributes: set[str] = field(init=False, default_factory=set)
+    """Records which attributes were set via the theme object (as opposed to CSS components)."""
+
+    def __post_init__(self) -> None:
+        theme_fields = fields(self)
+        for field in theme_fields:
+            if getattr(self, field.name) is not None:
+                self._theme_configured_attributes.add(field.name)
+
     def apply_css(self, text_area: TextArea) -> None:
         """Apply CSS rules from a TextArea to be used for fallback styling.
 
@@ -88,11 +97,13 @@ def apply_css(self, text_area: TextArea) -> None:
                 color=self.base_style.color, bgcolor=DEFAULT_DARK_SURFACE
             )
 
+        configured = self._theme_configured_attributes.__contains__
+
         assert self.base_style is not None
         assert self.base_style.color is not None
         assert self.base_style.bgcolor is not None
 
-        if self.gutter_style is None:
+        if not configured("gutter_style"):
             gutter_style = get_style("text-area--gutter")
             if gutter_style:
                 self.gutter_style = gutter_style
@@ -100,7 +111,7 @@ def apply_css(self, text_area: TextArea) -> None:
                 self.gutter_style = self.base_style.copy()
 
         background_color = Color.from_rich_color(self.base_style.bgcolor)
-        if self.cursor_style is None:
+        if not configured("cursor_style"):
             # If the theme doesn't contain a cursor style, fallback to component styles.
             cursor_style = get_style("text-area--cursor")
             if cursor_style:
@@ -113,13 +124,13 @@ def apply_css(self, text_area: TextArea) -> None:
                 )
 
         # Apply fallbacks for the styles of the active line and active line gutter.
-        if self.cursor_line_style is None:
+        if not configured("cursor_line_style"):
             self.cursor_line_style = get_style("text-area--cursor-line")
 
-        if self.cursor_line_gutter_style is None:
+        if not configured("cursor_line_gutter_style"):
             self.cursor_line_gutter_style = get_style("text-area--cursor-gutter")
 
-        if self.bracket_matching_style is None:
+        if not configured("bracket_matching_style"):
             matching_bracket_style = get_style("text-area--matching-bracket")
             if matching_bracket_style:
                 self.bracket_matching_style = matching_bracket_style
@@ -131,7 +142,7 @@ def apply_css(self, text_area: TextArea) -> None:
                     bgcolor=bracket_matching_background.rich_color
                 )
 
-        if self.selection_style is None:
+        if not configured("selection_style"):
             selection_style = get_style("text-area--selection")
             if selection_style:
                 self.selection_style = selection_style


Test patch:
diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr
--- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr
+++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr
@@ -34515,6 +34515,109 @@
   
   '''
 # ---
+# name: test_text_area_alternate_screen
+  '''
+  <svg class="rich-terminal" viewBox="0 0 604 294.0" xmlns="http://www.w3.org/2000/svg">
+      <!-- Generated with Rich https://www.textualize.io -->
+      <style>
+  
+      @font-face {
+          font-family: "Fira Code";
+          src: local("FiraCode-Regular"),
+                  url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
+                  url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");
+          font-style: normal;
+          font-weight: 400;
+      }
+      @font-face {
+          font-family: "Fira Code";
+          src: local("FiraCode-Bold"),
+                  url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),
+                  url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");
+          font-style: bold;
+          font-weight: 700;
+      }
+  
+      .terminal-266942663-matrix {
+          font-family: Fira Code, monospace;
+          font-size: 20px;
+          line-height: 24.4px;
+          font-variant-east-asian: full-width;
+      }
+  
+      .terminal-266942663-title {
+          font-size: 18px;
+          font-weight: bold;
+          font-family: arial;
+      }
+  
+      .terminal-266942663-r1 { fill: #1e1e1e }
+  .terminal-266942663-r2 { fill: #0178d4 }
+  .terminal-266942663-r3 { fill: #c5c8c6 }
+  .terminal-266942663-r4 { fill: #e1e1e1 }
+  .terminal-266942663-r5 { fill: #151515 }
+  .terminal-266942663-r6 { fill: #e2e2e2 }
+      </style>
+  
+      <defs>
+      <clipPath id="terminal-266942663-clip-terminal">
+        <rect x="0" y="0" width="584.5999999999999" height="243.0" />
+      </clipPath>
+      <clipPath id="terminal-266942663-line-0">
+      <rect x="0" y="1.5" width="585.6" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-266942663-line-1">
+      <rect x="0" y="25.9" width="585.6" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-266942663-line-2">
+      <rect x="0" y="50.3" width="585.6" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-266942663-line-3">
+      <rect x="0" y="74.7" width="585.6" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-266942663-line-4">
+      <rect x="0" y="99.1" width="585.6" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-266942663-line-5">
+      <rect x="0" y="123.5" width="585.6" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-266942663-line-6">
+      <rect x="0" y="147.9" width="585.6" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-266942663-line-7">
+      <rect x="0" y="172.3" width="585.6" height="24.65"/>
+              </clipPath>
+  <clipPath id="terminal-266942663-line-8">
+      <rect x="0" y="196.7" width="585.6" height="24.65"/>
+              </clipPath>
+      </defs>
+  
+      <rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="602" height="292" rx="8"/><text class="terminal-266942663-title" fill="#c5c8c6" text-anchor="middle" x="301" y="27">TABug</text>
+              <g transform="translate(26,22)">
+              <circle cx="0" cy="0" r="7" fill="#ff5f57"/>
+              <circle cx="22" cy="0" r="7" fill="#febc2e"/>
+              <circle cx="44" cy="0" r="7" fill="#28c840"/>
+              </g>
+          
+      <g transform="translate(9, 41)" clip-path="url(#terminal-266942663-clip-terminal)">
+      <rect fill="#0178d4" x="0" y="1.5" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="12.2" y="1.5" width="561.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="573.4" y="1.5" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#0178d4" x="0" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="12.2" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="24.4" y="25.9" width="0" height="24.65" shape-rendering="crispEdges"/><rect fill="#d8d8d8" x="24.4" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#262626" x="36.6" y="25.9" width="524.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="561.2" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="573.4" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#0178d4" x="0" y="50.3" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="12.2" y="50.3" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="24.4" y="50.3" width="48.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="73.2" y="50.3" width="488" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="561.2" y="50.3" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="573.4" y="50.3" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#0178d4" x="0" y="74.7" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="12.2" y="74.7" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="24.4" y="74.7" width="48.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="73.2" y="74.7" width="488" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="561.2" y="74.7" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="573.4" y="74.7" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#0178d4" x="0" y="99.1" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="12.2" y="99.1" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="24.4" y="99.1" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="36.6" y="99.1" width="524.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="561.2" y="99.1" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="573.4" y="99.1" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#0178d4" x="0" y="123.5" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="12.2" y="123.5" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="24.4" y="123.5" width="536.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="561.2" y="123.5" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="573.4" y="123.5" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#0178d4" x="0" y="147.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="12.2" y="147.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="24.4" y="147.9" width="536.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="561.2" y="147.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="573.4" y="147.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#0178d4" x="0" y="172.3" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="12.2" y="172.3" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="24.4" y="172.3" width="536.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="561.2" y="172.3" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="573.4" y="172.3" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#0178d4" x="0" y="196.7" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="12.2" y="196.7" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="24.4" y="196.7" width="536.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="561.2" y="196.7" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="573.4" y="196.7" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#0178d4" x="0" y="221.1" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="12.2" y="221.1" width="561.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="573.4" y="221.1" width="12.2" height="24.65" shape-rendering="crispEdges"/>
+      <g class="terminal-266942663-matrix">
+      <text class="terminal-266942663-r1" x="0" y="20" textLength="12.2" clip-path="url(#terminal-266942663-line-0)">▊</text><text class="terminal-266942663-r2" x="12.2" y="20" textLength="561.2" clip-path="url(#terminal-266942663-line-0)">▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔</text><text class="terminal-266942663-r2" x="573.4" y="20" textLength="12.2" clip-path="url(#terminal-266942663-line-0)">▎</text><text class="terminal-266942663-r3" x="585.6" y="20" textLength="12.2" clip-path="url(#terminal-266942663-line-0)">
+  </text><text class="terminal-266942663-r1" x="0" y="44.4" textLength="12.2" clip-path="url(#terminal-266942663-line-1)">▊</text><text class="terminal-266942663-r5" x="24.4" y="44.4" textLength="12.2" clip-path="url(#terminal-266942663-line-1)">f</text><text class="terminal-266942663-r6" x="36.6" y="44.4" textLength="524.6" clip-path="url(#terminal-266942663-line-1)">oo&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</text><text class="terminal-266942663-r2" x="573.4" y="44.4" textLength="12.2" clip-path="url(#terminal-266942663-line-1)">▎</text><text class="terminal-266942663-r3" x="585.6" y="44.4" textLength="12.2" clip-path="url(#terminal-266942663-line-1)">
+  </text><text class="terminal-266942663-r1" x="0" y="68.8" textLength="12.2" clip-path="url(#terminal-266942663-line-2)">▊</text><text class="terminal-266942663-r4" x="24.4" y="68.8" textLength="48.8" clip-path="url(#terminal-266942663-line-2)">bar&#160;</text><text class="terminal-266942663-r2" x="573.4" y="68.8" textLength="12.2" clip-path="url(#terminal-266942663-line-2)">▎</text><text class="terminal-266942663-r3" x="585.6" y="68.8" textLength="12.2" clip-path="url(#terminal-266942663-line-2)">
+  </text><text class="terminal-266942663-r1" x="0" y="93.2" textLength="12.2" clip-path="url(#terminal-266942663-line-3)">▊</text><text class="terminal-266942663-r4" x="24.4" y="93.2" textLength="48.8" clip-path="url(#terminal-266942663-line-3)">baz&#160;</text><text class="terminal-266942663-r2" x="573.4" y="93.2" textLength="12.2" clip-path="url(#terminal-266942663-line-3)">▎</text><text class="terminal-266942663-r3" x="585.6" y="93.2" textLength="12.2" clip-path="url(#terminal-266942663-line-3)">
+  </text><text class="terminal-266942663-r1" x="0" y="117.6" textLength="12.2" clip-path="url(#terminal-266942663-line-4)">▊</text><text class="terminal-266942663-r2" x="573.4" y="117.6" textLength="12.2" clip-path="url(#terminal-266942663-line-4)">▎</text><text class="terminal-266942663-r3" x="585.6" y="117.6" textLength="12.2" clip-path="url(#terminal-266942663-line-4)">
+  </text><text class="terminal-266942663-r1" x="0" y="142" textLength="12.2" clip-path="url(#terminal-266942663-line-5)">▊</text><text class="terminal-266942663-r2" x="573.4" y="142" textLength="12.2" clip-path="url(#terminal-266942663-line-5)">▎</text><text class="terminal-266942663-r3" x="585.6" y="142" textLength="12.2" clip-path="url(#terminal-266942663-line-5)">
+  </text><text class="terminal-266942663-r1" x="0" y="166.4" textLength="12.2" clip-path="url(#terminal-266942663-line-6)">▊</text><text class="terminal-266942663-r2" x="573.4" y="166.4" textLength="12.2" clip-path="url(#terminal-266942663-line-6)">▎</text><text class="terminal-266942663-r3" x="585.6" y="166.4" textLength="12.2" clip-path="url(#terminal-266942663-line-6)">
+  </text><text class="terminal-266942663-r1" x="0" y="190.8" textLength="12.2" clip-path="url(#terminal-266942663-line-7)">▊</text><text class="terminal-266942663-r2" x="573.4" y="190.8" textLength="12.2" clip-path="url(#terminal-266942663-line-7)">▎</text><text class="terminal-266942663-r3" x="585.6" y="190.8" textLength="12.2" clip-path="url(#terminal-266942663-line-7)">
+  </text><text class="terminal-266942663-r1" x="0" y="215.2" textLength="12.2" clip-path="url(#terminal-266942663-line-8)">▊</text><text class="terminal-266942663-r2" x="573.4" y="215.2" textLength="12.2" clip-path="url(#terminal-266942663-line-8)">▎</text><text class="terminal-266942663-r3" x="585.6" y="215.2" textLength="12.2" clip-path="url(#terminal-266942663-line-8)">
+  </text><text class="terminal-266942663-r1" x="0" y="239.6" textLength="12.2" clip-path="url(#terminal-266942663-line-9)">▊</text><text class="terminal-266942663-r2" x="12.2" y="239.6" textLength="561.2" clip-path="url(#terminal-266942663-line-9)">▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁</text><text class="terminal-266942663-r2" x="573.4" y="239.6" textLength="12.2" clip-path="url(#terminal-266942663-line-9)">▎</text>
+      </g>
+      </g>
+  </svg>
+  
+  '''
+# ---
 # name: test_text_area_language_rendering[css]
   '''
   <svg class="rich-terminal" viewBox="0 0 994 1782.3999999999999" xmlns="http://www.w3.org/2000/svg">
diff --git a/tests/snapshot_tests/snapshot_apps/text_area_alternate_screen.py b/tests/snapshot_tests/snapshot_apps/text_area_alternate_screen.py
new file mode 100644
--- /dev/null
+++ b/tests/snapshot_tests/snapshot_apps/text_area_alternate_screen.py
@@ -0,0 +1,24 @@
+from textual.app import App
+from textual.screen import Screen
+from textual.widgets import TextArea
+
+TEXT = """\
+foo
+bar
+baz
+"""
+
+
+class AltScreen(Screen[None]):
+    def compose(self):
+        yield TextArea(TEXT)
+
+
+class TABug(App[None]):
+    def on_mount(self):
+        self.push_screen(AltScreen())
+
+
+if __name__ == "__main__":
+    app = TABug()
+    app.run()
diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py
--- a/tests/snapshot_tests/test_snapshots.py
+++ b/tests/snapshot_tests/test_snapshots.py
@@ -945,6 +945,11 @@ def setup_theme(pilot):
     )
 
 
+def test_text_area_alternate_screen(snap_compare):
+    assert snap_compare(
+        SNAPSHOT_APPS_DIR / "text_area_alternate_screen.py", terminal_size=(48, 10)
+    )
+
 @pytest.mark.syntax
 def test_text_area_wrapping_and_folding(snap_compare):
     assert snap_compare(