Skip to content

Commit

Permalink
开放更多自定义属性
Browse files Browse the repository at this point in the history
- 开放更多自定义属性
  • Loading branch information
liyb93 committed Aug 17, 2022
1 parent 35165aa commit ba26084
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 104 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "131506CF-27F8-4B2E-8780-2A81A3D0366C"
type = "1"
version = "2.0">
</Bucket>
98 changes: 54 additions & 44 deletions Example/LYBProgressHUD/LYBProgressHUD/Source/LYBProgressHUD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@

import Cocoa

/// 'default'与'indicator'样式最小宽度
fileprivate let LYBProgressHUDContentMinWidth: CGFloat = 150.0
/// 'default'与'indicator'样式最小高度
fileprivate let LYBProgressHUDContentMinHeight: CGFloat = 150.0
/// 'text'样式最小高度
fileprivate let LYBProgressHUDContentTextMinHeight: CGFloat = 45.0
/// indicator宽高
fileprivate let LYBProgressHUDIndicatorWH: CGFloat = 50.0
/// indicator与text间距
fileprivate let LYBProgressHUDSpacing: CGFloat = 15.0

class LYBProgressHUDMaskView: NSView {
/// 阻止事件穿透
override func mouseDown(with event: NSEvent) {}
}

public class LYBProgressHUDStyle: NSObject {

fileprivate struct Const {
/// 'default'与'indicator'样式最小size
static var hudMinSize: CGSize = CGSize(width: 150, height: 150)
/// 'text'样式最小高度
static var textMinHeight: CGFloat = 45.0
/// indicator最小size
static var indicatorMinSize: CGFloat = 50.0
/// indicator与text间距
static var hudSpace: CGFloat = 15.0
}

// 显示样式
public enum Mode {
case `default` // 指示器与文本
Expand All @@ -46,6 +47,10 @@ public class LYBProgressHUDStyle: NSObject {
public var textColor: NSColor = .white
public var textFont: NSFont = .systemFont(ofSize: 15)
public var indicatorColor: NSColor = .white
public var hudSize: CGSize = Const.hudMinSize
public var textHeight: CGFloat = Const.textMinHeight
public var indicatorSize: CGFloat = Const.indicatorMinSize
public var hudSpace: CGFloat = Const.hudSpace

public init(_ mode: Mode = .default, position: Position = .center, backgroundColor: NSColor = NSColor.black.withAlphaComponent(0.75), textColor: NSColor = .white, textFont: NSFont = .systemFont(ofSize: 15), indicatorColor: NSColor = .white) {
super.init()
Expand All @@ -66,13 +71,15 @@ public class LYBProgressHUD: NSView {
private var textLabel: NSTextField?
private var indicatorActivity: LYBProgressIndicator?
private var superView: NSView!
private var style: LYBProgressHUDStyle!
private var style: LYBProgressHUDStyle = LYBProgressHUDStyle()
private var message: String?

public init(in view: NSView, message: String? = nil, style: LYBProgressHUDStyle? = nil) {
super.init(frame: view.bounds)
superView = view
self.style = style ?? LYBProgressHUDStyle.init()
if let style = style {
self.style = style
}
self.message = message
setupUI()
}
Expand All @@ -90,45 +97,45 @@ public class LYBProgressHUD: NSView {
// 遮罩
maskView = LYBProgressHUDMaskView.init(frame: bounds)

var contentSize: CGSize = CGSize.init(width: LYBProgressHUDContentMinWidth, height: LYBProgressHUDContentMinHeight)
var contentSize: CGSize = style.hudSize
switch style.mode {
case .default: // 指示器与文本
if let text = message { // 有文本
let maxWidth = superView.bounds.width - LYBProgressHUDSpacing * 2
let maxHeight = superView.bounds.height - LYBProgressHUDIndicatorWH - LYBProgressHUDSpacing * 3
let maxWidth = superView.bounds.width - style.hudSpace * 2
let maxHeight = superView.bounds.height - style.indicatorSize - style.textHeight * 3
let size = (text as NSString).boundingRect(with: CGSize.init(width: maxWidth, height: maxHeight), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: style.textFont]).size

let contentWidth = max(size.width + LYBProgressHUDSpacing * 2, LYBProgressHUDContentMinWidth)
let contentHeight = max(size.height + LYBProgressHUDSpacing * 3 + LYBProgressHUDIndicatorWH, LYBProgressHUDContentMinHeight)
let contentWidth = max(size.width + style.hudSpace * 2, LYBProgressHUDStyle.Const.hudMinSize.width)
let contentHeight = max(size.height + style.hudSpace * 3 + style.indicatorSize, LYBProgressHUDStyle.Const.hudMinSize.height)
contentSize = CGSize.init(width: contentWidth , height: contentHeight)

let indicatorX = (contentWidth / 2.0) - (LYBProgressHUDIndicatorWH / 2.0)
let indicatorY = (contentHeight / 2.0) - (LYBProgressHUDIndicatorWH / 2.0) + (size.height / 2.0)
indicatorActivity = createIndicator(with: .init(x: indicatorX, y: indicatorY, width: LYBProgressHUDIndicatorWH, height: LYBProgressHUDIndicatorWH))
let indicatorX = (contentWidth / 2.0) - (style.indicatorSize / 2.0)
let indicatorY = (contentHeight / 2.0) - (style.indicatorSize / 2.0) + (size.height / 2.0)
indicatorActivity = createIndicator(with: .init(x: indicatorX, y: indicatorY, width: style.indicatorSize, height: style.indicatorSize))

let labelY = indicatorY - size.height - LYBProgressHUDSpacing
let labelWidht = max(size.width + 10, contentWidth - LYBProgressHUDSpacing * 2)
textLabel = createLabel(with: .init(x: LYBProgressHUDSpacing - 5, y: labelY, width: labelWidht, height: size.height), text: text)
let labelY = indicatorY - size.height - style.hudSpace
let labelWidht = max(size.width + 10, contentWidth - style.hudSpace * 2)
textLabel = createLabel(with: .init(x: style.hudSpace - 5, y: labelY, width: labelWidht, height: size.height), text: text)
} else { // 无文本
let x = (contentSize.width / 2.0) - (LYBProgressHUDIndicatorWH / 2.0)
let y = (contentSize.height / 2.0) - (LYBProgressHUDIndicatorWH / 2.0)
indicatorActivity = createIndicator(with: .init(x: x, y: y, width: LYBProgressHUDIndicatorWH, height: LYBProgressHUDIndicatorWH))
let x = (contentSize.width / 2.0) - (style.indicatorSize / 2.0)
let y = (contentSize.height / 2.0) - (style.indicatorSize / 2.0)
indicatorActivity = createIndicator(with: .init(x: x, y: y, width: style.indicatorSize, height: style.indicatorSize))
indicatorActivity?.color = .white
}
case .indicator: // 指示器
let x = (contentSize.width / 2.0) - (LYBProgressHUDIndicatorWH / 2.0)
let y = contentSize.height / 2.0 - (LYBProgressHUDIndicatorWH / 2.0)
indicatorActivity = createIndicator(with: .init(x: x, y: y, width: LYBProgressHUDIndicatorWH, height: LYBProgressHUDIndicatorWH))
let x = (contentSize.width / 2.0) - (style.indicatorSize / 2.0)
let y = contentSize.height / 2.0 - (style.indicatorSize / 2.0)
indicatorActivity = createIndicator(with: .init(x: x, y: y, width: style.indicatorSize, height: style.indicatorSize))
case .text: // 文本
if let text = message {
let maxWidth = superView.bounds.width - LYBProgressHUDSpacing * 2
let maxHeight = superView.bounds.height - LYBProgressHUDSpacing * 2
let maxWidth = superView.bounds.width - style.hudSpace * 2
let maxHeight = superView.bounds.height - style.hudSpace * 2
let size = (text as NSString).boundingRect(with: CGSize.init(width: maxWidth, height: maxHeight), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedString.Key.font: style.textFont]).size

let contentWidth = size.width + LYBProgressHUDSpacing * 2
let contentHeight = max(size.height + LYBProgressHUDSpacing * 2, LYBProgressHUDContentTextMinHeight)
let contentWidth = size.width + style.hudSpace * 2
let contentHeight = max(size.height + style.hudSpace * 2, LYBProgressHUDStyle.Const.textMinHeight)
contentSize = CGSize.init(width: contentWidth , height: contentHeight)
textLabel = createLabel(with: .init(x: LYBProgressHUDSpacing - 5, y: LYBProgressHUDSpacing, width: size.width + 10, height: contentHeight - LYBProgressHUDSpacing * 2), text: text)
textLabel = createLabel(with: .init(x: style.hudSpace - 5, y: style.hudSpace, width: size.width + 10, height: contentHeight - style.hudSpace * 2), text: text)
} else {
fatalError("message不可为nil")
}
Expand All @@ -142,22 +149,22 @@ public class LYBProgressHUD: NSView {
y = superView.bounds.height / 2.0 - (contentSize.height / 2.0)
case .top:
x = superView.bounds.width / 2.0 - (contentSize.width / 2.0)
y = superView.bounds.height - contentSize.height - LYBProgressHUDSpacing
y = superView.bounds.height - contentSize.height - style.hudSpace
case .bottom:
x = superView.bounds.width / 2.0 - (contentSize.width / 2.0)
y = LYBProgressHUDSpacing
y = style.hudSpace
case .left:
x = LYBProgressHUDSpacing
x = style.hudSpace
y = superView.bounds.height / 2.0 - (contentSize.height / 2.0)
case .right:
x = superView.bounds.width - contentSize.width - LYBProgressHUDSpacing
x = superView.bounds.width - contentSize.width - style.hudSpace
y = superView.bounds.height / 2.0 - (contentSize.height / 2.0)
}

contentView = NSView.init(frame: CGRect.init(origin: CGPoint.init(x: x, y: y), size: contentSize))
contentView.wantsLayer = true
contentView.layer?.backgroundColor = style.backgroundColor.cgColor
contentView.layer?.cornerRadius = LYBProgressHUDSpacing
contentView.layer?.cornerRadius = style.hudSpace
contentView.layer?.masksToBounds = true

addSubview(maskView)
Expand Down Expand Up @@ -218,8 +225,11 @@ public class LYBProgressHUD: NSView {
}

public class func dismiss(in view: NSView, after delay: TimeInterval = 0, completionHandler: CompletionHandler? = nil) {
view.subviews.filter({$0 is LYBProgressHUD}).forEach { (view) in
(view as? LYBProgressHUD)?.dismiss(after: delay)
for view in view.subviews {
if let hud = view as? LYBProgressHUD {
hud.dismiss(after: delay)
break
}
}
completionHandler?()
}
Expand All @@ -228,8 +238,8 @@ public class LYBProgressHUD: NSView {
extension LYBProgressHUD: NSWindowDelegate {
public func windowWillResize(_ sender: NSWindow, to frameSize: NSSize) -> NSSize {
// 设置最小尺寸
let width = contentView.frame.width + LYBProgressHUDSpacing * 4
let height = contentView.frame.height + LYBProgressHUDSpacing * 4
let width = contentView.frame.width + LYBProgressHUDStyle.Const.hudMinSize.width * 4
let height = contentView.frame.height + LYBProgressHUDStyle.Const.hudSpace * 4
if frameSize.width < width && frameSize.height >= height {
return CGSize.init(width: width, height: frameSize.height)
} else if frameSize.width < width && frameSize.height < height {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ extension NSView: NamespaceCompatible { }

public typealias StyleHandler = (LYBProgressHUDStyle)->()
public extension Namespace where Base: NSView {

@discardableResult
func showMessage(_ message: String) -> Namespace {
return showHUD(message) { style in
style.mode = .text
}
}

@discardableResult
func showIndicator() -> Namespace {
return showHUD { style in
style.mode = .indicator
}
}

@discardableResult
func showHUD(_ message: String? = nil, style: StyleHandler? = nil) -> Namespace {
let hudStyle = LYBProgressHUDStyle.init()
Expand All @@ -44,7 +59,7 @@ public extension Namespace where Base: NSView {
}

@discardableResult
func dismiss(_ delay: TimeInterval = 0, completionHandler: CompletionHandler? = nil) -> Namespace {
func dismiss(delay: TimeInterval = 0, completionHandler: CompletionHandler? = nil) -> Namespace {
LYBProgressHUD.dismiss(in: base, after: delay, completionHandler: completionHandler)
return self
}
Expand Down
21 changes: 8 additions & 13 deletions Example/LYBProgressHUD/LYBProgressHUD/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,19 @@ class ViewController: NSViewController {
}

@IBAction func defaultClickHandler(_ sender: Any) {
view.lyb.showHUD("Loding...").dismiss(3)
view.lyb.showHUD("Loding...").dismiss(delay: 3)
// LYBProgressHUD.show(in: view, message: "")
// LYBProgressHUD.dismiss(in: view, after: 3)
}

@IBAction func indicatorClickHandler(_ sender: Any) {
view.lyb.showHUD { style in
style.mode = .indicator
}.dismiss(3)
view.lyb.showIndicator().dismiss(delay: 3)
// LYBProgressHUD.show(in: view, style: LYBProgressHUDStyle.init(.indicator))
// LYBProgressHUD.dismiss(in: view, after: 3)
}

@IBAction func textClickHandler(_ sender: Any) {
view.lyb.showHUD("加载成功") { style in
style.mode = .text
style.position = .top
style.textColor = NSColor.red
}.dismiss(3)
view.lyb.showMessage("加载成功").dismiss(delay: 3)
// LYBProgressHUD.show(in: view, message: "加载成功", style: LYBProgressHUDStyle.init(.text, position: .top))
// LYBProgressHUD.dismiss(in: view, after: 3)
}
Expand All @@ -48,7 +42,7 @@ class ViewController: NSViewController {
view.lyb.showHUD("加载中...") { style in
style.mode = .text
style.textColor = .orange
}.dismiss(3)
}.dismiss(delay: 3)
// let style = LYBProgressHUDStyle.init(.text, textColor: .orange)
// let hud = LYBProgressHUD.init(in: view, message: "加载中。。。", style: style)
// hud.show()
Expand All @@ -61,7 +55,7 @@ class ViewController: NSViewController {
style.mode = .text
style.textFont = textFont
style.textColor = .orange
}.dismiss(3)
}.dismiss(delay: 3)
// let style = LYBProgressHUDStyle.init(.text, textFont: textFont)
// let hud = LYBProgressHUD.init(in: view, message: "加载中。。。", style: style)
// hud.show()
Expand All @@ -72,7 +66,7 @@ class ViewController: NSViewController {
view.lyb.showHUD("加载中...") { style in
style.mode = .text
style.backgroundColor = .orange
}.dismiss(3)
}.dismiss(delay: 3)
// let style = LYBProgressHUDStyle.init(.text, backgroundColor: .orange)
// let hud = LYBProgressHUD.init(in: view, message: "加载中。。。", style: style)
// hud.show()
Expand All @@ -87,7 +81,8 @@ class ViewController: NSViewController {
view.lyb.showHUD { style in
style.mode = .indicator
style.indicatorColor = .orange
}.dismiss(3)
style.hudSize = CGSize(width: 80, height: 80)
}.dismiss(delay: 3)
}
}

2 changes: 1 addition & 1 deletion LYBProgressHUD.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "LYBProgressHUD"
spec.version = "1.1.1"
spec.version = "1.1.2"
spec.summary = "HUD"
spec.description = <<-DESC
一款OSX平台上简洁易用的HUD组件
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ LYBProgressHUD是一款在OSX上简洁易用的HUD组件。

## 更新记录

### v1.1.2

- 增加`text``indicator`mode快捷调用方式
- 开放更多自定义属性

### v1.1.1
- 增加链式调用方式

Expand Down
Loading

0 comments on commit ba26084

Please sign in to comment.