Skip to content

Commit

Permalink
Add params for animation -> menus_and_commands
Browse files Browse the repository at this point in the history
  • Loading branch information
carson-katri committed Aug 22, 2023
1 parent 770d153 commit d8cb34d
Show file tree
Hide file tree
Showing 71 changed files with 358 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import SwiftUI
/// ```html
/// <RenameButton
/// modifiers={
/// rename_action(@native, event: "begin_rename", target: @myself)
/// rename_action(%{ event: "begin_rename", target: @myself })
/// }
/// />
/// ```
Expand All @@ -30,33 +30,21 @@ struct RenameActionModifier: ViewModifier, Decodable {
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let event: String
/// The LiveView or LiveComponent to perform the event on.
///
/// In a component, you may use the `@myself` assign to handle the event on the LiveComponent.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let target: Int?
@Environment(\.coordinatorEnvironment) private var coordinatorEnvironment
@Event private var action: Event.EventHandler

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.event = try container.decode(String.self, forKey: .event)
self.target = try container.decode(Int?.self, forKey: .target)
self._action = try container.decode(Event.self, forKey: .action)
}

private enum CodingKeys: String, CodingKey {
case event
case target
case action
}

func body(content: Content) -> some View {
content
.renameAction {
Task {
try await coordinatorEnvironment?.pushEvent("click", event, [String:Any](), target)
}
action(value: [String:String]())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import SwiftUI
/// @native
/// |> foreground_color(color: :blue)
/// |> font(font: {:system, :large_title})
/// |> mask(alignment: :center, content: :mask)
/// |> mask(alignment: :center, mask: :mask)
/// }
/// >
/// <Rectangle template={:mask} modifiers={@native |> opacity(opacity: 0.1)} />
Expand All @@ -41,7 +41,7 @@ struct MaskModifier<R: RootRegistry>: ViewModifier, Decodable {
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let content: String
private let mask: String

@ObservedElement private var element
@LiveContext<R> private var context
Expand All @@ -50,17 +50,17 @@ struct MaskModifier<R: RootRegistry>: ViewModifier, Decodable {
let container = try decoder.container(keyedBy: CodingKeys.self)

self.alignment = try container.decode(Alignment.self, forKey: .alignment)
self.content = try container.decode(String.self, forKey: .content)
self.mask = try container.decode(String.self, forKey: .mask)
}

func body(content: Content) -> some View {
content.mask(alignment: alignment) {
context.buildChildren(of: element, forTemplate: self.content)
context.buildChildren(of: element, forTemplate: self.mask)
}
}

enum CodingKeys: String, CodingKey {
case alignment
case content
case mask
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,24 @@ struct OnDeleteCommandModifier: ViewModifier, Decodable {
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@Event private var action: Event.EventHandler
@Event private var perform: Event.EventHandler

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self._action = try container.decode(Event.self, forKey: .action)
self._perform = try container.decode(Event.self, forKey: .perform)
}

func body(content: Content) -> some View {
content
#if os(macOS)
.onDeleteCommand {
action()
perform()
}
#endif
}

enum CodingKeys: String, CodingKey {
case action
case perform
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ struct OnHoverModifier: ViewModifier, Decodable {
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@Event private var action: Event.EventHandler
@Event private var perform: Event.EventHandler

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self._action = try container.decode(Event.self, forKey: .action)
self._perform = try container.decode(Event.self, forKey: .perform)
}

func body(content: Content) -> some View {
content
#if os(iOS) || os(macOS)
.onHover { isHovering in
action(value: isHovering)
perform(value: isHovering)
}
#endif
}

enum CodingKeys: String, CodingKey {
case action
case perform
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ struct OnMoveCommandModifier: ViewModifier, Decodable {
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
@Event private var action: Event.EventHandler
@Event private var perform: Event.EventHandler

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

self._action = try container.decode(Event.self, forKey: .action)
self._perform = try container.decode(Event.self, forKey: .perform)
}

func body(content: Content) -> some View {
Expand All @@ -66,21 +66,21 @@ struct OnMoveCommandModifier: ViewModifier, Decodable {
.onMoveCommand { direction in
switch direction {
case .up:
action(value: ["direction": "up"])
perform(value: ["direction": "up"])
case .down:
action(value: ["direction": "down"])
perform(value: ["direction": "down"])
case .left:
action(value: ["direction": "left"])
perform(value: ["direction": "left"])
case .right:
action(value: ["direction": "right"])
perform(value: ["direction": "right"])
@unknown default:
action(value: ["direction": String(describing: direction)])
perform(value: ["direction": String(describing: direction)])
}
}
#endif
}

enum CodingKeys: String, CodingKey {
case action
case perform
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
/// Specifies whether the indicator on a ``Menu`` should be visible.
///
/// ```html
/// <Menu modifiers={menu_style(@native, style: :button) |> menu_indicator_visibility(visibility: :hidden)}>
/// <Menu modifiers={menu_style(@native, style: :button) |> menu_indicator(visibility: :hidden)}>
/// <Text template={:label}>
/// Edit Actions
/// </Text>
Expand All @@ -28,7 +28,7 @@ import SwiftUI
@_documentation(visibility: public)
#endif
@available(iOS 16.0, macOS 13.0, tvOS 16.0, *)
struct MenuIndicatorVisibilityModifier: ViewModifier, Decodable {
struct MenuIndicatorModifier: ViewModifier, Decodable {
/// The indicator visibility.
///
/// See ``LiveViewNative/SwiftUI/Visibility`` for possible values.
Expand All @@ -38,8 +38,9 @@ struct MenuIndicatorVisibilityModifier: ViewModifier, Decodable {
private let visibility: Visibility

func body(content: Content) -> some View {
#if !os(watchOS)
content.menuIndicator(visibility)
#endif
content
#if !os(watchOS)
.menuIndicator(visibility)
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ defmodule LiveViewNativeSwiftUi.Modifiers.MatchedGeometryEffect do
field :anchor, UnitPoint
field :is_source, :boolean, default: true
end

def params([id: id, in: namespace, properties: properties, anchor: anchor, is_source: is_source]),
do: [
id: id,
namespace: namespace,
properties: properties,
anchor: anchor,
is_source: is_source
]
def params(params), do: params
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ defmodule LiveViewNativeSwiftUi.Modifiers.ButtonBorderShape do
field(:shape, Ecto.Enum, values: ~w(automatic capsule rounded_rectangle)a)
field(:radius, :float, default: nil)
end

def params(shape) when is_atom(shape) and not is_boolean(shape) and not is_nil(shape), do: [shape: shape]
def params(params), do: params
end
14 changes: 12 additions & 2 deletions lib/live_view_native_swift_ui/modifiers/documents/rename_action.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
defmodule LiveViewNativeSwiftUi.Modifiers.RenameAction do
use LiveViewNativePlatform.Modifier

alias LiveViewNativePlatform.Types.Event

modifier_schema "rename_action" do
field :event, :string
field :target, :integer
field :action, Event
end

def params(params) do
with {:ok, _} <- Event.cast(params) do
[action: params]
else
_ ->
params
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ defmodule LiveViewNativeSwiftUi.Modifiers.Draggable do
field :payload, :string
field :preview, KeyName
end

def params(payload, [preview: preview]), do: [payload: payload, preview: preview]
def params(payload) when is_binary(payload), do: [payload: payload, preview: nil]
def params(params), do: params
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule LiveViewNativeSwiftUi.Modifiers.DropDestination do
use LiveViewNativePlatform.Modifier

alias LiveViewNativeSwiftUi.Types.Event
alias LiveViewNativePlatform.Types.Event

modifier_schema "drop_destination" do
field :action, Event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@ defmodule LiveViewNativeSwiftUi.Modifiers.BackgroundStyle do
modifier_schema "background_style" do
field :style, ShapeStyle
end

def params(params) do
with {:ok, _} <- ShapeStyle.cast(params) do
[style: params]
else
_ ->
params
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ defmodule LiveViewNativeSwiftUi.Modifiers.BlendMode do
destination_out
)a)
end

def params(blend_mode) when is_atom(blend_mode) and not is_boolean(blend_mode) and not is_nil(blend_mode),
do: [blend_mode: blend_mode]
def params(params), do: params
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ defmodule LiveViewNativeSwiftUi.Modifiers.Border do
field :content, ShapeStyle
field :width, :float, default: 1.0
end

def params(content, [width: width]), do: [content: content, width: width]
def params(params) do
with {:ok, _} <- ShapeStyle.cast(params) do
[content: params, width: 1.0]
else
_ ->
params
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ defmodule LiveViewNativeSwiftUi.Modifiers.Brightness do
modifier_schema "brightness" do
field :amount, :float
end

def params(amount) when is_number(amount), do: [amount: amount]
def params(params), do: params
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ defmodule LiveViewNativeSwiftUi.Modifiers.ClipShape do
field :shape, Shape
field :style, FillStyle
end

def params(shape, [style: style]), do: [shape: shape, style: style]
def params(params) do
with {:ok, _} <- Shape.cast(params) do
[shape: params, style: nil]
else
_ ->
params
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@ defmodule LiveViewNativeSwiftUi.Modifiers.ColorMultiply do
modifier_schema "color_multiply" do
field :color, Color
end

def params(params) do
with {:ok, _} <- Color.cast(params) do
[color: params]
else
_ ->
params
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ defmodule LiveViewNativeSwiftUi.Modifiers.Contrast do
modifier_schema "contrast" do
field :amount, :float
end

def params(amount) when is_number(amount), do: [amount: amount]
def params(params), do: params
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ defmodule LiveViewNativeSwiftUi.Modifiers.CornerRadius do
field(:radius, :float)
field(:antialiased, :boolean, default: true)
end

def params(radius, [antialiased: antialiased]), do: [radius: radius, antialiased: antialiased]
def params(radius) when is_number(radius), do: [radius: radius]
def params(params), do: params
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ defmodule LiveViewNativeSwiftUi.Modifiers.Grayscale do
modifier_schema "grayscale" do
field :amount, :float
end

def params(amount) when is_number(amount), do: [amount: amount]
def params(params), do: params
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@ defmodule LiveViewNativeSwiftUi.Modifiers.HueRotation do
modifier_schema "hue_rotation" do
field :angle, Angle
end

def params(params) do
with {:ok, _} <- Angle.cast(params) do
[angle: params]
else
_ ->
params
end
end
end
Loading

0 comments on commit d8cb34d

Please sign in to comment.