Skip to content

Review: adaptive window resizing and image centering on zoom#531

Closed
armm77 wants to merge 1 commit into
trunkmaster:masterfrom
armm77:review
Closed

Review: adaptive window resizing and image centering on zoom#531
armm77 wants to merge 1 commit into
trunkmaster:masterfrom
armm77:review

Conversation

@armm77

@armm77 armm77 commented May 15, 2026

Copy link
Copy Markdown
Collaborator

When the user changes the zoom level (via the scale popup or the new
Cmd+= / Cmd+- keyboard shortcuts), the window now resizes automatically
to follow the image:

  • Zoom out: window shrinks to fit the scaled image, minimum 200×200 px.
  • Zoom in: window grows with the image up to a hard cap of 70% of the screen width and 70% of the screen height. Beyond that cap zooming continues normally and scroll bars become active.
  • The window center is kept fixed on every resize; if the result would go off-screen it is clamped to the visible area.
  • The same 70% rule is applied when an image is first opened, replacing the previous ad-hoc margin calculations.

When the scaled image is smaller than the scroll view's visible area the
image view is expanded to fill that area so that NSImageAlignCenter keeps
the image horizontally and vertically centered instead of drifting to a
corner.

Zoom In / Zoom Out are wired to the existing Display menu items at
launch (Cmd+= and Cmd+-). The items are disabled automatically when no
image window is open.

  When the user changes the zoom level (via the scale popup or the new
  Cmd+= / Cmd+- keyboard shortcuts), the window now resizes automatically
  to follow the image:

  - Zoom out: window shrinks to fit the scaled image, minimum 200×200 px.
  - Zoom in: window grows with the image up to a hard cap of 70% of the
    screen width and 70% of the screen height. Beyond that cap zooming
    continues normally and scroll bars become active.
  - The window center is kept fixed on every resize; if the result would
    go off-screen it is clamped to the visible area.
  - The same 70% rule is applied when an image is first opened, replacing
    the previous ad-hoc margin calculations.

  When the scaled image is smaller than the scroll view's visible area the
  image view is expanded to fill that area so that NSImageAlignCenter keeps
  the image horizontally and vertically centered instead of drifting to a
  corner.

  Zoom In / Zoom Out are wired to the existing Display menu items at
  launch (Cmd+= and Cmd+-). The items are disabled automatically when no
  image window is open.

@trunkmaster trunkmaster left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Nice work. Please see my comments.

Comment on lines +40 to +58
NSMenu *mainMenu = [NSApp mainMenu];
NSMenuItem *displayItem = (NSMenuItem *)[mainMenu itemWithTitle:@"Display"];
if (displayItem) {
NSMenu *displayMenu = [displayItem submenu];
NSMenuItem *zoomInItem = (NSMenuItem *)[displayMenu itemWithTitle:@"Zoom In"];
NSMenuItem *zoomOutItem = (NSMenuItem *)[displayMenu itemWithTitle:@"Zoom Out"];
if (zoomInItem) {
[zoomInItem setTarget:self];
[zoomInItem setAction:@selector(zoomIn:)];
[zoomInItem setKeyEquivalent:@"="];
[zoomInItem setKeyEquivalentModifierMask:NSCommandKeyMask];
}
if (zoomOutItem) {
[zoomOutItem setTarget:self];
[zoomOutItem setAction:@selector(zoomOut:)];
[zoomOutItem setKeyEquivalent:@"-"];
[zoomOutItem setKeyEquivalentModifierMask:NSCommandKeyMask];
}
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

It's better to change .gorm. No need to add any code.


NSRect newFrame = [NSWindow frameRectForContentRect:NSMakeRect(0, 0, contentSize.width, contentSize.height)
styleMask:[_window styleMask]];

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

You need to get ScrollView frame first. Here you got window frame based on image size excluding scroll view controls. You may check it if open image smaller then screen size (no scroller knobs visible), zoom it out (90%, scroller knobs appears), zoom it in (100%, scroller knobs still visible).

Comment on lines +348 to +351
if (newFrame.origin.x < sf.origin.x) newFrame.origin.x = sf.origin.x;
if (newFrame.origin.y < sf.origin.y) newFrame.origin.y = sf.origin.y;
if (NSMaxX(newFrame) > NSMaxX(sf)) newFrame.origin.x = NSMaxX(sf) - newFrame.size.width;
if (NSMaxY(newFrame) > NSMaxY(sf)) newFrame.origin.y = NSMaxY(sf) - newFrame.size.height;

@trunkmaster trunkmaster May 19, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I've noticed window center drifting if you try to zoom in large image. Please check if this code is correct.

@armm77

armm77 commented May 20, 2026

Copy link
Copy Markdown
Collaborator Author

@trunkmaster, Thanks for checking, I'll make another change.

@armm77 armm77 closed this May 20, 2026
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