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

Centered scroll view with zoomin #144

Open
typoman opened this issue May 24, 2021 · 0 comments
Open

Centered scroll view with zoomin #144

typoman opened this issue May 24, 2021 · 0 comments

Comments

@typoman
Copy link

typoman commented May 24, 2021

Hi all! I just figured out how to do zooming while keeping the view centered in the scroll view demo example. I don't know where the clip view should be implemented, but if you want I can send a PR for the new centered zoomable scroll view. Here is the code:

from AppKit import NSView, NSColor, NSRectFill, NSScrollView, NSClipView
from vanilla import Window, ScrollView

class CenteredClipView(NSClipView):
    """
    Thanks to:
    https://stackoverflow.com/a/28154936/4100686
    """

    def constrainBoundsRect_(self, proposedBounds):
        rect = super(CenteredClipView, self).constrainBoundsRect_(proposedBounds)
        containerViewRect = self.documentView().frame()
        if (rect.size.width > containerViewRect.size.width):
            rect.origin.x = (containerViewRect.size.width - rect.size.width) / 2
        if(rect.size.height > containerViewRect.size.height):
            rect.origin.y = (containerViewRect.size.height - rect.size.height) / 2
        return rect

class DemoView(NSView):

    def drawRect_(self, rect):
        NSColor.redColor().set()
        NSRectFill(self.bounds())

class ScrollViewDemo:

    def __init__(self):
        self.w = Window((400, 400), minSize=(100, 100))
        self.view = DemoView.alloc().init()
        self.view.setFrame_(((0, 0), (300, 300)))
        self.w.scrollView = ScrollView((10, 10, -10, -10), self.view, clipView=CenteredClipView.alloc().init())
        self.w.scrollView._nsObject.setAllowsMagnification_(True)
        self.w.open()

ScrollViewDemo()
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

No branches or pull requests

1 participant