Skip to content

Commit

Permalink
editor: toggle keyboard toolbar without re-launching app
Browse files Browse the repository at this point in the history
  • Loading branch information
bummoblizard committed Jun 9, 2024
1 parent 4174930 commit 0ed5f8c
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 50 deletions.
2 changes: 1 addition & 1 deletion CodeApp/Localization/de.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@
"Initialize Repository" = "Repository initialisieren";
"Clone Repository" = "Repository klonen";
"TERMINAL" = "TERMINAL";
"Keyboard Toolbar (Effective in next launch)" = "Tastaturleiste (wirksam beim nächsten Start)";
"Keyboard Toolbar" = "Tastaturleiste";
"Always Open In New Tab" = "Immer in neuem Tab öffnen";
"Changes" = "Änderungen";
"Message (⌘Enter to commit)" = "Nachricht (⌘Enter zum committen)";
Expand Down
2 changes: 1 addition & 1 deletion CodeApp/Localization/ja.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@
"Initialize Repository" = "リポジトリを初期化する";
"Clone Repository" = "リポジトリのクローン";
"TERMINAL" = "ターミナル";
"Keyboard Toolbar (Effective in next launch)" = "キーボードツールバー (次回起動時より有効)";
"Keyboard Toolbar" = "キーボードツールバー";
"Always Open In New Tab" = "常に新しいタブで開く";
"Changes" = "変更";
"Message (⌘Enter to commit)" = "メッセージ (⌘Enterでコミット)";
Expand Down
2 changes: 1 addition & 1 deletion CodeApp/Localization/ko.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@
"Initialize Repository" = "저장소 초기화하기";
"Clone Repository" = "저장소 복제하기";
"TERMINAL" = "터미널";
"Keyboard Toolbar (Effective in next launch)" = "키보드 툴바(다음 실행 시 적용됨)";
"Keyboard Toolbar" = "키보드 툴바";
"Always Open In New Tab" = "항상 새 탭에서 열기";
"Changes" = "변경 사항";
"Message (⌘Enter to commit)" = "메시지(커밋하라면 ⌘Enter)";
Expand Down
4 changes: 2 additions & 2 deletions CodeApp/Localization/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@
"Initialize Repository" = "初始化存储库";
"Clone Repository" = "克隆存储库";
"TERMINAL" = "终端";
"Keyboard Toolbar (Effective in next launch)" = "键盘工具栏 (下次启动后生效)";
"Keyboard Toolbar" = "键盘工具栏";
"Always Open In New Tab" = "总是在新标签页中打开";
"Changes" = "变更";
"Message (⌘Enter to commit)" = "提交备注 (⌘ + Enter 以提交)";
Expand Down Expand Up @@ -578,4 +578,4 @@
"remote.port_forward.configure_description" = "您可以使用端口转发功能将本地端口转发到远程服务器。";
"remote.port_forward.address_example" = "例如 6000 或 127.0.0.1:6000";
"remote.settings.ssh_remote" = "SSH 远程";
"remote.settings.resolve_home_path" = "解析主目录路径";
"remote.settings.resolve_home_path" = "解析主目录路径";
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,7 @@ private struct _EditorImplementationView: UIViewRepresentable {

@EnvironmentObject var App: MainApp

private func injectBarButtons(textView: TextView) {
let toolbar = UIHostingController(
rootView: EditorKeyboardToolBar(isMonaco: false).environmentObject(App))
toolbar.view.frame = CGRect(
x: 0, y: 0, width: (textView.bounds.width), height: 40)
textView.inputAccessoryView = toolbar.view
}

private func injectBarButtons(webView: WebViewBase) {
let toolbar = UIHostingController(
rootView: EditorKeyboardToolBar(isMonaco: true).environmentObject(App))
toolbar.view.frame = CGRect(
x: 0, y: 0, width: (webView.bounds.width), height: 40)
webView.addInputAccessoryView(toolbar: toolbar.view)
}

func makeUIView(context: Context) -> UIView {
if let webView = implementation.view as? WebViewBase {
if implementation.options.toolBarEnabled {
injectBarButtons(webView: webView)
} else {
webView.addInputAccessoryView(toolbar: UIView.init())
}
} else if let textView = implementation.view as? TextView {
if implementation.options.toolBarEnabled {
injectBarButtons(textView: textView)
}
}
return implementation.view
}

Expand Down
13 changes: 12 additions & 1 deletion CodeApp/Managers/EditorImplementation/MonacoImplementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Foundation
import GameController
import SwiftUI

extension WKWebView {
@MainActor
Expand Down Expand Up @@ -97,7 +98,17 @@ class MonacoImplementation: NSObject {
await applyOptions(options: "renderWhitespace: '\(options.renderWhiteSpaces)'")
await applyOptions(options: "wordWrap: '\(options.wordWrap)'")
_ = try? await monacoWebView.evaluateJavaScriptAsync("toggleVimMode(\(options.vimEnabled))")

await MainActor.run {
if options.toolBarEnabled {
let toolbar = UIHostingController(
rootView: EditorKeyboardToolBar(editorImplementation: self))
toolbar.view.frame = CGRect(
x: 0, y: 0, width: (monacoWebView.bounds.width), height: 40)
monacoWebView.addInputAccessoryView(toolbar: toolbar.view)
} else {
monacoWebView.removeInputAccessoryView()
}
}
}

private func applyOptions(options: String) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Runestone
import SwiftUI
import TreeSitterAstroRunestone
import TreeSitterBashRunestone
import TreeSitterCPPRunestone
Expand Down Expand Up @@ -368,6 +369,15 @@ class RunestoneImplementation: NSObject {
if let currentState = states[currentURL ?? ""] {
textView.setState(currentState.state)
}
if options.toolBarEnabled {
let toolbar = UIHostingController(
rootView: EditorKeyboardToolBar(editorImplementation: self))
toolbar.view.frame = CGRect(
x: 0, y: 0, width: (textView.bounds.width), height: 40)
textView.inputAccessoryView = toolbar.view
} else {
textView.inputAccessoryView = nil
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion CodeApp/Managers/WebViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,21 @@ class WebViewBase: KBWebViewBase {
var candidateView: UIView? {
var candidateView: UIView? = nil
for view in self.scrollView.subviews {
if String(describing: type(of: view)).hasPrefix("WKContent") {
let description = String(describing: type(of: view))
if description.hasPrefix("WKContent")
|| description.hasSuffix("_CustomInputAccessoryView")
{
candidateView = view
}
}
return candidateView
}

func removeInputAccessoryView() {
objc_setAssociatedObject(
self, &ToolbarHandle, nil, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}

func addInputAccessoryView(toolbar: UIView?) {
guard let toolbar = toolbar else { return }
guard let targetView = candidateView else { return }
Expand Down
27 changes: 13 additions & 14 deletions CodeApp/Views/EditorKeyboardToolBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ import SwiftUI

struct EditorKeyboardToolBar: View {

@EnvironmentObject var App: MainApp
@Environment(\.horizontalSizeClass) var horizontalSizeClass
@State var pasteBoardHasContent = false
var isMonaco: Bool
var editorImplementation: EditorImplementation
var isMonaco: Bool { editorImplementation is MonacoImplementation }

var body: some View {
HStack(spacing: horizontalSizeClass == .compact ? 8 : 14) {
Group {
Button(
action: {
Task { await App.monacoInstance.undo() }
Task { await editorImplementation.undo() }
},
label: {
Image(systemName: "arrow.uturn.left")
})
Button(
action: {
Task { await App.monacoInstance.redo() }
Task { await editorImplementation.redo() }
},
label: {
Image(systemName: "arrow.uturn.right")
Expand All @@ -35,7 +35,7 @@ struct EditorKeyboardToolBar: View {
Button(
action: {
Task {
let selected = await App.monacoInstance.getSelectedValue()
let selected = await editorImplementation.getSelectedValue()
UIPasteboard.general.string = selected
}
},
Expand All @@ -47,7 +47,7 @@ struct EditorKeyboardToolBar: View {
action: {
if let string = UIPasteboard.general.string {
Task {
await App.monacoInstance.pasteText(text: string)
await editorImplementation.pasteText(text: string)
}
}
},
Expand All @@ -60,7 +60,7 @@ struct EditorKeyboardToolBar: View {
Button(
action: {
Task {
await App.monacoInstance.pasteText(text: "\t")
await editorImplementation.pasteText(text: "\t")
}
},
label: {
Expand All @@ -75,7 +75,7 @@ struct EditorKeyboardToolBar: View {
Button(
action: {
Task {
await App.monacoInstance.pasteText(text: char)
await editorImplementation.pasteText(text: char)
}
},
label: {
Expand All @@ -87,7 +87,7 @@ struct EditorKeyboardToolBar: View {
Button(
action: {
Task {
await App.monacoInstance.moveCursor(direction: .up)
await editorImplementation.moveCursor(direction: .up)
}
},
label: {
Expand All @@ -96,7 +96,7 @@ struct EditorKeyboardToolBar: View {
Button(
action: {
Task {
await App.monacoInstance.moveCursor(direction: .down)
await editorImplementation.moveCursor(direction: .down)
}
},
label: {
Expand All @@ -107,7 +107,7 @@ struct EditorKeyboardToolBar: View {
Button(
action: {
Task {
await App.monacoInstance.moveCursor(direction: .left)
await editorImplementation.moveCursor(direction: .left)
}
},
label: {
Expand All @@ -116,7 +116,7 @@ struct EditorKeyboardToolBar: View {
Button(
action: {
Task {
await App.monacoInstance.moveCursor(direction: .right)
await editorImplementation.moveCursor(direction: .right)
}
},
label: {
Expand All @@ -127,8 +127,7 @@ struct EditorKeyboardToolBar: View {
Button(
action: {
Task {
await App.monacoInstance.blur()
// await App.saveCurrentFile()
await editorImplementation.blur()
}
},
label: {
Expand Down
4 changes: 2 additions & 2 deletions CodeApp/Views/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ struct SettingsView: View {
)

Toggle(
"Keyboard Toolbar (Effective in next launch)",
"Keyboard Toolbar",
isOn: $editorOptions.value.toolBarEnabled
).onChange(
of: editorOptions.value.toolBarEnabled
Expand Down Expand Up @@ -232,7 +232,7 @@ struct SettingsView: View {

Section(header: Text("TERMINAL")) {
Toggle(
"Keyboard Toolbar (Effective in next launch)",
"Keyboard Toolbar",
isOn: $terminalToolBarEnabled)
Toggle("Show Command in Terminal", isOn: $compilerShowPath)
}
Expand Down

0 comments on commit 0ed5f8c

Please sign in to comment.