Skip to content

Commit

Permalink
🐛 Trying to improve window sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
KrLite committed Dec 20, 2024
1 parent 5fdbdfb commit ce1254e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 84 deletions.
38 changes: 12 additions & 26 deletions Sources/Luminare/Main Window/LuminareView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,38 @@ public struct LuminareView<Content>: View where Content: View {

@Environment(\.luminareTint) private var tint
@Environment(\.luminareWindow) private var window
@Environment(\.luminareWindowMinWidth) private var minWidth
@Environment(\.luminareWindowMaxWidth) private var maxWidth
@Environment(\.luminareWindowMinHeight) private var minHeight
@Environment(\.luminareWindowMaxHeight) private var maxHeight

// MARK: Fields

@ViewBuilder public let content: () -> Content

@State private var currentAnimation: LuminareWindowAnimation?
@State private var contentSize: CGSize = .zero

// MARK: Body

public var body: some View {
content()
.background {
GeometryReader { proxy in
Color.clear
.onAppear {
setSize(size: proxy.size, animate: false)
}
.onChange(of: proxy.size) {
setSize(size: $0, animate: true)
}
}
}
.frame(
minWidth: minWidth, maxWidth: maxWidth,
minHeight: minHeight, maxHeight: maxHeight,
alignment: .leading
)
.focusable(false)
.buttonStyle(.luminare)
.overrideTint(tint)
// Handle content size change
.onGeometryChange(for: CGSize.self) { proxy in
proxy.size
} action: { newValue in
contentSize = newValue
}
.onAppear { setSize(size: contentSize, animate: false) }
.onChange(of: contentSize) { setSize(size: $0, animate: true) }
}

// MARK: Functions

func setSize(size: CGSize, animate: Bool) {
guard let window else {
return
}
guard let window else { return }
guard size.width > 0, size.height > 0 else { return }

if let animation = currentAnimation {
animation.stop()
}
currentAnimation?.stop()

var frame = NSRect(
origin: window.frame.origin,
Expand Down
19 changes: 1 addition & 18 deletions Sources/Luminare/Main Window/LuminareWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class LuminareWindow: NSWindow {
/// - content: the content view of the window, wrapped in a ``LuminareView``.
public init(
blurRadius: CGFloat? = nil,
minFrame: CGSize = .init(width: 100, height: 100),
maxFrame: CGSize = .init(width: CGFloat.infinity, height: CGFloat.infinity),
content: @escaping () -> some View
) {
self.initializationTime = .now
Expand All @@ -28,13 +26,12 @@ public class LuminareWindow: NSWindow {
contentRect: .zero,
styleMask: [.titled, .fullSizeContentView, .closable, .resizable],
backing: .buffered,
defer: false // if true, background blur will break
defer: false // If true, background blur will break
)

let view = NSHostingView(
rootView: LuminareView(content: content)
.environment(\.luminareWindow, self)
.luminareWindowFrame(min: minFrame, max: maxFrame)
)

contentView = view
Expand All @@ -52,20 +49,6 @@ public class LuminareWindow: NSWindow {
alphaValue = 0
}

public convenience init(
blurRadius: CGFloat? = nil,
minWidth: CGFloat = 100, minHeight: CGFloat = 100,
maxWidth: CGFloat = .infinity, maxHeight: CGFloat = .infinity,
content: @escaping () -> some View
) {
self.init(
blurRadius: blurRadius,
minFrame: .init(width: minWidth, height: minHeight),
maxFrame: .init(width: maxWidth, height: maxHeight),
content: content
)
}

/// Shows this window.
/// This action activates the current application and orders the window to the frontmost.
public func show() {
Expand Down
5 changes: 0 additions & 5 deletions Sources/Luminare/Utilities/EnvironmentValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public extension EnvironmentValues {
@Entry var luminareWindow: NSWindow?
@Entry var luminareSidebarOverflow: CGFloat = 50
@Entry var luminareClickedOutside: Bool = false

@Entry var luminareWindowMinWidth: CGFloat = 100
@Entry var luminareWindowMaxWidth: CGFloat = .infinity
@Entry var luminareWindowMinHeight: CGFloat = 100
@Entry var luminareWindowMaxHeight: CGFloat = .infinity
}

// MARK: - Modals
Expand Down
25 changes: 4 additions & 21 deletions Sources/Luminare/Utilities/Extensions/View+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ extension View {
transform(self)
}

@ViewBuilder func assigning<V>(_ keyPath: WritableKeyPath<EnvironmentValues, V>, _ value: V?) -> some View {
@ViewBuilder func assigning<V>(
_ keyPath: WritableKeyPath<EnvironmentValues, V>,
_ value: V?
) -> some View {
if let value {
environment(keyPath, value)
} else {
Expand Down Expand Up @@ -134,26 +137,6 @@ public extension View {
environment(\.luminareAnimationFast, animation)
}

@ViewBuilder func luminareWindowFrame(
min: CGSize? = nil,
max: CGSize? = nil
) -> some View {
assigning(\.luminareWindowMinWidth, min?.width)
.assigning(\.luminareWindowMaxWidth, max?.width)
.assigning(\.luminareWindowMinHeight, min?.height)
.assigning(\.luminareWindowMaxHeight, max?.height)
}

@ViewBuilder func luminareWindowFrame(
minWidth: CGFloat? = nil, maxWidth: CGFloat? = nil,
minHeight: CGFloat? = nil, maxHeight: CGFloat? = nil
) -> some View {
assigning(\.luminareWindowMinWidth, minWidth)
.assigning(\.luminareWindowMaxWidth, maxWidth)
.assigning(\.luminareWindowMinHeight, minHeight)
.assigning(\.luminareWindowMaxHeight, maxHeight)
}

@ViewBuilder func luminareSizebarOverflow(_ overflow: CGFloat) -> some View {
environment(\.luminareSidebarOverflow, overflow)
}
Expand Down
17 changes: 3 additions & 14 deletions Sources/Luminare/Utilities/LuminareManagerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public protocol LuminareManagerProtocol: View {
var isVisible: Bool { get }

var blurRadius: CGFloat? { get }
var minFrame: CGSize { get }
var maxFrame: CGSize { get }

mutating func show()
mutating func close()
Expand All @@ -32,25 +30,16 @@ public extension LuminareManagerProtocol {
var blurRadius: CGFloat? {
nil
}

var minFrame: CGSize {
.init(width: 100, height: 100)
}

var maxFrame: CGSize {
.init(width: CGFloat.infinity, height: CGFloat.infinity)
}
}

public extension LuminareManagerProtocol {
mutating func show() {
if luminare == nil {
let body = body
let body = self
luminare = LuminareWindow(
blurRadius: blurRadius,
minFrame: minFrame,
maxFrame: maxFrame
) { body }
content: { body }
)
luminare?.center()
}

Expand Down

0 comments on commit ce1254e

Please sign in to comment.