Skip to content

Fix half-pixel offset on imported images#3942

Open
Keavon wants to merge 6 commits intomasterfrom
fix-image-import-offset
Open

Fix half-pixel offset on imported images#3942
Keavon wants to merge 6 commits intomasterfrom
fix-image-import-offset

Conversation

@Keavon
Copy link
Member

@Keavon Keavon commented Mar 23, 2026

Fixes #3670

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a visual bug where imported images would appear with a half-pixel offset, particularly when added through a non-mouse-driven workflow like a file open dialog. The changes refine the coordinate transformation logic to ensure that images are always precisely aligned within the viewport, improving the accuracy of image placement.

Highlights

  • Image Import Alignment: Refactored the logic for positioning imported images, specifically addressing a half-pixel offset that occurred when images were imported without a specific mouse location.
  • Viewport Centering Calculation: Modified the calculation of center_in_viewport_layerspace and center_in_viewport to ensure precise alignment, especially when importing images via the file-open flow where mouse input is None.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request fixes a half-pixel offset issue when importing images without a specific mouse position by using DAffine2::IDENTITY to default to the document origin. The change is applied to both PasteImage and PasteSvg message handlers. The fix appears correct. My only feedback is regarding code duplication between these two handlers, which could be refactored into a helper function to improve maintainability.

Comment on lines +721 to +725
let center_in_viewport = mouse.map_or(DAffine2::IDENTITY, |pos| {
let viewport_location: DVec2 = pos.into();
let document_to_viewport = self.navigation_handler.calculate_offset_transform(viewport.center_in_viewport_space().into(), &self.document_ptz);
DAffine2::from_translation(document_to_viewport.inverse().transform_point2(viewport_location - viewport.offset().into_dvec2()))
});
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This logic for calculating center_in_viewport is identical to the logic for center_in_viewport_layerspace in the PasteImage message handler above (lines 667-671). To improve maintainability and reduce code duplication, consider extracting this logic into a private helper function within DocumentMessageHandler.

For example:

impl DocumentMessageHandler {
    // ...

    fn get_document_space_transform_from_mouse(&self, mouse: Option<ViewportPosition>, viewport: &ViewportMessageHandler) -> DAffine2 {
        mouse.map_or(DAffine2::IDENTITY, |pos| {
            let viewport_location: DVec2 = pos.into();
            let document_to_viewport = self.navigation_handler.calculate_offset_transform(viewport.center_in_viewport_space().into(), &self.document_ptz);
            DAffine2::from_translation(document_to_viewport.inverse().transform_point2(viewport_location - viewport.offset().into_dvec2()))
        })
    }

    // ...
}

Then you could call self.get_document_space_transform_from_mouse(mouse, viewport) in both PasteImage and PasteSvg message handlers.

@github-actions github-actions bot temporarily deployed to graphite-dev (Preview) March 23, 2026 08:15 Inactive
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

2 issues found across 1 file

Confidence score: 3/5

  • There is a concrete regression risk in editor/src/messages/portfolio/document/document_message_handler.rs: both PasteImage and PasteSvg now fall back mouse: None to document origin, which can place pasted content far from the user’s current view.
  • This is medium severity but high confidence (6/10, 8/10), so the PR likely remains workable yet carries noticeable user-facing paste behavior risk in existing documents.
  • Pay close attention to editor/src/messages/portfolio/document/document_message_handler.rs - verify mouse: None paste placement logic so clipboard pastes stay visible in non-origin views.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="editor/src/messages/portfolio/document/document_message_handler.rs">

<violation number="1" location="editor/src/messages/portfolio/document/document_message_handler.rs:667">
P2: `PasteImage` now treats `mouse: None` as document origin for all flows, which regresses clipboard paste placement in existing documents.</violation>

<violation number="2" location="editor/src/messages/portfolio/document/document_message_handler.rs:721">
P2: `PasteSvg` applies the same `mouse: None -> origin` fallback, causing clipboard SVG paste to appear off-screen in non-origin views.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

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.

Opening an image to create a document has a half-pixel offset

1 participant