Create RoomContextualMenuToolbarView

This commit is contained in:
SBiOSoftWhare 2019-05-15 23:07:50 +02:00
parent 444a2ee452
commit cd238cc2f0
5 changed files with 476 additions and 0 deletions

View file

@ -0,0 +1,172 @@
/*
Copyright 2019 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import UIKit
import Reusable
final class ContextualMenuItemView: UIView, NibOwnerLoadable {
// MARK: - Constants
private enum ColorAlpha {
static let normal: CGFloat = 1.0
static let highlighted: CGFloat = 0.3
}
private enum ViewAlpha {
static let normal: CGFloat = 1.0
static let disabled: CGFloat = 0.5
}
// MARK: - Properties
// MARK: Outlets
@IBOutlet private weak var imageView: UIImageView!
@IBOutlet private weak var titleLabel: UILabel!
// MARK: Private
private var originalImage: UIImage?
private var isHighlighted: Bool = false {
didSet {
self.updateView()
}
}
// MARK: Public
var titleColor: UIColor = .black {
didSet {
self.updateView()
}
}
var imageColor: UIColor = .black {
didSet {
self.updateView()
}
}
var isEnabled: Bool = true {
didSet {
self.updateView()
}
}
var action: (() -> Void)?
// MARK: Setup
private func commonInit() {
self.setupGestureRecognizer()
}
convenience init() {
self.init(frame: CGRect.zero)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.loadNibContent()
self.commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.loadNibContent()
self.commonInit()
}
// MARK: - Public
func fill(title: String, image: UIImage?) {
self.originalImage = image?.withRenderingMode(.alwaysTemplate)
self.titleLabel.text = title
self.updateView()
}
func fill(menuItem: RoomContextualMenuItem) {
self.fill(title: menuItem.title, image: menuItem.image)
self.action = menuItem.action
self.isEnabled = menuItem.isEnabled
}
// MARK: - Private
private func setupGestureRecognizer() {
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(buttonAction(_:)))
gestureRecognizer.minimumPressDuration = 0
self.addGestureRecognizer(gestureRecognizer)
}
private func updateView() {
let viewAlpha = self.isEnabled ? ViewAlpha.normal : ViewAlpha.disabled
let colorAlpha = self.isHighlighted ? ColorAlpha.highlighted : ColorAlpha.normal
self.updateTitleAndImageAlpha(viewAlpha)
self.imageView.tintColor = self.imageColor
self.updateTitleAndImageColorAlpha(colorAlpha)
}
private func updateTitleAndImageAlpha(_ alpha: CGFloat) {
self.imageView.alpha = alpha
self.titleLabel.alpha = alpha
}
private func updateTitleAndImageColorAlpha(_ alpha: CGFloat) {
let titleColor: UIColor
let image: UIImage?
if alpha < 1.0 {
titleColor = self.titleColor.withAlphaComponent(alpha)
image = self.originalImage?.vc_tintedImage(usingColor: self.imageColor.withAlphaComponent(alpha))
} else {
titleColor = self.titleColor
image = self.originalImage
}
self.titleLabel.textColor = titleColor
self.imageView.image = image
}
// MARK: - Actions
@objc private func buttonAction(_ sender: UILongPressGestureRecognizer) {
guard self.isEnabled else {
return
}
let isBackgroundViewTouched = sender.vc_isTouchingInside()
switch sender.state {
case .began, .changed:
self.isHighlighted = isBackgroundViewTouched
case .ended:
self.isHighlighted = false
if isBackgroundViewTouched {
self.action?()
}
case .cancelled:
self.isHighlighted = false
default:
break
}
}
}

View file

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="ContextualMenuItemView" customModule="Riot" customModuleProvider="target">
<connections>
<outlet property="imageView" destination="RcD-qR-Hvt" id="NnI-K5-aTj"/>
<outlet property="titleLabel" destination="Wap-UK-AxI" id="AlN-i0-4IN"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="Eav-pf-73a">
<rect key="frame" x="0.0" y="0.0" width="64" height="69"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="icon-copy" translatesAutoresizingMaskIntoConstraints="NO" id="RcD-qR-Hvt">
<rect key="frame" x="21.5" y="8" width="21" height="29"/>
<constraints>
<constraint firstAttribute="width" secondItem="RcD-qR-Hvt" secondAttribute="height" multiplier="21:29" id="V50-97-yzO"/>
<constraint firstAttribute="height" constant="29" id="tBN-Ex-KO4"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Copy" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Wap-UK-AxI">
<rect key="frame" x="5" y="39" width="54" height="25"/>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="RcD-qR-Hvt" firstAttribute="centerX" secondItem="Eav-pf-73a" secondAttribute="centerX" id="6sB-0G-LfN"/>
<constraint firstItem="Wap-UK-AxI" firstAttribute="leading" secondItem="Eav-pf-73a" secondAttribute="leading" constant="5" id="989-CC-w7E"/>
<constraint firstItem="RcD-qR-Hvt" firstAttribute="top" secondItem="Eav-pf-73a" secondAttribute="top" constant="8" id="hos-Pl-Og0"/>
<constraint firstItem="Wap-UK-AxI" firstAttribute="top" secondItem="RcD-qR-Hvt" secondAttribute="bottom" constant="2" id="iQ8-gd-onK"/>
<constraint firstAttribute="trailing" secondItem="Wap-UK-AxI" secondAttribute="trailing" constant="5" id="tjm-F0-G8m"/>
<constraint firstAttribute="bottom" secondItem="Wap-UK-AxI" secondAttribute="bottom" constant="5" id="y2C-S8-hdF"/>
</constraints>
<nil key="simulatedTopBarMetrics"/>
<nil key="simulatedBottomBarMetrics"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<point key="canvasLocation" x="-121.73913043478262" y="-31.138392857142854"/>
</view>
</objects>
<resources>
<image name="icon-copy" width="24" height="24"/>
</resources>
</document>

View file

@ -0,0 +1,37 @@
/*
Copyright 2019 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
@objcMembers
final class RoomContextualMenuItem: NSObject {
// MARK: - Properties
let title: String
let image: UIImage?
var isEnabled: Bool = true
var action: (() -> Void)?
// MARK: - Setup
init(menuAction: RoomContextualMenuAction) {
self.title = menuAction.title
self.image = menuAction.image
super.init()
}
}

View file

@ -0,0 +1,141 @@
/*
Copyright 2019 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import UIKit
import Reusable
final class RoomContextualMenuToolbarView: MXKRoomInputToolbarView, NibOwnerLoadable, Themable {
// MARK: - Constants
private enum Constants {
static let menuItemMinWidth: CGFloat = 50.0
static let menuItemMaxWidth: CGFloat = 80.0
}
// MARK: - Properties
// MARK: Outlets
@IBOutlet private weak var menuItemsStackView: UIStackView!
@IBOutlet private weak var separatorView: UIView!
// MARK: Private
private var theme: Theme?
private var menuItemViews: [ContextualMenuItemView] = []
// MARK: - Public
@objc func update(theme: Theme) {
self.theme = theme
self.backgroundColor = theme.backgroundColor
self.tintColor = theme.tintColor
self.separatorView.backgroundColor = theme.lineBreakColor
for menuItemView in self.menuItemViews {
menuItemView.titleColor = theme.textPrimaryColor
menuItemView.imageColor = theme.tintColor
}
}
@objc func fill(contextualMenuItems: [RoomContextualMenuItem]) {
self.menuItemsStackView.vc_removeAllSubviews()
self.menuItemViews.removeAll()
for menuItem in contextualMenuItems {
let menuItemView = ContextualMenuItemView()
menuItemView.fill(menuItem: menuItem)
if let theme = theme {
menuItemView.titleColor = theme.textPrimaryColor
menuItemView.imageColor = theme.tintColor
}
self.add(menuItemView: menuItemView)
}
self.layoutIfNeeded()
}
// MARK: - Setup
private func commonInit() {
}
convenience init() {
self.init(frame: CGRect.zero)
self.loadNibContent()
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.loadNibContent()
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.loadNibContent()
commonInit()
}
// MARK: - Life cycle
override func awakeFromNib() {
super.awakeFromNib()
}
// MARK: - Private
private func add(menuItemView: ContextualMenuItemView) {
let menuItemContentView = UIView()
menuItemContentView.backgroundColor = .clear
self.add(menuItemView: menuItemView, on: menuItemContentView)
self.menuItemsStackView.addArrangedSubview(menuItemContentView)
let widthConstraint = menuItemContentView.widthAnchor.constraint(equalTo: self.menuItemsStackView.widthAnchor)
widthConstraint.priority = .defaultLow
widthConstraint.isActive = true
self.menuItemViews.append(menuItemView)
}
private func add(menuItemView: ContextualMenuItemView, on contentView: UIView) {
contentView.translatesAutoresizingMaskIntoConstraints = false
menuItemView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(menuItemView)
menuItemView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
menuItemView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
let widthConstraint = menuItemView.widthAnchor.constraint(equalToConstant: 0.0)
widthConstraint.priority = .defaultLow
widthConstraint.isActive = true
let minWidthConstraint = menuItemView.widthAnchor.constraint(greaterThanOrEqualToConstant: Constants.menuItemMinWidth)
minWidthConstraint.priority = .required
minWidthConstraint.isActive = true
let maxWidthConstraint = menuItemView.widthAnchor.constraint(lessThanOrEqualToConstant: Constants.menuItemMaxWidth)
maxWidthConstraint.priority = .required
maxWidthConstraint.isActive = true
}
}

View file

@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="RoomContextualMenuToolbarView" customModule="Riot" customModuleProvider="target">
<connections>
<outlet property="menuItemsStackView" destination="ayT-FO-8xC" id="v7N-rd-lEb"/>
<outlet property="separatorView" destination="dzn-dX-0at" id="KVI-n6-pkG"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="gik-f6-38I">
<rect key="frame" x="0.0" y="0.0" width="470" height="63"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="dzn-dX-0at" userLabel="Separator View">
<rect key="frame" x="10" y="0.0" width="450" height="1"/>
<color key="backgroundColor" red="0.93725490199999995" green="0.93725490199999995" blue="0.95686274510000002" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="1" id="Ktg-wG-Xuk"/>
</constraints>
</view>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="ayT-FO-8xC">
<rect key="frame" x="0.0" y="1" width="470" height="62"/>
</stackView>
<toolbar opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="kgj-Hq-tEB">
<rect key="frame" x="0.0" y="1" width="414" height="52"/>
<items>
<barButtonItem title="Copy" id="P1Q-AZ-Qem" userLabel="Copy"/>
<barButtonItem style="plain" systemItem="flexibleSpace" id="bzo-GF-Clo"/>
<barButtonItem title="Reply" id="pW0-ss-bsI"/>
<barButtonItem style="plain" systemItem="flexibleSpace" id="bbf-N0-Zis"/>
<barButtonItem title="Edit" id="X3Q-SB-6e1"/>
<barButtonItem style="plain" systemItem="flexibleSpace" id="2D4-jO-fyW"/>
<barButtonItem title="More" id="skA-D0-EBc"/>
</items>
</toolbar>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="dzn-dX-0at" firstAttribute="leading" secondItem="gik-f6-38I" secondAttribute="leading" constant="10" id="0qV-0P-IyR"/>
<constraint firstItem="kgj-Hq-tEB" firstAttribute="leading" secondItem="gik-f6-38I" secondAttribute="leading" id="14D-JP-KDd"/>
<constraint firstAttribute="bottom" secondItem="ayT-FO-8xC" secondAttribute="bottom" id="CtJ-Y4-5vr"/>
<constraint firstAttribute="bottom" secondItem="kgj-Hq-tEB" secondAttribute="bottom" constant="1" id="RpB-Ka-WmR"/>
<constraint firstItem="kgj-Hq-tEB" firstAttribute="top" secondItem="dzn-dX-0at" secondAttribute="bottom" id="SSI-IW-4eG"/>
<constraint firstItem="ayT-FO-8xC" firstAttribute="top" secondItem="dzn-dX-0at" secondAttribute="bottom" id="Stq-dM-Psw"/>
<constraint firstAttribute="trailing" secondItem="ayT-FO-8xC" secondAttribute="trailing" id="cPG-PI-jhM"/>
<constraint firstItem="dzn-dX-0at" firstAttribute="top" secondItem="gik-f6-38I" secondAttribute="top" id="dFk-HG-k1l"/>
<constraint firstAttribute="trailing" secondItem="dzn-dX-0at" secondAttribute="trailing" constant="10" id="uxF-0x-HFI"/>
<constraint firstItem="ayT-FO-8xC" firstAttribute="leading" secondItem="gik-f6-38I" secondAttribute="leading" id="yDp-d9-du7"/>
<constraint firstAttribute="trailing" secondItem="kgj-Hq-tEB" secondAttribute="trailing" id="yrf-JI-gvp"/>
</constraints>
<nil key="simulatedTopBarMetrics"/>
<nil key="simulatedBottomBarMetrics"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<variation key="default">
<mask key="subviews">
<exclude reference="kgj-Hq-tEB"/>
</mask>
</variation>
<point key="canvasLocation" x="-1457.9710144927537" y="-643.19196428571422"/>
</view>
</objects>
</document>