diff --git a/src/toad/client/bus_submission.py b/src/toad/client/bus_submission.py index 4b6691a1c..0aa4111fa 100644 --- a/src/toad/client/bus_submission.py +++ b/src/toad/client/bus_submission.py @@ -22,6 +22,7 @@ RouteChangedError, ) from agent_comms.messaging.delivery_failure import delivery_failure +from agent_comms.messaging.message_files import FileUpload from agent_comms.messaging.message_reference import MessageReference from toad.client.application import ClientApplication @@ -29,6 +30,7 @@ from toad.client.input_decision import InputAccepted, InputDecision, InputRefused from toad.client.line_blocks import MutedRole, Span from toad.client.notification_status import DetailLines +from toad.client.prompt_composer import PromptComposer from toad.client.submission import ( BusDestination, MessageDestination, @@ -57,13 +59,24 @@ def refusal_notice(description: str) -> str: def unknown_notice(description: str) -> str: return f"Send outcome unknown: {description}. Check this conversation before resending." + @property @abstractmethod - def delivery_refused(self, description: str, body: str) -> None: - """Nothing left this machine, or Core refused it: the draft comes back with why.""" + def submission_composer(self) -> PromptComposer: + """The composer a returned draft goes back into.""" @abstractmethod - def delivery_unknown(self, description: str, body: str) -> None: + def show_send_failure(self, text: str) -> None: + """Say why a send came back, where this view says what went wrong.""" + + def delivery_refused(self, description: str, draft: SubmittedText) -> None: + """Nothing left this machine, or Core refused it: the draft comes back with why.""" + self.submission_composer.restore(draft) + self.show_send_failure(self.refusal_notice(description)) + + def delivery_unknown(self, description: str, draft: SubmittedText) -> None: """The request may have reached Core; the draft comes back with a warning.""" + self.submission_composer.restore(draft) + self.show_send_failure(self.unknown_notice(description)) @abstractmethod async def refresh_composer(self, read_only: bool) -> None: @@ -88,9 +101,10 @@ def submissions_changed(self) -> None: @dataclass(frozen=True) class TypedText(SubmittedText): - """Text typed into a message composer, exactly as typed.""" + """Text typed into a message composer, exactly as typed, and the files attached to it.""" source_text: str + files: tuple[FileUpload, ...] @dataclass(frozen=True) @@ -121,7 +135,7 @@ def __init__(self, owner: BusSubmissions, submission: SubmittedText, refusal: In self.owner, self.submission, self.refusal = owner, submission, refusal async def prepare(self): - self.owner.view.delivery_refused(self.refusal.message, self.submission.source_text) + self.owner.view.delivery_refused(self.refusal.message, self.submission) class SubmissionExecution(SubmissionStart): @@ -256,7 +270,7 @@ def delivering(self) -> tuple[str, ...]: def retire_unsent(self) -> None: """A message to a view that changed before it left comes back as the draft.""" - self.owner.view.delivery_refused("Message route changed; reopen this view", self.submission.source_text) + self.owner.view.delivery_refused("Message route changed; reopen this view", self.submission) self.owner.finish(self) def settle(self) -> None: @@ -275,7 +289,7 @@ async def send(self): async def deliver(self) -> DeliveryOutcome: """Send after the earlier messages; a refusal is answered here.""" - view, body, destination = self.owner.view, self.submission.source_text, self.destination + view, draft, destination = self.owner.view, self.submission, self.destination access, preparation = view.app.coordination_access, view.app.preparation try: await self.after.wait() @@ -283,35 +297,36 @@ async def deliver(self) -> DeliveryOutcome: read_only = await preparation.run_thread(destination.conversation.read_only, observed.core) await view.refresh_composer(read_only) if read_only: - view.delivery_refused(MessageDestination.read_only_reason, body) + view.delivery_refused(MessageDestination.read_only_reason, draft) return RefusedMessage() route_current = await preparation.run_thread(lambda: access.current(destination.root)) if not route_current or not self.current: raise HumanSendRefusedError(RelationViolationError("Message route changed; reopen this view")) except asyncio.CancelledError: - view.delivery_refused("Cancelled before sending", body) + view.delivery_refused("Cancelled before sending", draft) raise except (HumanSendError, RouteChangedError) as error: - delivery_failure(error).present(view, body) + view.delivery_refused(delivery_failure(error).description, draft) return RefusedMessage() except Exception: # Nothing left this machine: the draft comes back, and the failure stays loud. - view.delivery_refused("the send failed before it left this machine", body) + view.delivery_refused("the send failed before it left this machine", draft) raise try: - return await self.commit(observed, body) + return await self.commit(observed, draft) except (HumanSendError, RouteChangedError) as error: - delivery_failure(error).present(view, body) + view.delivery_refused(delivery_failure(error).description, draft) return RefusedMessage() - async def commit(self, observed: Observing, body: str) -> DeliveryOutcome: + async def commit(self, observed: Observing, draft: SubmittedText) -> DeliveryOutcome: view = self.owner.view task = asyncio.create_task(asyncio.to_thread( - observed.send_user_message, self.destination.conversation.send_target(), body)) + observed.send_user_message, self.destination.conversation.send_target(), draft.source_text, + draft.files)) try: return SentMessage(await asyncio.shield(task)) except asyncio.CancelledError: - view.delivery_unknown("the send was cancelled while it may still be writing", body) + view.delivery_unknown("the send was cancelled while it may still be writing", draft) def _surface_late_exc(done: asyncio.Future) -> None: if done.cancelled(): return @@ -332,7 +347,7 @@ def _surface_late_exc(done: asyncio.Future) -> None: raise except Exception: # The request left: Core may have published it. Raise for the log, never hold compose. - view.delivery_unknown("its reply could not be read", body) + view.delivery_unknown("its reply could not be read", draft) raise diff --git a/src/toad/client/connection.py b/src/toad/client/connection.py index b4b56e7ab..7be70a00d 100644 --- a/src/toad/client/connection.py +++ b/src/toad/client/connection.py @@ -22,6 +22,7 @@ from typing import ClassVar, TypeVar from weakref import WeakKeyDictionary, WeakSet +from agent_comms.messaging.message_files import FileUpload from agent_comms.errors import RouteChangedError from agent_comms.execution.child_process import ( HelperStart, @@ -732,10 +733,12 @@ def session_thread(self, project: Path, preferred: str) -> ProjectThreadSelectio """The thread an unbound session at ``project`` names; Core's host resolves the path.""" return self.core.call(CommsSessionThreadTool(project=str(project), preferred=preferred)) - def send_user_message(self, target: NamedTarget, body: str) -> MessageReference: - """The owner's input, published by Core under the owner's identity, in process or - across the connector; the reference is what the view waits to draw.""" - return self.core.call(CommsUserSendTool(target=target.name, body=body)).message.source + def send_user_message(self, target: NamedTarget, body: str, + files: tuple[FileUpload, ...]) -> MessageReference: + """The owner's input and the files attached to it, published by Core under the + owner's identity, in process or across the connector; the reference is what the view + waits to draw.""" + return self.core.call(CommsUserSendTool(target=target.name, body=body, files=files)).message.source def mark_thread_read(self, identity, project: Path, through: TranscriptCursor) -> None: """Acknowledge, as the owner, what this UI displayed of one thread: Core applies it, diff --git a/src/toad/client/fragments.py b/src/toad/client/fragments.py index 095d07ca0..abb487a6b 100644 --- a/src/toad/client/fragments.py +++ b/src/toad/client/fragments.py @@ -42,6 +42,7 @@ ShownFragment, UncheckedNotifications, ) +from toad.client.message_file_links import MessageFileLink, file_chips from toad.commit_claims import CommitClaim from toad.core.projection import MroProjection from toad.message_rows import MarkdownMessageStyle, MessageRow, WireMessageStyle @@ -104,6 +105,10 @@ def status_blocks(self, shown: MessageShown, link: LineLink) -> tuple[LineBlock, @abstractmethod def outgoing_sources(self, publication: PublishedSentMessage): ... + @abstractmethod + def file_blocks(self, links: tuple[MessageFileLink, ...]) -> tuple[LineBlock, ...]: + """The chips of the message's files; only the part carrying its status draws them.""" + def draw_divider(self, consumer, divider: Divider) -> None: """A message's first part draws its header; later parts draw none.""" consumer.blocks.append(divider) @@ -155,6 +160,9 @@ def status_blocks(self, shown: MessageShown, link: LineLink): def outgoing_sources(self, publication: PublishedSentMessage): return (publication.reference,) + def file_blocks(self, links: tuple[MessageFileLink, ...]) -> tuple[LineBlock, ...]: + return file_chips(links) + class InnerPart(PartPosition): __slots__ = () @@ -169,6 +177,9 @@ def status_blocks(self, shown: MessageShown, link: LineLink): def outgoing_sources(self, publication: PublishedSentMessage): return () + def file_blocks(self, links: tuple[MessageFileLink, ...]) -> tuple[LineBlock, ...]: + return () + class OnlyPart(LastPart): """A message in one part: it draws the header and carries the status.""" @@ -374,7 +385,32 @@ def blocks(self, shown: Shown) -> tuple[LineBlock, ...]: @dataclass(kw_only=True) -class BusMessageHistoryFragment(HistoryFragment): +class MessageLines(HistoryFragment): + """A bus message drawn as lines: what leads it, its text, the chips of the files it + carries, then what trails it. `files` is what its page's read answered for it.""" + + files: tuple[MessageFileLink, ...] = () + + @property + def leading(self) -> tuple[LineBlock, ...]: + """Blocks above the message's own text.""" + return () + + @abstractmethod + def text(self, shown) -> tuple[LineBlock, ...]: + """The message's own text.""" + + def trailing(self, shown) -> tuple[LineBlock, ...]: + """Blocks below the message's own text and files.""" + return () + + def blocks(self, shown): + return (*self.leading, *self.text(shown), *self.part_position.file_blocks(self.files), + *self.trailing(shown)) + + +@dataclass(kw_only=True) +class BusMessageHistoryFragment(MessageLines): """One bus-message builder: header, styled body and Core notification feedback.""" shown: MessageShown = field(default_factory=MessageShown) @@ -427,12 +463,9 @@ def text(self, shown): def trailing(self, shown): return self.part_position.status_blocks(shown, self.details_link) - def blocks(self, shown): - return (*self.leading, *self.text(shown), *self.trailing(shown)) - @dataclass(kw_only=True) -class EvidencedFragment(DeclaredViews, HistoryFragment, ABC): +class EvidencedFragment(DeclaredViews, MessageLines, ABC): """One wire message drawn as lines. `evidence` is Core's page whose display evidence can mark this message @@ -465,21 +498,6 @@ def timestamp(self) -> float: def clipboard_text(self) -> str: return self.message.body - @property - def leading(self) -> tuple[LineBlock, ...]: - """Blocks above the message's own text.""" - return () - - def trailing(self, shown: MessageShown) -> tuple[LineBlock, ...]: - """Blocks below the message's own text.""" - return () - - @abstractmethod - def text(self, shown: MessageShown) -> tuple[LineBlock, ...]: - """The message's own text.""" - - def blocks(self, shown): - return (*self.leading, *self.text(shown), *self.trailing(shown)) class MembershipText(EvidencedFragment): diff --git a/src/toad/client/history.py b/src/toad/client/history.py index e1ee7c090..c69b146af 100644 --- a/src/toad/client/history.py +++ b/src/toad/client/history.py @@ -42,6 +42,7 @@ from toad.client.fragments import EvidencedFragment, FragmentKey, MessageShown from toad.client.history_preparation import HistorySourceSnapshot, PageRequest from toad.client.history_reads import NothingToRead, PaintedRead, ReadMarking, ReadWork +from toad.client.message_file_links import PageFiles from toad.client.history_state import ( LatestReplacementViewportRequest, ReplacementViewportRequest, @@ -62,7 +63,7 @@ SubmissionStart, SubmittedText, ) -from toad.message_rows import WireMessageStyle +from toad.message_rows import MessageRow, WireMessageStyle from toad.submission_queue import BusQueueSource from toad.target_commands import TargetContext @@ -429,11 +430,13 @@ def read(self, request: PageRequest[WireCut], through: AfterSequence) -> Prepare # "Checking": the answer that follows then changes nothing it has to redraw. scope = ReadNotifications(str(self.root), LiveNotificationRead(()), display=page.selection.notification_identity) + files = PageFiles.read(str(self.root), self.core, ( + (message.reference, MessageRow(message, self.me).file_reader) for message in page.messages)) fragments = tuple( self.fragments.of(message, shown=MessageShown( style=self.style, status=self.access.notification_status(scope, message.role.notification_references(message))), - evidence=evidence, reader=self.me, page=page) + evidence=evidence, reader=self.me, page=page, files=files.of(message.reference)) for message, evidence in page.message_evidence() ) wire = WirePage( diff --git a/src/toad/client/message_file_links.py b/src/toad/client/message_file_links.py new file mode 100644 index 000000000..77adffa29 --- /dev/null +++ b/src/toad/client/message_file_links.py @@ -0,0 +1,129 @@ +"""The files a message carries, as a history draws them: one chip per file under the +message's text, each a link to the read that returns the file's bytes. No toolkit here, and +nothing of the composer: the transcript and wire readers import this alone. + +A page learns its messages' files from Core beside the page, one comms_message_files read +for each conversation the page holds, never one per message. The read names the +conversation and the participant reading it, so Core returns only files of messages that +reader may read. Both frontends draw the chips as the spans they already draw, and open +them through `OpensLineLinks`. +""" + +from __future__ import annotations + +import base64 +import hashlib +from collections.abc import Iterable +from dataclasses import dataclass +from pathlib import Path + +from agent_comms.messaging.message_files import MessageFile, MessageFileContent +from agent_comms.messaging.message_reference import MessageReference +from agent_comms.transport.request import CoreAccess +from agent_comms.transport.tools.sidebar import ( + CommsMessageFilesTool, + CommsMessageFileTool, +) + +from toad import paths +from toad.client.line_blocks import LineBlock, LineLink, MutedRole, Span, Spans + + +def file_size(size: int) -> str: + """A byte count as a reader reads it: B under 1024, else one decimal in KB or MB.""" + if size < 1024: + return f"{size} B" + if size < 1024 * 1024: + return f"{size / 1024:.1f} KB" + return f"{size / (1024 * 1024):.1f} MB" + + +@dataclass(frozen=True, slots=True) +class FileReader: + """Whose read a message's files are: the conversation they are read in (a #channel, or + a DM's other participant) and the participant reading it.""" + + conversation: str + participant: str + + def files_of(self, core: CoreAccess, messages: tuple[MessageReference, ...]) -> tuple[MessageFile, ...]: + return core.call(CommsMessageFilesTool( + conversation=self.conversation, participant=self.participant, messages=messages)).files + + +@dataclass(frozen=True) +class MessageFileLink(LineLink): + """One file of one message on one bus, and the reader its read names.""" + + root: str + conversation: str + participant: str + file: MessageFile + + @property + def request(self) -> CommsMessageFileTool: + return CommsMessageFileTool( + conversation=self.conversation, participant=self.participant, + message=self.file.message, position=self.file.position) + + def fetched(self, core: CoreAccess) -> Path: + """The file's bytes, read from Core and kept where this machine opens them: under + the data folder, by the file's digest, in its own name. Bytes that do not match the + digest Core listed are refused, never kept.""" + return saved_copy(core.call(self.request)) + + @property + def chip(self) -> Span: + return Span(f"[{self.file.name} · {file_size(self.file.size)}]", MutedRole, self) + + +@dataclass(frozen=True) +class PageFiles: + """The files of one page's messages, as Core answered them for the page's readers.""" + + links: tuple[tuple[MessageReference, tuple[MessageFileLink, ...]], ...] = () + + @classmethod + def read(cls, root: str, core: CoreAccess, + readers: Iterable[tuple[MessageReference, FileReader]]) -> PageFiles: + """One read per reader the page's messages name.""" + grouped: dict[FileReader, list[MessageReference]] = {} + for reference, reader in readers: + grouped.setdefault(reader, []).append(reference) + links: dict[MessageReference, list[MessageFileLink]] = {} + for reader, references in grouped.items(): + for file in reader.files_of(core, tuple(references)): + links.setdefault(file.message, []).append(MessageFileLink( + str(root), reader.conversation, reader.participant, file)) + return cls(tuple((reference, tuple(found)) for reference, found in links.items())) + + def of(self, reference: MessageReference) -> tuple[MessageFileLink, ...]: + for message, found in self.links: + if message == reference: + return found + return () + + +NO_PAGE_FILES = PageFiles() +"""A page read with no file store beside it: an attached history, or a draft not yet committed.""" + + +def file_chips(links: tuple[MessageFileLink, ...]) -> tuple[LineBlock, ...]: + """Under a message's text, one line per file it carries, each its chip.""" + return tuple(Spans((link.chip,)) for link in links) + + +def saved_copy(content: MessageFileContent) -> Path: + data = base64.b64decode(content.data, validate=True) + file = content.file + # The record's name already names one file: MessageFile refuses any other as it decodes. + if hashlib.sha256(data).hexdigest() != file.sha256: + raise ValueError(f"{file.name} arrived with bytes that do not match its SHA-256.") + folder = paths.get_data() / "message-files" / file.sha256[:16] + folder.mkdir(mode=0o700, parents=True, exist_ok=True) + path = folder / file.name + if not path.is_file() or path.read_bytes() != data: + staged = path.with_name(f".{file.name}.part") + staged.write_bytes(data) + staged.replace(path) + return path diff --git a/src/toad/client/message_files.py b/src/toad/client/message_files.py new file mode 100644 index 000000000..50f57cbfd --- /dev/null +++ b/src/toad/client/message_files.py @@ -0,0 +1,169 @@ +"""The files a message carries, in Toad: what a message composer attaches and sends, and what +a history draws under each message. No toolkit here. + +Attaching: the attach action opens the picker the `ui.input.file_picker` setting names. Each +frontend answers only its pickers and how it draws the attached files: Qt's composer opens a +file dialog (on Android, the system's own file picker) or its path search, and Textual's +prompt opens its terminal pickers. A picked file becomes the upload Core checks again. The +files ride the submission with the text, and a send that does not land returns them to the +composer with its draft. + +A history draws the files under each message through toad.client.message_file_links. +""" + +from __future__ import annotations + +import asyncio +import mimetypes +from abc import abstractmethod +from pathlib import Path + +from agent_comms.messaging.message_files import ( + MAX_MESSAGE_FILE_BYTES, + FileUpload, +) +from agent_comms.nominal.declared_choice import DeclaredChoice, TitledChoice +from formbridge.operations import ExclusiveKind, OperationKind + +from toad.client.input_decision import InputAccepted, InputDecision +from toad.client.message_file_links import file_size +from toad.client.prompt_composer import PromptComposer + + +class FilePicker(DeclaredChoice, affix="FilePicker"): + """Which picker the attach action opens; each member names the composer's own.""" + + @classmethod + @abstractmethod + def open(cls, composer: MessagePromptComposer) -> None: ... + + +class WindowFilePicker(TitledChoice, FilePicker): + """The platform's file dialog. A phone shows its own file picker, which reaches the + phone's storage and its cloud drives.""" + + title = "File dialog" + + @classmethod + def open(cls, composer: MessagePromptComposer) -> None: + composer.pick_in_window() + + +class PathSearchFilePicker(TitledChoice, FilePicker): + """The path search @ completion uses, over the project's files.""" + + title = "Project path search" + + @classmethod + def open(cls, composer: MessagePromptComposer) -> None: + composer.pick_by_path_search() + + +class FileSearch(OperationKind, ExclusiveKind): + """A path search picker listing or ranking the project's paths; a newer one replaces it.""" + + +class AttachedFileChip: + """One attached file as its composer draws it: its caption, and taking it off. Qt's and + Textual's chip widgets inherit it; a plain mixin, as the toolkits' metaclasses need.""" + + composer: MessagePromptComposer + position: int + + def show_file(self, composer: MessagePromptComposer, position: int) -> None: + self.composer, self.position = composer, position + + @property + def caption(self) -> str: + """Named apart from the toolkits' own: Textual's Button renders `label`.""" + return self.composer.chip_label(self.composer.attached_files[self.position]) + + def take_off(self) -> None: + """Named apart from the toolkits' own: Textual's `Widget.remove` leaves the DOM.""" + self.composer.detach_file(self.position) + + +class MessagePromptComposer(PromptComposer): + """A message composer: always the agent glyph, every input accepted, and the files it + sends with the text.""" + + attached_files: tuple[FileUpload, ...] = () + + @property + def input_decision(self) -> InputDecision: + """A message is accepted whether or not an agent is attached: its submission owner + refuses a send it cannot make. An agent prompt asks its owner instead.""" + return InputAccepted() + + def prompt_symbol(self, text: str, shell_mode: bool) -> str: + return self.settings.ui.input.agent_symbol + + def attach_file(self) -> None: + """The attach action: the picker this composer's settings name opens.""" + self.settings.ui.input.file_picker.open(self) + + @abstractmethod + def pick_in_window(self) -> None: + """Open this frontend's file dialog; what it returns reaches `file_picked`.""" + + @abstractmethod + def pick_by_path_search(self) -> None: + """Open this frontend's path search; what it returns reaches `file_picked`.""" + + @abstractmethod + def show_attached(self) -> None: + """Draw `attached_files` beside the editor, each with a way to take it off.""" + + @property + def read_limit(self) -> int: + """The most bytes a picker reads of one file: one past what still fits, so an + oversized file is refused below without being read whole.""" + return MAX_MESSAGE_FILE_BYTES - sum(upload.size for upload in self.attached_files) + 1 + + async def attach_path(self, path: Path) -> None: + """A picker chose a file on this machine: read at most `read_limit` of it and attach it.""" + limit = self.read_limit + + def read() -> bytes: + with path.open("rb") as source: + return source.read(limit) + + try: + content = await asyncio.to_thread(read) + except OSError as error: + self.show_failure(f"{path.name} not attached: {error.strerror or error}") + return + self.file_picked(path.name, content) + + def file_picked(self, name: str, content: bytes, mime_type: str = "") -> None: + """A picker returned a file: attach it, or say why it cannot go with this message.""" + try: + upload = FileUpload.of( + name, mime_type or mimetypes.guess_type(name)[0] or "application/octet-stream", content) + FileUpload.require_bounded((*self.attached_files, upload)) + except ValueError as error: + self.show_failure(f"{name} not attached: {error}") + return + self.attached_files = (*self.attached_files, upload) + self.show_attached() + + def detach_file(self, position: int) -> None: + files = self.attached_files + self.attached_files = files[:position] + files[position + 1:] + self.show_attached() + + def take_files(self) -> tuple[FileUpload, ...]: + """The files the message being sent carries; the composer holds none after.""" + files, self.attached_files = self.attached_files, () + self.show_attached() + return files + + def restore(self, draft) -> None: + """A send that did not land returns its files with its text.""" + super().restore(draft) + self.attached_files = (*self.attached_files, *draft.files) + self.show_attached() + + @staticmethod + def chip_label(upload: FileUpload) -> str: + return f"{upload.name} · {file_size(upload.size)}" diff --git a/src/toad/client/project_paths.py b/src/toad/client/project_paths.py new file mode 100644 index 000000000..d8a0883ce --- /dev/null +++ b/src/toad/client/project_paths.py @@ -0,0 +1,51 @@ +"""A path search over a project's paths, with no toolkit: Textual's PathSearch (the @ +completion and its file picker) and Qt's QtPathSearch inherit it and draw what it answers. + +The listing is what git does not ignore under the root, each path spelled relative to it (a +directory with a trailing slash), shallow paths first, and ranked against what was typed +by `PathSearchRanking`. +""" + +from __future__ import annotations + +import asyncio +from pathlib import Path + +from toad import directory +from toad.path_filter import PathFilter +from toad.path_search_ranking import PathSearchRanking + + +class ProjectPathSearch: + """One project root's listing and its ranking. A plain mixin: Textual's widget + metaclass refuses an ABC base and Qt's shiboken metaclass clashes with one.""" + + root: Path + ranking: PathSearchRanking + path_filter: PathFilter | None = None + + def start_path_search(self, root: Path) -> None: + self.root = root + self.ranking = PathSearchRanking() + + async def scan_paths(self, *, add_directories: bool) -> list[Path]: + """Every path git does not ignore under the root, absolute.""" + root = self.root + self.path_filter = await asyncio.to_thread(PathFilter.from_git_root, root) + paths = await directory.scan(root, path_filter=self.path_filter, add_directories=add_directories) + return await asyncio.to_thread(lambda: [path.absolute() for path in paths]) + + def spell_paths(self, paths: list[Path]) -> list[str]: + """Each path relative to the root, a directory with a trailing slash, shallow first.""" + spellings = sorted( + (str(path.relative_to(self.root)) + ("/" if path.is_dir() else "") for path in paths), + key=str.lower) + spellings.sort(key=lambda spelling: spelling.count("/")) + return spellings + + async def rank_paths(self, spellings: list[str]) -> None: + """Rank these spellings from now on.""" + await self.ranking.update_paths(spellings) + + async def ranked(self, text: str) -> list[str]: + return [match.path for match in await self.ranking.search(text)] diff --git a/src/toad/client/prompt_composer.py b/src/toad/client/prompt_composer.py index 81cc0727b..39b3ab5f4 100644 --- a/src/toad/client/prompt_composer.py +++ b/src/toad/client/prompt_composer.py @@ -11,9 +11,11 @@ from abc import abstractmethod from typing import ClassVar +from agent_comms.messaging.message_files import FileUpload from metaclass_registry import AbstractHooksMeta +from ui_theme.scheme import PromptToneColour, ThemeColour -from toad.client.input_decision import InputAccepted, InputDecision +from toad.client.input_decision import InputDecision from toad.setting_declarations import EachOpenWidget @@ -46,6 +48,8 @@ class PromptComposer(metaclass=AbstractHooksMeta): queue_summary_lines: ClassVar[int] = 3 """The queue summary's most lines; a longer one is cut there, so its wrapping never pushes the history further.""" + # The surface the composer's area is painted on, a tier off the transcript above it. + area_tone: ClassVar[type[ThemeColour]] = PromptToneColour @property @abstractmethod @@ -115,6 +119,22 @@ def prompt_symbol(self, text: str, shell_mode: bool) -> str: return symbols.shell_symbol return symbols.multiline_symbol if "\n" in text or "```" in text else symbols.agent_symbol + @abstractmethod + def show_failure(self, text: str) -> None: + """Say, under the input, why something typed or attached did not go.""" + + @abstractmethod + def restore_draft(self, text: str) -> None: + """Join returned text to what is typed (`restored_suffix`).""" + + def restore(self, draft) -> None: + """A send that did not land comes back as the draft.""" + self.restore_draft(draft.source_text) + + def take_files(self) -> tuple[FileUpload, ...]: + """The files the input being sent carries: an agent prompt attaches none.""" + return () + @staticmethod def restored_suffix(current: str, text: str) -> str: """What a send that did not land appends to the draft: nothing when the draft @@ -124,19 +144,6 @@ def restored_suffix(current: str, text: str) -> str: return ("\n\n" if current else "") + text -class MessagePromptComposer(PromptComposer): - """A message composer: always the agent glyph, and every input is accepted.""" - - @property - def input_decision(self) -> InputDecision: - """A message is accepted whether or not an agent is attached: its submission owner - refuses a send it cannot make. An agent prompt asks its owner instead.""" - return InputAccepted() - - def prompt_symbol(self, text: str, shell_mode: bool) -> str: - return self.settings.ui.input.agent_symbol - - class ComposerStatus: """The status the info bar shows: why compose is held, else the thread's own status from its owner (Core's configuration answer for a CLI thread, the context measurement diff --git a/src/toad/client/submission.py b/src/toad/client/submission.py index cac08a99c..706a472d9 100644 --- a/src/toad/client/submission.py +++ b/src/toad/client/submission.py @@ -9,6 +9,7 @@ from typing import ClassVar from agent_comms.messaging.channel_targets import NamedTarget +from agent_comms.messaging.message_files import FileUpload from agent_comms.messaging.message_reference import MessageReference from agent_comms.transport.request import CoreAccess @@ -23,9 +24,11 @@ async def prepare(self): ... class SubmittedText(ABC): - """Text the owner submitted, exactly as typed; its request sends it stripped.""" + """Text the owner submitted, exactly as typed, and the files attached to it; its request + sends the text stripped.""" source_text: str + files: tuple[FileUpload, ...] @property def text(self) -> str: diff --git a/src/toad/client/transcript_fragments.py b/src/toad/client/transcript_fragments.py index c351b8410..651e2035b 100644 --- a/src/toad/client/transcript_fragments.py +++ b/src/toad/client/transcript_fragments.py @@ -18,6 +18,7 @@ from functools import cached_property, partial from typing import ClassVar +from agent_comms.messaging.message_reference import MessageReference from agent_comms.messaging.sent_message import PublishedSentMessage from agent_comms.nominal.field_codec import ToolCallId from agent_comms.storage.recorded_time import EventTime @@ -43,6 +44,7 @@ PartPosition, Shown, ) +from toad.client.message_file_links import NO_PAGE_FILES, FileReader, PageFiles from toad.message_rows import MessageRow from toad.client.line_blocks import ( AgentRole, @@ -364,12 +366,13 @@ class SentSlot(BuiltSlot): report: SentTranscript classes: FragmentClasses + files: PageFiles def sent_reports(self) -> tuple[SentTranscript, ...]: return (self.report,) def fragments(self, resolved: ResolvedSent) -> tuple[TranscriptFragment, ...]: - builder = TranscriptFragmentConsumer(self.classes) + builder = TranscriptFragmentConsumer(self.classes, self.files) builder.project_value(resolved[self.report]) return builder.unresolved() @@ -426,13 +429,35 @@ def unresolved(self) -> tuple[TranscriptFragment, ...]: return self.fragments({}) +class BusMessageRow(MroProjection[tuple[MessageRow, ...]]): + """The bus message a transcript event draws, as its row: a received message read by its + recipient, a sent one once resolved read by the recipient it was published to. Every + other event, a sent report not yet resolved included, draws none.""" + + @handles(IncomingTranscript) + def incoming(self, event: IncomingTranscript) -> tuple[MessageRow, ...]: + return (MessageRow(event.message, event.reader),) + + @handles(PublishedSentMessage) + def published(self, message: PublishedSentMessage) -> tuple[MessageRow, ...]: + return (MessageRow(message.original, message.scope.recipient.name),) + + @handles(TranscriptEvent) + def other(self, event: TranscriptEvent) -> tuple[MessageRow, ...]: + return () + + +BUS_MESSAGE_ROW = BusMessageRow() + + class TranscriptFragmentConsumer(MroProjection[None]): """Build a frontend's fragment family from a stream of transcript events. The frontend picks which subclasses to construct by passing its `FragmentClasses`; Textual passes `TEXTUAL_FRAGMENT_CLASSES` from `widgets/transcript_fragments.py`, Qt passes its own.""" - def __init__(self, classes: FragmentClasses) -> None: + def __init__(self, classes: FragmentClasses, files: PageFiles) -> None: self.classes = classes + self.files = files self.slots: list[FragmentSlot] = [] self.tools: dict[str, ToolSlot] = {} self.budget = RenderBudget() @@ -448,20 +473,22 @@ def context(self, event: ContextTranscript) -> None: @handles(IncomingTranscript) def incoming(self, event: IncomingTranscript) -> None: - self.text(event, self.classes.bus_message, row=MessageRow(event.message, event.reader)) + (row,) = BUS_MESSAGE_ROW.project_value(event) + self.text(event, self.classes.bus_message, row=row, files=self.files.of(row.message.reference)) @handles(SentTranscript) def sent(self, event: SentTranscript) -> None: - self.slots.append(SentSlot(self.classes.transcript((event,)), event, self.classes)) + self.slots.append(SentSlot(self.classes.transcript((event,)), event, self.classes, self.files)) @handles(PublishedSentMessage) def published(self, message: PublishedSentMessage) -> None: - original = message.original + (row,) = BUS_MESSAGE_ROW.project_value(message) self.text( message.message, self.classes.sent_message, publication=message, - row=MessageRow(original, message.scope.recipient.name), + row=row, + files=self.files.of(row.message.reference), ) @handles(UserTranscript, MarkdownTranscript) @@ -492,8 +519,9 @@ def read( *, resolve_sent: Callable[[Iterable[SentTranscript]], ResolvedSent], classes: FragmentClasses, + files: PageFiles, ) -> FragmentPlan: - consumer = TranscriptFragmentConsumer(classes) + consumer = TranscriptFragmentConsumer(classes, files) for event in events: consumer.project_value(event) resolved = resolve_sent(report for slot in consumer.slots for report in slot.sent_reports()) @@ -504,7 +532,7 @@ def for_draft(cls, draft, identity, classes: FragmentClasses) -> FragmentPlan: """Core's in-progress CLI draft, read as committed events are, its sent reports resolved against the read identity's wire frontier.""" return cls.read(draft.events, resolve_sent=identity.wire_frontier.resolve_sent_messages, - classes=classes) + classes=classes, files=NO_PAGE_FILES) def fragments(self) -> tuple[TranscriptFragment, ...]: return tuple(fragment for slot in self.slots for fragment in slot.fragments(self.resolved)) @@ -515,5 +543,20 @@ def transcript_fragments( *, resolve_sent: Callable[[Iterable[SentTranscript]], ResolvedSent], classes: FragmentClasses, + files: PageFiles, ) -> tuple[TranscriptFragment, ...]: - return FragmentPlan.read(events, resolve_sent=resolve_sent, classes=classes).fragments() + return FragmentPlan.read(events, resolve_sent=resolve_sent, classes=classes, files=files).fragments() + + +def transcript_file_readers( + events: tuple[TranscriptEvent, ...], + *, + resolve_sent: Callable[[Iterable[SentTranscript]], ResolvedSent], + classes: FragmentClasses, +) -> tuple[tuple[MessageReference, FileReader], ...]: + """Every bus message these events draw once their sent reports resolve, with the read its + files are listed under: what one page's file read asks before the page is drawn.""" + plan = FragmentPlan.read(events, resolve_sent=resolve_sent, classes=classes, files=NO_PAGE_FILES) + return tuple((row.message.reference, row.file_reader) + for drawn in (*events, *plan.resolved.values()) + for row in BUS_MESSAGE_ROW.project_value(drawn)) diff --git a/src/toad/client/transcript_reader.py b/src/toad/client/transcript_reader.py index 0ebffd260..87d8646cf 100644 --- a/src/toad/client/transcript_reader.py +++ b/src/toad/client/transcript_reader.py @@ -13,6 +13,7 @@ from __future__ import annotations import asyncio +from collections.abc import Callable, Iterable from functools import partial from agent_comms.acp_extension import TranscriptDraftUpdate, TranscriptSnapshotUpdate @@ -22,10 +23,12 @@ PageLimit, PagePosition, ) +from agent_comms.messaging.message_reference import MessageReference from agent_comms.registry.thread_presentation import ThreadPresentation from agent_comms.transcript.transcripts import ( LivePageSource, PageSource, + TranscriptPage, ) from agent_comms.transport.tools.sidebar import CommsThreadPresentationTool from agent_comms.ui_model.observation import ( @@ -36,6 +39,7 @@ UnreadableHistory, ) +from toad.client.message_file_links import FileReader, PageFiles from toad.client.transcript_requests import TranscriptRequests @@ -66,6 +70,16 @@ async def thread_presentation( partial(core.call, CommsThreadPresentationTool(thread=thread)), ) + async def page_files( + self, page: TranscriptPage, + readers: Callable[[], Iterable[tuple[MessageReference, FileReader]]], + ) -> PageFiles: + """One read of the files the page's bus messages carry, on the bus the page was read + from; the messages are found and read off the UI thread, like the page itself.""" + root = page.snapshot.root + core = await self.core(root) + return await self.access.preparation.run_thread(lambda: PageFiles.read(root, core, readers())) + async def publication( self, update: TranscriptSnapshotUpdate ) -> TranscriptSnapshotUpdate: diff --git a/src/toad/conversation_submission.py b/src/toad/conversation_submission.py index 768f8aa15..0d8ddfdb7 100644 --- a/src/toad/conversation_submission.py +++ b/src/toad/conversation_submission.py @@ -15,6 +15,7 @@ QueueScope, SteerPromptRequest, ) +from agent_comms.messaging.message_files import FileUpload from agent_comms.nominal.declared_family import DeclaredFamily from toad.acp.agent import Agent @@ -34,6 +35,7 @@ @dataclass(frozen=True) class InputSubmission(SubmittedText, DeclaredFamily, affix="InputSubmission"): source_text: str + files: tuple[FileUpload, ...] priority: ClassVar[int] = 0 @classmethod @@ -50,7 +52,7 @@ def from_event(cls, event: UserInputSubmitted, owner: ConversationSubmissions): for kind in sorted(cls.members_with(InputSubmission), key=lambda kind: kind.priority, reverse=True) if kind.matches(event, owner) ) - return kind(event.body) + return kind(event.body, event.files) class NoInputSubmission(InputSubmission): diff --git a/src/toad/core/input_events.py b/src/toad/core/input_events.py index f8838bb19..8fbad3a33 100644 --- a/src/toad/core/input_events.py +++ b/src/toad/core/input_events.py @@ -3,6 +3,8 @@ from dataclasses import dataclass from typing import Literal +from agent_comms.messaging.message_files import FileUpload + from toad.core.events import CoreEvent from toad.goal_controls import GoalInteraction from toad.input_history import InputHistory @@ -28,6 +30,8 @@ class UserInputSubmitted(CoreEvent): shell: bool = False auto_complete: bool = False immediate: bool = False + files: tuple[FileUpload, ...] = () + """What the message composer attached; an agent prompt attaches none.""" @dataclass(frozen=True) diff --git a/src/toad/delivery_failure_view.py b/src/toad/delivery_failure_view.py index e85134a29..25abe2ba4 100644 --- a/src/toad/delivery_failure_view.py +++ b/src/toad/delivery_failure_view.py @@ -55,15 +55,15 @@ async def refresh_composer(self, read_only: bool) -> None: if not area.disabled and self.screen.focused is None: self.screen.set_focus(area) - def delivery_unknown(self, description: str, body: str) -> None: - """The request may have reached Core. The owner sees whether it landed in this view, - so the draft comes back instead of waiting on an answer it cannot get.""" - self.prompt.restore_draft(body) - self.feedback.flash(self.unknown_notice(description), style="error") - - def delivery_refused(self, description: str, body: str) -> None: - self.prompt.restore_draft(body) - self.feedback.flash(self.refusal_notice(description), style="error") + @property + def submission_composer(self): + """The prompt a returned draft goes back into. A send whose outcome is unknown comes + back too: the owner sees whether it landed in this view, rather than waiting on an + answer it cannot get.""" + return self.prompt + + def show_send_failure(self, text: str) -> None: + self.feedback.flash(text, style="error") def record_input(self, text: str) -> None: InputHistoryRecord.run_on(self, partial(self.input_histories.prompt.record, text)) diff --git a/src/toad/link_opening.py b/src/toad/link_opening.py index 427841681..efc4b0466 100644 --- a/src/toad/link_opening.py +++ b/src/toad/link_opening.py @@ -11,6 +11,9 @@ from __future__ import annotations from abc import abstractmethod +from collections.abc import Awaitable, Callable +from functools import partial +from pathlib import Path from agent_comms.messaging.display_references import ( ChannelDisplayReference, @@ -20,6 +23,7 @@ from metaclass_registry.mro_dispatch import MroDispatch, handles from toad.client.line_blocks import LineLink, OpenHistoricalTarget, OpenReference, OpenTarget +from toad.client.message_file_links import MessageFileLink from toad.navigation_target import NavigationTarget from toad.project_link import ProjectLink @@ -55,6 +59,22 @@ def open_recorded_reference(self, reference) -> None: def open_project_link(self, link: ProjectLink) -> None: self.preview_link(link) + @handles(MessageFileLink) + def open_message_file(self, link: MessageFileLink) -> None: + self.run_link(partial(self.fetch_message_file, link)) + + async def fetch_message_file(self, link: MessageFileLink) -> None: + """Read the file from the bus its message is on, keep it, and open it; a refusal or a + lost connection is told to the reader.""" + access = self.app.coordination_access + try: + path = await access.preparation.run_thread( + lambda: link.fetched(access.observing(link.root).core)) + except (ValueError, OSError) as error: + self.warn_link(f"{link.file.name} could not be read: {error}") + return + self.show_file(path) + def open_link(self, href: str) -> None: """A drawn link hands its href back as a string; decode it once into the link it carries.""" self.dispatch_sync(LineLink.from_href(href)) @@ -76,3 +96,11 @@ def preview_link(self, link: ProjectLink) -> None: @abstractmethod def warn_link(self, text: str) -> None: """Tell the reader why a link opened nothing.""" + + @abstractmethod + def run_link(self, work: Callable[[], Awaitable[None]]) -> None: + """Run a link's background work as this view's; its failure reaches the view's owner.""" + + @abstractmethod + def show_file(self, path: Path) -> None: + """Open a file kept on this machine the way this frontend opens files.""" diff --git a/src/toad/message_rows.py b/src/toad/message_rows.py index a25016847..5782e4275 100644 --- a/src/toad/message_rows.py +++ b/src/toad/message_rows.py @@ -28,6 +28,7 @@ from agent_comms.storage.recorded_time import EventTime, RecordedEventTime, UnrecordedEventTime from metaclass_registry.mro_dispatch import handles +from toad.client.message_file_links import FileReader from toad.core.projection import MroProjection from toad.inline_spans import inline_spans from toad.client.line_blocks import ( @@ -132,6 +133,22 @@ def recorded(self, source: HistorySource, name: str) -> LineLink: THREAD_LINK = ThreadLink() +class MessageFileReader(MroProjection[FileReader]): + """Whose read a message's files are, from where it was sent: a channel message is read in + its channel; a direct message in the DM with the participant that is not its reader.""" + + @handles(ChannelTarget) + def channel(self, target: ChannelTarget, row: MessageRow) -> FileReader: + return FileReader(target.name, row.reader) + + @handles(ThreadTarget) + def direct(self, target: ThreadTarget, row: MessageRow) -> FileReader: + return FileReader(target.name if row.outgoing else row.message.sender, row.reader) + + +MESSAGE_FILE_READER = MessageFileReader() + + @dataclass(frozen=True) class MessageRow(MroProjection[LineLink]): """One message: who sent it to whom, when, its text, and whether the reader's thread sent it.""" @@ -172,6 +189,11 @@ def thread_link(self, target: ThreadTarget) -> LineLink: def channel_link(self, target: ChannelTarget) -> LineLink: return OpenTarget(target.name) + @property + def file_reader(self) -> FileReader: + """The read this message's files are listed and fetched under.""" + return MESSAGE_FILE_READER.project_value(CommandTarget.named(self.message.target), self) + @property def sender(self) -> LineLink: """What opens the thread that sent the message.""" diff --git a/src/toad/motion.py b/src/toad/motion.py index 49a061725..d84e82d8c 100644 --- a/src/toad/motion.py +++ b/src/toad/motion.py @@ -25,6 +25,9 @@ # A row under the composer (the queue summary, the delivery controls) opens or closes its height # instead of appearing in one frame and pushing the history up. ROW_REVEAL = Transition(Amount, 0.0, 1.0, _TAB_DURATION, _TAB_FPS) +# A scrollbar drawn over the text while it moves fades away once it has been still a moment, +# rather than vanishing in one frame. +SCROLLBAR_FADE = Transition(Amount, 1.0, 0.0, 0.3, _TAB_FPS) def animations_enabled() -> bool: diff --git a/src/toad/preferences.py b/src/toad/preferences.py index 5ca130afc..cde83eba0 100644 --- a/src/toad/preferences.py +++ b/src/toad/preferences.py @@ -45,7 +45,6 @@ LoadingStyle, LeftColumnAlignment, NavigationBarPosition, - NormalScrollbar, NotificationPolicy, PulseLoading, SchemeName, @@ -56,6 +55,7 @@ TopNavigationBar, ) from toad.setting_declarations import FamilyChoices, SettingsDeclaration +from toad.client.message_files import FilePicker, WindowFilePicker from toad.client.prompt_composer import PromptSettingEffects from toad.client.view_settings import ConversationSettingsOwner, ViewSettingsOwner from toad.terminal_origin import TerminalLayout @@ -91,6 +91,11 @@ class InputSettings(SettingsDeclaration, scopes=(ConversationSettingsOwner,)): ] = "$" """Shown before a shell command.""" + file_picker: Annotated[ + type[FilePicker], FieldLabel("File picker"), FamilyChoices(FilePicker), WireName("file-picker"), + ] = WindowFilePicker + """What attaching a file to a message opens: the file dialog (on a phone, its own file picker) or the project path search.""" + delivery_preview: Annotated[ bool, FieldLabel("Show delivery preview?"), PromptSettingEffects.apply_delivery_preview, WireName("delivery-preview"), @@ -115,10 +120,10 @@ class ViewUiSettings(SettingsDeclaration, scopes=(ViewSettingsOwner,)): """Place the conversation column on the left, in the center, or on the right.""" scrollbar: Annotated[ type[Scrollbar], - FieldLabel("Scrollbar size"), + FieldLabel("Scrollbar"), FamilyChoices(Scrollbar), effects.conversation_layout, - ] = NormalScrollbar + ] = Scrollbar.for_host() @dataclass(frozen=True) diff --git a/src/toad/prompt_actions.py b/src/toad/prompt_actions.py index 5fa8b9ea5..1c59bad23 100644 --- a/src/toad/prompt_actions.py +++ b/src/toad/prompt_actions.py @@ -5,6 +5,7 @@ from toad.widget_actions import Action, EngineKey from toad.core.input_events import UserInputSubmitted +from agent_comms.messaging.message_files import FileUpload from typing import TYPE_CHECKING @@ -53,6 +54,20 @@ async def apply(self, editor: PromptTextArea) -> None: editor.prompt.shell_mode = True +class MessageAction(Action, affix="Action"): + """Keys only a message composer's editor answers: an agent prompt binds none of them.""" + + +class AttachFileAction(MessageAction): + """Attach a file to the message: the picker the file picker setting names opens.""" + + keys = {TextEntryMode: ("ctrl+t",)} + title = "Attach" + + async def apply(self, editor: PromptTextArea) -> None: + editor.prompt.attach_file() + + class SubmitInputAction(PromptAction, declared_name=None): """Submission shares eligibility, while each command owns how it sends.""" priority = True @@ -66,8 +81,8 @@ async def apply(self, editor: PromptTextArea) -> None: def coalesce(self, following: SubmitInputAction) -> SubmitInputAction: return following - def submission_event(self, text: str, shell: bool) -> UserInputSubmitted: - return UserInputSubmitted(text, shell) + def submission_event(self, text: str, shell: bool, files: tuple[FileUpload, ...]) -> UserInputSubmitted: + return UserInputSubmitted(text, shell, files=files) class SubmitAction(SubmitInputAction): @@ -90,8 +105,8 @@ def available(self, editor): def coalesce(self, following: SubmitInputAction) -> SubmitInputAction: return self - def submission_event(self, text: str, shell: bool) -> UserInputSubmitted: - return UserInputSubmitted(text, shell, immediate=True) + def submission_event(self, text: str, shell: bool, files: tuple[FileUpload, ...]) -> UserInputSubmitted: + return UserInputSubmitted(text, shell, immediate=True, files=files) class CopyOrClearInputAction(PromptAction): diff --git a/src/toad/qt/activity.py b/src/toad/qt/activity.py index f5f521f97..d4af5bc6f 100644 --- a/src/toad/qt/activity.py +++ b/src/toad/qt/activity.py @@ -4,16 +4,14 @@ from __future__ import annotations -from array import array -from bisect import bisect_left from collections.abc import Callable from time import monotonic from agent_comms.registry.thread_identity import ThreadIncarnation from agent_comms.ui_model.sidebar import SidebarModel from agent_comms.ui_model.status import ThreadStatusModel -from PySide6.QtCore import Qt, QTimer -from PySide6.QtGui import QColor, QImage, QPainter +from PySide6.QtCore import QPointF, Qt, QTimer +from PySide6.QtGui import QColor, QLinearGradient, QPainter, QPolygonF from PySide6.QtWidgets import QLabel, QVBoxLayout, QWidget from pyqt_reactive.widgets.fitted_text import ElidedLine from ui_theme.effects import ACTIVITY_SWEEP @@ -75,8 +73,19 @@ def close_activity(self) -> None: class ActivitySweep(QWidget): - """Toad's activity sweep: the shared ACTIVITY_SWEEP ribbon sampled across the strip, - each sample's two stops blended and drawn as tall as the ribbon is thick there.""" + """Toad's activity sweep: the shared ACTIVITY_SWEEP ribbon drawn across the strip as one + filled outline, its colours a gradient sampled from the ribbon and its height the + ribbon's thickness, tapered at both tails as `Ribbon.sample` says. + + The effect's shape, colours and timing are shared with Textual; how often it is drawn is + each toolkit's own. A terminal draws whole cells, so the effect's 20 frames a second already + move the band a cell at a time; Qt draws pixels, and at 20 a second the band jumped about + 6 px a frame on the phone and looked laggy (Tristan, 2026-11-18). Qt draws at the screen's + refresh rate, and each frame is 33 ribbon samples and one antialiased fill in Qt, where it + was a Python sample and a pixel write per column.""" + + samples = 32 + """Points along the ribbon: each gives a gradient stop and the outline's half-height.""" def __init__(self, renderer: QtLineRenderer) -> None: super().__init__() @@ -86,13 +95,14 @@ def __init__(self, renderer: QtLineRenderer) -> None: # One text row, always reserved, as Textual's throbber row: only its paint changes. self.setFixedHeight(self.fontMetrics().lineSpacing()) self.timer = QTimer(self) - self.timer.setInterval(int(1000 / ACTIVITY_SWEEP.frames_per_second)) self.timer.timeout.connect(self.update) def sweep(self, working: bool) -> None: self.working = working if working: - self.timer.start() + screen = self.screen() + rate = screen.refreshRate() if screen is not None else 0.0 + self.timer.start(max(8, round(1000 / (rate if rate >= 20 else 60)))) else: self.timer.stop() self.update() @@ -101,51 +111,29 @@ def paintEvent(self, event) -> None: if not self.working: return frame = ACTIVITY_SWEEP.frame(monotonic() - self.started) - scheme = self.renderer.scheme - # The ribbon is sampled once per logical pixel and Qt scales the image onto the - # whole strip. The image used to be tagged with the device pixel ratio, which drew - # it at 1/ratio of the strip: on a Pixel 8a (2.625) the sweep ran across the first - # 38% of the row and its band never passed it. width, height = self.width(), self.height() - if width <= 0 or height <= 0: + if width <= 0 or height <= 0 or frame.end <= frame.start: return - # The ribbon inks one run of columns (`Ribbon.sample` is zero outside its ends), so - # only that run is sampled: the inked columns are contiguous. - first = max(0, int(frame.start * width)) - last = min(width, int(frame.end * width) + 1) - x0, pixels, bars = -1, array("I"), [] - for x in range(first, last): - start, end, amount, thickness = frame.sample((x + 0.5) / width) - if thickness <= 0: - if bars: - break - continue - x0 = x if x0 < 0 else x0 + scheme = self.renderer.scheme + left, right = frame.start * width, frame.end * width + gradient = QLinearGradient(left, 0.0, right, 0.0) + upper, lower = [], [] + for step in range(self.samples + 1): + place = step / self.samples + x = left + (right - left) * place + # The ribbon's ends sample as no ink; their colour is the one just inside. + inside = frame.start + (frame.end - frame.start) * min(max(place, 1e-6), 1 - 1e-6) + start, end, amount, _ = frame.sample(inside) ink = start.rgb(scheme).blend(end.rgb(scheme), amount) - pixels.append((0xFF << 24) | (ink.red << 16) | (ink.green << 8) | ink.blue) - bars.append(max(1, round(height * thickness))) - if not bars: - return - image = QImage(width, height, QImage.Format.Format_ARGB32_Premultiplied) - image.fill(0) - row_stride = image.bytesPerLine() // 4 - # Flat: CPython memoryviews cannot index a multi-dimensional row (`buf[y]`). - buf = memoryview(image.bits()).cast("I") - # Each column's ink is centred and its height rises to the body and falls again - # (the taper is a smoothstep), so a row's ink is one run of columns around the - # tallest: one slice write per row, found by bisecting the rising and falling sides, - # not one Python write per pixel. - peak = bars.index(max(bars)) - rising, falling = bars[:peak + 1], bars[peak:] - for y in range((height - bars[peak]) // 2, (height - bars[peak]) // 2 + bars[peak]): - def inks(bar: int, y: int = y) -> bool: - return (height - bar) // 2 <= y < (height - bar) // 2 + bar - left = bisect_left(rising, True, key=inks) - right = peak + bisect_left(falling, True, key=lambda bar: not inks(bar)) - offset = y * row_stride + x0 + left - buf[offset:offset + right - left] = pixels[left:right] + gradient.setColorAt(place, QColor(ink.red, ink.green, ink.blue)) + half = height * frame.sample(x / width)[3] / 2 + upper.append(QPointF(x, height / 2 - half)) + lower.append(QPointF(x, height / 2 + half)) painter = QPainter(self) - painter.drawImage(self.rect(), image) + painter.setRenderHint(QPainter.RenderHint.Antialiasing) + painter.setPen(Qt.PenStyle.NoPen) + painter.setBrush(gradient) + painter.drawPolygon(QPolygonF(upper + lower[::-1])) painter.end() diff --git a/src/toad/qt/channel_sort.py b/src/toad/qt/channel_sort.py index 452d22fdb..ff85de4cc 100644 --- a/src/toad/qt/channel_sort.py +++ b/src/toad/qt/channel_sort.py @@ -4,7 +4,8 @@ Both inherit `SortControlBase` (the current form, label and tooltip) and `CommsSortWrite` (the request each order is stored by, written through the observed Core) — the same two owners Textual's `ChannelListSort` and `ChannelSortChoice` inherit — so the two frontends -label and write an order identically. This module adds only the QToolButton and its menu. +label and write an order identically. This module adds only the QToolButton; its choices +open in the window's one pop-up menu (`QtMenu`). """ from __future__ import annotations @@ -15,56 +16,54 @@ from typing import TYPE_CHECKING from PySide6.QtCore import Qt -from PySide6.QtGui import QAction -from PySide6.QtWidgets import QMenu, QToolButton, QWidget +from PySide6.QtWidgets import QToolButton, QWidget from pyqt_reactive.protocols.widget_adapters import PyQtWidgetMeta from agent_comms.display_order import ChannelSort, DisplayOrder, ThreadSort +from toad.client.menu_entry import MenuEntry from toad.client.sort_control import ChannelListOrder, ChannelMemberOrder, ChannelSortForm, CommsSortWrite, SortControlBase, ThreadSortForm +from toad.qt.menu import QtMenu + if TYPE_CHECKING: from toad.qt.window import QtToadApp class QtSortButton[Order: DisplayOrder](SortControlBase[Order], QToolButton, metaclass=PyQtWidgetMeta): - """A sort choice as a one-line button whose menu lists its form's orders, then any - settings it toggles; a pick hands the order to `write_picked`, and only the observation - that follows repaints the label.""" + """A sort choice as a one-line button whose tap lists its form's orders, then any + settings it toggles, in the window's pop-up menu under the button; a pick hands the + order to `write_picked`, and only the observation that follows repaints the label.""" def __init__(self, form_type, scope: str, parent: QWidget | None = None) -> None: super().__init__(form_type=form_type, scope=scope, parent=parent) self.setWindowFlags(Qt.WindowType.Widget) self.setAutoRaise(True) - self.setPopupMode(QToolButton.ToolButtonPopupMode.InstantPopup) - # The label already ends in the shared "▾"; Qt's own menu arrow would draw a second. - self.setStyleSheet("QToolButton { padding: 0px; border: none; }" - "QToolButton::menu-indicator { image: none; width: 0px; }") + # The label already ends in the shared "▾". + self.setStyleSheet("QToolButton { padding: 0px; border: none; }") # One text line tall, as Textual's one-row segment. self.setFixedHeight(self.fontMetrics().lineSpacing()) self._form_type = form_type - menu = QMenu(self) - menu.aboutToShow.connect(self._fill) - self.setMenu(menu) - self._fill() + self.clicked.connect(self.open_choices) self.sort_changed() def visibility_fields(self): """Settings the menu toggles beside the orders, as Textual's `SortChoice` lists them.""" return () - def _fill(self) -> None: - menu = self.menu() - menu.clear() + def choice_entries(self) -> tuple[MenuEntry, ...]: + """The orders, then the settings this choice toggles, each with its current mark.""" choices = self._form_type.choices - for order in choices.choices(): - action = QAction(choices.label(order), menu) - action.triggered.connect(partial(self._picked, order)) - menu.addAction(action) - for path in self.visibility_fields(): - field = path.bind(self.saved_settings().state) - action = QAction(f"{'✓' if field.value else '○'} {field.label}", menu) - action.triggered.connect(partial(self._toggle_visibility, path)) - menu.addAction(action) + orders = tuple(MenuEntry(label=choices.label(order), run=partial(self._picked, order)) + for order in choices.choices()) + toggles = tuple( + MenuEntry(label=f"{'✓' if field.value else '○'} {field.label}", + run=partial(self._toggle_visibility, path)) + for path in self.visibility_fields() + for field in (path.bind(self.saved_settings().state),)) + return orders + toggles + + def open_choices(self) -> None: + QtMenu.of(self).show_entries(self.choice_entries(), QtMenu.anchor_of(self)) def saved_settings(self): raise NotImplementedError(f"{type(self).__name__} toggles no settings") diff --git a/src/toad/qt/composer.py b/src/toad/qt/composer.py index 949ada4f1..2b6058258 100644 --- a/src/toad/qt/composer.py +++ b/src/toad/qt/composer.py @@ -6,7 +6,9 @@ from __future__ import annotations from abc import ABC, abstractmethod +from collections.abc import Callable from dataclasses import dataclass +from typing import ClassVar from agent_comms.messaging.mentions import MentionCandidate from agent_comms.messaging.message_reference import MessageReference @@ -35,7 +37,8 @@ from toad.client.commands import ConversationCommands from toad.client.line_blocks import FailureRole, LineRole, MutedRole from toad.client.mention_completion import MentionCompletion, MentionEditor -from toad.client.prompt_composer import MessagePromptComposer, PromptComposer, PromptSettingEffects +from toad.client.message_files import AttachedFileChip, MessagePromptComposer +from toad.client.prompt_composer import PromptComposer, PromptSettingEffects from toad.client.condensed_path import home_relative from toad.key_modes import Standard, VimInsert from pyqt_reactive.protocols.widget_adapters import PyQtWidgetMeta @@ -47,7 +50,15 @@ UncheckedNotifications, ) from toad.client.subscribers import NotificationSubscriber -from toad.prompt_actions import NewlineAction, SubmitAction, SubmitInputAction, SubmitNowAction +from toad.prompt_actions import ( + AttachFileAction, + MessageAction, + NewlineAction, + PromptAction, + SubmitAction, + SubmitInputAction, + SubmitNowAction, +) from toad.widget_actions import DismissAction from toad.widget_contracts import ( DismissibleContract, @@ -56,6 +67,7 @@ SubmitsContract, ) from toad.qt.composer_vim import ComposerVimMode, apply_normal_key +from toad.qt.file_pickers import open_file_dialog, open_path_search from toad.qt.lines import QtLineRenderer from toad.qt.queue_ui import QtDeliveryControls, QtQueueHint, QtQueueInspection, QtQueueSummary, RowReveal from toad.qt.prompt_stack import QtInfoBar @@ -64,25 +76,13 @@ from toad.qt.metrics import UiGap -# The declared TextEntryMode key spellings each prompt action exposes drive the Qt -# composer's QShortcuts. Keeping the composer method as the callback (not the action's -# own .apply, which expects a Textual `PromptTextArea`) lets both backends consume the -# same key declarations without Qt dragging Textual's PromptSubmission chassis. -_COMPOSER_ACTION_METHODS = ( - (SubmitAction, "submit"), - (SubmitNowAction, "submit_now"), - (DismissAction, "dismiss_control"), - (NewlineAction, "_insert_newline"), -) - - def _bind_composer_shortcuts(composer: Composer): """One `QShortcut` per composer action on the editor, answering the action's keys in the current key mode and keymap (`ActionKeys`) and running the composer's own method while the editor (or a child) has focus. Textual reaches these methods through its own PromptTextArea action system — the keys live in one place.""" - return [composer.host.app.action_keys.bind(composer.editor, action_cls(), getattr(composer, method_name)) - for action_cls, method_name in _COMPOSER_ACTION_METHODS] + return [composer.host.app.action_keys.bind(composer.editor, action_cls(), method.__get__(composer)) + for action_cls, method in composer.action_methods] class ComposerHost(ConversationCommands): @@ -305,9 +305,19 @@ class Composer(PromptSettingEffects, PromptComposer, MentionEditor, SubmitsContr tap runs `submit`: one send path, two triggers. The shared action families bind to it through `SubmitsContract` + `DismissibleContract`.""" + # The declared TextEntryMode key spellings each prompt action exposes drive the Qt + # composer's QShortcuts. Keeping the composer method as the callback (not the action's + # own .apply, which expects a Textual `PromptTextArea`) lets both backends consume the + # same key declarations without Qt dragging Textual's PromptSubmission chassis. A kind + # of composer adds the actions only it answers. + action_methods: ClassVar[tuple[tuple[type[PromptAction | MessageAction], Callable[..., None]], ...]] + def __init__(self, host: ComposerHost, renderer: QtLineRenderer, placeholder: str) -> None: super().__init__() self.host, self.renderer = host, renderer + # The composer's area is its own surface (`area_tone`), as Textual's prompt area is. + self.setObjectName("composer") + self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True) self.given_placeholder = placeholder self.mention_candidates: tuple[MentionCandidate, ...] = () self.commands: list[SlashCommand] = [] @@ -343,7 +353,9 @@ def __init__(self, host: ComposerHost, renderer: QtLineRenderer, placeholder: st self.send_button = QPushButton(SubmitAction.title, self) self.send_button.setFocusPolicy(Qt.FocusPolicy.NoFocus) # tapping keeps the keyboard up self.send_button.clicked.connect(self.submit) - # The row's margins are the input box setting's frame margin (`place_input_box`). + # The row's margins are the input box setting's frame margin (`place_input_box`); every + # button beside the box is the box's one-line height, pinned to its bottom + # (`fit_buttons`). self.frame_row = frame_row = QHBoxLayout() frame_row.addWidget(self.frame, 1) frame_row.addWidget(self.send_button, 0, Qt.AlignmentFlag.AlignBottom) @@ -387,6 +399,25 @@ def cells(spacing): left, top, right, bottom = cells(box.label_padding) self.glyph.setContentsMargins(left, top, right, bottom) self.focus_changed(self.editor.hasFocus()) + self.fit_buttons() + + def one_line_box_height(self) -> int: + """The input box's height holding one line: the editor's one-line height, the box's + padding, and its border where it draws one.""" + padding = self.frame.layout().contentsMargins() + border = 2 if self.settings.ui.input.box.bordered else 0 + return self.editor_height(1) + padding.top() + padding.bottom() + border + + def fit_buttons(self) -> None: + """Every button in the box's row (send, and any a composer adds beside it) is the + box's one-line height and at least as wide as tall: level with a one-line box, and + pinned to the bottom of a taller one (each is added bottom-aligned).""" + height = self.one_line_box_height() + for index in range(self.frame_row.count()): + button = self.frame_row.itemAt(index).widget() + if button is not None and button is not self.frame: + button.setFixedHeight(height) + button.setMinimumWidth(height) def update_prompt(self) -> None: """A leading symbol or the placeholder setting changed: draw both again.""" @@ -402,8 +433,9 @@ def submissions(self) -> BusSubmissions: return self.host.submissions def paint_colours(self) -> None: - """The muted placeholder, the offer list, the glyph and the frame's border, under the - renderer's current scheme.""" + """The area's tone, the muted placeholder, the offer list, the glyph and the frame's + border, under the renderer's current scheme.""" + self.setStyleSheet(f"#composer {{ background: {self.renderer.theme(self.area_tone)} }}") palette = self.editor.palette() palette.setColor(QPalette.ColorRole.PlaceholderText, QColor(self.renderer.colour(MutedRole))) self.editor.setPalette(palette) @@ -451,7 +483,11 @@ def fit_height(self) -> None: lines = max(1, min(5, typed)) self.editor.setVerticalScrollBarPolicy( Qt.ScrollBarPolicy.ScrollBarAsNeeded if typed > 5 else Qt.ScrollBarPolicy.ScrollBarAlwaysOff) - self.editor.setFixedHeight(self.editor.fontMetrics().lineSpacing() * lines + 24) + self.editor.setFixedHeight(self.editor_height(lines)) + + def editor_height(self, lines: int) -> int: + """The editor's height showing `lines` lines.""" + return self.editor.fontMetrics().lineSpacing() * lines + 24 @property def text(self) -> str: @@ -510,7 +546,7 @@ def send(self, action: SubmitInputAction = SubmitAction()) -> None: self.editor.clear() self.dismissed_text = "" self.failure.hide() - self.host.submit(TypedText(text), action) + self.host.submit(TypedText(text, self.take_files()), action) # Toolkit-neutral action-family contract: Textual's Prompt inherits Submits + # DismissibleControl from widget_actions.py; the mixin lift itself is fork @@ -602,11 +638,66 @@ def labelled(self, label: str) -> None: """The view's label changed; a channel's composer keeps the placeholder its kind gave it.""" -class ChannelComposer(MessagePromptComposer, Composer): +Composer.action_methods = ( + (SubmitAction, Composer.submit), + (SubmitNowAction, Composer.submit_now), + (DismissAction, Composer.dismiss_control), + (NewlineAction, Composer._insert_newline), +) + + +class QtAttachedFileChip(AttachedFileChip, QPushButton): + """An attached file as a button over the editor; tapping it takes the file off.""" + + def __init__(self, composer: QtMessageComposer, position: int) -> None: + super().__init__(composer.attached_row) + self.show_file(composer, position) + self.setText(f"{self.caption} ✕") + self.setFocusPolicy(Qt.FocusPolicy.NoFocus) + self.setToolTip("Remove this file") + self.clicked.connect(self.take_off) + + +class QtMessageComposer(MessagePromptComposer, Composer): + """A bus message's composer on Qt: the shared message composer, an Attach button beside + Send, the attached files as chips over the editor, and Qt's two pickers.""" + + action_methods = (*Composer.action_methods, (AttachFileAction, MessagePromptComposer.attach_file)) + + def __init__(self, host: ComposerHost, renderer: QtLineRenderer, placeholder: str) -> None: + super().__init__(host, renderer, placeholder) + self.attach_button = QPushButton(AttachFileAction.title, self) + self.attach_button.setFocusPolicy(Qt.FocusPolicy.NoFocus) # tapping keeps the keyboard up + self.attach_button.clicked.connect(self.attach_file) + self.frame_row.insertWidget(1, self.attach_button, 0, Qt.AlignmentFlag.AlignBottom) + self.attached_row = QWidget(self) + chips = QHBoxLayout(self.attached_row) + chips.setContentsMargins(UiGap.frame, 0, UiGap.frame, 0) + chips.setAlignment(Qt.AlignmentFlag.AlignLeft) + layout = self.layout() + layout.insertWidget(layout.indexOf(self.frame_row), self.attached_row) + self.attached_row.hide() + + def pick_in_window(self) -> None: + open_file_dialog(self, self) + + def pick_by_path_search(self) -> None: + open_path_search(self, self, self.host.app.project_dir.path, self.host.app.run_operation) + + def show_attached(self) -> None: + chips = self.attached_row.layout() + while (item := chips.takeAt(0)) is not None: + item.widget().deleteLater() + for position in range(len(self.attached_files)): + chips.addWidget(QtAttachedFileChip(self, position)) + self.attached_row.setVisible(bool(self.attached_files)) + + +class ChannelComposer(QtMessageComposer): """A channel's composer, which offers its members after an @.""" -class MessageComposer(MessagePromptComposer, Composer): +class MessageComposer(QtMessageComposer): """A direct message's composer: it names its peer, as Textual's MessagePrompt does.""" def __init__(self, host: ComposerHost, renderer: QtLineRenderer) -> None: diff --git a/src/toad/qt/conversation.py b/src/toad/qt/conversation.py index ad6d017a0..20da8dc3d 100644 --- a/src/toad/qt/conversation.py +++ b/src/toad/qt/conversation.py @@ -329,7 +329,7 @@ def submit(self, text: TypedText, action: SubmitInputAction) -> None: """The composer's input enters the view's submissions, as Textual's prompt's `UserInputSubmitted` reaches `Conversation.submissions.submit`. No Qt view runs a shell, so typed input is never a shell command.""" - event = action.submission_event(text.text, False) + event = action.submission_event(text.text, False, text.files) self.run_worker(partial(self.submissions.submit, event), group="submit", exclusive=False) def update_slash_commands(self) -> None: diff --git a/src/toad/qt/file_pickers.py b/src/toad/qt/file_pickers.py new file mode 100644 index 000000000..75a8c2091 --- /dev/null +++ b/src/toad/qt/file_pickers.py @@ -0,0 +1,103 @@ +"""Qt's file pickers for a message composer: the file dialog and the project path search. + +The dialog is Qt's own QFileDialog. On Android, Qt answers it with the system's document +picker (the Storage Access Framework), which returns a `content://` URL that QFile reads; +on a desktop it is the platform's dialog. The path search ranks the project's paths through +the shared ProjectPaths, as Textual's @ completion does. Both hand the bytes they read to +`MessagePromptComposer.file_picked`, which decides what is attached. +""" + +from __future__ import annotations + +from collections.abc import Callable +from pathlib import Path + +from PySide6.QtCore import QFile, QFileInfo, QIODevice, Qt, QUrl +from PySide6.QtWidgets import QDialog, QFileDialog, QLineEdit, QListWidget, QVBoxLayout, QWidget + +from toad.client.message_files import FileSearch, MessagePromptComposer +from toad.client.project_paths import ProjectPathSearch +from toad.prompt_actions import AttachFileAction + + +def read_picked(url: QUrl, composer: MessagePromptComposer) -> None: + """Read what a picker chose, up to what the composer can still attach, and attach it.""" + # A local file's path, else the URL itself: QFile opens an Android content:// URL as is. + location = url.toLocalFile() or url.toString() + picked = QFile(location) + if not picked.open(QIODevice.OpenModeFlag.ReadOnly): + composer.show_failure(f"Could not read {location}: {picked.errorString()}") + return + try: + content = bytes(picked.read(composer.read_limit).data()) + finally: + picked.close() + composer.file_picked(QFileInfo(location).fileName(), content) + + +def open_file_dialog(parent: QWidget, composer: MessagePromptComposer) -> QFileDialog: + """Qt's file dialog over ``parent``, attaching the file it returns.""" + dialog = QFileDialog(parent, AttachFileAction.title) + dialog.setFileMode(QFileDialog.FileMode.ExistingFile) + # An accepted dialog holds its one choice: Qt's own dialog emits no urlSelected, and + # Android's document picker answers with a content:// URL, so both are read from here. + dialog.accepted.connect(lambda: [read_picked(url, composer) for url in dialog.selectedUrls()]) + dialog.finished.connect(dialog.deleteLater) + dialog.open() + return dialog + + +class QtPathSearch(ProjectPathSearch, QDialog): + """The project's paths, ranked as you type; choosing one attaches that file.""" + + shown_limit = 100 + + def __init__(self, parent: QWidget, root: Path, chosen: Callable[[Path], None], + run: Callable) -> None: + super().__init__(parent) + self.start_path_search(root) + self.setWindowTitle(AttachFileAction.title) + self.chosen, self.run = chosen, run + self.listing: list[str] = [] + self.query = QLineEdit(self) + self.query.setPlaceholderText(f"Search {root}") + self.results = QListWidget(self) + layout = QVBoxLayout(self) + layout.addWidget(self.query) + layout.addWidget(self.results, 1) + self.query.textChanged.connect(self.search) + self.query.returnPressed.connect(self.choose_current) + self.results.itemActivated.connect(lambda item: self.choose(item.text())) + self.finished.connect(self.deleteLater) + run(FileSearch, self.list_project) + + async def list_project(self) -> None: + self.listing = self.spell_paths(await self.scan_paths(add_directories=False)) + await self.rank_paths(self.listing) + self.show_paths(self.listing) + + def search(self, text: str) -> None: + self.run(FileSearch, lambda: self.show_ranked(text)) + + async def show_ranked(self, text: str) -> None: + self.show_paths(await self.ranked(text) if text else self.listing) + + def show_paths(self, spellings: list[str]) -> None: + self.results.clear() + self.results.addItems(spellings[:self.shown_limit]) + self.results.setCurrentRow(0) + + def choose_current(self) -> None: + if (item := self.results.currentItem()) is not None: + self.choose(item.text()) + + def choose(self, spelling: str) -> None: + self.accept() + self.chosen(self.root / spelling) + + +def open_path_search(parent: QWidget, composer: MessagePromptComposer, root: Path, run: Callable) -> QtPathSearch: + search = QtPathSearch(parent, root, lambda path: run(FileSearch, lambda: composer.attach_path(path)), run) + search.setWindowModality(Qt.WindowModality.WindowModal) + search.open() + return search diff --git a/src/toad/qt/history.py b/src/toad/qt/history.py index f33b0d287..c77f12676 100644 --- a/src/toad/qt/history.py +++ b/src/toad/qt/history.py @@ -21,6 +21,7 @@ from contextlib import asynccontextmanager, contextmanager from dataclasses import dataclass from functools import partial +from pathlib import Path from typing import ClassVar from weakref import WeakSet @@ -812,7 +813,8 @@ class QtHistoryWindow(ReaderWindow, WorkspaceHosted, VirtualRowView, # Emitted when the viewer right-clicks or long-presses a message row; carries the # fragment under that point. Consumers (ConversationView) build a block menu from # the shared `MenuCommand` family and offer it through the host's offer panel. - block_menu_requested = Signal(object, name="blockMenuRequested") + #: The row's fragment, and where it was pressed in the window's coordinates. + block_menu_requested = Signal(object, QPoint, name="blockMenuRequested") history: QtConversationHistory # `scroll_revision` + `history_change` are declared on JumpsToLatest (shared with Textual). @@ -1036,6 +1038,14 @@ def preview_link(self, link: ProjectLink) -> None: def warn_link(self, text: str) -> None: self.history.view.flash_error(text) + def run_link(self, work: Callable[[], Awaitable[None]]) -> None: + self.history.run_worker(work) + + def show_file(self, path: Path) -> None: + """A kept file opens in the application the system names for it.""" + if not QDesktopServices.openUrl(QUrl.fromLocalFile(str(path))): + self.warn_link(f"No application opens {path.name}; it is kept at {path}.") + @handles(FragmentDetails) def toggle_details(self, link: FragmentDetails) -> None: """A tap on a ▶ row opens or closes that fragment's details, as Textual's page does.""" @@ -1053,7 +1063,7 @@ def _emit_block_menu_at(self, point: QPoint) -> None: fragment = self.model_rows.data(index, HistoryRows.FRAGMENT) if fragment is None: return - self.block_menu_requested.emit(fragment) + self.block_menu_requested.emit(fragment, self.viewport().mapTo(self.window(), point)) # Geometry, in pixels diff --git a/src/toad/qt/lines.py b/src/toad/qt/lines.py index 8765e6021..d86998ccd 100644 --- a/src/toad/qt/lines.py +++ b/src/toad/qt/lines.py @@ -19,7 +19,7 @@ from markdown_it import MarkdownIt from metaclass_registry.mro_dispatch import handles -from ui_theme.scheme import ColourScheme, ThemeColour +from ui_theme.scheme import ColourScheme, RuleColour, ThemeColour from toad.client.line_blocks import ( Body, @@ -200,17 +200,20 @@ def barred(self, text: str, bar: type[LineRole]) -> str: @handles(Divider) def divider(self, block: Divider) -> str: """Every row of the title flanked by rules filling the width, as Textual's divider does. - One paragraph so Qt adds no extra block gap between wrapped rows.""" + A terminal can only draw its rule in whole `─` cells; Qt draws each side as a real line + reaching the edge, so every rule spans the full width whatever the title's length. + The title's cell takes its content's width and the side cells share the rest.""" bold = QFont(self.font) bold.setBold(True) room = PixelRoom(QFontMetricsF(bold), self.width) groups = [row for group in DIVIDER_TIME.project_value(block.timestamp, block.spans, room) for row in room.wrapped(group)] - rows = "
".join(f'{self.styled("─" * max(room.rule(group), 0), block.role)} ' - f'{self.title(group, block)} ' - f'{self.styled("─" * max(room.rule(group), 0), block.role)}' - for group in groups) - return f'

{rows}

' + colour = self.theme(RuleColour) + rule = f'
' + rows = "".join(f'{rule}' + f' {self.title(group, block)} {rule}' + for group in groups) + return f'{rows}
' def title(self, spans: tuple[Span, ...], block: Divider) -> str: return "".join(self.span(span, span.link.heading_role(span.role, block.role)) for span in spans) diff --git a/src/toad/qt/menu.py b/src/toad/qt/menu.py index dea11c09c..0b18d826b 100644 --- a/src/toad/qt/menu.py +++ b/src/toad/qt/menu.py @@ -1,9 +1,15 @@ -"""Qt's structured menu widget: renders a tuple of toolkit-neutral MenuEntry rows as +"""Qt's one pop-up menu: renders a tuple of toolkit-neutral MenuEntry rows as `[bold key] [label / description]`, matching the Textual `Menu` widget's row structure. -A single widget renders both the application (hamburger) menu and the message-context -(long-press on a row) menu, so the two paths share rendering, layout and click handling, -and the only thing that differs is which MenuEntry tuple each one supplies. +A single widget renders every pop-up Toad opens (the application menu, a message's and a +sidebar row's held/right-click menu, the bus-connect choices, a sort choice), so they share +rendering, placement, dismissal and click handling, and the only thing that differs is +which MenuEntry tuple each one supplies and where it was asked for. + +It floats: a child of the window placed over the window's content and outside every layout, +so opening or closing it moves nothing underneath. It is not a top-level window (a QMenu or +a `Qt.Popup`): on Qt for Android every QWindow needs its own EGL surface, and a second one +deadlocks the compositor. """ from __future__ import annotations @@ -11,22 +17,27 @@ from collections.abc import Callable, Iterable from functools import partial -from PySide6.QtCore import Qt +from PySide6.QtCore import QEvent, QObject, QRect, Qt from PySide6.QtGui import QGuiApplication from PySide6.QtWidgets import ( + QApplication, QFrame, QHBoxLayout, QLabel, QPushButton, + QScrollArea, QVBoxLayout, QWidget, ) +from ui_theme.scheme import FloatHoverToneColour, FloatToneColour, MutedColour, RuleColour + from pyqt_reactive.widgets.fitted_text import ElidedLine from toad.client.block_menu import BlockMenu from toad.client.input_modality import InputModality, KeyboardInput, TouchInput from toad.client.menu_entry import MenuEntry +from toad.qt.lines import QtLineRenderer from toad.qt.metrics import UiGap @@ -75,7 +86,7 @@ def __init__(self, entry: MenuEntry, menu: QtMenu, chosen: Callable[[MenuEntry], if entry.description and not menu.input_modality.describes_inline(): description = ElidedLine(entry.description) description.setParent(self._button) - description.setEnabled(False) # the theme's muted text, as a secondary line + description.setObjectName("toad-menu-description") # muted (`QtMenu.paint`) text.addWidget(description) inner.addLayout(text, 1) # The row is as tall as its lines need, as the layout measures them (each line's own @@ -90,32 +101,146 @@ def _fire(self) -> None: class QtMenu(BlockMenu, QFrame): - """A column of MenuEntry rows. The container itself is a QFrame (not a top-level - window — important on Qt-for-Android where every QWindow needs its own EGL surface - and a floating menu would deadlock the compositor). Its visibility is managed by - whatever opened it (hamburger button, long-press handler). `show_entries()` swaps - the current entry list in place and shows the menu; `hide()` is inherited.""" - - def __init__(self, parent: QWidget | None = None) -> None: - super().__init__(parent) + """The window's pop-up menu: one surface of MenuEntry rows floating over the window's + content (see the module docstring for why it is a child widget). `show_entries` lists + the rows and places the menu at the `anchor` it was asked from: under it, or above it + where there is no room below, kept inside the window. A tap outside the menu closes it + and does nothing else (the press and its release are eaten), as does Escape; Back closes + it through the window's `go_back`. A choice closes it and runs the entry. + + Its chrome is one surface: the theme's float tone, one rule-coloured border, the rows + flat on it, the float hover tone under a finger or the pointer (`paint`).""" + + #: The widest a menu draws, in pixels; a phone's narrower window caps it first, and + #: the rows elide within whichever is narrower. + WIDEST = 420 + + def __init__(self, window: QWidget) -> None: + super().__init__(window) self.setWindowFlags(Qt.WindowType.Widget) + self.setObjectName("toad-menu") self.input_modality = platform_input() - self._layout = QVBoxLayout(self) - self._layout.setContentsMargins(UiGap.frame, UiGap.normal, UiGap.frame, UiGap.normal) - self._layout.setSpacing(UiGap.tight) + self._anchor = QRect() + self._eating_release = False + self._rows = QWidget() + self._rows.setObjectName("toad-menu-rows") + self._layout = QVBoxLayout(self._rows) + self._layout.setContentsMargins(0, 0, 0, 0) + self._layout.setSpacing(0) + self._scroll = QScrollArea(self) + self._scroll.setFrameShape(QFrame.Shape.NoFrame) + self._scroll.setWidgetResizable(True) + self._scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) + self._scroll.setWidget(self._rows) + outer = QVBoxLayout(self) + outer.setContentsMargins(1, UiGap.normal, 1, UiGap.normal) # 1px: inside the border + outer.setSpacing(0) + outer.addWidget(self._scroll) + window.installEventFilter(self) self.hide() - def show_entries(self, entries: Iterable[MenuEntry]) -> None: - """Replace the rows with ``entries`` and show the menu. A selection runs the - entry's action and dismisses the menu.""" + @classmethod + def of(cls, widget: QWidget) -> QtMenu: + """The pop-up menu of the window `widget` is in: the window's own direct child.""" + return widget.window().findChild(cls, "toad-menu", Qt.FindChildOption.FindDirectChildrenOnly) + + @staticmethod + def anchor_of(widget: QWidget) -> QRect: + """`widget`'s rectangle in its window, as `show_entries` takes an anchor.""" + return QRect(widget.mapTo(widget.window(), widget.rect().topLeft()), widget.size()) + + def paint(self, renderer: QtLineRenderer) -> None: + """One surface in the renderer's theme: the float tone (a tier above what it floats + over) and one rule-coloured border; rows flat on it, the float hover tone under a + finger or the pointer; descriptions muted.""" + hover = renderer.theme(FloatHoverToneColour) + self.setStyleSheet( + f"#toad-menu {{ background: {renderer.theme(FloatToneColour)};" + f" border: 1px solid {renderer.theme(RuleColour)}; border-radius: 6px; }}" + "#toad-menu QScrollArea, #toad-menu-rows { background: transparent; border: none; }" + "#toad-menu QPushButton { background: transparent; border: none; border-radius: 0; padding: 0;" + " text-align: left; }" + f"#toad-menu QPushButton:hover, #toad-menu QPushButton:pressed {{ background: {hover}; }}" + f"#toad-menu-description {{ color: {renderer.theme(MutedColour)}; }}") + + def show_entries(self, entries: Iterable[MenuEntry], anchor: QRect | None = None) -> None: + """Replace the rows with ``entries`` and show the menu at ``anchor`` (a rectangle in + the window's coordinates: the button or the pressed point that asked for it), or at + the top of the window with none. A selection runs the entry's action and dismisses + the menu.""" while self._layout.count(): item = self._layout.takeAt(0) widget = item.widget() if widget is not None: + widget.setParent(None) widget.deleteLater() for entry in entries: - self._layout.addWidget(QtMenuRow(entry, self, self._chose, parent=self)) + self._layout.addWidget(QtMenuRow(entry, self, self._chose, parent=self._rows)) + self._anchor = anchor if anchor is not None else QRect() + self.place() self.show() + self.raise_() + QApplication.instance().installEventFilter(self) + + def place(self) -> None: + """Under the anchor (or above it, where there is more room there and too little + below), inside the window by a frame gap, WIDEST wide or the window's width, as + tall as its rows within the room it has; it scrolls only when they do not fit.""" + window = self.parentWidget() + area = window.rect().adjusted(UiGap.frame, UiGap.frame, -UiGap.frame, -UiGap.frame) + margins = self.layout().contentsMargins() + width = min(area.width(), self.WIDEST) + wanted = self._rows.sizeHint().height() + margins.top() + margins.bottom() + anchor = self._anchor + if not anchor.isValid(): + geometry = QRect(area.left() + (area.width() - width) // 2, area.top(), width, min(wanted, area.height())) + else: + below, above = area.bottom() - anchor.bottom(), anchor.top() - area.top() + x = max(area.left(), min(anchor.left(), area.right() + 1 - width)) + if wanted <= below or below >= above: + geometry = QRect(x, anchor.bottom() + 1, width, min(wanted, below)) + else: + height = min(wanted, above) + geometry = QRect(x, anchor.top() - height, width, height) + self._scroll.setVerticalScrollBarPolicy( + Qt.ScrollBarPolicy.ScrollBarAlwaysOff if geometry.height() >= wanted + else Qt.ScrollBarPolicy.ScrollBarAsNeeded) + self.setGeometry(geometry) + + def eventFilter(self, watched: QObject, event: QEvent) -> bool: # noqa: N802 - Qt virtual method name + kind = event.type() + if watched is self.parentWidget() and kind == QEvent.Type.Resize and self.isVisible(): + self.place() + return False + if self._eating_release: + if kind in (QEvent.Type.MouseButtonRelease, QEvent.Type.MouseButtonDblClick): + self._eating_release = False + QApplication.instance().removeEventFilter(self) + return True + return kind == QEvent.Type.MouseMove + if not self.isVisible(): + return False + if kind == QEvent.Type.MouseButtonPress and not self.geometry().contains( + self.parentWidget().mapFromGlobal(event.globalPosition().toPoint())): + self._eating_release = True + self.hide() + return True + if kind == QEvent.Type.ShortcutOverride and event.key() == Qt.Key.Key_Escape: + # Escape is the open menu's: no shortcut bound to it (the composer's dismiss) runs, + # and the key press that follows comes here and closes the menu. + event.accept() + return True + if kind == QEvent.Type.KeyPress and event.key() == Qt.Key.Key_Escape: + self.hide() + return True + return False + + def hideEvent(self, event) -> None: # noqa: N802 - Qt virtual method name + """Closed: the app stops passing its events here, unless the outside tap that + closed it still has its release to eat.""" + if not self._eating_release: + QApplication.instance().removeEventFilter(self) + super().hideEvent(event) def _chose(self, entry: MenuEntry) -> None: self.hide() diff --git a/src/toad/qt/overlay_scrollbar.py b/src/toad/qt/overlay_scrollbar.py new file mode 100644 index 000000000..349ad38d6 --- /dev/null +++ b/src/toad/qt/overlay_scrollbar.py @@ -0,0 +1,143 @@ +"""A scroll area's vertical bar drawn over its text while it moves (`Scrollbar.overlay`). + +A bar beside the text takes its width for good; this one floats over the viewport's right +edge only while the area scrolls, and fades out a moment after it goes still. It mirrors the +area's own bar (hidden, still the model of the position), so dragging it scrolls the area and +any scroll of the area, by wheel, key, finger or code, shows it. Once faded it is hidden, so +it never takes a tap meant for the text under it. +""" + +from __future__ import annotations + +from PySide6.QtCore import QEvent, QObject, QRect, Qt, QTimer +from PySide6.QtWidgets import QAbstractScrollArea, QGraphicsOpacityEffect, QScrollBar +from ui_theme.clock import AnimationClock +from ui_theme.scheme import SurfaceHoverColour + +from toad.motion import SCROLLBAR_FADE, TransitionTarget +from toad.qt.lines import QtLineRenderer +from toad.qt.widget_animation import WidgetAnimation + + +class ScrollbarFade(WidgetAnimation, TransitionTarget): + """The overlay bar fading out on the app's clock; it is hidden where the fade lands.""" + + def __init__(self, bar: OverlayScrollBar, clock: AnimationClock) -> None: + self.bar = bar + self.bind_widget(bar, clock) + self.start_transition(clock, SCROLLBAR_FADE) + + def apply_amount(self, amount: float) -> None: + self.bar.opacity.setOpacity(amount) + + def land(self) -> None: + self.bar.fade = None + self.bar.hide() + + +class OverlayScrollBar(QScrollBar): + """The overlay for one area; `follow` attaches it, or detaches it at width 0.""" + + STILL_MS = 1000 + """How long the area stays still before the bar fades.""" + + def __init__(self, area: QAbstractScrollArea, clock: AnimationClock, renderer: QtLineRenderer) -> None: + super().__init__(Qt.Orientation.Vertical, area) + self.area, self.clock, self.renderer = area, clock, renderer + self.source = area.verticalScrollBar() + self.thickness = 0 + self.fade: ScrollbarFade | None = None + self.opacity = QGraphicsOpacityEffect(self) + self.opacity.setOpacity(1.0) + self.setGraphicsEffect(self.opacity) + self.still = QTimer(self) + self.still.setSingleShot(True) + self.still.setInterval(self.STILL_MS) + self.still.timeout.connect(self.fade_out) + self.source.rangeChanged.connect(self.setRange) + self.source.valueChanged.connect(self.moved) + self.valueChanged.connect(self.source.setValue) + self.geometry_watch = _ViewportGeometry(self) + area.viewport().installEventFilter(self.geometry_watch) + renderer.themed(self, self.paint_colours) + self.hide() + + def paint_colours(self) -> None: + """Only the handle shows, a raised tier over whatever text it floats on; the track and + the arrows draw nothing.""" + handle = SurfaceHoverColour.rgb(self.renderer.scheme).hex + self.setStyleSheet( + "QScrollBar:vertical { background: transparent; margin: 0; border: none; }" + f"QScrollBar::handle:vertical {{ background: {handle}; border-radius: {max(1, self.thickness // 2)}px;" + " min-height: 24px; }" + "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical { height: 0; border: none; }" + "QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical { background: transparent; }") + + @classmethod + def follow(cls, area: QAbstractScrollArea, clock: AnimationClock, renderer: QtLineRenderer, + thickness: int) -> None: + """Draw ``area``'s bar over its text ``thickness`` pixels wide while it moves; at 0 + the area has no overlay. The area's own bar is then sized by its column setting.""" + bar = area.findChild(cls, options=Qt.FindChildOption.FindDirectChildrenOnly) + if bar is None: + if not thickness: + return + bar = cls(area, clock, renderer) + if bar.thickness != thickness: + bar.thickness = thickness + bar.paint_colours() + if not thickness: + bar.stop() + bar.hide() + else: + bar.place() + + def stop(self) -> None: + self.still.stop() + if self.fade is not None: + self.fade.animation.stop() + self.fade = None + + def moved(self, value: int) -> None: + """The area scrolled: show the bar at once at full strength and restart the wait.""" + self.setValue(value) + if not self.thickness: + return + self.stop() + self.opacity.setOpacity(1.0) + self.setPageStep(self.source.pageStep()) + self.setSingleStep(self.source.singleStep()) + self.place() + if self.maximum() > self.minimum(): + self.show() + self.raise_() + self.still.start() + + def fade_out(self) -> None: + """Still long enough: fade, unless the reader is holding or pointing at the bar.""" + if self.isSliderDown() or self.underMouse(): + self.still.start() + return + if self.clock.enabled: + self.fade = ScrollbarFade(self, self.clock) + else: + self.hide() + + def place(self) -> None: + """Over the right edge of the viewport, its full height.""" + viewport = self.area.viewport().geometry() + self.setGeometry(QRect(viewport.right() + 1 - self.thickness, viewport.top(), + self.thickness, viewport.height())) + + +class _ViewportGeometry(QObject): + """Keeps the overlay over the viewport when the viewport is resized or moved.""" + + def __init__(self, bar: OverlayScrollBar) -> None: + super().__init__(bar) + self.bar = bar + + def eventFilter(self, watched, event) -> bool: # type: ignore[override] + if event.type() in (QEvent.Type.Resize, QEvent.Type.Move) and self.bar.thickness: + self.bar.place() + return False diff --git a/src/toad/qt/submission_view.py b/src/toad/qt/submission_view.py index 7aa632f5d..6b29dfacd 100644 --- a/src/toad/qt/submission_view.py +++ b/src/toad/qt/submission_view.py @@ -11,13 +11,12 @@ class QtSubmissionView(SubmissionView): - def delivery_refused(self, description: str, body: str) -> None: - self.composer.restore_draft(body) - self.composer.show_failure(self.refusal_notice(description)) + @property + def submission_composer(self): + return self.composer - def delivery_unknown(self, description: str, body: str) -> None: - self.composer.restore_draft(body) - self.composer.show_failure(self.unknown_notice(description)) + def show_send_failure(self, text: str) -> None: + self.composer.show_failure(text) composer_block: str = "" diff --git a/src/toad/qt/thread_comms_panel.py b/src/toad/qt/thread_comms_panel.py index 3ed397702..573b9bf1f 100644 --- a/src/toad/qt/thread_comms_panel.py +++ b/src/toad/qt/thread_comms_panel.py @@ -24,12 +24,10 @@ from agent_comms.registry.relationships import SortableRelationshipGroup from PySide6.QtCore import Qt from pyqt_reactive.protocols.widget_adapters import PyQtWidgetMeta -from PySide6.QtGui import QAction from PySide6.QtWidgets import ( QCheckBox, QHBoxLayout, QLabel, - QMenu, QToolButton, QTreeWidget, QTreeWidgetItem, diff --git a/src/toad/qt/transcript_history.py b/src/toad/qt/transcript_history.py index 059569c1d..bc89ffcb1 100644 --- a/src/toad/qt/transcript_history.py +++ b/src/toad/qt/transcript_history.py @@ -39,11 +39,11 @@ def empty_page(cls): def _initial_page_source(self, prepared, app): """Bind a `SavedTranscript` over the view's `TranscriptPresentation`. The loader - callable is `view.transcript.get_transcript_page`; the fragment family is + source is `view.transcript`, its pages and their files; the fragment family is `QT_FRAGMENT_CLASSES` (Qt-flavored siblings of the canonical neutral family — same decode, Qt-flavored rendering, no data duplication).""" return SavedTranscript( - self.view.transcript.get_transcript_page, + self.view.transcript, prepared.page.after, app.preparation, fragments=QT_FRAGMENT_CLASSES, diff --git a/src/toad/qt/view_settings.py b/src/toad/qt/view_settings.py index 1f2ac6e85..290832db5 100644 --- a/src/toad/qt/view_settings.py +++ b/src/toad/qt/view_settings.py @@ -11,6 +11,7 @@ from PySide6.QtWidgets import QWidget from toad.client.view_layout import ConversationColumn, LayoutFollower +from toad.qt.overlay_scrollbar import OverlayScrollBar def capability_widgets(root: QWidget, widget_type: type) -> list: @@ -26,8 +27,8 @@ def settings_widgets(self, widget_type): def apply_layout_preferences(self) -> None: """The conversation takes the column's width (all of it with the column off), set - where the column alignment puts it; the transcript's scrollbar is the setting's cells - wide.""" + where the column alignment puts it; the transcript's scrollbar takes the setting's + cells beside the text, and draws its `overlay` cells over the text while it moves.""" ui = self.configuration_state.fields.ui cell = self.fontMetrics().averageCharWidth() width = self.width() @@ -38,6 +39,8 @@ def apply_layout_preferences(self) -> None: self.layout().setContentsMargins(left, margins.top(), width - column - left, margins.bottom()) bar = self.messages.verticalScrollBar() bar.setFixedWidth(self.scrollbar_cells(ui) * cell) + OverlayScrollBar.follow(self.messages, self.messages.animation_clock, self.messages.rows.app_renderer, + ui.scrollbar.overlay * cell) def showEvent(self, event) -> None: # type: ignore[override] super().showEvent(event) diff --git a/src/toad/qt/window.py b/src/toad/qt/window.py index 7f622760c..920d485a5 100644 --- a/src/toad/qt/window.py +++ b/src/toad/qt/window.py @@ -23,7 +23,7 @@ from agent_comms.transport.request import CoreAccess from agent_comms.ui_model.sidebar import ChannelRowModel, SidebarModel from formbridge.operations import OperationKind -from PySide6.QtCore import QEvent, QMargins, QObject, QPoint, Qt, QTimer +from PySide6.QtCore import QEvent, QMargins, QObject, QPoint, QRect, QSize, Qt, QTimer from PySide6.QtGui import QColor, QKeySequence, QShortcut from PySide6.QtWidgets import ( QApplication, @@ -34,6 +34,8 @@ QLabel, QLineEdit, QPushButton, + QScrollArea, + QSizePolicy, QStackedWidget, QToolButton, QTreeWidget, @@ -549,7 +551,10 @@ def offer(self, point: QPoint) -> None: return entries = item.menu_entries(self.listing) if entries: - self.listing.host.offer_entries(entries) + # In the host window's coordinates, where `offer_entries` places its anchor + # (`ConversationRows.window` names the app, hiding QWidget.window()). + pressed = rows.viewport().mapTo(self.listing.host, point) + self.listing.host.offer_entries(entries, QRect(pressed, QSize(1, 1))) class QtWelcomeOrigin(HomeSessionOrigin): @@ -1397,12 +1402,13 @@ def thread_identity(self): # through `widgets/menu.Menu`. Keeping the action list derived means a new menu # command declared in `src/toad/menus.py` reaches Qt without any edit here. - def open_block_menu(self, fragment: QtWireFragment) -> None: + def open_block_menu(self, fragment: QtWireFragment, pressed: QPoint) -> None: + """The block menu for `fragment`, at `pressed` (the window's coordinates).""" self.held_fragment = fragment entries = QtMenu.command_entries(_block_menu_commands(fragment), self.app.settings, self._run_block_menu_command) if entries: - self.app.offer_entries(entries) + self.app.offer_entries(entries, QRect(pressed, QSize(1, 1))) def _run_block_menu_command(self, command: MenuCommand) -> None: result = command.apply(self) @@ -1617,29 +1623,46 @@ def __init__(self, connection: CoreConnection, project: Checkout, self.theme.color_scheme.to_qcolor(self.theme.color_scheme.text_primary)) self.tabs.setTabsClosable(True) self.tabs.setExpanding(False) - self.tabs.setUsesScrollButtons(True) - self.tabs.setElideMode(Qt.TextElideMode.ElideRight) + # Each tab keeps its own width and the strip scrolls: a swipe moves it on the phone + # (every scroll area scrolls by finger, `finger_scrolling`), where many tabs were + # squeezed behind two arrow buttons. A right-click, which a held finger is + # (`finger_press`), offers the shared tab menu: moving and closing. + self.tabs.setUsesScrollButtons(False) + self.tabs.setElideMode(Qt.TextElideMode.ElideNone) self.tabs.tabBarClicked.connect(self.visit_index) self.tabs.tabCloseRequested.connect(self.close_index) self.tabs.tabMoved.connect(self.move_index) + self.tabs.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) + self.tabs.customContextMenuRequested.connect(self.offer_tab_menu) + self.tab_strip = QScrollArea(self) + self.tab_strip.setWidget(self.tabs) + self.tab_strip.setWidgetResizable(True) + self.tab_strip.setFrameShape(QFrame.Shape.NoFrame) + self.tab_strip.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) + self.tab_strip.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) + self.tab_strip.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed) + # The header band's sheet pads and borders every frame inside it (a 5 px inset here, + # which clipped the tabs' lower 10 px); the strip is only a window onto the tabs. + self.tab_strip.setObjectName("toad-tab-strip") + self.tab_strip.setStyleSheet("#toad-tab-strip { border: none; padding: 0px; background: transparent; }") + # A renamed or added tab changes the strip's range: the selected one stays in view. + self.tab_strip.horizontalScrollBar().rangeChanged.connect(lambda *_: self.show_current_tab()) bar = QHBoxLayout() bar.setSpacing(UiGap.tight) for widget in (self.menu, self.back, self.forward): bar.addWidget(widget) - bar.addWidget(self.tabs, 1) + bar.addWidget(self.tab_strip, 1) # The bar row is Toad's header band: a frame in the theme's header tone. Notices # float over the pages in `float_layer`; they are no part of this band. self.navigation = chrome = QFrame(self) chrome_layout = QVBoxLayout(chrome) chrome_layout.setContentsMargins(0, 0, 0, 0) chrome_layout.setSpacing(0) - # QtMenu is the shared rendering for both the hamburger (application) menu and - # the row-context (message) menu. Its rows render MenuEntry data (bold key - # shortcut + label), matching Textual's MenuKey/MenuDescription widgets. Parent - # is QtToadApp so the menu is a child QWidget, not a top-level QWindow — the - # EGL-deadlock hazard that killed off QMessageBox applies to any popup window. + # QtMenu is the window's one pop-up menu (the application menu, the held/right-click + # row menus, the bus-connect choices, a sort choice). It floats over the content as + # a child of the window, in no layout, so opening it moves nothing (`QtMenu`). self.menu_panel = QtMenu(self) - chrome_layout.addWidget(self.menu_panel) + self.renderer.themed(self.menu_panel, lambda: self.menu_panel.paint(self.renderer)) # Bus switcher: inline overlay panel for the hamburger "Switch bus" entry. # Registered as a `BusListing` so `tailnet_answered` routes answers to it # while shown (same shape as Welcome, which is always registered). @@ -1750,6 +1773,15 @@ def mark_current(self) -> None: modes = self.session_navigation.tab_modes mode = self.selected_mode self.tabs.setCurrentIndex(modes.index(mode) if mode in modes else -1) + # Once the bar has laid its tabs out (a restyle or a new title resizes them after this), + # the strip is as tall as its tabs and the selected one is in view. + QTimer.singleShot(0, self.show_current_tab) + + def show_current_tab(self) -> None: + self.tab_strip.setFixedHeight(self.tabs.sizeHint().height()) + if (index := self.tabs.currentIndex()) >= 0: + rect = self.tabs.tabRect(index) + self.tab_strip.ensureVisible(rect.center().x(), rect.center().y(), rect.width() // 2 + UiGap.dialog, 0) self.present_panes() self.input_attention.update() @@ -1812,7 +1844,7 @@ def follow_keymap(self) -> None: def show_session_tabs(self, shown: bool) -> None: self.paint_tabs() - self.tabs.setVisible(shown) + self.tab_strip.setVisible(shown) def pane_open(self, identity: type[SidebarPlacement]) -> bool: """Read through SidebarLayout — the single collapsed-state owner. A collapsed @@ -1998,7 +2030,7 @@ def show_menu(self) -> None: if self.menu_panel.isVisible(): self.menu_panel.hide() return - self.menu_panel.show_entries(self._application_menu_entries()) + self.menu_panel.show_entries(self._application_menu_entries(), QtMenu.anchor_of(self.menu)) def _application_menu_entries(self) -> tuple[MenuEntry, ...]: """Project ApplicationAction.menu(self) into MenuEntry rows. The key_display @@ -2025,10 +2057,11 @@ def offer(self, actions: tuple[OfferedAction, ...]) -> None: — the same widget the hamburger and the message-row menus use.""" self.offer_entries(tuple(MenuEntry.from_offered(action) for action in actions)) - def offer_entries(self, entries: tuple[MenuEntry, ...]) -> None: + def offer_entries(self, entries: tuple[MenuEntry, ...], anchor: QRect | None = None) -> None: """Rich-row API: callers that have `MenuEntry` with keybindings and descriptions - (the hamburger menu, the row-context menu) populate the shared QtMenu directly.""" - self.menu_panel.show_entries(entries) + (the hamburger menu, the row-context menu) populate the shared QtMenu directly, at + `anchor` in this window's coordinates (where they were asked from).""" + self.menu_panel.show_entries(entries, anchor) def offer_bus_connect(self, decision: BusConnectDecision) -> None: """Render the shared bus-connect decision through the same menu panel the hamburger @@ -2134,6 +2167,12 @@ def visit_index(self, index: int) -> None: def close_index(self, index: int) -> None: self.close_session(self.session_navigation.tabs[index].mode_name) + def offer_tab_menu(self, point: QPoint) -> None: + """The tab under a right-click or a held finger offers the shared tab menu.""" + index = self.tabs.tabAt(point) + if index >= 0: + self.offer_entries(self.session_navigation.tab_menu(self.session_navigation.tabs[index].mode_name)) + def move_index(self, start: int, end: int) -> None: """A tab dragged from one place in the bar to another: it moves past the tab it displaced in the shared order, which also holds saved tabs this window has not diff --git a/src/toad/screens/file_picker.py b/src/toad/screens/file_picker.py new file mode 100644 index 000000000..f9d343046 --- /dev/null +++ b/src/toad/screens/file_picker.py @@ -0,0 +1,111 @@ +"""The terminal's file pickers for a message composer: a file browser that can leave the +project, and the project's path search. Either reads the chosen file through the +composer's shared `attach_path`; Escape closes either and attaches nothing.""" + +from __future__ import annotations + +from abc import abstractmethod +from pathlib import Path + +from formbridge.key_declarations import TextEntryMode +from textual import events, on +from textual.app import ComposeResult +from textual.widgets import DirectoryTree, Input, Label, OptionList +from textual.widgets.option_list import Option + +from toad.client.message_files import MessagePromptComposer +from toad.client.project_paths import ProjectPathSearch +from toad.screens.modal import CenteredScreen, DismissibleScreen, ModalFrame +from toad.widget_actions import Action, NativeMethodKeys + + +class FilePickerScreen(CenteredScreen, DismissibleScreen[None]): + """A picker over the files of one composer's machine, starting at its project.""" + + heading: str + + def __init__(self, composer: MessagePromptComposer, root: Path) -> None: + super().__init__() + self.composer, self.root = composer, root + + def compose(self) -> ComposeResult: + with ModalFrame(): + yield Label(self.heading) + yield from self.compose_picker() + + @abstractmethod + def compose_picker(self) -> ComposeResult: + """The picker's own controls, under its title.""" + + async def attach(self, path: Path) -> None: + """The chosen file goes to the composer; the picker closes first.""" + self.dismiss() + await self.composer.attach_path(path) + + +class FileBrowserAction(Action, affix="Action"): + """Keys the file browser answers.""" + + +class ParentFolderAction(FileBrowserAction): + """Browse the folder above the one shown.""" + + keys = {TextEntryMode: ("backspace",)} + show = True + + def apply(self, tree: FileBrowserTree) -> None: + tree.path = Path(tree.path).parent + + +class FileBrowserTree(NativeMethodKeys, DirectoryTree, actions=(FileBrowserAction,)): + """Every file and folder, not only what git keeps: a file to attach can be anywhere.""" + + +class FileBrowserScreen(FilePickerScreen): + heading = "Attach a file · enter opens or attaches · backspace goes up · escape closes" + + def compose_picker(self) -> ComposeResult: + yield FileBrowserTree(self.root) + + @on(DirectoryTree.FileSelected) + async def file_selected(self, event: DirectoryTree.FileSelected) -> None: + event.stop() + await self.attach(event.path) + + +class PathSearchScreen(ProjectPathSearch, FilePickerScreen): + heading = "Attach a project file · type to search · enter attaches · escape closes" + + def __init__(self, composer: MessagePromptComposer, root: Path) -> None: + super().__init__(composer, root) + self.start_path_search(root) + self.spellings: list[str] = [] + + def compose_picker(self) -> ComposeResult: + yield Input(compact=True, placeholder="fuzzy search") + yield OptionList() + + @on(events.Mount) + async def list_project(self) -> None: + self.query_one(Input).focus() + self.spellings = self.spell_paths(await self.scan_paths(add_directories=False)) + await self.rank_paths(self.spellings) + self.show_paths(self.spellings) + + def show_paths(self, paths: list[str]) -> None: + options = self.query_one(OptionList) + options.set_options([Option(path, id=path) for path in paths[:100]]) + options.highlighted = 0 + + @on(Input.Changed) + async def search(self, event: Input.Changed) -> None: + self.show_paths(await self.ranked(event.value) if event.value else self.spellings) + + @on(Input.Submitted) + def submit(self, event: Input.Submitted) -> None: + """Enter in the search attaches the highlighted path, as choosing it does.""" + self.query_one(OptionList).action_select_cursor() + + @on(OptionList.OptionSelected) + async def option_selected(self, event: OptionList.OptionSelected) -> None: + await self.attach(self.root / event.option.id) diff --git a/src/toad/screens/historical_sessions.py b/src/toad/screens/historical_sessions.py index 71459c74b..dbe8e965e 100644 --- a/src/toad/screens/historical_sessions.py +++ b/src/toad/screens/historical_sessions.py @@ -28,7 +28,8 @@ from toad.widgets.history_anchor import HistoryWindow from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES from toad.widgets.transcript_history import LineHistory -from toad.transcript_preparation import SavedTranscript, TranscriptPageRenderTask +from toad.client.message_file_links import NO_PAGE_FILES +from toad.transcript_preparation import LoadedTranscriptPages, SavedTranscript, TranscriptPageRenderTask class RecordedContents(Contents): @@ -148,10 +149,10 @@ async def load(**kwargs): Static("No saved conversation records are available for this identity.") ) else: - prepared = await TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES).prepared(self.app.preparation) + prepared = await TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES).prepared(self.app.preparation) if generation != self._selection_generation or not self.is_attached: return - history = LineHistory(prepared, SavedTranscript(load, page.after, self.app.preparation, + history = LineHistory(prepared, SavedTranscript(LoadedTranscriptPages(load), page.after, self.app.preparation, fragments=TEXTUAL_FRAGMENT_CLASSES), notification_view=content, source_state=LiveTranscript()) window = self.query_one(HistoryWindow) diff --git a/src/toad/session_navigation.py b/src/toad/session_navigation.py index 239c6e9d7..d05704594 100644 --- a/src/toad/session_navigation.py +++ b/src/toad/session_navigation.py @@ -42,6 +42,7 @@ from toad.navigation_preparation import CommsNavigationRequest, ThreadNavigationRequest from toad.navigation_target import DirectTarget, HistoryRouted from toad.client.history_entry import HistorySessionEntry +from toad.client.menu_entry import MenuEntry from toad.client.session_entry import NativeSessionEntry, PreviewSessionEntry, SessionEntry from toad.client.session_scope import SessionScope from toad.session_host import NavigationHost, SessionNavigator @@ -472,6 +473,26 @@ def tabs(self) -> tuple[OpenTab, ...]: observed = self.app.coordination_access.observation return self.app.tab_order.project(observed.tabs(self)) + def tab_menu(self, mode_name: str) -> tuple[MenuEntry, ...]: + """What a right-click or a held finger on an open tab offers, in every frontend: move + it past the open tab beside it, or close it. A move counts its steps in the shared + order, which also holds saved tabs this window has not opened, as a dragged tab's does.""" + tabs = self.tabs + modes = [tab.mode_name for tab in tabs] + if mode_name not in modes: + return () + index, order = modes.index(mode_name), self.app.tab_order.names + here = order.index(tabs[index].identity) + publish = self.events.publish + entries = [ + MenuEntry(label, partial(publish, session_requests.SessionMove( + mode_name, order.index(tabs[beside].identity) - here))) + for label, beside in (("Move left", index - 1), ("Move right", index + 1)) + if 0 <= beside < len(tabs) + ] + entries.append(MenuEntry("Close tab", partial(publish, session_requests.SessionClose(mode_name)))) + return tuple(entries) + @property def tab_modes(self) -> tuple[str, ...]: """The open tabs' modes in tab order. A tab's mode is its identity's, so this reads no diff --git a/src/toad/setting_choices.py b/src/toad/setting_choices.py index 848d755c1..9ff99fbcf 100644 --- a/src/toad/setting_choices.py +++ b/src/toad/setting_choices.py @@ -186,9 +186,20 @@ def enabled(cls, focused: bool) -> bool: class Scrollbar(DeclaredChoice, affix="Scrollbar"): - """The conversation's vertical scrollbar width, in cells, declared once by each member.""" + """The conversation's vertical scrollbar, declared once by each member: `size` is the + width it takes beside the text, in cells; `overlay` is the width it draws over the text + while the conversation moves, gone a moment after. A terminal cannot draw over its text, + so Textual shows only `size`; Qt shows both.""" size: ClassVar[int] + overlay: ClassVar[int] = 0 + + @classmethod + def for_host(cls) -> type[Scrollbar]: + """The platform's default: a touch platform scrolls by finger, so its bar only shows + while the conversation moves and leaves the text its whole width; on a desktop the + bar stays beside the text to be grabbed.""" + return host.select({"android": TransientScrollbar, "*": NormalScrollbar}) class NormalScrollbar(Scrollbar): @@ -199,6 +210,13 @@ class ThinScrollbar(Scrollbar): size = 1 +class TransientScrollbar(Scrollbar): + """Drawn over the text while it scrolls, then faded out; it takes no width.""" + + size = 0 + overlay = 1 + + class HiddenScrollbar(Scrollbar): size = 0 diff --git a/src/toad/transcript_preparation.py b/src/toad/transcript_preparation.py index e62014a06..5a692c1ff 100644 --- a/src/toad/transcript_preparation.py +++ b/src/toad/transcript_preparation.py @@ -5,10 +5,16 @@ import asyncio import pickle +from abc import ABC, abstractmethod +from collections.abc import Awaitable, Callable from dataclasses import dataclass, field, replace from hashlib import sha256 from agent_comms.execution.transcript_sources import ( + AtFrontier, + LatestPage, + PageLimit, + PagePosition, TranscriptCursor, ) from agent_comms.nominal.field_codec import ContentExclude, FieldCodec @@ -26,6 +32,7 @@ SourcePageWork, TranscriptNoProgress, ) +from toad.client.message_file_links import NO_PAGE_FILES, PageFiles from toad.client.transcript_fragments import FragmentClasses, transcript_fragments from toad.render_backend import ContentAddressedRenderTask from toad.work_preparation import ( @@ -37,14 +44,46 @@ ) +class TranscriptPages(ABC): + """Where a saved transcript's pages are read, and the files each page's messages carry.""" + + @abstractmethod + async def get_transcript_page( + self, *, position: PagePosition = LatestPage(), limit: PageLimit = AtFrontier(), + ) -> TranscriptPage: ... + + @abstractmethod + async def page_files(self, page: TranscriptPage, classes: FragmentClasses) -> PageFiles: + """One read of the files every bus message on ``page`` carries.""" + + +@dataclass(frozen=True) +class LoadedTranscriptPages(TranscriptPages): + """Pages read by ``load`` with no file store beside them, so a page shows no files: an + attached history's, whose files stay with the bus that recorded them.""" + + load: Callable[..., Awaitable[TranscriptPage]] + + async def get_transcript_page( + self, *, position: PagePosition = LatestPage(), limit: PageLimit = AtFrontier(), + ) -> TranscriptPage: + return await self.load(position=position, limit=limit) + + async def page_files(self, page: TranscriptPage, classes: FragmentClasses) -> PageFiles: + return NO_PAGE_FILES + + @dataclass(frozen=True) class TranscriptPageRenderTask(ReusableStorage[PreparedTranscriptPage], ContentAddressedRenderTask[PreparedTranscriptPage]): page: TranscriptPage classes: FragmentClasses + files: PageFiles def preparation_digest(self, work: RenderPreparation[PreparedTranscriptPage]) -> bytes: - """Read marks and coordination activity cannot change prepared content.""" - return sha256(pickle.dumps(FieldCodec.project(self.page, ContentExclude), protocol=5)).digest() + """Read marks and coordination activity cannot change prepared content; the files its + messages carry do.""" + return sha256(pickle.dumps( + (FieldCodec.project(self.page, ContentExclude), self.files), protocol=5)).digest() def result_size(self, result: PreparedTranscriptPage) -> int: return result.retained_bytes @@ -55,6 +94,7 @@ def execute(self) -> PreparedTranscriptPage: self.page.events, resolve_sent=self.page.resolve_sent_messages, classes=self.classes, + files=self.files, ) return PreparedTranscriptPage.measured(self.page, fragments) @@ -72,12 +112,13 @@ class TranscriptPageWork(SourcePageWork[TranscriptCursor]): async def execute(self, runtime: PreparationRuntime, key: WorkKey) -> PreparedResult[PreparedTranscriptPage]: request = self.request - page = await request.end.load_page(self.loader, request.cursor, self.through) + page = await request.end.load_page(self.loader.get_transcript_page, request.cursor, self.through) if not request.progressed(page): raise TranscriptNoProgress(request, self.through, page) + files = await self.loader.page_files(page, self.classes) if self.scope.closed: raise asyncio.CancelledError - prepared = await TranscriptPageRenderTask(page, self.classes).prepared(runtime) + prepared = await TranscriptPageRenderTask(page, self.classes, files).prepared(runtime) return await runtime.run_thread(self.finish_result, key, prepared) diff --git a/src/toad/transcript_publication.py b/src/toad/transcript_publication.py index c45e37075..802889a90 100644 --- a/src/toad/transcript_publication.py +++ b/src/toad/transcript_publication.py @@ -63,7 +63,9 @@ from toad.session_scope import SessionScope from toad.transcript_scope import NoTranscript, TranscriptScope from toad.entry_placement import ChronologicalEntry, EntryPlacement -from toad.transcript_preparation import SavedSuffix +from toad.client.message_file_links import PageFiles +from toad.client.transcript_fragments import FragmentClasses, transcript_file_readers +from toad.transcript_preparation import SavedSuffix, TranscriptPages from toad.client.prepared_pages import ( FrozenClosedHistory, FrozenSavedPrefix, @@ -113,6 +115,11 @@ def current(self): async def read_page(self, scope, *, position: PagePosition, limit: PageLimit) -> TranscriptPage: return await self.binding.reader.page(scope, position=position, limit=limit) + async def page_files(self, page: TranscriptPage, classes: FragmentClasses) -> PageFiles: + return await self.binding.reader.page_files(page, partial( + transcript_file_readers, page.events, resolve_sent=page.resolve_sent_messages, + classes=classes)) + def after_refresh(self, callback: Callable[[], object]) -> None: self.binding.view.after_refresh(callback) @@ -320,6 +327,10 @@ async def read_page(self, scope, *, position: PagePosition, limit: PageLimit) -> # stands, and the request a new binding makes reads again. raise ReadAgain() + async def page_files(self, page: TranscriptPage, classes: FragmentClasses) -> PageFiles: + """A page read before the view lost its binding is read again, its files with it.""" + raise ReadAgain() + async def publish(self) -> bool: """Nothing to publish: the request is done.""" return True @@ -756,7 +767,7 @@ async def keep_before_later_history(self, owner: TranscriptPresentation, entry: await history.retire_source(parked=True) source = history.page_source prefix = cursor.painted_read( - lambda at: FrozenSavedPrefix(source, owner.get_transcript_page, at, self.view.app.preparation, fragments=source.fragments), + lambda at: FrozenSavedPrefix(source, owner, at, self.view.app.preparation, fragments=source.fragments), FrozenClosedHistory(cursor, self.view.app.preparation), ) prepared = await prefix.latest(history.pages[-1].prepared) @@ -772,7 +783,7 @@ async def keep_before_later_history(self, owner: TranscriptPresentation, entry: )) later = self.view.later_history( PreparedTranscriptPage.empty(UnreadTranscriptPage(before=cursor, after=cursor)), - SavedSuffix(owner.get_transcript_page, cursor, self.view.app.preparation, after=cursor, fragments=source.fragments), + SavedSuffix(owner, cursor, self.view.app.preparation, after=cursor, fragments=source.fragments), entry.conversation_order + 1, ) self._history = later @@ -959,7 +970,7 @@ def commits(self, live_blocks: int, limit: int) -> bool: COMMIT_DEMAND = CommitDemand() -class TranscriptPresentation(EntryPlacement): +class TranscriptPresentation(TranscriptPages, EntryPlacement): """Owns the transcript document; attached native publishers can be replaced.""" def __init__(self) -> None: @@ -1021,6 +1032,9 @@ async def get_transcript_page( raise ValueError("Thread source changed while reading transcript") return page + async def page_files(self, page: TranscriptPage, classes: FragmentClasses) -> PageFiles: + return await self.binding.capture(self, BoundFrame).page_files(page, classes) + @property def retained_source_bytes(self) -> int: return sum(source.retained_source_bytes for source in self.documents) diff --git a/src/toad/widgets/committed_presentation.py b/src/toad/widgets/committed_presentation.py index e1367db65..a91c16361 100644 --- a/src/toad/widgets/committed_presentation.py +++ b/src/toad/widgets/committed_presentation.py @@ -120,7 +120,9 @@ async def read_snapshot[Snapshot]( @classmethod async def capture(cls, view: Conversation, history: TranscriptHistory, page: TranscriptPage, sequences: frozenset[int]) -> Self: - prepared = await TranscriptPageRenderTask(page, history.page_source.fragments).prepared(view.app.preparation) + classes = history.page_source.fragments + files = await view.transcript.page_files(page, classes) + prepared = await TranscriptPageRenderTask(page, classes, files).prepared(view.app.preparation) return cls(history, sequences, prepared) @property diff --git a/src/toad/widgets/linked_rows.py b/src/toad/widgets/linked_rows.py index c10373570..4d43d343f 100644 --- a/src/toad/widgets/linked_rows.py +++ b/src/toad/widgets/linked_rows.py @@ -3,6 +3,7 @@ from __future__ import annotations from abc import abstractmethod +from collections.abc import Awaitable, Callable from functools import cached_property, partial from pathlib import Path from typing import cast @@ -84,6 +85,13 @@ def preview_link(self, link: ProjectLink) -> None: def warn_link(self, text: str) -> None: self.notify(text, severity="warning") + def run_link(self, work: Callable[[], Awaitable[None]]) -> None: + self.run_worker(work(), group="open-link") + + def show_file(self, path: Path) -> None: + """A kept file previews as a project file does.""" + self.app.run_worker(partial(self.app.session_navigation.preview, path, position=None)) + async def link_menu(self, href: str, event: object) -> bool: return await LineLink.from_href(href).copy_menu(self, event) diff --git a/src/toad/widgets/path_search.py b/src/toad/widgets/path_search.py index 1d9950440..aca5e0e76 100644 --- a/src/toad/widgets/path_search.py +++ b/src/toad/widgets/path_search.py @@ -43,13 +43,11 @@ from metaclass_registry.mro_dispatch import handles -from toad import directory from toad.core.events import LinkContextChanged from toad.core_event_carrier import CoreEventReceiver from toad.project_path_owner import ProjectPathOwner -from toad.path_search_ranking import PathSearchRanking +from toad.client.project_paths import ProjectPathSearch from toad.messages import Dismiss, InsertPath, PromptSuggestion -from toad.path_filter import PathFilter from ui_theme.scheme import ForegroundColour from toad.native_style import MutedText, SideMargin, SidePadding @@ -268,7 +266,7 @@ async def apply(self, context: PathSearch) -> None: class PathSearch( - CoreEventReceiver, PriorityActions, HiddenActions, VisibleActions, ForwardsCursor, Submits, CompletionPopup, + ProjectPathSearch, CoreEventReceiver, PriorityActions, HiddenActions, VisibleActions, ForwardsCursor, Submits, CompletionPopup, scopes={HiddenActions: (DismissAction, SubmitAction)}, actions=(SwitchPathPickerAction,), ): @@ -286,23 +284,20 @@ class PathSearch( def __init__(self) -> None: super().__init__() - self.ranking = PathSearchRanking() self._paths_dirty = True @classmethod def for_prompt(cls, prompt: Prompt) -> Self | None: return cls() - @property - def root(self) -> Path: - return ProjectPathOwner.containing(self).project_root - @on(events.Mount) def on_mount(self) -> None: + self.start_path_search(ProjectPathOwner.containing(self).project_root) self.observe_core(ProjectPathOwner.containing(self).link_context_events) @handles(LinkContextChanged) def project_changed(self, event: LinkContextChanged) -> None: + self.start_path_search(ProjectPathOwner.containing(self).project_root) for tree in self.query(ProjectDirectoryTree): tree.path = self.root self.invalidate_paths() @@ -454,27 +449,10 @@ async def refresh_paths(self): self.option_list.set_loading(True) root = self.root try: - path_filter = await asyncio.to_thread(PathFilter.from_git_root, root) + paths = await self.scan_paths(add_directories=True) if tree := self.query_one_optional(ProjectDirectoryTree): - tree.path_filter = path_filter + tree.path_filter = self.path_filter tree.invalidate() - paths = await directory.scan( - root, path_filter=path_filter, add_directories=True - ) - - def make_absolute(paths: list[Path]) -> list[Path]: - """Make all paths absolute. - - Args: - paths: A list of paths. - - Returns: - List of absolute paths, - - """ - return [path.absolute() for path in paths] - - paths = await asyncio.to_thread(make_absolute, paths) if self.root != root: return self.paths = paths @@ -496,20 +474,7 @@ def highlight_path(self, path: str) -> PathContent: @work(description="watch_paths") async def watch_paths(self, paths: list[Path]) -> None: - - def path_display(path: Path) -> str: - is_directory = path.is_dir() - if is_directory: - return str(path.relative_to(self.root)) + "/" - else: - return str(path.relative_to(self.root)) - - def make_display_paths() -> list[str]: - display_paths = sorted(map(path_display, paths), key=str.lower) - display_paths.sort(key=lambda path: path.count("/")) - return display_paths - - self.display_paths = await asyncio.to_thread(make_display_paths) + self.display_paths = await asyncio.to_thread(self.spell_paths, paths) self.option_list.highlighted = None self._update_paths(self.display_paths) @@ -532,5 +497,5 @@ async def _update_paths(self, paths: list[str]) -> None: Args: paths: A list of paths. """ - await self.ranking.update_paths(paths) + await self.rank_paths(paths) self.call_after_refresh(self.option_list.set_loading, False) diff --git a/src/toad/widgets/prompt.py b/src/toad/widgets/prompt.py index d91ad9a68..29fc02926 100644 --- a/src/toad/widgets/prompt.py +++ b/src/toad/widgets/prompt.py @@ -35,10 +35,10 @@ from textual.reactive import Initialize, Required, var from textual.strip import Strip from textual.style import Style -from textual.widgets import Label, OptionList, TextArea +from textual.widgets import Button, Label, OptionList, TextArea from textual.widgets.option_list import Option, OptionListComponent from textual.widgets.text_area import Selection, TextAreaComponent -from ui_theme.scheme import ErrorColour, MutedColour, PrimaryColour, PromptToneColour, SecondaryColour +from ui_theme.scheme import ErrorColour, MutedColour, PrimaryColour, SecondaryColour from toad import messages from toad.clipboard_image import PastesClipboard, read_clipboard, save_clipboard_png @@ -46,7 +46,9 @@ from toad.core import input_events from toad.core_event_carrier import CoreEventReceiver from toad.client.input_decision import InputAccepted, InputDecision, RefusalFeedback -from toad.client.prompt_composer import MessagePromptComposer, PromptComposer +from toad.client.message_files import AttachedFileChip, MessagePromptComposer +from toad.screens.file_picker import FileBrowserScreen, PathSearchScreen +from toad.client.prompt_composer import PromptComposer from toad.client.prompt_stack import EditingModeBadge from toad.client.queue_inspection import QueueDeliveryControls from toad.native_style import ( @@ -75,7 +77,7 @@ ) from toad.tone_paint import TonePaint from toad.path_complete import AnyPath, CompletingEditor, DirectoriesOnly, FilesOnly, PathComplete -from toad.prompt_actions import PromptAction, SubmitInputAction, SubmitNowAction +from toad.prompt_actions import MessageAction, PromptAction, SubmitInputAction, SubmitNowAction from toad.app_node import ToadNode from toad.prompt_cursor import ( CommandText, @@ -655,7 +657,7 @@ async def _submit(self, action: SubmitInputAction, mode: EditingMode) -> None: shell = self.shell_mode text = self.text self.clear() - self.publish_core(action.submission_event(text, shell)) + self.publish_core(action.submission_event(text, shell, self.prompt.take_files())) if shell: self.prompt.shell_mode = False @@ -700,7 +702,7 @@ def watch_selection( self.input_cursor.changed(previous_selection, selection) -class MessageTextArea(PromptTextArea): +class MessageTextArea(PromptTextArea, actions=(MessageAction,)): @property def can_enter_shell(self) -> bool: return False @@ -724,7 +726,8 @@ class Prompt(NativeMethodKeys, HiddenActions, DismissibleControl, PromptSettingE containers.VerticalGroup, ToadNode, PromptComposer, actions=(PromptFrameAction,)): """The Textual composer; `PromptComposer` owns its submissions, queue, glyph and draft decisions, shared with Qt's `Composer`.""" - tone = PromptToneColour + # `TonePaint` comes first in the bases, so its `tone` would hide the composer's own. + tone = PromptComposer.area_tone TEXT_AREA_CLASS = PromptTextArea @@ -795,6 +798,10 @@ def restore_draft(self, text: str) -> None: editor.insert(suffix, editor.document.end, maintain_selection_offset=True) editor.history.checkpoint() + def show_failure(self, text: str) -> None: + """Where this prompt's conversation tells a send failure.""" + self.conversation.show_send_failure(text) + def watch_agent_binding(self) -> None: if self.is_mounted: self.sync_configuration() @@ -1076,9 +1083,14 @@ def compose_mode_controls(self) -> ComposeResult: yield ModeSwitcher() yield ModeInfo("mode") + def compose_attached(self) -> ComposeResult: + """What the message being written carries; an agent prompt carries nothing.""" + return () + def compose(self) -> ComposeResult: yield from self.compose_completions() yield from self.compose_delivery_controls() + yield from self.compose_attached() with PromptContainer(): yield PromptQuestion() with TextPrompt(): @@ -1115,9 +1127,33 @@ def dismiss_control(self) -> None: raise SkipAction() +class FileChip(AttachedFileChip, Button): + """One attached file as Textual draws it; pressing it takes the file off the message.""" + + def __init__(self, composer: MessagePromptComposer, position: int) -> None: + self.show_file(composer, position) + super().__init__(f"{self.caption} ✕", compact=True, tooltip="Remove from this message") + + @on(Button.Pressed) + def pressed(self, event: Button.Pressed) -> None: + event.stop() + self.take_off() + + +class AttachedFiles(containers.HorizontalGroup): + """The files the message being written carries, as chips above the text.""" + + def show(self, composer: MessagePromptComposer) -> None: + self.remove_children() + self.mount_all(FileChip(composer, position) for position in range(len(composer.attached_files))) + self.display = bool(composer.attached_files) + + class MessagePrompt(MessagePromptComposer, Prompt): TEXT_AREA_CLASS = MessageTextArea + attached = getters.query_one(AttachedFiles) + def __init__(self, *, placeholder: str | None = None, **kwargs): self.simple_placeholder = placeholder super().__init__(**kwargs) @@ -1156,6 +1192,21 @@ def compose_completions(self) -> ComposeResult: if (popup := SlashComplete.for_prompt(self)) is not None: yield popup + def compose_attached(self) -> ComposeResult: + files = AttachedFiles() + files.display = False + yield files + + def pick_in_window(self) -> None: + """A terminal's window is a file browser over the whole disk, opened at the project.""" + self.app.push_screen(FileBrowserScreen(self, self.project_path.path)) + + def pick_by_path_search(self) -> None: + self.app.push_screen(PathSearchScreen(self, self.project_path.path)) + + def show_attached(self) -> None: + self.attached.show(self) + class MailboxPrompt(MessagePrompt): """A CLI thread's composer shows its project; Core's status line names the model, read-only.""" diff --git a/src/toad/widgets/session_tabs.py b/src/toad/widgets/session_tabs.py index 5af0d7ddd..2901b543f 100644 --- a/src/toad/widgets/session_tabs.py +++ b/src/toad/widgets/session_tabs.py @@ -29,6 +29,7 @@ ForegroundColour, HeaderToneColour, MutedColour, + SelectedTabToneColour, ) from toad.core import events as core_events @@ -52,6 +53,16 @@ class SessionLabel(WidgetView, PrimaryActivation, ViewLabel, ChangingText, widge def current(self) -> bool: return self.mode_name == self.app.selected_mode + # TonePaint declares a class tone; the open tab is raised a tier off the bar, the rest sit on it. + @property + def tone(self): # type: ignore[override] + return SelectedTabToneColour if self.current else None + + def paint_current(self) -> None: + """The tab opened or closed: its tone is part of its cached paint.""" + self.refresh_paint() + super().paint_current() + @property def paint_text_style(self): return RichStyle(dim=not self.mouse_hover) diff --git a/src/toad/widgets/transcript_history.py b/src/toad/widgets/transcript_history.py index 3db7e7bae..8783d43ef 100644 --- a/src/toad/widgets/transcript_history.py +++ b/src/toad/widgets/transcript_history.py @@ -951,7 +951,7 @@ def empty_page(cls): @classmethod def empty_source(cls, view, through): from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES - return SavedTranscript(view.transcript.get_transcript_page, through, view.app.preparation, + return SavedTranscript(view.transcript, through, view.app.preparation, fragments=TEXTUAL_FRAGMENT_CLASSES) async def _page_added(self, page: TranscriptPage, fragments: tuple[TranscriptFragment, ...]) -> None: diff --git a/tests/model/test_message_file_reader.py b/tests/model/test_message_file_reader.py new file mode 100644 index 000000000..7104011fd --- /dev/null +++ b/tests/model/test_message_file_reader.py @@ -0,0 +1,21 @@ +"""A message's files are read in the conversation its reader sees it in: a channel message in +its channel, a direct message in the DM with the participant that is not the reader.""" + +import pytest +from agent_comms.messaging.messages import InfoKind, Message + +from toad.client.message_file_links import FileReader, file_size +from toad.message_rows import MessageRow + + +@pytest.mark.parametrize(("sender", "target", "reader", "read"), ( + ("peer", "owner", "owner", FileReader("peer", "owner")), + ("owner", "peer", "owner", FileReader("peer", "owner")), + ("peer", "#team", "owner", FileReader("#team", "owner")), +), ids=("dm-incoming", "dm-outgoing", "channel")) +def test_a_row_names_the_read_its_files_are_listed_under(sender, target, reader, read): + assert MessageRow(Message(sender, target, "body", InfoKind), reader).file_reader == read + + +def test_a_chip_names_its_size_as_a_reader_reads_it(): + assert [file_size(size) for size in (512, 1536, 2 * 1024 * 1024)] == ["512 B", "1.5 KB", "2.0 MB"] diff --git a/tests/model/test_message_files.py b/tests/model/test_message_files.py new file mode 100644 index 000000000..13f0d0cc8 --- /dev/null +++ b/tests/model/test_message_files.py @@ -0,0 +1,97 @@ +"""The message composer's files, through the shared owner both frontends inherit.""" + +from __future__ import annotations + +import base64 +from dataclasses import dataclass, field + +from agent_comms.messaging.message_files import MAX_MESSAGE_FILE_BYTES + +from toad.client.bus_submission import TypedText +from toad.client.message_files import FilePicker, MessagePromptComposer, PathSearchFilePicker, WindowFilePicker +from toad.client.message_file_links import file_size + + +@dataclass +class Input: + file_picker: type[FilePicker] + agent_symbol: str = "❯" + + +@dataclass +class Ui: + input: Input + + +@dataclass +class Settings: + ui: Ui + + +@dataclass +class Composer(MessagePromptComposer): + """A frontend that records what the shared owner asks of it.""" + + picker: type[FilePicker] = WindowFilePicker + asked: list[str] = field(default_factory=list) + text: str = "" + submissions = None + + @property + def settings(self): + return Settings(Ui(Input(self.picker))) + + def pick_in_window(self) -> None: + self.asked.append("window") + + def pick_by_path_search(self) -> None: + self.asked.append("path search") + + def show_attached(self) -> None: + self.asked.append(f"chips {[self.chip_label(upload) for upload in self.attached_files]}") + + def show_failure(self, text: str) -> None: + self.asked.append(f"failure {text}") + + def restore_draft(self, text: str) -> None: + self.text += self.restored_suffix(self.text, text) + + +def test_attach_opens_the_picker_the_setting_names() -> None: + for picker, asked in ((WindowFilePicker, "window"), (PathSearchFilePicker, "path search")): + composer = Composer(picker=picker) + composer.attach_file() + assert composer.asked == [asked] + + +def test_picked_files_ride_the_send_and_come_back_with_a_returned_draft() -> None: + composer = Composer() + composer.file_picked("/home/me/notes.txt", b"notes") + composer.file_picked("shot.png", b"\x89PNG") + assert [(upload.name, upload.mime_type) for upload in composer.attached_files] == [ + ("notes.txt", "text/plain"), ("shot.png", "image/png")] + assert composer.asked[-1] == "chips ['notes.txt · 5 B', 'shot.png · 4 B']" + composer.detach_file(0) + assert [upload.name for upload in composer.attached_files] == ["shot.png"] + sent = TypedText("look at this", composer.take_files()) + assert composer.attached_files == () and composer.asked[-1] == "chips []" + assert base64.b64decode(sent.files[0].data) == b"\x89PNG" + composer.restore(sent) + assert composer.text == "look at this" + assert composer.attached_files == sent.files + + +def test_a_file_past_the_message_limit_is_refused_and_attaches_nothing() -> None: + composer = Composer() + composer.file_picked("half.bin", b"a" * (MAX_MESSAGE_FILE_BYTES // 2 + 1)) + assert composer.read_limit == MAX_MESSAGE_FILE_BYTES // 2 + composer.file_picked("other-half.bin", b"b" * composer.read_limit) + assert [upload.name for upload in composer.attached_files] == ["half.bin"] + assert composer.asked[-1].startswith("failure other-half.bin not attached: ") + composer.file_picked("empty.txt", b"") + assert composer.asked[-1].startswith("failure empty.txt not attached: ") + + +def test_sizes_read_at_a_glance() -> None: + assert [file_size(size) for size in (0, 1023, 1536, 5 * 1024 * 1024)] == [ + "0 B", "1023 B", "1.5 KB", "5.0 MB"] diff --git a/tests/support/textual_steps.py b/tests/support/textual_steps.py index 40f35d074..04ccc9133 100644 --- a/tests/support/textual_steps.py +++ b/tests/support/textual_steps.py @@ -5,7 +5,8 @@ from __future__ import annotations from toad.client.history_state import LiveTranscript, ProvisionalTranscript -from toad.transcript_preparation import SavedTranscript, TranscriptPageRenderTask +from toad.client.message_file_links import NO_PAGE_FILES +from toad.transcript_preparation import LoadedTranscriptPages, SavedTranscript, TranscriptPageRenderTask from toad.widgets.side_bar import ThreadSidebar from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES from toad.widgets.transcript_history import TranscriptHistory @@ -139,7 +140,7 @@ def painted_text(widget, text) -> bool: async def saved_history(view, page, loader, *, committed=True): """A saved transcript history prepared and paged the way the application builds one.""" - prepared = await view.app.preparation.submit(RenderPreparation(TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES))) - return TranscriptHistory(prepared, SavedTranscript(loader, page.after, view.app.preparation, fragments=TEXTUAL_FRAGMENT_CLASSES), + prepared = await view.app.preparation.submit(RenderPreparation(TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES))) + return TranscriptHistory(prepared, SavedTranscript(LoadedTranscriptPages(loader), page.after, view.app.preparation, fragments=TEXTUAL_FRAGMENT_CLASSES), view=view, notification_view=view.contents, source_state=LiveTranscript() if committed else ProvisionalTranscript()) diff --git a/tests/textual/test_fragment_source_occurrence.py b/tests/textual/test_fragment_source_occurrence.py index 0c7010932..83cbf4a6c 100644 --- a/tests/textual/test_fragment_source_occurrence.py +++ b/tests/textual/test_fragment_source_occurrence.py @@ -9,12 +9,13 @@ ThinkingTranscript, ) +from toad.client.message_file_links import NO_PAGE_FILES from toad.client.transcript_fragments import RenderBudget, transcript_fragments from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES def fragments(*events): - return transcript_fragments(events, resolve_sent=lambda reports: {}, classes=TEXTUAL_FRAGMENT_CLASSES) + return transcript_fragments(events, resolve_sent=lambda reports: {}, classes=TEXTUAL_FRAGMENT_CLASSES, files=NO_PAGE_FILES) def test_thinking_parts_of_one_reply_have_distinct_keys(): diff --git a/tests/textual/test_input_failure_draft.py b/tests/textual/test_input_failure_draft.py index 3a894cc24..454212f30 100644 --- a/tests/textual/test_input_failure_draft.py +++ b/tests/textual/test_input_failure_draft.py @@ -86,7 +86,7 @@ async def test_a_prompt_the_agent_cannot_be_reached_for_is_restored_to_the_compo ui.attach_agent(receiver) await ui.write_draft("") with patch.object(receiver.process, "prompt", side_effect=FileNotFoundError(2, "agent socket disappeared")): - worker = await ImmediateInputSubmission("the local failure prompt").execute(conversation.submissions) + worker = await ImmediateInputSubmission("the local failure prompt", ()).execute(conversation.submissions) await worker.wait() await ui.until(lambda: ui.draft() == "the local failure prompt", why="the failed prompt back in the draft") await receiver.stop() @@ -123,9 +123,9 @@ async def test_wire_sends_queue_in_order_while_compose_stays_open(workspace): gate = threading.Event() send = Observing.send_user_message - def held(observed, target, body): + def held(observed, target, body, files): gate.wait(10) - return send(observed, target, body) + return send(observed, target, body, files) async with workspace.app(size=(110, 32)) as ui: await ui.open_direct("peer", actor=workspace.viewer) diff --git a/tests/textual/test_message_file_attach.py b/tests/textual/test_message_file_attach.py new file mode 100644 index 000000000..e43919e02 --- /dev/null +++ b/tests/textual/test_message_file_attach.py @@ -0,0 +1,112 @@ +"""A message prompt attaches files from its terminal pickers, sends them with the message, +and gets them back with the draft when a send does not land.""" + +import hashlib + +from agent_comms.messaging.channel_targets import ChannelTarget, CommandTarget + +from toad.client.conversation import ChannelConversation +from toad.client.history import ChannelDestination +from toad.client.message_files import PathSearchFilePicker +from toad.conversation_submission import OrdinaryInputSubmission +from toad.preferences import SETTINGS +from toad.screens.file_picker import FileBrowserScreen, PathSearchScreen +from toad.widgets.comms_chat import CommsChatView +from toad.widgets.prompt import FileChip, MessageTextArea, PromptTextArea +from toad.prompt_actions import AttachFileAction + + +def chips(prompt) -> list[str]: + return [str(chip.label) for chip in prompt.query(FileChip)] + + +async def test_a_picked_file_rides_the_message_and_comes_back_with_a_refused_draft(workspace): + workspace.thread("peer", "team") + workspace.comms.messaging.send("peer", "#team", "CURRENT_HISTORY") + async with workspace.app() as ui: + await ui.open_channel("#team", actor=workspace.viewer) + await ui.until_shows("CURRENT_HISTORY") + view = ui.app.selected_session.query_one(CommsChatView) + prompt = view.prompt + content = b"line one\nline two\n" + picked = prompt.project_path.path / "notes.txt" + picked.write_bytes(content) + + await prompt.attach_path(picked) + await ui.until(lambda: chips(prompt) == ["notes.txt · 18 B ✕"], why="the attached file's chip") + + prompt.prompt_text_area.focus() + await ui.type("with a file") + await ui.press("enter") + + def sent_messages(): + return [message for message in workspace.comms.views.history_page(CommandTarget.named("#team")).messages + if message.body == "with a file"] + + await ui.until(sent_messages, why="the message on the bus") + (sent,) = sent_messages() + (stored,) = workspace.comms.messaging.files.of((sent.reference,)) + assert (stored.name, stored.size, stored.sha256) == ( + "notes.txt", len(content), hashlib.sha256(content).hexdigest()) + assert chips(prompt) == [] and prompt.attached_files == () + + await prompt.attach_path(picked) + files = prompt.take_files() + stale = ChannelDestination(workspace.roots.wire, ChannelConversation(ChannelTarget("#elsewhere")), view) + await view.submissions.start_bus(OrdinaryInputSubmission("REFUSED_DRAFT", files), stale).prepare() + await ui.until(lambda: prompt.text == "REFUSED_DRAFT", why="the refused text back in the composer") + assert prompt.attached_files == files + await ui.until(lambda: chips(prompt) == ["notes.txt · 18 B ✕"], why="the refused file's chip") + + prompt.query_one(FileChip).press() + await ui.until(lambda: chips(prompt) == [], why="the chip took its file off") + assert prompt.attached_files == () + + +async def test_the_attach_key_opens_the_named_picker_and_escape_attaches_nothing(workspace): + workspace.thread("peer", "team") + workspace.comms.messaging.send("peer", "#team", "CURRENT_HISTORY") + async with workspace.app() as ui: + app, _pilot = ui.textual + await ui.open_channel("#team", actor=workspace.viewer) + await ui.until_shows("CURRENT_HISTORY") + prompt = app.selected_session.query_one(CommsChatView).prompt + (prompt.project_path.path / "picked-by-search.txt").write_bytes(b"found") + + prompt.prompt_text_area.focus() + await ui.press("ctrl+t") + await ui.until(lambda: isinstance(app.screen, FileBrowserScreen), why="the file browser") + await ui.press("escape") + await ui.until(lambda: not isinstance(app.screen, FileBrowserScreen), why="escape closed it") + assert prompt.attached_files == () + + app.saved_settings.set(SETTINGS.ui.input.file_picker, PathSearchFilePicker) + prompt.prompt_text_area.focus() + await ui.press("ctrl+t") + await ui.until(lambda: isinstance(app.screen, PathSearchScreen), why="the path search") + await ui.until(lambda: "picked-by-search.txt" in app.screen.spellings, why="the project listing") + await ui.type("picked-by") + await ui.press("enter") + await ui.until(lambda: chips(prompt) == ["picked-by-search.txt · 5 B ✕"], why="the searched file's chip") + + +def test_only_a_message_editor_answers_the_attach_key(): + assert AttachFileAction in MessageTextArea.declared_actions() + assert AttachFileAction not in PromptTextArea.declared_actions() + + +async def test_a_sent_file_draws_as_a_chip_under_its_message_in_the_terminal(workspace): + """The history row a reader sees: the message's text, then a chip per file it carries.""" + workspace.thread("peer", "team") + async with workspace.app() as ui: + await ui.open_channel("#team", actor=workspace.viewer) + view = ui.app.selected_session.query_one(CommsChatView) + prompt = view.prompt + picked = prompt.project_path.path / "shot.png" + picked.write_bytes(b"\x89PNG" + bytes(2044)) + await prompt.attach_path(picked) + prompt.prompt_text_area.focus() + await ui.type("a screenshot") + await ui.press("enter") + await ui.until(lambda: ui.shows("a screenshot") and ui.shows("[shot.png · 2.0 KB]"), + why="the sent message and its file's chip under it") diff --git a/tests/textual/test_message_file_chips.py b/tests/textual/test_message_file_chips.py new file mode 100644 index 000000000..f3ad80fb9 --- /dev/null +++ b/tests/textual/test_message_file_chips.py @@ -0,0 +1,71 @@ +"""A page's messages draw their files as chips, from one read beside the page, and a chip's +link fetches the very bytes that were sent.""" + +import base64 +import os +from pathlib import Path + +from agent_comms.comms import Comms +from agent_comms.execution.launch_root import LaunchRoot +from agent_comms.messaging.channel_targets import ChannelTarget +from agent_comms.messaging.historical_views import UnobservedConversation +from agent_comms.messaging.message_files import FileUpload +from agent_comms.projects.checkouts import Checkout +from agent_comms.registry.threads import NativeThread +from agent_comms.transport.request import CoreAccess +from agent_comms.transport.tool_caller import HumanCaller +from agent_comms.transport.tool_request import ServedRequest +from agent_comms.transport.tools.served import ServedCommsUserSendTool +from support.agents import AppAgents +from support.roots import installed_wire + +from toad.client.conversation import ChannelConversation +from toad.client.history import WireSource +from toad.client.history_preparation import OLDER_END, PageRequest +from toad.client.line_blocks import Spans +from toad.client.message_file_links import MessageFileLink +from toad.message_rows import IrcMessageStyle +from toad.widgets.wire_history import WireFragment + + +class ServedCore(CoreAccess): + """This test's Core, every request served in process as the human caller, the caller the + observation child serves a UI's reads as.""" + + def __init__(self, comms: Comms) -> None: + self.comms = comms + self.called: list[str] = [] + + def call(self, request): + self.called.append(request.declared_name) + return ServedRequest.serving(request).apply_without_thread(self.comms, HumanCaller()) + + +def test_a_page_draws_each_message_s_files_and_a_chip_fetches_the_sent_bytes(tmp_path: Path, monkeypatch) -> None: + monkeypatch.setenv("XDG_DATA_HOME", str(tmp_path / "data")) + comms = installed_wire(tmp_path / "wire") + comms.registry.declare(NativeThread("peer", frozenset({"team"}), Checkout(tmp_path))) + content = os.urandom(3000) + HumanCaller().invoke(ServedCommsUserSendTool(target="#team", body="the trace", files=( + FileUpload("trace.bin", "application/octet-stream", base64.b64encode(content).decode()),)), comms) + HumanCaller().invoke(ServedCommsUserSendTool(target="#team", body="no file"), comms) + project = Checkout(tmp_path) + core = ServedCore(comms) + source = WireSource(comms.root, ChannelConversation(ChannelTarget("#team")), project, + comms.messaging.user_identity(project).name, IrcMessageStyle(), + access=AppAgents.unrun(project.path, LaunchRoot.from_path(comms.root)).app.coordination_access, + core=core, fragments=WireFragment) + through = source.observe(UnobservedConversation()).through + prepared = source.read(PageRequest(OLDER_END, through), through) + assert core.called.count("comms_message_files") == 1 + with_file, without = prepared.fragments + chips = [span for block in with_file.blocks(with_file.shown) if isinstance(block, Spans) + for span in block.spans if isinstance(span.link, MessageFileLink)] + assert [span.text for span in chips] == ["[trace.bin · 2.9 KB]"] + assert not [span for block in without.blocks(without.shown) if isinstance(block, Spans) + for span in block.spans if isinstance(span.link, MessageFileLink)] + (link,) = (span.link for span in chips) + assert (link.conversation, link.root) == ("#team", str(comms.root)) + kept = link.fetched(core) + assert kept.read_bytes() == content + assert kept.is_relative_to(tmp_path / "data") and kept.name == "trace.bin" diff --git a/tests/textual/test_message_filter_restyle.py b/tests/textual/test_message_filter_restyle.py index 86706c673..9c4649461 100644 --- a/tests/textual/test_message_filter_restyle.py +++ b/tests/textual/test_message_filter_restyle.py @@ -12,6 +12,8 @@ ) from support.journals import assistant_entry, user_entry, write_journal +from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES +from toad.client.message_file_links import NO_PAGE_FILES from toad.message_rows import MarkdownMessageStyle from toad.transcript_preparation import TranscriptPageRenderTask from toad.client.history_state import PreparedReplacementViewportRequest @@ -75,7 +77,7 @@ async def test_filtering_saved_history_paints_an_older_projection_and_returns_to workspace.comms.registry.require("source"), session_file=RegistrySessionLocation.from_path(str(replacement), workspace.comms.root))) page = await ui.app.preparation.run_thread( lambda: workspace.comms.transcripts.capture_page_read("source").read()) - prepared = await TranscriptPageRenderTask(page).prepared(ui.app.preparation) + prepared = await TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES).prepared(ui.app.preparation) # A replacement is source work, as the app's own commit publication runs it: wait until # that operation releases the source, then reserve, so the two reads never overlap. await ui.until(lambda: history.state.accepts_source_work, why="the app's source operation to finish") diff --git a/tests/textual/test_remote_transcript_publications.py b/tests/textual/test_remote_transcript_publications.py index d48b9b416..99536e2dc 100644 --- a/tests/textual/test_remote_transcript_publications.py +++ b/tests/textual/test_remote_transcript_publications.py @@ -3,6 +3,8 @@ from agent_comms.nominal.field_codec import FieldCodec from agent_comms.transcript.transcripts import TranscriptPage +from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES +from toad.client.message_file_links import NO_PAGE_FILES from toad.transcript_preparation import TranscriptPageRenderTask @@ -16,7 +18,7 @@ def test_remote_page_renders_without_the_server_bus(workspace): absent = database.with_suffix(".off-device") database.rename(absent) try: - prepared = TranscriptPageRenderTask(remote).execute() + prepared = TranscriptPageRenderTask(remote, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES).execute() finally: absent.rename(database) assert len(prepared.outgoing_coverage) == 1 diff --git a/tests/textual/test_saved_history_coverage.py b/tests/textual/test_saved_history_coverage.py index e83807684..0e5f7c946 100644 --- a/tests/textual/test_saved_history_coverage.py +++ b/tests/textual/test_saved_history_coverage.py @@ -11,6 +11,8 @@ from agent_comms.registry.threads import NativeThread from support import journals +from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES +from toad.client.message_file_links import NO_PAGE_FILES from toad.screens.main import MainScreen from toad.client.tab_identity.kinds import MailboxTab from toad.widgets.agent_response import AgentResponse @@ -84,13 +86,13 @@ async def test_read_receipts_reuse_original_prepared_suppliers(workspace): async with workspace.app(size=(110, 35)) as ui: page, _, _ = await saved_history(workspace, ui) runtime = ui.app.preparation - first = await TranscriptPageRenderTask(page).prepared(runtime) + first = await TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES).prepared(runtime) page.snapshot.records_cut.mark_read( workspace.comms.bus.reads, "saved", page.snapshot.runtime.read_key, page.after.offset) current = workspace.comms.transcripts.capture_page_read("saved").read() assert page.snapshot != current.snapshot assert page.snapshot.same_content(current.snapshot) - second = await TranscriptPageRenderTask(current).prepared(runtime) + second = await TranscriptPageRenderTask(current, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES).prepared(runtime) assert first is not second assert first.page is page assert second.page is current diff --git a/tests/textual/test_stale_route_writes.py b/tests/textual/test_stale_route_writes.py index e09b90080..6a84661a1 100644 --- a/tests/textual/test_stale_route_writes.py +++ b/tests/textual/test_stale_route_writes.py @@ -20,7 +20,7 @@ async def test_a_view_that_changed_before_its_send_left_keeps_the_draft_editable before = messaging.bus.bus.revision() # Built for a conversation the view no longer shows: the view moved on before the send left. stale = ChannelDestination(workspace.roots.wire, ChannelConversation(ChannelTarget("#elsewhere")), view) - await view.submissions.start_bus(OrdinaryInputSubmission("STALE_DRAFT"), stale).prepare() + await view.submissions.start_bus(OrdinaryInputSubmission("STALE_DRAFT", ()), stale).prepare() await ui.until(lambda: view.prompt.text == "STALE_DRAFT", why="the refused draft back in the composer") assert not view.submissions.delivering assert not view.prompt.prompt_text_area.disabled diff --git a/tests/textual/test_transcript_presentation_fencing.py b/tests/textual/test_transcript_presentation_fencing.py index 59a46c985..96657d678 100644 --- a/tests/textual/test_transcript_presentation_fencing.py +++ b/tests/textual/test_transcript_presentation_fencing.py @@ -10,6 +10,8 @@ from agent_comms.registry.threads import NativeThread from support.journals import assistant_entry, write_journal +from toad.widgets.transcript_fragments import TEXTUAL_FRAGMENT_CLASSES +from toad.client.message_file_links import NO_PAGE_FILES from toad.transcript_publication import ( CLEAN_PUBLICATION, COMMIT_DEMAND, @@ -394,7 +396,7 @@ async def test_a_closed_history_accepts_a_prepared_replacement_without_a_storage () if empty else (assistant_entry("PREPARED_REPLACEMENT", id="new"),))) workspace.comms.registry.declare(replace(thread, session_file=RegistrySessionLocation.from_path(str(new), workspace.comms.root))) page = workspace.comms.transcripts.capture_page_read("source").read() - prepared = await TranscriptPageRenderTask(page).prepared(ui.app.preparation) + prepared = await TranscriptPageRenderTask(page, TEXTUAL_FRAGMENT_CLASSES, NO_PAGE_FILES).prepared(ui.app.preparation) assert await history.show_newest(PreparedReplacementViewportRequest( LatestPosition.hold(history.window), prepared)) await ui.step() diff --git a/tools/qt_drive.py b/tools/qt_drive.py index 8023bd24e..d319ab7e4 100644 --- a/tools/qt_drive.py +++ b/tools/qt_drive.py @@ -15,6 +15,7 @@ from __future__ import annotations import asyncio +import hashlib import time import os import sys @@ -25,10 +26,11 @@ from typing import ClassVar import qasync -from PySide6.QtCore import QEvent, QObject, QPoint, QPointF, Qt, QTimer -from PySide6.QtGui import QInputMethodEvent, QKeyEvent, QKeySequence, QMouseEvent, QTextDocument +from PySide6.QtCore import QEvent, QObject, QPoint, QPointF, QRect, QTimer, Qt +from PySide6.QtGui import QContextMenuEvent, QImage, QInputMethodEvent, QKeyEvent, QKeySequence, QMouseEvent, QPainter, QTextDocument + from PySide6.QtTest import QTest -from PySide6.QtWidgets import QAbstractButton, QAbstractScrollArea, QDialog, QLabel, QScroller, QStyleOptionViewItem, QApplication, QPushButton, QWidget +from PySide6.QtWidgets import QAbstractButton, QAbstractScrollArea, QDialog, QFileDialog, QLabel, QLineEdit, QScroller, QStyleOptionViewItem, QApplication, QPushButton, QWidget from agent_comms.messaging.channel_targets import ChannelTarget from agent_comms.registry.thread_presentation import LiveThreadOwnerBinding @@ -80,19 +82,21 @@ from toad.qt.opening_view import QtOpeningSessionView from toad.thread_sidebar_declarations import panel_specs_for_view from toad.client.bus_submission import ConversationSubmission -from toad.setting_choices import CenterColumnAlignment +from toad.setting_choices import CenterColumnAlignment, HiddenScrollbar, TransientScrollbar from toad.message_rows import IrcMessageStyle, MarkdownMessageStyle from toad.client.input_modality import KeyboardInput, TouchInput from toad.qt.float_layer import QtFloatLayer, QtNoticeCard -from toad.qt.menu import QtMenuRow +from toad.qt.menu import QtMenu, QtMenuRow from toad.qt.activity import ChannelActivity from toad import paths from toad.qt.thread_comms_panel import QtThreadCommsPanel +from toad.qt.channel_sort import QtChannelListSort from toad.qt.window import ConversationRow, ConversationView, MenuActionRun, QtToadApp from toad.conversation_turn import LocalTurnBinding from toad.core.events import TextAgentFail, UnsupportedResumeAgentFail from toad.live_output import ResponseStream, ThoughtStream from toad.qt.history import HistoryRows +from toad.qt.overlay_scrollbar import OverlayScrollBar from toad.qt.live_turn import QtLiveFragment PROJECT = Path(__file__).resolve().parents[1] @@ -306,6 +310,52 @@ async def run(self) -> None: "saved:", [row.identity.mode_name for row in saved], flush=True) +class TabStripScenario(Scenario): + summary = ("at phone width, open COUNT (6) sidebar threads as tabs: the strip scrolls instead of " + "squeezing them, no tab's title is cut short, the selected tab is in view, and a " + "right-click (a held finger) on a tab offers Move left/right and Close tab; Move right " + "moves it past its neighbour") + + async def run(self) -> None: + app = self.app + app.resize(412, 915) + await self.listed() + count = int(os.environ.get("COUNT", "6")) + await self.expand("#comms") + await until(lambda: len(self.thread_rows()) >= 3, "three sidebar threads") + rows = self.thread_rows()[:count] + for row in rows: + # Its own tab, found by title: the live sidebar can list a thread whose tab is + # already open, so a running count of tabs is not what one open adds. + app.open_row(row) + name = row.target.name + await until(lambda: any(name in app.tabs.tabText(index) for index in range(app.tabs.count())), + f"a tab for {name}") + assert app.tabs.count() >= min(count, len(rows)) - 1, f"only {app.tabs.count()} tabs opened" + await asyncio.sleep(1.5) + strip, tabs = app.tab_strip, app.tabs + titles = [tabs.tabText(index) for index in range(tabs.count())] + cut = [title for index, title in enumerate(titles) + if tabs.fontMetrics().horizontalAdvance(title) > tabs.tabRect(index).width()] + scrolls = strip.horizontalScrollBar().maximum() + current = tabs.tabRect(tabs.currentIndex()) + seen = strip.viewport().rect().translated(strip.horizontalScrollBar().value(), 0) + self.report("1-many-tabs", tabs=len(titles), strip_scrolls=scrolls, cut=cut[:3], + selected_in_view=seen.contains(current.center())) + assert scrolls > 0, "the tabs were squeezed into the strip instead of scrolling" + assert not cut, f"tab titles cut short: {cut[:3]}" + assert seen.contains(current.center()), "the selected tab is scrolled out of view" + first = tabs.tabData(0) + tabs.customContextMenuRequested.emit(tabs.tabRect(0).center()) + await asyncio.sleep(0.4) + offered = [row.label.text() for row in app.menu_panel.findChildren(QtMenuRow)] + self.report("2-tab-menu", offered=offered) + assert offered == ["Move right", "Close tab"], f"first tab offered {offered}" + next(row for row in app.menu_panel.findChildren(QtMenuRow) if row.label.text() == "Move right")._fire() + await until(lambda: tabs.tabData(1) == first, "the first tab moved right") + self.report("3-moved", order=[tabs.tabData(index) for index in range(tabs.count())][:4]) + + class ActivityScenario(Scenario): summary = "open a working thread's DM (else the first thread); print its info row and activity" @@ -790,6 +840,63 @@ async def run(self) -> None: self.report("20-end", **self.rows()) +class HeaderRuleScenario(Scenario): + summary = ("open #comms at phone size; for each message header shown, report where its rule's " + "pixels start and end against the row width and the title's length; then the " + "conversation scrollbar Transient: report the viewport width and the bar over the " + "text while scrolling and gone once still; then Hidden") + + def rule_extent(self, header, width: int) -> tuple[int, int]: + """The leftmost and rightmost drawn pixel of the header laid out at ``width``.""" + window = self.page.messages + document = window.rows.header_document(header, window.font(), width) + image = QImage(width, max(1, int(document.size().height())), QImage.Format.Format_ARGB32) + image.fill(Qt.GlobalColor.transparent) + painter = QPainter(image) + document.drawContents(painter) + painter.end() + drawn = [x for x in range(width) if any(image.pixelColor(x, y).alpha() for y in range(image.height()))] + return (drawn[0], drawn[-1]) if drawn else (-1, -1) + + def bar(self) -> dict[str, object]: + window = self.page.messages + overlay = window.findChild(OverlayScrollBar) + return dict(viewport=window.viewport().width(), bar_beside=window.verticalScrollBar().width(), + overlay=None if overlay is None else (overlay.isVisible(), round(overlay.opacity.opacity(), 2), + overlay.geometry().getRect())) + + async def run(self) -> None: + app = self.app + app.resize(412, 915) + await self.open_named(os.environ.get("HEADER_TARGET", "#comms")) + window = self.page.messages + width = window.viewport().width() - 8 + headers = [fragment.sticky_header(len(fragment.line_blocks) - 1, 1)[-1] + for fragment in window.model_rows.fragments + if fragment.line_blocks and fragment.sticky_header(len(fragment.line_blocks) - 1, 1)] + seen: dict[int, tuple] = {} + for header in headers: + title = "".join(span.text for span in header.spans) + seen.setdefault(len(title), (title, *self.rule_extent(header, width))) + for length, (title, left, right) in sorted(seen.items())[:8]: + print(f"rule: width={width} title_len={length} left={left} right={right} title={title[:40]!r}", + flush=True) + self.report("1-headers", headers=len(headers), **self.bar()) + app.saved_settings.set(SETTINGS.ui.scrollbar, TransientScrollbar) + await asyncio.sleep(0.5) + self.report("2-transient-idle", **self.bar()) + window.scroll_y = max(0, window.scroll_y - window.viewport().height()) + await asyncio.sleep(0.15) + self.report("3-transient-scrolling", **self.bar()) + await asyncio.sleep(2.0) + self.report("4-transient-still", **self.bar()) + app.saved_settings.set(SETTINGS.ui.scrollbar, HiddenScrollbar) + await asyncio.sleep(0.5) + window.scroll_y = max(0, window.scroll_y - window.viewport().height()) + await asyncio.sleep(0.15) + self.report("5-hidden-scrolling", **self.bar()) + + class StickyScenario(Scenario): summary = ("open STICKY_TARGET (#comms by default); scroll into the middle of its tallest message " "and report the pinned header over the top row, then put that message's header on the " @@ -973,6 +1080,132 @@ async def run(self) -> None: assert not clipped, f"{len(clipped)} menu lines drawn shorter than their font: {clipped[:3]}" +def changed_outside(before: QImage, after: QImage, kept: QRect) -> int: + """How many pixels differ between two grabs of one window outside `kept`.""" + before, after = (image.convertToFormat(QImage.Format.Format_RGB32) for image in (before, after)) + width, height = before.width(), before.height() + if (width, height) != (after.width(), after.height()): + return width * height + left, right = max(0, kept.left()), min(width, kept.right() + 1) + changed = 0 + for y in range(height): + a, b = before.constScanLine(y).tobytes(), after.constScanLine(y).tobytes() + if kept.top() <= y <= kept.bottom(): + pairs = ((a[:left * 4], b[:left * 4]), (a[right * 4:], b[right * 4:])) + else: + pairs = ((a, b),) + for one, other in pairs: + if one != other: + changed += sum(1 for x in range(0, len(one), 4) if one[x:x + 4] != other[x:x + 4]) + return changed + + +class MenuFloatScenario(Scenario): + summary = ("every pop-up menu floats: the page keeps every pixel outside it, an outside tap " + "closes it without touching what was under it, Escape closes it; the composer's " + "buttons are the input box's one-line height; then quit with the menu open") + + async def floats(self, opening, step: str, faults: list[str]) -> None: + """Open a menu, check nothing underneath moved, screenshot it.""" + app, menu = self.app, self.app.menu_panel + menu.hide() + await asyncio.sleep(0.4) + for attempt in range(3): # a live row may repaint between the grabs; retry such a race + before = app.grab().toImage() + opening() + QApplication.processEvents() + after = app.grab().toImage() + moved = changed_outside(before, after, menu.geometry()) + if moved == 0 or attempt == 2: + break + menu.hide() + await asyncio.sleep(0.6) + self.report(step, shown=menu.isVisible(), geometry=menu.geometry().getRect(), moved_pixels=moved, + rows=[row.label.text() for row in menu.findChildren(QtMenuRow)]) + if not menu.isVisible(): + faults.append(f"{step}: the menu did not open") + if moved: + faults.append(f"{step}: opening it changed {moved} pixels outside it") + if menu.parentWidget() is not app or menu.isWindow() or app.navigation.layout().indexOf(menu) != -1: + faults.append(f"{step}: the menu is not a free child of the window") + + async def run(self) -> None: + app = self.app + app.resize(int(os.environ.get("WIDTH", 412)), 915) + await self.open_named("#comms") + faults: list[str] = [] + menu = app.menu_panel + # 1. The application menu, from its button. + await self.floats(app.menu.click, "1-app-menu", faults) + # 2. A tap outside closes it and the tap goes nowhere else. + tabs_before, page_before = self.tabs(), self.page + history = self.page.messages.viewport() + # Any point of the history the floating menu does not cover: it floats over part of it. + outside = next(QPoint(x, y) for y in (12, history.height() // 2, history.height() - 12) + for x in (history.width() - 12, 12) + if not menu.geometry().contains(history.mapTo(app, QPoint(x, y)))) + QTest.mouseClick(history, Qt.MouseButton.LeftButton, Qt.KeyboardModifier.NoModifier, outside) + await asyncio.sleep(0.3) + self.report("2-outside-tap", shown=menu.isVisible()) + if menu.isVisible(): + faults.append("2: an outside tap left the menu open") + if self.tabs() != tabs_before or self.page is not page_before: + faults.append("2: the outside tap also acted on what was under it") + # 3. Escape closes it. + app.menu.click() + await asyncio.sleep(0.3) + QTest.keyClick(app, Qt.Key.Key_Escape) + await asyncio.sleep(0.3) + self.report("3-escape", shown=menu.isVisible()) + if menu.isVisible(): + faults.append("3: Escape left the menu open") + # 4. A message's menu, where the row was pressed (a right-click; a held finger asks the same). + messages = self.page.messages + point = QPoint(messages.viewport().width() // 2, messages.viewport().height() // 2) + + def right_click() -> None: + QApplication.sendEvent(messages.viewport(), QContextMenuEvent( + QContextMenuEvent.Reason.Mouse, point, messages.viewport().mapToGlobal(point))) + await self.floats(right_click, "4-message-menu", faults) + pressed = messages.viewport().mapTo(app, point) + if menu.isVisible() and not (menu.geometry().top() == pressed.y() + 1 or menu.geometry().bottom() == pressed.y() - 1): + faults.append(f"4: the message menu is not at the pressed row ({pressed.y()} vs {menu.geometry().getRect()})") + menu.hide() + # 5. A sort choice's menu, under its button. + sorts = app.findChildren(QtChannelListSort) + if sorts and not app.sidebar.isVisible(): + app.show_sidebar() + await asyncio.sleep(0.5) + if sorts and sorts[0].isVisible(): + await self.floats(sorts[0].click, "5-sort-menu", faults) + under = QtMenu.anchor_of(sorts[0]) + if menu.isVisible() and under.bottom() + 1 != menu.geometry().top() and menu.geometry().bottom() + 1 != under.top(): + faults.append(f"5: the sort menu is not at its button ({under.getRect()} vs {menu.geometry().getRect()})") + menu.hide() + app.hide_sidebar() + await asyncio.sleep(0.4) + else: + print("5-sort-menu: no visible channel-list sort button here; skipped", flush=True) + # 6. The composer's buttons, one line then three. + composer = self.page.composer + for lines in (1, 3): + await self.type("\n".join(["composer line"] * lines)) + await asyncio.sleep(0.3) + frame, send = composer.frame.geometry(), composer.send_button.geometry() + self.report(f"6-composer-{lines}-lines", box=frame.getRect(), send=send.getRect()) + if send.height() != composer.one_line_box_height(): + faults.append(f"6: with {lines} line(s) the send button is {send.height()}px, the one-line box {composer.one_line_box_height()}px") + if send.bottom() != frame.bottom(): + faults.append(f"6: with {lines} line(s) the send button's bottom {send.bottom()} is not the box's {frame.bottom()}") + composer.editor.clear() + # 7. Quit with the menu open: the drive's own close ends the app with it showing. + app.menu.click() + await asyncio.sleep(0.3) + self.report("7-open-at-quit", shown=menu.isVisible()) + if faults: + raise AssertionError("menu_float faults:\n" + "\n".join(faults)) + + class FeedbackScenario(Scenario): summary = ("the reader's feedback at a Pixel 8a's width: the wait a slow thread open shows in place, " "#comms' first page loading, an operation's wait floating over the page, a notice that " @@ -1133,6 +1366,12 @@ async def press_bound_keys(self, label: str) -> None: combination = sequence[0] QTest.keyClick(target, combination.key(), combination.keyboardModifiers()) await asyncio.sleep(0.3) + # A key whose action opens a dialog (Attach's file picker) leaves it open; it + # is closed unanswered and the window active again, so the next key reaches the + # view; offscreen Qt hands activation back to no window when a dialog closes. + self.close_dialogs() + self.app.activateWindow() + await asyncio.sleep(0.1) ran = [type(action).__name__ for action in keys.ran] results.append(f"{where}:{spelling}->{'+'.join(ran) or 'none available'}") if ran != expected: @@ -1158,6 +1397,7 @@ async def run(self) -> None: self.report(f"2-settings-page-{index}", page=dialog.tabs.tabText(index)) self.close_dialogs() await self.step("3-channel", lambda: self.open_named("#comms")) + await self.step("3-channel-attach", self.open_attach) await self.press_bound_keys("3-channel-keys") await self.noticeless("3-channel-activity", lambda: self.tap_activity("3-channel-activity")) await self.step("4-thread", lambda: self.open_named("scratch-live-pipeline")) @@ -1166,6 +1406,7 @@ async def run(self) -> None: await self.step("4-thread-from-channel", lambda: self.open_named("#comms")) await self.noticeless("4-thread-tapped-twice", lambda: self.open_twice(thread)) await self.step("4-thread-details", self.open_details) + await self.step("4-thread-attach", self.open_attach) await self.press_bound_keys("4-thread-keys") # The reader keys (ctrl+d, ctrl+u, G) are declared for the vim modes only: press every # key again in vim normal mode, as a person choosing that keyboard mode would. @@ -1177,6 +1418,18 @@ async def run(self) -> None: if self.faults: raise AssertionError(f"{len(self.faults)} fault(s) on the shipped app:\n" + "\n".join(self.faults)) + async def open_attach(self) -> None: + """The composer's Attach opens its file picker; it is closed without picking.""" + self.page.composer.attach_button.click() + await asyncio.sleep(0.5) + dialogs = [dialog for dialog in self.app.findChildren(QFileDialog) if dialog.isVisible()] + if len(dialogs) != 1: + raise AssertionError(f"Attach opened {len(dialogs)} file dialogs") + dialogs[0].reject() + await asyncio.sleep(0.3) + if self.page.composer.attached_files: + raise AssertionError("closing the picker attached a file") + async def apply_offered_themes(self) -> None: """The app started with its saved theme (shipped_run saves one Qt does not offer); then every scheme Qt offers is applied in turn through the `ui.theme` effect a start applies. @@ -1206,6 +1459,40 @@ async def open_details(self) -> None: await asyncio.sleep(1.0) +class AttachScenario(Scenario): + summary = ("attach OUTDIR/attached.bin to a message to the scratch thread scratch-live-pipeline " + "through the composer's file dialog and send it: run it on a scratch bus only") + + async def run(self) -> None: + await self.listed() + await self.open_named("scratch-live-pipeline") + composer = self.page.composer + picked = self.out / "attached.bin" + picked.write_bytes(os.urandom(300_000) + b"the end of the attached file\n") + composer.attach_button.click() + await until(lambda: any(dialog.isVisible() for dialog in self.app.findChildren(QFileDialog)), + "file dialog", tenths=50) + (dialog,) = [dialog for dialog in self.app.findChildren(QFileDialog) if dialog.isVisible()] + # As a person picks it: the path typed into the dialog's file name field, then Enter. + # (QFileDialog.selectFile leaves a field that has focus as it is.) + (field,) = dialog.findChildren(QLineEdit) + field.setFocus() + field.setText(str(picked)) + QTest.keyClick(field, Qt.Key.Key_Return) + await asyncio.sleep(1.0) + await until(lambda: composer.attached_files, "attached file", tenths=50) + chips = [chip.text() for chip in composer.attached_row.findChildren(QPushButton)] + self.report("1-attached", chips=chips, sha256=hashlib.sha256(picked.read_bytes()).hexdigest()) + await self.type("a file from the Qt composer") + composer.submit() + await until(lambda: not composer.attached_files and not composer.submissions.delivering, + "sent", tenths=150) + await asyncio.sleep(2.0) + self.report("2-sent", chips=[chip.text() for chip in composer.attached_row.findChildren(QPushButton) + if chip.isVisible()], + failure=composer.failure.text() if composer.failure.isVisible() else "") + + class MissingChannelDestination(ChannelDestination): """A destination its view still shows whose channel Core does not know: its read-only check fails before anything leaves, the way a refused send does.""" @@ -1243,7 +1530,7 @@ async def run(self) -> None: missing = ChannelConversation(ChannelTarget("#composer-drive-missing")) destination = MissingChannelDestination(app.source().root, missing, view) app.run_operation(ConversationSubmission, - lambda: view.submissions.start_bus(TypedText("this send must fail"), destination).prepare()) + lambda: view.submissions.start_bus(TypedText("this send must fail", ()), destination).prepare()) await until(lambda: composer.failure.isVisible(), "failure", tenths=100) await asyncio.sleep(0.5) self.report("6-failed", failure=composer.failure.text(), draft=composer.text, @@ -3111,6 +3398,10 @@ async def run(self) -> None: # 1. A sidebar row that isn't open: the hold offers its menu, and letting go doesn't open it. other = await self.found(lambda row: isinstance(row, ConversationRow) and row.target.name != "#comms" and not tree.visualItemRect(row).isEmpty(), "another listed row") + # Pressed where it is drawn: a row below the visible list would put the finger on + # whatever is drawn there instead. + tree.scrollToItem(other) + await asyncio.sleep(0.3) held_on: list[str] = [] FingerPress.install(QApplication.instance()).long_pressed.connect( lambda press: held_on.append(type(press.target).__name__)) @@ -3136,12 +3427,9 @@ async def run(self) -> None: history.block_menu_requested.connect(lambda fragment: requested.append(type(fragment).__name__)) await self.held_menu("2-history-row", viewport, on_row) print(f"2-history-row: block menu asked for {requested}", flush=True) - # 3. A tab offers no menu on right-click: a hold there stays the tab's press. + # 3. A tab offers the tab menu (moving, closing) on a right-click, and so on a hold. tabs = app.tabs - await self.touch(tabs, tabs.tabRect(0).center(), 1100) - self.report("3-tab", menu=self.menu()) - self.expect("3-tab", not self.menu(), f"a tab offered a menu {self.menu()} a right-click doesn't") - self.close_menu() + await self.held_menu("3-tab", tabs, tabs.tabRect(0).center()) # 4. A hold that then drags scrolls the history, and opens no menu. bar = history.verticalScrollBar() bar.setValue(bar.maximum()) diff --git a/tools/qt_inherit_ratchet.py b/tools/qt_inherit_ratchet.py index c702b3a2a..a1b4d44df 100644 --- a/tools/qt_inherit_ratchet.py +++ b/tools/qt_inherit_ratchet.py @@ -101,6 +101,8 @@ 'toad.qt.conversation.ComposerPlaceholder': "a Qt composer's place held before it is built: Qt builds a view's widgets in " "its constructor, so a composer built there holds the view's first frame (and on Android races the first frame's EGL " "setup); Textual composes a screen's widgets and mounts them after its first frame, so it has no placeholder to hold", + 'toad.qt.overlay_scrollbar.OverlayScrollBar': "a QScrollBar floated over a QAbstractScrollArea's viewport, mirroring the area's hidden bar: a terminal cannot draw over its text, so Textual shows `Scrollbar.size` only; the shared fact is `Scrollbar.overlay`", + 'toad.qt.overlay_scrollbar._ViewportGeometry': "an event filter keeping that overlay over the viewport on QEvent.Resize/Move; Textual has no floating child to place", 'toad.qt.widget_animation.WidgetAnimation': "a clock target's life bound to its Qt widget's C++ lifetime (destroyed signal, shiboken validity); a Textual widget is a Python object the clock can always ask", 'toad.qt.history.ArrivalFade': "a burst's rows painted at the ARRIVAL cascade's frame as QPainter opacity; Textual paints the same frame into its strips (TranscriptHistory.arrivals_began), and both read the cascade from toad.frame_presentation", 'toad.qt.action_shortcuts.BoundAction': "a live QShortcut and the action it answers: Qt fires neither of two shortcuts sharing a key (activatedAmbiguously), so ActionKeys keeps each to apply the declared binding order; Textual walks its binding chain itself",