Skip to content

Commit

Permalink
Merge pull request #1 from martindufort/macos-appkit-with-nsui
Browse files Browse the repository at this point in the history
  • Loading branch information
martindufort authored Dec 30, 2023
2 parents d5f7cbf + a358b8d commit 2815171
Show file tree
Hide file tree
Showing 11 changed files with 538 additions and 168 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CI

on:
push:
branches:
- main
paths-ignore:
- 'README.md'
- 'CODE_OF_CONDUCT.md'
- 'CONTRIBUTING.md'
- 'LICENSE'
- 'SECURITY.md'
- 'ios.yml'
pull_request:
branches:
- main

jobs:
test:
name: Test
runs-on: macOS-latest
strategy:
matrix:
destination:
- "platform=macOS"
# - "platform=macOS,variant=Mac Catalyst"
- "platform=iOS Simulator,name=iPhone 11"

steps:
- uses: actions/checkout@v4
- name: Install XCBeautify
run: brew install xcbeautify
- name: Show buildable schemes
run: xcodebuild -list
- name: Test Each Platform
run: set -o pipefail && xcodebuild -scheme PillboxView -destination "${{ matrix.destination }}" test | xcbeautify --renderer github-actions
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

*.xcuserstate
## User settings
xcuserdata/
**/.swiftpm
**/.DS_Store
*.xcuserstate
13 changes: 9 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import PackageDescription
let package = Package(
name: "PillboxView",
platforms: [
.iOS(.v13), .macCatalyst(.v13), .tvOS(.v13)
.iOS(.v13),
.macCatalyst(.v13),
.tvOS(.v13),
.macOS(.v11)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
Expand All @@ -15,15 +18,17 @@ let package = Package(
targets: ["PillboxView"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
// Add NSUI to support macOS compatibility
.package(url: "https://github.com/mattmassicotte/nsui", branch: "main"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "PillboxView",
dependencies: [],
dependencies: [
.product(name: "NSUI", package: "nsui")
],
path: "Sources"),
.testTarget(
name: "PillboxViewTests",
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ PillboxView is a small pill that presents a view on an asynchronous on-going tas

PillboxView is available through [Swift Package Manager](https://www.swift.org/package-manager).

## Project Dependency
In order to support both native `AppKit` and `UIKit`, PillboxView is leveraging the `NSUI` project.
[NSUI](https://github.com/mattmassicotte/nsui) allows a single codebase to support both platforms with less #if pragma statements

The package description file `package.swift` defines that dependency. You should be aware of that information before including
`PillboxView` into your own project

## Example

- Display a title message
Expand Down Expand Up @@ -41,7 +48,10 @@ class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

pill.show(title: "Refreshing Data", vcView: self.view)
pill.showTask(message: "Refreshing Data", vcView: self.view)

// Update the task message while the task is ongoing
pill.updateTask(message: "Still refreshing data...")

// some time later...
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
Expand Down
Binary file removed Sources/.DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions Sources/PillboxView/Conformance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import Foundation

/*
// Deriving PillView from NSUIView (aka UIView) provides Hashable conformance automatically
// The code below in unneeded
extension PillView: Hashable {
public static func == (lhs: PillView, rhs: PillView) -> Bool {
lhs.pillView == rhs.pillView
Expand Down Expand Up @@ -34,3 +37,4 @@ extension PillView: Hashable {
hasher.combine(vcView)
}
}
*/
48 changes: 48 additions & 0 deletions Sources/PillboxView/NSUI+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// NSUI+Extensions.swift
//
//

import CoreGraphics
import Foundation

// ---
import NSUI

internal extension NSUIView {

/// Return the origin `UXPoint` for a view which needs to be centered horizontally within its superview
/// This is performed with frame math only and it is set at init type.
/// Changing the window size will not recalculate the origin.
func originForCenter(inRelationTo parentView: NSUIView) -> CGPoint {
guard
parentView.frame != CGRect.zero
else {
fatalError("Your parentView must have a non-zero size")
}

let midPoint = CGRectGetMidX(parentView.frame)

// Now get the half the width of our view and substract than from the midPoint
let selfMidPoint = self.frame.width / 2

let newOriginX = (midPoint - selfMidPoint).rounded()
let newOriginY = self.frame.origin.y
return CGPoint(x: newOriginX, y: newOriginY)
}
}

#if canImport(AppKit)
import AppKit
#endif

internal extension NSUIColor {
#if os(macOS)
@available(OSX 10.14, *)
static var isLight: Bool { NSApp.effectiveAppearance.name == NSAppearance.Name.aqua }

@available(OSX 10.14, *)
static var isDark: Bool { NSApp.effectiveAppearance.name == NSAppearance.Name.darkAqua }
#endif
}

23 changes: 23 additions & 0 deletions Sources/PillboxView/PillAnimation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// PillPosition.swift
//
//
// Created by Martin Dufort on 2023-12-14.
//


/// Defines the direction from which the ``PillboxView/PillView`` will appear and also the offset from the edge of
/// the containing view to where it will rest. This allows stop coordinates to be different if showing from bottom versus
/// showing from top.
///
/// It also removes the need to inform the ``PillboxView/PillView`` about the presence of a navigation controller.
import CoreGraphics

public struct PillAnimation {
enum AnimationDirection {
case fromTop
case fromBottom
}
var direction: AnimationDirection
var offsetFromEdge: CGFloat
}
46 changes: 37 additions & 9 deletions Sources/PillboxView/PillColors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,54 @@
// PillboxView
//
// Created by Jacob Trentini on 1/2/22.
//

import UIKit
import NSUI
#if canImport(AppKit)
import AppKit
#endif

internal extension UIColor {
static var PillboxBackgroundColor: UIColor {
return UIColor { (traits) -> UIColor in
internal extension NSUIColor {
#if os(macOS)
@available(OSX 10.14, *)
static var isLightModeOn: Bool { NSApp.effectiveAppearance.name == NSAppearance.Name.aqua }

@available(OSX 10.14, *)
static var isDarkModeOn: Bool { NSApp.effectiveAppearance.name == NSAppearance.Name.darkAqua }
#endif

static var PillboxBackgroundColor: NSUIColor {
#if os(macOS)
if NSUIColor.isLightModeOn {
return NSUIColor.white
}
else {
return NSUIColor.lightGray
}
#else
return NSUIColor { (traits) -> NSUIColor in
// Return one of two colors depending on light or dark mode

#if targetEnvironment(macCatalyst)
return traits.userInterfaceStyle == .light ?
.white : UIColor(red: 0.09, green: 0.09, blue: 0.09, alpha: 1)
.white : NSUIColor(red: 0.09, green: 0.09, blue: 0.09, alpha: 1)
#else
return traits.userInterfaceStyle == .light ?
.white : UIColor(red: 0.12941176, green: 0.12156863, blue: 0.10588235, alpha: 1)
.white : NSUIColor(red: 0.12941176, green: 0.12156863, blue: 0.10588235, alpha: 1)
#endif
}
#endif
}

static var PillboxTitleColor: UIColor {
return UIColor(displayP3Red: 0.54117647, green: 0.5372549, blue: 0.55294118, alpha: 1)
static var PillboxTitleColor: NSUIColor {
#if os(macOS)
if NSUIColor.isLightModeOn {
return NSUIColor.darkGray
}
else {
return NSUIColor.white
}
#else
return NSUIColor(displayP3Red: 0.54117647, green: 0.5372549, blue: 0.55294118, alpha: 1)
#endif
}
}
Loading

0 comments on commit 2815171

Please sign in to comment.