-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadmode.py
More file actions
55 lines (38 loc) · 1.78 KB
/
Copy pathreadmode.py
File metadata and controls
55 lines (38 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from __future__ import annotations
"""Utilities and controller for managing application reading mode."""
from typing import Optional
from PyQt6.QtCore import QObject, pyqtSignal
from PyQt6.QtWidgets import QApplication, QPlainTextEdit
from styles import build_app_styles
class ReadingModeController(QObject):
"""Manage reading mode state and emit updates to interested listeners."""
modeChanged = pyqtSignal(bool)
def __init__(self, parent: Optional[QObject] = None, *, enabled: bool = False) -> None:
super().__init__(parent)
self._enabled: bool = bool(enabled)
def is_enabled(self) -> bool:
"""Return whether reading mode is currently active."""
return self._enabled
def set_enabled(self, enabled: bool) -> None:
"""Enable or disable reading mode."""
state = bool(enabled)
if self._enabled == state:
return
self._enabled = state
self.modeChanged.emit(self._enabled)
def apply_app_style(self) -> None:
"""Update global application palette for the current reading state."""
update_application_style(self._enabled)
def update_application_style(enabled: bool) -> None:
"""Apply the appropriate stylesheet for the reading mode state."""
app = QApplication.instance()
if app is None:
return
app.setStyleSheet(build_app_styles(enabled))
def configure_plain_text_edit(edit: QPlainTextEdit, *, reading_mode: bool) -> None:
"""Configure basic editor flags for the requested reading state."""
edit.setReadOnly(reading_mode)
edit.setCursorWidth(0 if reading_mode else 1)
def display_text_for_mode(text: str, *, reading_mode: bool) -> str:
"""Return text adapted for reading mode rendering."""
return text.replace("|", "") if reading_mode else text