Giter Site home page Giter Site logo

intercept's Introduction

Intercept for SwiftUI

Intercept is a collection of extensions for SwiftUI that enable subtle yet important features.

It’s meant to be used in conjuction with Introspect for SwiftUI, hence the name.

Installation

.package(url: "https://github.com/freysie/intercept.git", branch: "main"),
.product(name: "Intercept", package: "intercept"),

Overview

List Clip Inset for macOS

The listClipInset() modifier insets the list view’s clip view by the specified insets.

objc_allocateClassPair() is used under the hood for this.

List(selection: $selection) {
  // …
  // …
  // …
}
.listClipInset(EdgeInsets(top: -5, leading: 0, bottom: 0, trailing: 0))

Due to an unfortunate side effect which makes the inset jump when the list first appears, it’s recommended you hide your list, or the window containing it, for a single run loop iteration.

Alternatives Considered

Why not use a negative padding? That could sort of work, but it messes up keyboard navigation.

List Double-Clicking for macOS

The modifier onListDoubleClick() intercepts the underlying tabel view’s doubleAction target-action invocation for a macOS List.

InterceptionProxy is used under the hood for this.

List(selection: $selection) {
  // …
  // …
  // …
}
.onListDoubleClick { sender in
  print(sender.selectedRow)
}

Note: This appears to have been introduced in macOS 13 with the contextAction() modifier.

Alternatives Considered

Why not use onTapGesture(count: 2)? It doesn’t reach all the way to the edge of the rows.

List Selection Emphasis for macOS

The listSelectionEmphasized() modifier sets NSTableRowView’s isEmphasized to true.

objc_allocateClassPair() is used under the hood for this.

List(selection: $selection) {
  // …
  // …
  // …
}
.listSelectionEmphasized()

Alternatives Considered

Why not simply draw an emphasized selection yourself? That doesn’t work 100% as there’s no isSelected environment value in SwiftUI, and the selection bindings in lists are not updated while the mouse is down.

Interception Proxy

InterceptionProxy is like a meddler-in-the-middle attack for Swift and Objective-C.

It’s an NSProxy subclass which allows you to override the behavior of existing methods.

Since NSInvocation and its related APIs are unavailable in Swift, the proxy is implemented in Objective-C.

- (void)forwardInvocation:(NSInvocation *)invocation {
  NSAssert(self.middleDelegate != self.originalDelegate, @"delegates should be unique");
  
  if ([self.middleDelegate respondsToSelector:invocation.selector]) {
    [invocation invokeWithTarget:self.middleDelegate];
  } else if ([self.originalDelegate respondsToSelector:invocation.selector]) {
    [invocation invokeWithTarget:self.originalDelegate];
  }
}

- (nullable NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
  id result = [self.middleDelegate methodSignatureForSelector:sel];

  if (!result) {
    result = [self.originalDelegate methodSignatureForSelector:sel];
  }

  return result;
}

intercept's People

Contributors

freysie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.