Best Python code snippet using fMBT_python
refresh.py
Source:refresh.py  
1from toga_cocoa.libs import (2    SEL,3    NSClipView,4    NSEvent,5    NSEventPhaseEnded,6    NSLayoutAttributeCenterX,7    NSLayoutAttributeCenterY,8    NSLayoutAttributeHeight,9    NSLayoutAttributeNotAnAttribute,10    NSLayoutAttributeTop,11    NSLayoutAttributeWidth,12    NSLayoutConstraint,13    NSLayoutRelationEqual,14    NSMakePoint,15    NSMakeRect,16    NSNotificationCenter,17    NSPoint,18    NSProgressIndicator,19    NSProgressIndicatorSpinningStyle,20    NSRect,21    NSScrollElasticityAllowed,22    NSScrollView,23    NSView,24    NSViewBoundsDidChangeNotification,25    ObjCInstance,26    core_graphics,27    kCGScrollEventUnitLine,28    objc_method,29    send_super30)31HEADER_HEIGHT = 45.032class RefreshableClipView(NSClipView):33    @objc_method34    def constrainScrollPoint_(self, proposedNewOrigin: NSPoint) -> NSPoint:35        constrained = send_super(36            __class__, self, 'constrainScrollPoint:', proposedNewOrigin,37            restype=NSPoint, argtypes=[NSPoint]38        )39        if self.superview and self.superview.refreshTriggered:40            return NSMakePoint(41                constrained.x,42                max(proposedNewOrigin.y, -self.superview.refreshView.frame.size.height)43            )44        return constrained45    @objc_method46    def documentRect(self) -> NSRect:47        rect = send_super(__class__, self, 'documentRect', restype=NSRect, argtypes=[])48        if self.superview and self.superview.refreshTriggered:49            return NSMakeRect(50                rect.origin.x, rect.origin.y - self.superview.refreshView.frame.size.height,51                rect.size.width, rect.size.height + self.superview.refreshView.frame.size.height52            )53        return rect54class RefreshableScrollView(NSScrollView):55    # Create Header View56    @objc_method57    def viewDidMoveToWindow(self) -> None:58        self.refreshTriggered = False59        self.isRefreshing = False60        self.refreshView = None61        self.refreshIndicator = None62        self.createRefreshView()63    @objc_method64    def createContentView(self):65        superClipView = ObjCInstance(send_super(__class__, self, 'contentView'))66        if not isinstance(superClipView, RefreshableClipView):67            # create new clipview68            documentView = superClipView.documentView69            clipView = RefreshableClipView.alloc().initWithFrame(superClipView.frame)70            clipView.documentView = documentView71            clipView.copiesOnScroll = False72            clipView.drawsBackground = False73            self.setContentView(clipView)74            superClipView = ObjCInstance(send_super(__class__, self, 'contentView'))75        return superClipView76    @objc_method77    def createRefreshView(self) -> None:78        # delete old stuff if any79        if self.refreshView:80            self.refreshView.removeFromSuperview()81            self.refreshView.release()82            self.refreshView = None83        self.verticalScrollElasticity = NSScrollElasticityAllowed84        # create new content view85        self.createContentView()86        self.contentView.postsFrameChangedNotifications = True87        self.contentView.postsBoundsChangedNotifications = True88        NSNotificationCenter.defaultCenter.addObserver(89            self,90            selector=SEL('viewBoundsChanged:'),91            name=NSViewBoundsDidChangeNotification,92            object=self.contentView,93        )94        # Create view to hold the refresh widgets refreshview95        contentRect = self.contentView.documentView.frame96        self.refreshView = NSView.alloc().init()97        self.refreshView.translatesAutoresizingMaskIntoConstraints = False98        # Create spinner99        self.refreshIndicator = NSProgressIndicator.alloc().init()100        self.refreshIndicator.style = NSProgressIndicatorSpinningStyle101        self.refreshIndicator.translatesAutoresizingMaskIntoConstraints = False102        self.refreshIndicator.displayedWhenStopped = True103        self.refreshIndicator.usesThreadedAnimation = True104        self.refreshIndicator.indeterminate = True105        self.refreshIndicator.bezeled = False106        self.refreshIndicator.sizeToFit()107        # Center the spinner in the header108        self.refreshIndicator.setFrame(109            NSMakeRect(110                self.refreshView.bounds.size.width / 2 - self.refreshIndicator.frame.size.width / 2,111                self.refreshView.bounds.size.height / 2 - self.refreshIndicator.frame.size.height / 2,112                self.refreshIndicator.frame.size.width,113                self.refreshIndicator.frame.size.height114            )115        )116        # Put everything in place117        self.refreshView.addSubview(self.refreshIndicator)118        # self.refreshView.addSubview(self.refreshArrow)119        self.contentView.addSubview(self.refreshView)120        # set layout constraints121        indicatorHCenter = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_(  # noqa: E501122            self.refreshIndicator, NSLayoutAttributeCenterX,123            NSLayoutRelationEqual,124            self.refreshView, NSLayoutAttributeCenterX,125            1.0, 0,126        )127        self.refreshView.addConstraint(indicatorHCenter)128        indicatorVCenter = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_(  # noqa: E501129            self.refreshIndicator, NSLayoutAttributeCenterY,130            NSLayoutRelationEqual,131            self.refreshView, NSLayoutAttributeCenterY,132            1.0, 0,133        )134        self.refreshView.addConstraint(indicatorVCenter)135        refreshWidth = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_(  # noqa: E501136            self.refreshView, NSLayoutAttributeWidth,137            NSLayoutRelationEqual,138            self.contentView, NSLayoutAttributeWidth,139            1.0, 0,140        )141        self.contentView.addConstraint(refreshWidth)142        refreshHeight = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_(  # noqa: E501143            self.refreshView, NSLayoutAttributeHeight,144            NSLayoutRelationEqual,145            None, NSLayoutAttributeNotAnAttribute,146            1.0, HEADER_HEIGHT,147        )148        self.contentView.addConstraint(refreshHeight)149        refreshHeight = NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant_(  # noqa: E501150            self.refreshView, NSLayoutAttributeTop,151            NSLayoutRelationEqual,152            self.contentView, NSLayoutAttributeTop,153            1.0, -HEADER_HEIGHT,154        )155        self.contentView.addConstraint(refreshHeight)156        # Scroll to top157        self.contentView.scrollToPoint(NSMakePoint(contentRect.origin.x, 0))158        self.reflectScrolledClipView(self.contentView)159    # Detecting scroll160    @objc_method161    def scrollWheel_(self, event) -> None:162        if event.phase == NSEventPhaseEnded:163            if self.refreshTriggered and not self.isRefreshing:164                self.reload()165        send_super(__class__, self, 'scrollWheel:', event)166    @objc_method167    def viewBoundsChanged_(self, note) -> None:168        if self.isRefreshing:169            return170        if self.contentView.bounds.origin.y <= -self.refreshView.frame.size.height:171            self.refreshTriggered = True172    # Reload173    @objc_method174    def reload(self) -> None:175        """Start a reload, starting the reload spinner"""176        self.isRefreshing = True177        self.refreshIndicator.startAnimation(self)178        self.interface.on_refresh(self.interface)179    @objc_method180    def finishedLoading(self):181        """Invoke to mark the end of a reload, stopping and hiding the reload spinner"""182        self.isRefreshing = False183        self.refreshTriggered = False184        self.refreshIndicator.stopAnimation(self)185        self.detailedlist.reloadData()186        # Force a scroll event to make the scroll hide the reload187        cgEvent = core_graphics.CGEventCreateScrollWheelEvent(None, kCGScrollEventUnitLine, 2, 1, 0)188        scrollEvent = NSEvent.eventWithCGEvent(cgEvent)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
