add pin button for the sending cmd view

This commit is contained in:
True Zhuanjia committed 2024-07-25 21:02:05 +08:00
1 parent b587279450
commit b00d836c13
1 file changed
+25 -1
@@ -15,10 +15,12 @@ import UIKit
private let deleteButton = UIButton(type: .system) private let deleteButton = UIButton(type: .system)
private let editButton = UIButton(type: .system) private let editButton = UIButton(type: .system)
private let exitButton = UIButton(type: .system) private let exitButton = UIButton(type: .system)
private let pinButton = UIButton(type: .system)
private let viewBackgroundColor = UIColor(white: 0.2, alpha: 1.0); private let viewBackgroundColor = UIColor(white: 0.2, alpha: 1.0);
private let highlightColor = UIColor(white: 0.3, alpha: 1.0); private let highlightColor = UIColor(white: 0.3, alpha: 1.0);
private let titleLabel = UILabel() private let titleLabel = UILabel()
private var viewPinned: Bool = false
private var isEditingMode: Bool = false { private var isEditingMode: Bool = false {
didSet { didSet {
updateEditingMode() updateEditingMode()
@@ -67,10 +69,12 @@ import UIKit
deleteButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Delete"), for: .normal) deleteButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Delete"), for: .normal)
editButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Edit"), for: .normal) editButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Edit"), for: .normal)
exitButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Exit"), for: .normal) exitButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Exit"), for: .normal)
pinButton.setTitle("📌", for: .normal)
addButton.titleLabel?.font = UIFont.systemFont(ofSize: 20) // Adjust the size as needed addButton.titleLabel?.font = UIFont.systemFont(ofSize: 20) // Adjust the size as needed
deleteButton.titleLabel?.font = UIFont.systemFont(ofSize: 20) deleteButton.titleLabel?.font = UIFont.systemFont(ofSize: 20)
editButton.titleLabel?.font = UIFont.systemFont(ofSize: 20) editButton.titleLabel?.font = UIFont.systemFont(ofSize: 20)
exitButton.titleLabel?.font = UIFont.systemFont(ofSize: 20) exitButton.titleLabel?.font = UIFont.systemFont(ofSize: 20)
pinButton.titleLabel?.font = UIFont.systemFont(ofSize: 20)
// Add subviews // Add subviews
view.addSubview(tableView) view.addSubview(tableView)
@@ -78,12 +82,16 @@ import UIKit
view.addSubview(deleteButton) view.addSubview(deleteButton)
view.addSubview(editButton) view.addSubview(editButton)
view.addSubview(exitButton) view.addSubview(exitButton)
view.addSubview(pinButton)
// Setup button targets // Setup button targets
addButton.addTarget(self, action: #selector(addButtonTapped), for: .touchUpInside) addButton.addTarget(self, action: #selector(addButtonTapped), for: .touchUpInside)
deleteButton.addTarget(self, action: #selector(deleteButtonTapped), for: .touchUpInside) deleteButton.addTarget(self, action: #selector(deleteButtonTapped), for: .touchUpInside)
editButton.addTarget(self, action: #selector(editButtonTapped), for: .touchUpInside) editButton.addTarget(self, action: #selector(editButtonTapped), for: .touchUpInside)
exitButton.addTarget(self, action: #selector(exitButtonTapped), for: .touchUpInside) exitButton.addTarget(self, action: #selector(exitButtonTapped), for: .touchUpInside)
pinButton.addTarget(self, action: #selector(pinButtonTapped), for: .touchUpInside)
pinButton.contentEdgeInsets = UIEdgeInsets(top: 7, left: 20, bottom: 7, right: 20)
} }
private func setupConstraints() { private func setupConstraints() {
@@ -99,6 +107,7 @@ import UIKit
deleteButton.translatesAutoresizingMaskIntoConstraints = false deleteButton.translatesAutoresizingMaskIntoConstraints = false
editButton.translatesAutoresizingMaskIntoConstraints = false editButton.translatesAutoresizingMaskIntoConstraints = false
exitButton.translatesAutoresizingMaskIntoConstraints = false exitButton.translatesAutoresizingMaskIntoConstraints = false
pinButton.translatesAutoresizingMaskIntoConstraints = false
titleLabel.translatesAutoresizingMaskIntoConstraints = false titleLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
@@ -138,9 +147,14 @@ import UIKit
// DeleteButton constraints // DeleteButton constraints
deleteButton.trailingAnchor.constraint(equalTo: addButton.leadingAnchor, constant: -50), deleteButton.trailingAnchor.constraint(equalTo: addButton.leadingAnchor, constant: -50),
deleteButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10), deleteButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
// DeleteButton constraints
pinButton.trailingAnchor.constraint(equalTo: deleteButton.leadingAnchor, constant: -50),
pinButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
]) ])
} }
private func updateEditingMode() { private func updateEditingMode() {
addButton.isEnabled = isEditingMode addButton.isEnabled = isEditingMode
deleteButton.isEnabled = isEditingMode deleteButton.isEnabled = isEditingMode
@@ -148,6 +162,16 @@ import UIKit
else{ editButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Edit"), for: .normal) } else{ editButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Edit"), for: .normal) }
} }
@objc private func pinButtonTapped() {
viewPinned = !viewPinned
if viewPinned {
pinButton.backgroundColor = highlightColor
}
else{
pinButton.backgroundColor = viewBackgroundColor
}
}
@objc private func addButtonTapped() { @objc private func addButtonTapped() {
let alert = UIAlertController(title: "New Command", message: "Enter a new command and alias", preferredStyle: .alert) let alert = UIAlertController(title: "New Command", message: "Enter a new command and alias", preferredStyle: .alert)
alert.addTextField { $0.placeholder = "Command" } alert.addTextField { $0.placeholder = "Command" }
@@ -254,7 +278,7 @@ import UIKit
// Call the dummy method for key-value sending implementation // Call the dummy method for key-value sending implementation
let command = CommandManager.shared.getAllCommands()[indexPath.row] let command = CommandManager.shared.getAllCommands()[indexPath.row]
sendKeyboardCommand(command) sendKeyboardCommand(command)
dismiss(animated: false, completion: nil) // dimiss the view in sending mode if !viewPinned { dismiss(animated: false, completion: nil) } // dimiss the view in sending mode & the view is not pinned
} }
} }