Added a notice naming the page when a source opens without it (#1233)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-25 16:38:31 +01:00
1 parent e63e70a5cf
commit deb75c8dd1
18 files changed
+447 -57

No files matched your search

@@ -0,0 +1,32 @@
namespace AIStudio.Tools.Rust;
/// <summary>
/// What became of the page a document was to be shown at, as the runtime reports it.
/// </summary>
/// <remarks>
/// The runtime names these values in PageOutcome in runtime/src/file_actions.rs. The app tells the user
/// from them why a document opened without its page, so that they know AI Studio could not do otherwise.
/// </remarks>
public enum DocumentPageOutcome
{
/// <summary>A value of a newer runtime which the app does not know.</summary>
UNKNOWN,
/// <summary>The document was handed to its program together with the page.</summary>
APPLIED,
/// <summary>No page was asked for.</summary>
NOT_REQUESTED,
/// <summary>AI Studio runs as a Flatpak, whose sandbox lets no page through to the program.</summary>
SANDBOXED,
/// <summary>The program which shows the document cannot be told a page, or the document is no PDF.</summary>
PROGRAM_TAKES_NO_PAGE,
/// <summary>The program takes a page, but it could not be started with one.</summary>
PROGRAM_FAILED,
/// <summary>The document did not open at all.</summary>
DOCUMENT_NOT_OPENED
}
@@ -4,11 +4,9 @@ namespace AIStudio.Tools.Rust;
/// Says how opening a document went.
/// </summary>
/// <param name="Success">Whether the document was opened at all.</param>
/// <param name="PageApplied">
/// Whether the document was handed to its program together with the page. False means it opens on
/// its first page: no page was asked for, the system uses a program which cannot be told one, or
/// starting that program failed. None of these is an error, so this belongs in the log rather than
/// in front of the user, who is told the page by the source itself.
/// <param name="PageOutcome">
/// What became of the page the document was to be shown at. A page which did not arrive is no error,
/// since the document opens either way, but the user hears why, see SourceDocumentLocationExtensions.
/// </param>
/// <param name="Issue">Why the document could not be opened, or an empty text when it was.</param>
public readonly record struct OpenDocumentResponse(bool Success, bool PageApplied, string Issue);
public readonly record struct OpenDocumentResponse(bool Success, DocumentPageOutcome PageOutcome, string Issue);
@@ -0,0 +1,20 @@
namespace AIStudio.Tools.Rust;
public static class RuntimeInfoResponseExtensions
{
/// <param name="runtimeInfo">What the runtime tells about itself.</param>
extension(RuntimeInfoResponse runtimeInfo)
{
/// <summary>
/// Whether a document can be opened at a page at all.
/// </summary>
/// <remarks>
/// Never inside a Flatpak: its sandbox lets AI Studio start no program itself, and the desktop
/// portal, which opens the document instead, knows no pages, see PageOutcome in
/// runtime/src/file_actions.rs. Everywhere else, whether the page arrives depends on the
/// program, which the runtime learns only while it opens the document.
/// </remarks>
/// <value>False inside a Flatpak.</value>
public bool CanOpenDocumentsAtPage => runtimeInfo.LinuxPackageType is not LinuxPackageType.FLATPAK;
}
}
@@ -177,11 +177,11 @@ public sealed partial class RustService
/// </summary>
/// <remarks>
/// The page is best effort and never decides whether this succeeded. Which programs can be
/// told a page is the runtime's business, and it says afterwards whether it managed to.
/// told a page is the runtime's business, and it says afterwards what became of the page.
/// </remarks>
/// <param name="path">The document to open.</param>
/// <param name="pageNumber">The page to show, counted from one, or null when there is none.</param>
/// <returns>Whether the document was opened, whether the page was applied, and what went wrong.</returns>
/// <returns>Whether the document was opened, what became of the page, and what went wrong.</returns>
public async Task<OpenDocumentResponse> TryOpenDocumentInSystemViewer(string path, int? pageNumber)
{
HttpResponseMessage result;
@@ -192,12 +192,12 @@ public sealed partial class RustService
catch (HttpRequestException e)
{
this.logger!.LogWarning(e, "Failed to reach the Rust runtime document endpoint.");
return new OpenDocumentResponse(false, false, TB("The runtime document endpoint is not available."));
return new OpenDocumentResponse(false, DocumentPageOutcome.DOCUMENT_NOT_OPENED, TB("The runtime document endpoint is not available."));
}
catch (TaskCanceledException e)
{
this.logger!.LogWarning(e, "Timed out while reaching the Rust runtime document endpoint.");
return new OpenDocumentResponse(false, false, TB("The runtime document endpoint is not available."));
return new OpenDocumentResponse(false, DocumentPageOutcome.DOCUMENT_NOT_OPENED, TB("The runtime document endpoint is not available."));
}
try
@@ -205,28 +205,24 @@ public sealed partial class RustService
if (!result.IsSuccessStatusCode)
{
this.logger!.LogWarning("Failed to open a document through the Rust runtime: '{StatusCode}'", result.StatusCode);
return new OpenDocumentResponse(false, false, string.Format(TB("The runtime document endpoint returned '{0}'."), result.StatusCode));
return new OpenDocumentResponse(false, DocumentPageOutcome.DOCUMENT_NOT_OPENED, string.Format(TB("The runtime document endpoint returned '{0}'."), result.StatusCode));
}
var response = await result.Content.ReadFromJsonAsync<OpenDocumentResponse>(JSON_OPTIONS);
if (response.Success)
{
//
// A page which was asked for but not applied is noted here and nowhere else: the
// document is open, and the source the user clicked names the page anyway.
//
if (pageNumber is > 0 && !response.PageApplied)
this.logger!.LogInformation("Opened a document without the requested page {PageNumber}, because the system uses a program which cannot be told one.", pageNumber);
if (pageNumber is > 0 && response.PageOutcome is not DocumentPageOutcome.APPLIED)
this.logger!.LogInformation("Opened a document without the requested page {PageNumber}: {PageOutcome}.", pageNumber, response.PageOutcome);
return response;
}
return new OpenDocumentResponse(false, false, string.IsNullOrWhiteSpace(response.Issue) ? TB("The runtime document endpoint failed without details.") : response.Issue);
return new OpenDocumentResponse(false, DocumentPageOutcome.DOCUMENT_NOT_OPENED, string.IsNullOrWhiteSpace(response.Issue) ? TB("The runtime document endpoint failed without details.") : response.Issue);
}
catch (Exception e)
{
this.logger!.LogWarning(e, "Failed to process the Rust runtime document endpoint response.");
return new OpenDocumentResponse(false, false, TB("The runtime document endpoint failed without details."));
return new OpenDocumentResponse(false, DocumentPageOutcome.DOCUMENT_NOT_OPENED, TB("The runtime document endpoint failed without details."));
}
finally
{
@@ -17,6 +17,8 @@ public static class SourceDocumentLocationExtensions
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(SourceDocumentLocationExtensions).Namespace, nameof(SourceDocumentLocationExtensions));
private static string TA(string fallbackEN) => I18N.I.TA(fallbackEN, typeof(SourceDocumentLocationExtensions).Namespace, nameof(SourceDocumentLocationExtensions));
/// <param name="document">The document to show, and the page to show it at.</param>
extension(SourceDocumentLocation document)
{
@@ -25,8 +27,9 @@ public static class SourceDocumentLocationExtensions
/// </summary>
/// <remarks>
/// Whether the program can be sent to a page is the runtime's business, and it says afterwards
/// whether it managed to. Nothing is shown about that: the document is open, and the page is
/// named where the user asked to open it.
/// what became of the page. When the page did not arrive, the user hears where the passage is
/// and why AI Studio could not do otherwise, see GetPageNotice. Otherwise, they would take the
/// beginning of the document, or the place their program remembered, for the passage.
/// </remarks>
/// <param name="rustService">The runtime, which opens the document.</param>
public async Task OpenAsync(RustService rustService)
@@ -44,12 +47,42 @@ public static class SourceDocumentLocationExtensions
}
if (response.Success)
{
if (document.GetPageNotice(response) is { } notice)
await MessageBus.INSTANCE.SendInfo(new(AppIcons.DOCUMENT, notice));
return;
}
var issue = string.IsNullOrWhiteSpace(response.Issue) ? TB("Unknown error") : response.Issue;
await MessageBus.INSTANCE.SendError(new(AppIcons.DOCUMENT, string.Format(TB("Could not open the document: {0}"), issue)));
}
/// <summary>
/// Tells the user on which page the passage is, when the document opened without that page, and
/// why AI Studio could not open it there.
/// </summary>
/// <remarks>
/// Each reason lies outside of AI Studio, and the notice says so: the sandbox of the Flatpak, a
/// program which does not let anybody choose its page, or a program which did not start with
/// one. A reason of a newer runtime is told like a program which takes no page.
/// </remarks>
/// <param name="response">How opening the document went.</param>
/// <returns>The notice, or null when the page arrived, none was asked for, or the document did not open at all, which the user hears about as an error.</returns>
internal string? GetPageNotice(OpenDocumentResponse response)
{
if (!response.Success || document.PageNumber is not { } page || page <= 0)
return null;
return response.PageOutcome switch
{
DocumentPageOutcome.APPLIED or DocumentPageOutcome.NOT_REQUESTED or DocumentPageOutcome.DOCUMENT_NOT_OPENED => null,
DocumentPageOutcome.SANDBOXED => string.Format(TB("As a Flatpak, AI Studio cannot open the document at its page: the sandbox of the Flatpak lets no page through to the program which shows the document. The passage is on page {0}."), page),
DocumentPageOutcome.PROGRAM_FAILED => string.Format(TB("AI Studio could not open the document at its page: the program which shows the document could not be started with the page, so it opened without it. The passage is on page {0}."), page),
_ => string.Format(TA("AI Studio cannot open the document at its page: the program your system shows it with does not allow choosing a page. The passage is on page {0}."), page),
};
}
/// <summary>
/// Opens the file browser of the system and selects the document in it.
/// </summary>