Skip to content

Centered scroll view with zoomin #144

@typoman

Description

@typoman

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions