Skip to content

Commit

Permalink
Update MacOS styles
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanov91 committed Nov 3, 2024
1 parent 8af2ddb commit 97220d1
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 35 deletions.
101 changes: 83 additions & 18 deletions Sources/OversizeUI/Controls/HUD/HUD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public struct HUD<Title, Icon>: View where Title: View, Icon: View {
private let isAutoHide: Bool

@Binding private var isPresented: Bool
#if os(macOS)
@State private var offset: CGFloat = 200
#else
@State private var offset: CGFloat = -200
#endif

@State private var bottomOffset: CGFloat = 0
@State private var opacity: CGFloat = 0

// MARK: Initializers
Expand All @@ -41,7 +45,12 @@ public struct HUD<Title, Icon>: View where Title: View, Icon: View {
if let text {
Text(text)
.body(.medium)
.foregroundColor(.onSurfaceHighEmphasis)
#if os(macOS)
.foregroundColor(Color.onPrimaryHighEmphasis)
#else
.foregroundColor(Color.onSurfaceHighEmphasis)

#endif

} else if let title {
title
Expand All @@ -50,26 +59,41 @@ public struct HUD<Title, Icon>: View where Title: View, Icon: View {
.padding(.leading, icon == nil ? .medium : .small)
.padding(.trailing, .medium)
.padding(.vertical, .xSmall)
.background(
Capsule()
.foregroundColor(Color.surfacePrimary)
.shadowElevaton(.z2)
)
.padding(.small)
.opacity(opacity)
.offset(y: bottomOffset)
.onChange(of: isPresented, perform: { present in
if present {
presentAnimated()
} else {
dismissAnimated()
#if os(macOS)
.background(
RoundedRectangle(cornerRadius: .small, style: .continuous)
.foregroundColor(Color.onBackgroundHighEmphasis)
.shadowElevaton(.z2)
)
#else
.background(
Capsule()
.foregroundColor(Color.surfacePrimary)
.shadowElevaton(.z2)
)
#endif
.padding(.small)
.opacity(opacity)
.offset(y: offset)
.onChange(of: isPresented) { present in
if present {
if offset == 0 {
dismissAnimated()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
presentAnimated()
}
} else {
presentAnimated()
}
} else {
dismissAnimated()
}
}
})
}

private func presentAnimated() {
withAnimation {
bottomOffset = 0
offset = 0
opacity = 1
}
if isAutoHide {
Expand All @@ -83,7 +107,11 @@ public struct HUD<Title, Icon>: View where Title: View, Icon: View {

private func dismissAnimated() {
withAnimation {
bottomOffset = -200
#if os(macOS)
offset = 200
#else
offset = -200
#endif
opacity = 0
}
}
Expand Down Expand Up @@ -132,6 +160,42 @@ public extension HUD where Icon == EmptyView {
}
}

#if os(macOS)
@MainActor
public extension View {
func hud(_ text: String, autoHide: Bool = true, isPresented: Binding<Bool>) -> some View {
overlay(alignment: .bottomTrailing) {
HUD(text, autoHide: autoHide, isPresented: isPresented)
}
}

func hud(_ text: String, isPresented: Binding<Bool>, @ViewBuilder icon: () -> some View) -> some View {
overlay(alignment: .bottomTrailing) {
HUD(text, isPresented: isPresented, icon: icon)
}
}

func hud(isPresented: Binding<Bool>, @ViewBuilder title: () -> some View) -> some View {
overlay(alignment: .bottomTrailing) {
HUD(isPresented: isPresented, title: title)
}
}

func hud(isPresented: Binding<Bool>, @ViewBuilder title: () -> some View, @ViewBuilder icon: () -> some View) -> some View {
overlay(alignment: .bottomTrailing) {
HUD(isPresented: isPresented, title: title, icon: icon)
}
}

func hudLoader(_ text: String = "Loading", isPresented: Binding<Bool>) -> some View {
overlay(alignment: .bottomTrailing) {
HUD(text, autoHide: false, isPresented: isPresented) {
ProgressView()
}
}
}
}
#else
public extension View {
func hud(_ text: String, isPresented: Binding<Bool>) -> some View {
overlay(alignment: .top) {
Expand Down Expand Up @@ -165,3 +229,4 @@ public extension View {
}
}
}
#endif
4 changes: 3 additions & 1 deletion Sources/OversizeUI/Controls/PageView/Page.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import SwiftUI
public struct Page<Content, Header, LeadingBar, TrailingBar, TopToolbar, TitleLabel>: View
where Content: View, Header: View, LeadingBar: View, TrailingBar: View, TopToolbar: View, TitleLabel: View
{
@Environment(\.platform) var platform

public typealias ScrollAction = (_ offset: CGPoint, _ headerVisibleRatio: CGFloat) -> Void

private let title: String?
Expand Down Expand Up @@ -103,7 +105,7 @@ public struct Page<Content, Header, LeadingBar, TrailingBar, TopToolbar, TitleLa
var scrollView: some View {
GeometryReader { proxy in
ScrollViewWithOffsetTracking(
showsIndicators: false,
showsIndicators: platform == .macOS,
onScroll: handleOffset
) {
VStack(spacing: 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension View {
@ViewBuilder
@inlinable func alignmentGuide(_ alignment: HorizontalAlignment,
isActive: Bool,
computeValue: @escaping (ViewDimensions) -> CGFloat) -> some View
computeValue: @Sendable @escaping (ViewDimensions) -> CGFloat) -> some View
{
if isActive {
alignmentGuide(alignment, computeValue: computeValue)
Expand All @@ -33,7 +33,7 @@ extension View {
@ViewBuilder
@inlinable func alignmentGuide(_ alignment: VerticalAlignment,
isActive: Bool,
computeValue: @escaping (ViewDimensions) -> CGFloat) -> some View
computeValue: @Sendable @escaping (ViewDimensions) -> CGFloat) -> some View
{
if isActive {
alignmentGuide(alignment, computeValue: computeValue)
Expand Down
102 changes: 102 additions & 0 deletions Sources/OversizeUI/Controls/Stacks/LeadingVStack.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// Copyright © 2024 Alexander Romanov
// LeadingVStack.swift, created on 28.10.2024
//

import SwiftUI

public struct LeadingVStack<Content: View>: View {
private let spacing: Space
private let content: Content

public init(spacing: Space = .zero, @ViewBuilder content: () -> Content) {
self.spacing = spacing
self.content = content()
}

public var body: some View {
VStack(alignment: .leading, spacing: spacing.rawValue) {
content
}
}
}

public struct TrailingVStack<Content: View>: View {
private let spacing: Space
private let content: Content

public init(spacing: Space = .zero, @ViewBuilder content: () -> Content) {
self.spacing = spacing
self.content = content()
}

public var body: some View {
VStack(alignment: .trailing, spacing: spacing.rawValue) {
content
}
}
}

public struct CenterVStack<Content: View>: View {
private let spacing: Space
private let content: Content

public init(spacing: Space = .zero, @ViewBuilder content: () -> Content) {
self.spacing = spacing
self.content = content()
}

public var body: some View {
VStack(alignment: .center, spacing: spacing.rawValue) {
content
}
}
}

public struct LeadingLazyVStack<Content: View>: View {
private let spacing: Space
private let content: Content

public init(spacing: Space = .zero, @ViewBuilder content: () -> Content) {
self.spacing = spacing
self.content = content()
}

public var body: some View {
LazyVStack(alignment: .leading, spacing: spacing.rawValue) {
content
}
}
}

public struct TrailingLazyVStack<Content: View>: View {
private let spacing: Space
private let content: Content

public init(spacing: Space = .zero, @ViewBuilder content: () -> Content) {
self.spacing = spacing
self.content = content()
}

public var body: some View {
LazyVStack(alignment: .trailing, spacing: spacing.rawValue) {
content
}
}
}

public struct CenterLazyVStack<Content: View>: View {
private let spacing: Space
private let content: Content

public init(spacing: Space = .zero, @ViewBuilder content: () -> Content) {
self.spacing = spacing
self.content = content()
}

public var body: some View {
LazyVStack(alignment: .center, spacing: spacing.rawValue) {
content
}
}
}
33 changes: 31 additions & 2 deletions Sources/OversizeUI/Controls/Surface/Surface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public struct Surface<Label: View>: View {
private let forceContentInsets: EdgeSpaceInsets?
private var isSurfaceClipped: Bool = false

@State var isHover = false

public init(
action: (() -> Void)? = nil,
@ViewBuilder label: () -> Label
Expand All @@ -58,8 +60,12 @@ public struct Surface<Label: View>: View {
action?()
} label: {
surface
.contentShape(Rectangle())
}
.buttonStyle(SurfaceButtonStyle())
.onHover { hover in
isHover = hover
}
}

private var surface: some View {
Expand All @@ -68,14 +74,33 @@ public struct Surface<Label: View>: View {
.padding(.bottom, forceContentInsets?.bottom ?? contentInsets.bottom)
.padding(.leading, forceContentInsets?.leading ?? contentInsets.leading)
.padding(.trailing, forceContentInsets?.trailing ?? contentInsets.trailing)
.background(
.background {
#if os(macOS)
ZStack {
RoundedRectangle(
cornerRadius: surfaceRadius,
style: .continuous
)
.fill(surfaceBackgroundColor)
.shadowElevaton(elevation)

if isHover {
RoundedRectangle(
cornerRadius: surfaceRadius,
style: .continuous
)
.fill(Color.onSurfaceDisabled.opacity(0.04))
}
}
#else
RoundedRectangle(
cornerRadius: surfaceRadius,
style: .continuous
)
.fill(surfaceBackgroundColor)
.shadowElevaton(elevation)
)
#endif
}
.overlay(
RoundedRectangle(
cornerRadius: surfaceRadius,
Expand Down Expand Up @@ -171,7 +196,11 @@ public struct SurfaceButtonStyle: ButtonStyle {

public func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
#if os(macOS)
.scaleEffect(configuration.isPressed ? 0.98 : 1)
#else
.scaleEffect(configuration.isPressed ? 0.96 : 1)
#endif
}
}

Expand Down
1 change: 0 additions & 1 deletion Sources/OversizeUI/Controls/Toast/Snackbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public struct Snackbar<Label, Actions>: View where Label: View, Actions: View {
}
}
}

.padding(.leading, .medium)
.padding(.trailing, .xSmall)
.padding(.vertical, .xSmall)
Expand Down
Loading

0 comments on commit 97220d1

Please sign in to comment.