cs193p-emojiart/EmojiArt/UtilityViews.swift
Ching 89d38faea4 feat(view, viewmodel, model): 增加 document 和 palette,增加手势
1. 可以从另一个 app 中拖入图片或图片链接作为背景
2. 可以从 palette 中拖入 emoji
3. 可以拖动和双击缩放背景图

Signed-off-by: Ching <loooching@gmail.com>
2023-02-20 21:45:33 +08:00

66 lines
1.8 KiB
Swift

//
// UtilityViews.swift
// EmojiArt
//
// Created by CS193p Instructor on 4/26/21.
// Copyright © 2021 Stanford University. All rights reserved.
//
import SwiftUI
// syntactic sure to be able to pass an optional UIImage to Image
// (normally it would only take a non-optional UIImage)
struct OptionalImage: View {
var uiImage: UIImage?
var body: some View {
if uiImage != nil {
Image(uiImage: uiImage!)
}
}
}
// syntactic sugar
// lots of times we want a simple button
// with just text or a label or a systemImage
// but we want the action it performs to be animated
// (i.e. withAnimation)
// this just makes it easy to create such a button
// and thus cleans up our code
struct AnimatedActionButton: View {
var title: String? = nil
var systemImage: String? = nil
let action: () -> Void
var body: some View {
Button {
withAnimation {
action()
}
} label: {
if title != nil && systemImage != nil {
Label(title!, systemImage: systemImage!)
} else if title != nil {
Text(title!)
} else if systemImage != nil {
Image(systemName: systemImage!)
}
}
}
}
// simple struct to make it easier to show configurable Alerts
// just an Identifiable struct that can create an Alert on demand
// use .alert(item: $alertToShow) { theIdentifiableAlert in ... }
// where alertToShow is a Binding<IdentifiableAlert>?
// then any time you want to show an alert
// just set alertToShow = IdentifiableAlert(id: "my alert") { Alert(title: ...) }
// of course, the string identifier has to be unique for all your different kinds of alerts
struct IdentifiableAlert: Identifiable {
var id: String
var alert: () -> Alert
}