Skip to content

Commit

Permalink
✨ Add option for having padding on windows; close #16
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKai77 authored Sep 2, 2023
2 parents 3d023c1 + 5fb9b0d commit b251c83
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 6 deletions.
1 change: 1 addition & 0 deletions Loop/Extensions/Defaults+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@ extension Defaults.Keys {

// BETA
static let animateWindowResizes = Key<Bool>("animateWindowResizes", default: false)
static let windowPadding = Key<CGFloat>("windowPadding", default: 0)
}
2 changes: 1 addition & 1 deletion Loop/Helpers/Views/BetaIndicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct BetaIndicator: View {
Text(text)
.font(.caption2)
.padding(.horizontal, 4)
.padding(.vertical, 2)
.padding(.vertical, 1)
.background {
RoundedRectangle(cornerRadius: 50)
.stroke(lineWidth: 1)
Expand Down
25 changes: 25 additions & 0 deletions Loop/Helpers/WindowDirection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,29 @@ enum WindowDirection: CaseIterable {
default: [[]]
}
}

var sidesThatTouchScreen: [Edge] {
switch self {
case .maximize: [.top, .bottom, .leading, .trailing]
case .topHalf: [.top, .leading, .trailing]
case .rightHalf: [.top, .bottom, .trailing]
case .bottomHalf: [.bottom, .leading, .trailing]
case .leftHalf: [.top, .bottom, .leading]
case .topLeftQuarter: [.top, .leading]
case .topRightQuarter: [.top, .trailing]
case .bottomRightQuarter: [.bottom, .trailing]
case .bottomLeftQuarter: [.bottom, .leading]
case .rightThird: [.top, .bottom, .trailing]
case .rightTwoThirds: [.top, .bottom, .trailing]
case .horizontalCenterThird: [.top, .bottom]
case .leftThird: [.top, .bottom, .leading]
case .leftTwoThirds: [.top, .bottom, .leading]
case .topThird: [.top, .leading, .trailing]
case .topTwoThirds: [.top, .leading, .trailing]
case .verticalCenterThird: [.leading, .trailing]
case .bottomThird: [.bottom, .leading, .trailing]
case .bottomTwoThirds: [.bottom, .leading, .trailing]
default: []
}
}
}
36 changes: 33 additions & 3 deletions Loop/Helpers/WindowEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ struct WindowEngine {
let oldWindowFrame = window.frame
guard let screenFrame = screen.safeScreenFrame else { return }
guard var targetWindowFrame = WindowEngine.generateWindowFrame(oldWindowFrame, screenFrame, direction) else { return }
targetWindowFrame = WindowEngine.applyPadding(targetWindowFrame, direction)

// Calculate the window's minimum window size and change the target accordingly
window.getMinSize(screen: screen) { minSize in
if (targetWindowFrame.minX + minSize.width) > screen.frame.maxX {
targetWindowFrame.origin.x = screen.frame.maxX - minSize.width
targetWindowFrame.origin.x = screen.frame.maxX - minSize.width - Defaults[.windowPadding]
}

if (targetWindowFrame.minY + minSize.height) > screen.frame.maxY {
targetWindowFrame.origin.y = screen.frame.maxY - minSize.height
targetWindowFrame.origin.y = screen.frame.maxY - minSize.height - Defaults[.windowPadding]
}

// Resize window
window.setFrame(targetWindowFrame, animate: true)
window.setFrame(targetWindowFrame, animate: Defaults[.animateWindowResizes])
}
}

Expand Down Expand Up @@ -101,4 +102,33 @@ struct WindowEngine {
return nil
}
}

private static func applyPadding(_ windowFrame: CGRect, _ direction: WindowDirection) -> CGRect {
var paddingAppliedRect = windowFrame
for side in [Edge.top, Edge.bottom, Edge.leading, Edge.trailing] {
if direction.sidesThatTouchScreen.contains(side) {
paddingAppliedRect.inset(side, amount: Defaults[.windowPadding])
} else {
paddingAppliedRect.inset(side, amount: Defaults[.windowPadding] / 2)
}
}
return paddingAppliedRect
}
}

extension CGRect {
mutating func inset(_ side: Edge, amount: CGFloat) {
switch side {
case .top:
self.origin.y += amount
self.size.height -= amount
case .leading:
self.origin.x += amount
self.size.width -= amount
case .bottom:
self.size.height -= amount
case .trailing:
self.size.width -= amount
}
}
}
16 changes: 14 additions & 2 deletions Loop/Settings/Views/GeneralSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct GeneralSettingsView: View {
@Default(.currentIcon) var currentIcon
@Default(.timesLooped) var timesLooped
@Default(.animateWindowResizes) var animateWindowResizes
@Default(.windowPadding) var windowPadding

let iconManager = IconManager()

Expand All @@ -39,12 +40,23 @@ struct GeneralSettingsView: View {
}
}

Toggle(isOn: $animateWindowResizes, label: {
Toggle(isOn: $animateWindowResizes) {
HStack {
Text("Animate windows being resized")
BetaIndicator("BETA")
}
})
}

Slider(value: $windowPadding,
in: 0...25,
step: 5,
minimumValueLabel: Text("0"),
maximumValueLabel: Text("25")) {
HStack {
Text("Window Padding")
BetaIndicator("BETA")
}
}
}

Section("Loop's icon") {
Expand Down
1 change: 1 addition & 0 deletions Loop/Settings/Views/MoreSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import SwiftUI
import Sparkle
import Defaults

struct MoreSettingsView: View {

Expand Down

0 comments on commit b251c83

Please sign in to comment.