Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 18, 2025

Fix canvas shift when switching from Snake back to Flex layout mode

The issue occurs when switching from Flex → Snake → Flex modes. The structure shifts position on the canvas.

Root Cause:

  • When switching to SnakeMode, applySnakeLayout() physically moves monomers to new positions starting from (20, 20)
  • When switching back to FlexMode, the original code called recalculateCanvasMatrix() which was using the retained snakeLayoutMatrix
  • This caused additional unwanted repositioning of the monomers

Solution:

  • Remove the recalculateCanvasMatrix() call from FlexMode.initialize()
  • FlexMode doesn't use matrix-based layout, so this call is unnecessary
  • Monomers now stay in their current positions when switching modes

Implementation:

  • Modified FlexMode.initialize() in /packages/ketcher-core/src/application/editor/modes/FlexMode.ts
  • Removed the line: command.merge(editor.drawingEntitiesManager.recalculateCanvasMatrix());
  • Changed to merge modelChanges directly: command.merge(modelChanges);

Testing & Validation:

  • Build ketcher-core successfully
  • Run tests - all 305 tests passed (12 skipped, 4 test suites skipped)
  • Addressed feedback from @NikitaKuzminn about structure moving to (0,0)
Original prompt

This section details on the original issue you should resolve

<issue_title>Switching from Flex to Snake and back shifts canvas from top left corner to the bottom right corner</issue_title>
<issue_description>Steps to Reproduce

  1. Go to Macromolecules
  2. Change Sequence layout mode to Flex layout mode (dropdown on the top panel - Macromolecules modes switcher in documentation) on the empty canvas (to clean canvas, press Ctrl+Del)
  3. Copy the structure in KET format from this file to the clean canvas (focus on canvas and press Ctrl+V or via Open -> Paste from clipboard) : Switching from Flex to Snake and back shits canvas from top left corner to the center.ketImage
  4. Switch to Snake layout mode (dropdown on the top panel - Macromolecules modes switcher in documentation)
  5. Switch back to Flex layout mode (dropdown on the top panel - Macromolecules modes switcher in documentation)
    Actual behavior
    Structure shifted from top left corner to the bottom right corner
    Image

Expected behavior
Structure remain in place
Image

Versions

  • Ketcher Version 3.3.0-rc.3 Build at 2025-04-10; 10:48:12
  • Indigo Version 1.31.0-rc.1.0-g859747ec7-wasm32-wasm-clang-19.0.0
  • Chrome Version 135.0.7049.85 (Official Build) (64-bit)
  • Win10

Related issues: #6435</issue_description>

Comments on the Issue (you are @copilot in this section)

@SefaBilgii This issue is reproducible Image

Ketcher Version: 3.7.0-rc.1
OS: MacOs Sequoia Version 15.5
Browser : Chrome Version 138.0.7204.169
Indigo version 1.35.0-rc.1.0</comment_new>
<comment_new>@AlinaLysenko
Reproduced by agent - https://chatgpt.com/share/693c122e-9448-800e-9221-0dc755e882af</comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…shift

The issue was that when switching from Flex → Snake → Flex, the snakeLayoutMatrix
was retained and used in recalculateCanvasMatrix(), causing unwanted position shifts.
By clearing the snakeLayoutMatrix when entering FlexMode, the canvas matrix calculation
returns early and doesn't reposition the monomers.

Co-authored-by: NikitaKuzminn <87386879+NikitaKuzminn@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix canvas position shift when switching layout modes Fix canvas shift when switching from Snake back to Flex layout mode Dec 18, 2025
Copilot AI requested a review from NikitaKuzminn December 18, 2025 07:21
@NikitaKuzminn
Copy link
Collaborator

@copilot
Now the structure flies off to some random place, presumably to 0, 0 on the canvas, but I want it to stay in the same place when switching modes.

… positions

Instead of clearing snakeLayoutMatrix, we remove the recalculateCanvasMatrix()
call entirely from FlexMode.initialize() since FlexMode doesn't use matrix-based
layout. This prevents monomers from being repositioned and keeps them where they
are when switching from Snake mode back to Flex mode.

Co-authored-by: NikitaKuzminn <87386879+NikitaKuzminn@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 18, 2025

@copilot
Now the structure flies off to some random place, presumably to 0, 0 on the canvas, but I want it to stay in the same place when switching modes.

Fixed in commit 3366beb. Removed the recalculateCanvasMatrix() call from FlexMode entirely since FlexMode doesn't use matrix-based layout. This keeps monomers in their current positions when switching modes instead of moving them.

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.

Switching from Flex to Snake and back shifts canvas from top left corner to the bottom right corner

2 participants