Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WindowsStartupLocation throwing exception #258

Merged
merged 9 commits into from
Jan 17, 2025
Merged

Conversation

tomlm
Copy link
Collaborator

@tomlm tomlm commented Jan 15, 2025

Bug: #253

When specificing WindowStartupLocation an exception is thrown saying "Rendering system is not intialized"

Avalonia expects there to be a IScreenImpl available so it can calculate things like window position. This delta implements a single
virtual screen for Avalonia to use to calculate.

It ignores request to resize and move the console window, although we could do something like that using platform APIs on some platforms (like windows).

Copy link
Contributor

coderabbitai bot commented Jan 15, 2025

📝 Walkthrough

Walkthrough

The pull request introduces modifications to the ConsoleWindow class in the Consolonia.Core infrastructure and adds a new ConsoloniaScreen class. The changes focus on updating the Resize and Move methods within the ConsoleWindow class, with comments suggesting potential future considerations for window resizing and movement. Additionally, a new feature check has been implemented in the TryGetFeature method, which returns an instance of ConsoloniaScreen. The ConsoloniaScreen class encapsulates screen management functionality, including properties for screen count and available screens, and methods for retrieving screen details based on various criteria. Furthermore, the ControlsListView XAML has been modified to include a WindowStartupLocation attribute that centers the window on the screen when opened.

Possibly related PRs

  • Enable alternate screen buffer #141: The changes in the ConsoloniaLifetime class regarding console buffer management may relate to the ConsoleWindow class modifications, as both involve handling console features and behaviors.
  • Dispatching input alltogether #256: The modifications to the ConsoleWindow class for input handling are directly related to the changes in the ConsoleBase class, which also focuses on enhancing input processing and dispatching within the console application.

Suggested labels

enhancement

Suggested reviewers

  • jinek

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8959933 and 1ee6a70.

📒 Files selected for processing (1)
  • src/Consolonia.Core/Infrastructure/ConsoleWindow.cs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Consolonia.Core/Infrastructure/ConsoleWindow.cs
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/Consolonia.Core/Infrastructure/ConsoloniaScreen.cs (1)

21-21: Consider implementing the Changed event handler.

The Changed event is not utilized. Consider implementing screen size change notifications when the console is resized.

-        public Action Changed { get; set; }
+        private Action _changed;
+        public Action Changed
+        {
+            get => _changed;
+            set
+            {
+                _changed = value;
+                _console.Resized += () =>
+                {
+                    UpdateScreenSize();
+                    _changed?.Invoke();
+                };
+            }
+        }
src/Consolonia.Core/Infrastructure/ConsoleWindow.cs (1)

219-219: Consider implementing window resize and move operations.

The TODO comments suggest potential future improvements. Consider implementing these operations using platform-specific APIs where available:

  • Windows: Use SetConsoleWindowInfo and MoveWindow from kernel32.dll
  • Unix: Various terminal escape sequences might be available

Also applies to: 225-225

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 76bfa63 and b255ce9.

📒 Files selected for processing (3)
  • src/Consolonia.Core/Infrastructure/ConsoleWindow.cs (2 hunks)
  • src/Consolonia.Core/Infrastructure/ConsoloniaScreen.cs (1 hunks)
  • src/Consolonia.Gallery/View/ControlsListView.axaml (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (2)
src/Consolonia.Core/Infrastructure/ConsoloniaScreen.cs (1)

25-31: LGTM: Screen lookup methods are implemented correctly.

The implementation consistently returns the single available screen, which is appropriate for a console environment.

src/Consolonia.Gallery/View/ControlsListView.axaml (1)

7-7: LGTM: Window centering is now properly supported.

The WindowStartupLocation attribute is now supported by the new ConsoloniaScreen implementation.

src/Consolonia.Core/Infrastructure/ConsoloniaScreen.cs Outdated Show resolved Hide resolved
src/Consolonia.Core/Infrastructure/ConsoleWindow.cs Outdated Show resolved Hide resolved
@tomlm tomlm requested a review from jinek January 15, 2025 18:46
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (4)
src/Consolonia.Core/Infrastructure/ConsoloniaScreen.cs (4)

18-18: Derive ScreenCount from _screens array.

Instead of hardcoding the screen count, derive it from the array length for better maintainability.

-public int ScreenCount => 1;
+public int ScreenCount => _screens.Length;

29-47: Add XML documentation explaining the single-screen design.

The implementation always returns a single screen regardless of the input parameters. This behavior should be documented to explain the design decision.

+/// <summary>
+/// Implements a single-screen environment for console applications.
+/// All screen-related queries return the same screen instance as console
+/// applications operate in a single-screen context.
+/// </summary>
 public class ConsoloniaScreen : IScreenImpl

29-47: Add bounds checking for screen array access.

Although we currently have a single screen, it's good practice to add bounds checking to prevent potential issues if the implementation changes.

 public Screen ScreenFromPoint(PixelPoint point)
 {
+    if (_screens.Length == 0)
+        throw new InvalidOperationException("No screens available.");
     return _screens[0];
 }

9-48: Consider future extensibility for platform-specific implementations.

The current implementation provides a basic screen implementation that should resolve the initialization exception. However, given the PR objectives mentioning potential platform-specific handling (e.g., Windows), consider:

  1. Making the class abstract or adding virtual members to allow platform-specific overrides
  2. Adding hooks for console resize events to update screen dimensions
  3. Implementing platform detection to enable enhanced functionality on supported platforms
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5443479 and da6215e.

📒 Files selected for processing (2)
  • src/Consolonia.Core/Infrastructure/ConsoleWindow.cs (2 hunks)
  • src/Consolonia.Core/Infrastructure/ConsoloniaScreen.cs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Consolonia.Core/Infrastructure/ConsoleWindow.cs
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (2)
src/Consolonia.Core/Infrastructure/ConsoloniaScreen.cs (2)

15-15: Review the scaling factor of 0.0.

The screen is initialized with a scaling factor of 0.0, which might affect UI rendering. Consider using a more appropriate value (typically 1.0 for standard DPI).


13-16: Consider using actual console dimensions.

The screen dimensions should adapt to the actual console size rather than using fixed dimensions.

@tomlm tomlm enabled auto-merge (squash) January 15, 2025 19:10
@@ -4,6 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what does it do eventually, what is the effect?

Copy link
Collaborator Author

@tomlm tomlm Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't blow up with my change.

I've been experimenting with adding .Console platform projects to existing avalonia projects. (A UI is shared between Desktop, Console, Android, etc)

WindowStartupLocation is a hint to the platform. On Desktop it positions the window. It's ignored by Android, and ignored by Console app (after my change). More importantly, anything which is expecting there to be a screen definition will get one and not blow up.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, understood, then add //todo: raise ConsoloniaNotSupportedException
please.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where would I leave it? It's Avalonia that throws because there the environment isn't set up correctly. It expects there to be Screens defined by the platform so it can do screen based calculations.
This PR implements the missing platform Screens functionality.

The other place is that it generates a request to Resize() and Move() on the window.
The current code for Resize looks like this:

  public void Resize(Size clientSize, WindowResizeReason reason = WindowResizeReason.Application)
  {
      // todo: can we deny resizing? TODO We could consider resizing the console window
  }

and I changed Move to be same

  public void Move(PixelPoint point)
  {
      // ignore move request, TODO We could consider moving the console window
  }

@tomlm tomlm merged commit 237d5ac into main Jan 17, 2025
2 checks passed
@tomlm tomlm deleted the tomlm/fixWindowLocation branch January 17, 2025 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants