Skip to content

Commit

Permalink
Add popupBarCustomizer() and popupContentViewCustomizer() modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoNatan committed Mar 22, 2021
1 parent 4a48771 commit dc60544
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ extension View {
.if(UserDefaults.standard.object(forKey: PopupSettingsVisualEffectViewBlurEffect) != nil) { view in
view.popupBarBackgroundStyle(UIBlurEffect.Style(rawValue: UserDefaults.standard.integer(forKey: PopupSettingsVisualEffectViewBlurEffect)))
}
.if(UserDefaults.standard.bool(forKey: PopupSettingsEnableCustomizations)) { view in
view.popupBarCustomizer { popupBar in
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .right
paragraphStyle.lineBreakMode = .byTruncatingTail

popupBar.inheritsVisualStyleFromDockingView = false
popupBar.backgroundStyle = .dark
popupBar.titleTextAttributes = [ .paragraphStyle: paragraphStyle, .font: UIFont(name: "Chalkduster", size: 14)!, .foregroundColor: UIColor.yellow ]
popupBar.subtitleTextAttributes = [ .paragraphStyle: paragraphStyle, .font: UIFont(name: "Chalkduster", size: 12)!, .foregroundColor: UIColor.green ]
popupBar.tintColor = .yellow
}
}
.popupBarShouldExtendPopupBarUnderSafeArea(UserDefaults.standard.bool(forKey: PopupSettingsExtendBar))
.if(includeContextMenu) { view in
view.popupBarContextMenu {
Expand Down
48 changes: 24 additions & 24 deletions LNPopupUIExample/LNPopupUIExample/Settings/Settings.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,30 @@
</tableViewCell>
</cells>
</tableViewSection>
<tableViewSection headerTitle="Customizations" id="5N7-Di-fpw">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" textLabel="Nzg-UC-dQ6" style="IBUITableViewCellStyleDefault" id="Cj6-vg-Ukc">
<rect key="frame" x="20" y="1895" width="374" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Cj6-vg-Ukc" id="PhZ-T2-yds">
<rect key="frame" x="0.0" y="0.0" width="374" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Enabled" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" id="Nzg-UC-dQ6">
<rect key="frame" x="15" y="0.0" width="351" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
<connections>
<outlet property="accessoryView" destination="L7U-zD-OqE" id="VpU-ME-dMU"/>
</connections>
</tableViewCell>
</cells>
</tableViewSection>
</sections>
<connections>
<outlet property="dataSource" destination="cgm-yQ-Jfx" id="N9D-cb-IPZ"/>
Expand Down Expand Up @@ -605,30 +629,6 @@
</tableViewCell>
</cells>
</tableViewSection>
<tableViewSection headerTitle="Customizations" id="5N7-Di-fpw">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" textLabel="Nzg-UC-dQ6" style="IBUITableViewCellStyleDefault" id="Cj6-vg-Ukc">
<rect key="frame" x="20" y="1989" width="374" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Cj6-vg-Ukc" id="PhZ-T2-yds">
<rect key="frame" x="0.0" y="0.0" width="374" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Enabled" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" id="Nzg-UC-dQ6">
<rect key="frame" x="15" y="0.0" width="351" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
<connections>
<outlet property="accessoryView" destination="L7U-zD-OqE" id="VpU-ME-dMU"/>
</connections>
</tableViewCell>
</cells>
</tableViewSection>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" id="L7U-zD-OqE">
<rect key="frame" x="0.0" y="0.0" width="51" height="31"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
Expand Down
50 changes: 50 additions & 0 deletions Sources/LNPopupUI/LNPopupUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,59 @@ public extension View {
return environment(\.popupBarCustomBarView, LNPopupBarCustomView(wantsDefaultTapGesture: wantsDefaultTapGesture, wantsDefaultPanGesture: wantsDefaultPanGesture, wantsDefaultHighlightGesture: wantsDefaultHighlightGesture, popupBarCustomBarView: AnyView(popupBarContent())))
}

/// Adds a context menu to the popup bar.
///
/// Use contextual menus to add actions that change depending on the user's
/// current focus and task.
///
/// The following example creates a popup bar with a contextual menu.
/// Note that the actions invoked by the menu selection could be coded
/// directly inside the button closures or, as shown below, invoked via
/// function references.
///
/// func selectHearts() { ... }
/// func selectClubs() { ... }
/// func selectSpades() { ... }
/// func selectDiamonds() { ... }
///
/// TabView {
/// }
/// .popup(isBarPresented: $isPopupPresented, isPopupOpen: $isPopupOpen) {
/// PlayerView(song: currentSong)
/// }
/// .popupBarContextMenu {
/// Button("♥️ - Hearts", action: selectHearts)
/// Button("♣️ - Clubs", action: selectClubs)
/// Button("♠️ - Spades", action: selectSpades)
/// Button("♦️ - Diamonds", action: selectDiamonds)
/// }
///
/// - Parameter menuItems: A `contextMenu` that contains one or more menu items.
func popupBarContextMenu<MenuItems>(@ViewBuilder menuItems: () -> MenuItems) -> some View where MenuItems : View {
return environment(\.popupBarContextMenu, AnyView(menuItems()))
}

/// Gives a low-level access to the `LNPopupBar` object for customization, beyond what is exposed by LNPopupUI.
///
/// The popup bar customization closure is called after all other popup bar modifiers have been applied.
///
/// - Parameters:
/// - customizer: A customizing closure that is called to customize the popup bar object.
/// - popupBar: The popup bar to customize.
func popupBarCustomizer(_ customizer: @escaping (_ popupBar: LNPopupBar) -> Void) -> some View {
return environment(\.popupBarCustomizer, customizer)
}

/// Gives a low-level access to the `LNPopupContentView` object for customization, beyond what is exposed by LNPopupUI.
///
/// The popup content view customization closure is called after all other popup content view modifiers have been applied.
///
/// - Parameters:
/// - customizer: A customizing closure that is called to customize the popup content view object.
/// - popupContentView: The popup content view to customize.
func popupContentViewCustomizer(_ customizer: @escaping (_ popupContentView: LNPopupContentView) -> Void) -> some View {
return environment(\.popupContentViewCustomizer, customizer)
}
}

public extension View {
Expand Down
18 changes: 18 additions & 0 deletions Sources/LNPopupUI/Private/LNPopupEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ internal struct LNPopupBarContextMenuKey: EnvironmentKey {
static let defaultValue: AnyView? = nil
}

internal struct LNPopupBarCustomizer: EnvironmentKey {
static let defaultValue: ((LNPopupBar) -> Void)? = nil
}

internal struct LNPopupContentViewCustomizer: EnvironmentKey {
static let defaultValue: ((LNPopupContentView) -> Void)? = nil
}

internal extension EnvironmentValues {
var popupInteractionStyle: LNPopupInteractionStyle {
get { self[LNPopupInteractionStyleKey.self] }
Expand Down Expand Up @@ -89,6 +97,16 @@ internal extension EnvironmentValues {
get { self[LNPopupBarContextMenuKey.self] }
set { self[LNPopupBarContextMenuKey.self] = newValue }
}

var popupBarCustomizer: ((LNPopupBar) -> Void)? {
get { self[LNPopupBarCustomizer.self] }
set { self[LNPopupBarCustomizer.self] = newValue }
}

var popupContentViewCustomizer: ((LNPopupContentView) -> Void)? {
get { self[LNPopupContentViewCustomizer.self] }
set { self[LNPopupContentViewCustomizer.self] = newValue }
}
}

internal func UIImageOrientationToImageOrientation(_ o: UIImage.Orientation) -> Image.Orientation {
Expand Down
3 changes: 3 additions & 0 deletions Sources/LNPopupUI/Private/LNPopupProxyViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ internal class LNPopupProxyViewController<Content, PopupContent> : UIHostingCont
self.popupContextMenuInteraction = nil
}

self.currentPopupState.barCustomizer?(self.target.popupBar)
self.currentPopupState.contentViewCustomizer?(self.target.popupContentView)

if self.currentPopupState.isBarPresented == true {
popupContentHandler()

Expand Down
2 changes: 2 additions & 0 deletions Sources/LNPopupUI/Private/LNPopupState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ internal struct LNPopupState<PopupContent: View> {
let contentController: UIViewController?
let onOpen: (() -> Void)?
let onClose: (() -> Void)?
let barCustomizer: ((LNPopupBar) -> Void)?
let contentViewCustomizer: ((LNPopupContentView) -> Void)?
}
6 changes: 5 additions & 1 deletion Sources/LNPopupUI/Private/LNPopupViewWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ internal struct LNPopupViewWrapper<Content, PopupContent>: UIViewControllerRepre
@Environment(\.popupBarBackgroundStyle) var popupBarBackgroundStyle: UIBlurEffect.Style?
@Environment(\.popupBarCustomBarView) var popupBarCustomBarView: LNPopupBarCustomView?
@Environment(\.popupBarContextMenu) var popupBarContextMenu: AnyView?
@Environment(\.popupBarCustomizer) var popupBarCustomizer: ((LNPopupBar) -> Void)?
@Environment(\.popupContentViewCustomizer) var popupContentViewCustomizer: ((LNPopupContentView) -> Void)?

init(isBarPresented: Binding<Bool>, isOpen: Binding<Bool>, onOpen: (() -> Void)?, onClose: (() -> Void)?, popupContent: (() -> PopupContent)? = nil, popupContentController: UIViewController? = nil, @ViewBuilder content: @escaping () -> Content) {
self._isBarPresented = isBarPresented
Expand Down Expand Up @@ -61,7 +63,9 @@ internal struct LNPopupViewWrapper<Content, PopupContent>: UIViewControllerRepre
content: popupContent,
contentController: popupContentController,
onOpen: onOpen,
onClose: onClose)
onClose: onClose,
barCustomizer: popupBarCustomizer,
contentViewCustomizer: popupContentViewCustomizer)
uiViewController.handlePopupState(state)
}
}

0 comments on commit dc60544

Please sign in to comment.