Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// DropProposalPositionCalculationsTests.swift
// CodeEditModules/SplitView
//
// Created by Mateusz Bąk on 2022/07/03.
//

import Foundation
@testable import SplitEditors
import XCTest

final class DropProposalPositionCalculationsTests: XCTestCase {
func testPointInLeadingReact() {
let rect = CGRect(x: 0, y: 0, width: 10, height: 10)
let point = CGPoint(x: 1, y: 1)
let margin = CGFloat(3)

let result = calculateDropProposalPosition(
in: rect,
for: point,
margin: margin
)

XCTAssertEqual(result, .leading)
}

func testPointInTrailingReact() {
let rect = CGRect(x: 0, y: 0, width: 10, height: 10)
let point = CGPoint(x: 9, y: 9)
let margin = CGFloat(3)

let result = calculateDropProposalPosition(
in: rect,
for: point,
margin: margin
)

XCTAssertEqual(result, .trailing)
}

func testPointInTopReact() {
let rect = CGRect(x: 0, y: 0, width: 10, height: 10)
let point = CGPoint(x: 5, y: 2)
let margin = CGFloat(3)

let result = calculateDropProposalPosition(
in: rect,
for: point,
margin: margin
)

XCTAssertEqual(result, .top)
}

func testPointInBottomReact() {
let rect = CGRect(x: 0, y: 0, width: 10, height: 10)
let point = CGPoint(x: 5, y: 8)
let margin = CGFloat(3)

let result = calculateDropProposalPosition(
in: rect,
for: point,
margin: margin
)

XCTAssertEqual(result, .bottom)
}

func testPointInCenterReact() {
let rect = CGRect(x: 0, y: 0, width: 10, height: 10)
let point = CGPoint(x: 5, y: 5)
let margin = CGFloat(3)

let result = calculateDropProposalPosition(
in: rect,
for: point,
margin: margin
)

XCTAssertEqual(result, .center)
}

func testPointOutsideReact() {
let rect = CGRect(x: 0, y: 0, width: 10, height: 10)
let point = CGPoint(x: 100, y: 100)
let margin = CGFloat(3)

let result = calculateDropProposalPosition(
in: rect,
for: point,
margin: margin
)

XCTAssertNil(result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//
// DropProposalPositionCalculations.swift
// CodeEditModules/SplitView
//
// Created by Mateusz Bąk on 2022/07/03.
//

import Foundation

/*
Leading Rect:
+------+--------------------------+------+
|xxxxxx| | |
|xxxxxx| | |
|xxxxxx+--------------------------+ |
|xxxxxx| | |
|xxxxxx| | |
|xxxxxx| | |
|xxxxxx+--------------------------+ |
|xxxxxx| | |
|xxxxxx| | |
+------+--------------------------+------+

Trailing Rect:
+------+--------------------------+------+
| | |xxxxxx|
| | |xxxxxx|
| +--------------------------+xxxxxx|
| | |xxxxxx|
| | |xxxxxx|
| | |xxxxxx|
| +--------------------------+xxxxxx|
| | |xxxxxx|
| | |xxxxxx|
+------+--------------------------+------+

Top Rect:
+------+--------------------------+------+
| |xxxxxxxxxxxxxxxxxxxxxxxxxx| |
| |xxxxxxxxxxxxxxxxxxxxxxxxxx| |
| +--------------------------+ |
| | | |
| | | |
| | | |
| +--------------------------+ |
| | | |
| | | |
+------+--------------------------+------+

Bottom Rect:
+------+--------------------------+------+
| | | |
| | | |
| +--------------------------+ |
| | | |
| | | |
| | | |
| +--------------------------+ |
| |xxxxxxxxxxxxxxxxxxxxxxxxxx| |
| |xxxxxxxxxxxxxxxxxxxxxxxxxx| |
+------+--------------------------+------+

Center Rect:
+------+--------------------------+------+
| | | |
| | | |
| +--------------------------+ |
| |xxxxxxxxxxxxxxxxxxxxxxxxxx| |
| |xxxxxxxxxxxxxxxxxxxxxxxxxx| |
| |xxxxxxxxxxxxxxxxxxxxxxxxxx| |
| +--------------------------+ |
| | | |
| | | |
+------+--------------------------+------+
*/
func calculateDropProposalPosition(
in rect: CGRect,
for point: CGPoint,
margin: CGFloat
) -> SplitViewProposalDropPosition? {
let leadingRect = CGRect(
x: rect.minX,
y: rect.minY,
width: margin,
height: rect.height
)

let trailingRect = CGRect(
x: rect.maxX - margin,
y: rect.minY,
width: margin,
height: rect.height
)

let topRect = CGRect(
x: rect.minX + margin,
y: rect.minY,
width: rect.width - 2 * margin,
height: margin
)

let bottomRect = CGRect(
x: rect.minX + margin,
y: rect.maxY - margin,
width: rect.width - 2 * margin,
height: margin
)

if leadingRect.contains(point) {
return .leading
}

if trailingRect.contains(point) {
return .trailing
}

if topRect.contains(point) {
return .top
}

if bottomRect.contains(point) {
return .bottom
}

if rect.contains(point) {
return .center
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// SplitEditorDropProposalPosition.swift
// CodeEditModules/SplitView
//
// Created by Mateusz Bąk on 2022/07/03.
//

public enum SplitViewProposalDropPosition: Equatable, CaseIterable {
case leading, trailing, top, bottom, center
}
45 changes: 45 additions & 0 deletions CodeEditModules/Modules/SplitView/src/SplitViewDropDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// SplitViewDropDelegate.swift
// CodeEditModules/SplitView
//
// Created by Mateusz Bąk on 2022/07/03.
//

import Foundation
import SwiftUI

struct SplitViewDropDelegate: DropDelegate {
let availablePositions: [SplitViewProposalDropPosition]
@Binding var proposalPosition: SplitViewProposalDropPosition?
let geometryProxy: GeometryProxy
let margin: CGFloat
let onDrop: ((SplitViewProposalDropPosition) -> Void)?

func performDrop(info: DropInfo) -> Bool {
if let proposalPosition = proposalPosition {
onDrop?(proposalPosition)
}

return false
}

func dropUpdated(info: DropInfo) -> DropProposal? {
let localFrame = geometryProxy.frame(in: .local)

if let calculatedProposalPosition = calculateDropProposalPosition(
in: localFrame,
for: info.location,
margin: margin
), availablePositions.contains(calculatedProposalPosition) {
proposalPosition = calculatedProposalPosition
} else {
proposalPosition = nil
}

return nil
}

func dropExited(info: DropInfo) {
proposalPosition = nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//
// SplitViewDropProposalOverlay.swift
// CodeEditModules/SplitView
//
// Created by Mateusz Bąk on 2022/07/03.
//

import Foundation
import SwiftUI
import CodeEditUI

private enum Const {
static let padding: CGFloat = 5.5
static let animationDuration: TimeInterval = 0.2
static let overlayCornerRadius: CGFloat = 5
static let overlayBorderColorOpacity: CGFloat = 0.2
static let overlayBorderLineWidth: CGFloat = 1
static let overlayIconSize: CGFloat = 30.5
}

struct SplitViewDropProposalOverlay: View {
private enum MatchedGeometryEffect {
case overlay
}
@Namespace private var animation

let proposalPosition: SplitViewProposalDropPosition

var body: some View {
contentView
.padding(Const.padding)
.animation(
.easeInOut(duration: Const.animationDuration),
value: proposalPosition
)
}

@ViewBuilder
private var contentView: some View {
switch proposalPosition {
case .leading:
leadingPositionOverlay
case .trailing:
trailingPositionOverlay
case .top:
topPositionOverlay
case .bottom:
bottomPositionOverlay
case .center:
centerPositionOverlay
}
}

private var leadingPositionOverlay: some View {
HStack(spacing: 0) {
overlay
Color.clear
}
}

private var trailingPositionOverlay: some View {
HStack(spacing: 0) {
Color.clear
overlay
}
}

private var topPositionOverlay: some View {
VStack(spacing: 0) {
overlay
Color.clear
}
}

private var bottomPositionOverlay: some View {
VStack(spacing: 0) {
Color.clear
overlay
}
}

private var centerPositionOverlay: some View {
overlay
}

private var overlay: some View {
ZStack {
EffectView(
.fullScreenUI,
blendingMode: .withinWindow,
emphasized: false
)
.cornerRadius(Const.overlayCornerRadius)
.overlay {
RoundedRectangle(cornerRadius: Const.overlayCornerRadius)
.stroke(
Color(nsColor: .secondaryLabelColor)
.opacity(Const.overlayBorderColorOpacity),
lineWidth: Const.overlayBorderLineWidth
)
}
Image(systemName: "plus")
.foregroundColor(Color(nsColor: .secondaryLabelColor))
.font(.system(size: Const.overlayIconSize, weight: .light))
}
.matchedGeometryEffect(id: MatchedGeometryEffect.overlay, in: animation)
}
}
Loading