|
| 1 | +// |
| 2 | +// MainSplitViewControllerShortcuts.swift |
| 3 | +// Rocket.Chat |
| 4 | +// |
| 5 | +// Created by Matheus Cardoso on 8/6/18. |
| 6 | +// Copyright © 2018 Rocket.Chat. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +// MARK: Keyboard Shortcuts |
| 12 | + |
| 13 | +extension MainSplitViewController { |
| 14 | + override var keyCommands: [UIKeyCommand]? { |
| 15 | + return [ |
| 16 | + UIKeyCommand(input: "\t", modifierFlags: [], action: #selector(shortcutFocusOnComposer(_:)), discoverabilityTitle: localized("shortcuts.type_message")), |
| 17 | + UIKeyCommand(input: "p", modifierFlags: .command, action: #selector(shortcutTogglePreferences(_:)), discoverabilityTitle: localized("shortcuts.preferences")), |
| 18 | + UIKeyCommand(input: "f", modifierFlags: [.command, .alternate], action: #selector(shortcutRoomSearch(_:)), discoverabilityTitle: localized("shortcuts.room_search")), |
| 19 | + UIKeyCommand(input: "1...9", modifierFlags: .command, action: #selector(shortcutSelectRoom(_:)), discoverabilityTitle: localized("shortcuts.room_selection")), |
| 20 | + UIKeyCommand(input: "]", modifierFlags: .command, action: #selector(shortcutSelectRoom(_:)), discoverabilityTitle: localized("shortcuts.next_room")), |
| 21 | + UIKeyCommand(input: "[", modifierFlags: .command, action: #selector(shortcutSelectRoom(_:)), discoverabilityTitle: localized("shortcuts.previous_room")), |
| 22 | + UIKeyCommand(input: "n", modifierFlags: .command, action: #selector(shortcutSelectRoom(_:)), discoverabilityTitle: localized("shortcuts.new_room")), |
| 23 | + UIKeyCommand(input: "i", modifierFlags: .command, action: #selector(shortcutRoomActions(_:)), discoverabilityTitle: localized("shortcuts.room_actions")), |
| 24 | + UIKeyCommand(input: "u", modifierFlags: .command, action: #selector(shortcutUpload(_:)), discoverabilityTitle: localized("shortcuts.upload_room")), |
| 25 | + UIKeyCommand(input: "f", modifierFlags: .command, action: #selector(shortcutRoomMessageSearch(_:)), discoverabilityTitle: localized("shortcuts.search_messages")), |
| 26 | + UIKeyCommand(input: "↑ ↓", modifierFlags: .alternate, action: #selector(shortcutScrollMessages(_:)), discoverabilityTitle: localized("shortcuts.scroll_messages")), |
| 27 | + UIKeyCommand(input: UIKeyInputUpArrow, modifierFlags: .alternate, action: #selector(shortcutScrollMessages(_:))), |
| 28 | + UIKeyCommand(input: UIKeyInputDownArrow, modifierFlags: .alternate, action: #selector(shortcutScrollMessages(_:))), |
| 29 | + UIKeyCommand(input: "r", modifierFlags: .command, action: #selector(shortcutReplyLatest(_:)), discoverabilityTitle: localized("shortcuts.reply_latest")), |
| 30 | + UIKeyCommand(input: "`", modifierFlags: [.command, .alternate], action: #selector(shortcutSelectServer(_:)), discoverabilityTitle: localized("shortcuts.server_selection")), |
| 31 | + UIKeyCommand(input: "1...9", modifierFlags: [.command, .alternate], action: #selector(shortcutSelectServer(_:)), discoverabilityTitle: localized("shortcuts.server_selection_numbers")), |
| 32 | + UIKeyCommand(input: "n", modifierFlags: [.command, .alternate], action: #selector(shortcutSelectServer(_:)), discoverabilityTitle: localized("shortcuts.add_server")) |
| 33 | + ] + ((0...9).map({ "\($0)" })).map { (input: String) -> UIKeyCommand in |
| 34 | + UIKeyCommand(input: input, modifierFlags: .command, action: #selector(shortcutSelectRoom(_:))) |
| 35 | + } + ((0...9).map({ "\($0)" })).map { (input: String) -> UIKeyCommand in |
| 36 | + UIKeyCommand(input: input, modifierFlags: [.command, .alternate], action: #selector(shortcutSelectServer(_:))) |
| 37 | + } + [ |
| 38 | + UIKeyCommand(input: "t", modifierFlags: .command, action: #selector(shortcutChangeTheme(_:)), discoverabilityTitle: localized("shortcuts.change_theme")) |
| 39 | + ] |
| 40 | + } |
| 41 | + |
| 42 | + @objc func shortcutFocusOnComposer(_ command: UIKeyCommand) { |
| 43 | + guard |
| 44 | + !isPresenting, |
| 45 | + let textView = chatViewController?.textInputbar.textView |
| 46 | + else { |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + if textView.isFirstResponder { |
| 51 | + textView.resignFirstResponder() |
| 52 | + } else { |
| 53 | + textView.becomeFirstResponder() |
| 54 | + subscriptionsViewController?.searchController?.dismiss(animated: true) { [weak self] in |
| 55 | + self?.chatViewController?.keyboardFrame?.updateFrame() |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @objc func shortcutTogglePreferences(_ command: UIKeyCommand) { |
| 61 | + subscriptionsViewController?.togglePreferences() |
| 62 | + } |
| 63 | + |
| 64 | + @objc func shortcutRoomSearch(_ command: UIKeyCommand) { |
| 65 | + guard !isPresenting else { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + if subscriptionsViewController?.searchBar?.isFirstResponder ?? false { |
| 70 | + subscriptionsViewController?.searchController?.dismiss(animated: true, completion: nil) |
| 71 | + } else { |
| 72 | + subscriptionsViewController?.searchBar?.becomeFirstResponder() |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @objc func shortcutSelectServer(_ command: UIKeyCommand) { |
| 77 | + guard let input = command.input, !isPresenting else { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + switch input { |
| 82 | + case "`": |
| 83 | + subscriptionsViewController?.toggleServersList() |
| 84 | + case "n": |
| 85 | + AppManager.addServer(serverUrl: "") |
| 86 | + default: |
| 87 | + guard let position = Int(input), position > 0 else { |
| 88 | + break |
| 89 | + } |
| 90 | + |
| 91 | + let index = position - 1 |
| 92 | + |
| 93 | + if index < DatabaseManager.servers?.count ?? 0 { |
| 94 | + AppManager.changeSelectedServer(index: index) |
| 95 | + } else { |
| 96 | + subscriptionsViewController?.openServersList() |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @objc func shortcutSelectRoom(_ command: UIKeyCommand) { |
| 102 | + guard |
| 103 | + let viewController = subscriptionsViewController, |
| 104 | + let input = command.input |
| 105 | + else { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + if input != "n" && isPresenting { |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + switch input { |
| 114 | + case "n": |
| 115 | + viewController.toggleNewRoom() |
| 116 | + case "]": |
| 117 | + viewController.selectNextRoom() |
| 118 | + case "[": |
| 119 | + viewController.selectPreviousRoom() |
| 120 | + default: |
| 121 | + guard let position = Int(input), position > 0 else { |
| 122 | + break |
| 123 | + } |
| 124 | + |
| 125 | + viewController.selectRoomAt(position - 1) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @objc func shortcutUpload(_ command: UIKeyCommand) { |
| 130 | + guard chatViewController?.subscription?.validated() != nil else { |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + chatViewController?.toggleUpload() |
| 135 | + } |
| 136 | + |
| 137 | + @objc func shortcutRoomActions(_ command: UIKeyCommand) { |
| 138 | + guard chatViewController?.subscription?.validated() != nil else { |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + chatViewController?.toggleActions() |
| 143 | + } |
| 144 | + |
| 145 | + @objc func shortcutRoomMessageSearch(_ command: UIKeyCommand) { |
| 146 | + guard chatViewController?.subscription?.validated() != nil else { |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + chatViewController?.toggleSearchMessages() |
| 151 | + } |
| 152 | + |
| 153 | + @objc func shortcutScrollMessages(_ command: UIKeyCommand) { |
| 154 | + guard |
| 155 | + !isPresenting, |
| 156 | + let input = command.input, |
| 157 | + let offset = chatViewController?.collectionView?.contentOffset, |
| 158 | + let maxHeight = chatViewController?.collectionView?.contentSize.height, |
| 159 | + let collectionView = chatViewController?.collectionView |
| 160 | + else { |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + let heightDelta: CGFloat = input == UIKeyInputUpArrow ? -50.0 : 50.0 |
| 165 | + |
| 166 | + UIView.animate(withDuration: 0.1, animations: { [weak collectionView] in |
| 167 | + let offset = min(offset.y + heightDelta, maxHeight) |
| 168 | + collectionView?.contentOffset = CGPoint(x: 0, y: offset) |
| 169 | + }) |
| 170 | + } |
| 171 | + |
| 172 | + @objc func shortcutReplyLatest(_ command: UIKeyCommand) { |
| 173 | + guard !isPresenting else { |
| 174 | + return |
| 175 | + } |
| 176 | + |
| 177 | + if let message = chatViewController?.subscription?.roomLastMessage { |
| 178 | + chatViewController?.reply(to: message) |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + @objc func shortcutChangeTheme(_ command: UIKeyCommand) { |
| 183 | + if let currentThemeIndex = ThemeManager.themes.index(where: { $0.title == ThemeManager.themeTitle }) { |
| 184 | + if ThemeManager.themes.count > currentThemeIndex + 1 { |
| 185 | + ThemeManager.theme = ThemeManager.themes[currentThemeIndex + 1].theme |
| 186 | + } else { |
| 187 | + ThemeManager.theme = ThemeManager.themes.first?.theme ?? .light |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments