Skip to content

Commit

Permalink
FranzCocoa,Editor: add JumptoLine support
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdanp committed Nov 24, 2023
1 parent aae5169 commit 94b5d72
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 0 deletions.
12 changes: 12 additions & 0 deletions FranzCocoa.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@
43D3AEA828FFD96500D52E92 /* Defaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D3AEA728FFD96500D52E92 /* Defaults.swift */; };
43D3AEAB28FFDF3500D52E92 /* PreferencesWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D3AEA928FFDF3500D52E92 /* PreferencesWindowController.swift */; };
43D3AEAC28FFDF3500D52E92 /* PreferencesWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 43D3AEAA28FFDF3500D52E92 /* PreferencesWindowController.xib */; };
43DF30722B0FC9AC00308269 /* JumpToLineWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43DF30702B0FC9AC00308269 /* JumpToLineWindowController.swift */; };
43DF30732B0FC9AC00308269 /* JumpToLineWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43DF30702B0FC9AC00308269 /* JumpToLineWindowController.swift */; };
43DF30742B0FC9AC00308269 /* JumpToLineWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 43DF30712B0FC9AC00308269 /* JumpToLineWindowController.xib */; };
43DF30752B0FC9AC00308269 /* JumpToLineWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 43DF30712B0FC9AC00308269 /* JumpToLineWindowController.xib */; };
43E5AE8C291245E000C74966 /* DispatchQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43E5AE8B291245E000C74966 /* DispatchQueue.swift */; };
43E64A082928F2BF00FDBF29 /* ScriptWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43E64A062928F2BF00FDBF29 /* ScriptWindowController.swift */; };
43E64A092928F2BF00FDBF29 /* ScriptWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 43E64A072928F2BF00FDBF29 /* ScriptWindowController.xib */; };
Expand Down Expand Up @@ -263,6 +267,8 @@
43D3AEA728FFD96500D52E92 /* Defaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Defaults.swift; sourceTree = "<group>"; };
43D3AEA928FFDF3500D52E92 /* PreferencesWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesWindowController.swift; sourceTree = "<group>"; };
43D3AEAA28FFDF3500D52E92 /* PreferencesWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = PreferencesWindowController.xib; sourceTree = "<group>"; };
43DF30702B0FC9AC00308269 /* JumpToLineWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JumpToLineWindowController.swift; sourceTree = "<group>"; };
43DF30712B0FC9AC00308269 /* JumpToLineWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = JumpToLineWindowController.xib; sourceTree = "<group>"; };
43E5AE8B291245E000C74966 /* DispatchQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DispatchQueue.swift; sourceTree = "<group>"; };
43E64A062928F2BF00FDBF29 /* ScriptWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptWindowController.swift; sourceTree = "<group>"; };
43E64A072928F2BF00FDBF29 /* ScriptWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ScriptWindowController.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -367,6 +373,8 @@
43B8DCC729546B200056E9FD /* HexViewerViewController.xib */,
43D03DD728FB03940049D44D /* Identifiers.swift */,
433C5E8A29066310005F4C4B /* Infos.swift */,
43DF30702B0FC9AC00308269 /* JumpToLineWindowController.swift */,
43DF30712B0FC9AC00308269 /* JumpToLineWindowController.xib */,
43D03DD328FAC6030049D44D /* Keychain.swift */,
43CD621C28CE24B100095144 /* MainMenu.xib */,
431C698028F2E0C60082986A /* NewTopicFormViewController.swift */,
Expand Down Expand Up @@ -579,6 +587,7 @@
43CD621E28CE24B100095144 /* MainMenu.xib in Resources */,
43804AB928D1A026002880FE /* ConnectionTableCellView.xib in Resources */,
43B8DCC929546B200056E9FD /* HexViewerViewController.xib in Resources */,
43DF30742B0FC9AC00308269 /* JumpToLineWindowController.xib in Resources */,
43D3AEAC28FFDF3500D52E92 /* PreferencesWindowController.xib in Resources */,
435EBE5629542E7C00DCFEBF /* DataViewController.xib in Resources */,
437A77E0290D5AA1001965B0 /* TopicRecordsTableViewController.xib in Resources */,
Expand Down Expand Up @@ -613,6 +622,7 @@
43CDBAF02AE411A0003063D7 /* MainMenu.xib in Resources */,
43CDBAF12AE411A0003063D7 /* ConnectionTableCellView.xib in Resources */,
43CDBAF22AE411A0003063D7 /* HexViewerViewController.xib in Resources */,
43DF30752B0FC9AC00308269 /* JumpToLineWindowController.xib in Resources */,
43CDBAF32AE411A0003063D7 /* PreferencesWindowController.xib in Resources */,
43CDBAF42AE411A0003063D7 /* DataViewController.xib in Resources */,
43CDBAF52AE411A0003063D7 /* TopicRecordsTableViewController.xib in Resources */,
Expand Down Expand Up @@ -759,6 +769,7 @@
4396EAE12905B0D200A15D25 /* Tabs.swift in Sources */,
437A77DF290D5AA1001965B0 /* TopicRecordsTableViewController.swift in Sources */,
43CD623E28CE282400095144 /* WelcomeWindowContentViewController.swift in Sources */,
43DF30722B0FC9AC00308269 /* JumpToLineWindowController.swift in Sources */,
4314EDF828D85E0E00431770 /* SidebarGroupCellView.swift in Sources */,
436E71FA28D7511400ECBF49 /* WorkspaceWindowController.swift in Sources */,
43D3AEA628FFD8D100D52E92 /* Pasteboard.swift in Sources */,
Expand Down Expand Up @@ -816,6 +827,7 @@
43CDBACE2AE411A0003063D7 /* Tabs.swift in Sources */,
43CDBACF2AE411A0003063D7 /* TopicRecordsTableViewController.swift in Sources */,
43CDBAD02AE411A0003063D7 /* WelcomeWindowContentViewController.swift in Sources */,
43DF30732B0FC9AC00308269 /* JumpToLineWindowController.swift in Sources */,
43CDBAD12AE411A0003063D7 /* SidebarGroupCellView.swift in Sources */,
43CDBAD22AE411A0003063D7 /* WorkspaceWindowController.swift in Sources */,
43CDBAD32AE411A0003063D7 /* Pasteboard.swift in Sources */,
Expand Down
6 changes: 6 additions & 0 deletions FranzCocoa/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ DQ
<action selector="toggleScript:" target="-1" id="Dey-MT-QfY"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="UpN-HH-VbW"/>
<menuItem title="Jump to Line..." keyEquivalent="l" id="B5u-q8-x6N">
<connections>
<action selector="jumpToLine:" target="-1" id="amY-RT-Nch"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
Expand Down
27 changes: 27 additions & 0 deletions FranzCocoa/Editor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,33 @@ class EditorViewController: NSViewController {
private func didChangeCode() {
delegate?.codeDidChange(self)
}

@objc func jumpToLine(_ sender: Any) {
let ctl = WindowManager.shared.showJumpToLineWindow()
ctl.delegate = self
}
}

// MARK: - JumpToLineWindowDelegate
extension EditorViewController: JumpToLineWindowDelegate {
func willJumpToLine(_ lineNumber: Int) -> Bool {
guard lineNumber > 0 else { return false }
let idx = lineNumber-1
let lines = textView.string.split(
separator: "\n",
omittingEmptySubsequences: false
)
if idx >= lines.count {
return false
}
var lo = 0
for i in 0..<idx {
lo += lines[i].count + 1
}
let hi = lo + lines[idx].count + 1
textView.setSelectedRange(.init(lo..<hi))
return true
}
}

// MARK: - EditorViewControllerDelegate
Expand Down
58 changes: 58 additions & 0 deletions FranzCocoa/JumpToLineWindowController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Cocoa

class JumpToLineWindowController: NSWindowController {
@IBOutlet weak var textField: NSTextField!

weak var delegate: JumpToLineWindowDelegate?

private let numberFormatter = {
let formatter = NumberFormatter()
formatter.allowsFloats = false
formatter.numberStyle = .none
return formatter
}()

convenience init() {
self.init(windowNibName: "JumpToLineWindowController")
}

override func windowDidLoad() {
super.windowDidLoad()

textField.delegate = self
textField.focusRingType = .none
textField.formatter = numberFormatter

window?.delegate = self
window?.center()
}
}

// MARK: - JumpToLineWindowDelegate
protocol JumpToLineWindowDelegate: AnyObject {
func willJumpToLine(_ line: Int) -> Bool
}

// MARK: - NSTextFieldDelegate
extension JumpToLineWindowController: NSTextFieldDelegate {
func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
if commandSelector == #selector(cancelOperation(_:)) {
WindowManager.shared.closeJumpToLineWindow()
return true
} else if commandSelector == #selector(insertNewline(_:)) {
guard let delegate else { return false }
if delegate.willJumpToLine(textField.integerValue) {
WindowManager.shared.closeJumpToLineWindow()
return true
}
}
return false
}
}

// MARK: - NSWindowDelegate
extension JumpToLineWindowController: NSWindowDelegate {
func windowDidResignKey(_ notification: Notification) {
WindowManager.shared.closeJumpToLineWindow()
}
}
48 changes: 48 additions & 0 deletions FranzCocoa/JumpToLineWindowController.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="22155" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22155"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="JumpToLineWindowController" customModule="Franz" customModuleProvider="target">
<connections>
<outlet property="textField" destination="11n-sU-VX3" id="cSc-UC-oyi"/>
<outlet property="window" destination="F0z-JX-Cv5" id="gIp-Ho-8D9"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="default" titlebarAppearsTransparent="YES" titleVisibility="hidden" id="F0z-JX-Cv5">
<windowStyleMask key="styleMask" titled="YES" fullSizeContentView="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="480" height="48"/>
<rect key="screenRect" x="0.0" y="0.0" width="1728" height="1079"/>
<view key="contentView" id="se5-gp-TjO">
<rect key="frame" x="0.0" y="0.0" width="480" height="48"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField focusRingType="none" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="11n-sU-VX3">
<rect key="frame" x="18" y="10" width="444" height="28"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" placeholderString="Line Number" id="5J9-hs-OD2">
<font key="font" metaFont="system" size="24"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
<constraints>
<constraint firstItem="11n-sU-VX3" firstAttribute="top" secondItem="se5-gp-TjO" secondAttribute="top" constant="10" id="8kH-9b-F7O"/>
<constraint firstItem="11n-sU-VX3" firstAttribute="leading" secondItem="se5-gp-TjO" secondAttribute="leading" constant="20" symbolic="YES" id="EdY-s0-Ug9"/>
<constraint firstAttribute="bottom" secondItem="11n-sU-VX3" secondAttribute="bottom" constant="10" id="Ody-CH-shg"/>
<constraint firstAttribute="trailing" secondItem="11n-sU-VX3" secondAttribute="trailing" constant="20" symbolic="YES" id="TIT-Er-Pnz"/>
</constraints>
</view>
<connections>
<outlet property="delegate" destination="-2" id="0bl-1N-AYu"/>
</connections>
<point key="canvasLocation" x="65" y="14"/>
</window>
</objects>
</document>
21 changes: 21 additions & 0 deletions FranzCocoa/WindowManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class WindowManager {
private var preferencesWindowController: PreferencesWindowController?
private var updatesWindowController: UpdatesWindowController?
private var updatesProgressWindowController: UpdatesProgressWindowController?
private var jumpToLineWindowController: JumpToLineWindowController?
private var workspaces = [WorkspaceWindowController]()
private var scripts = [ScriptWindowKey: ScriptWindowController]() // workspace id -> ctl
private var secureURLs = [UVarint: [URL]]() // connection id -> URLs
Expand Down Expand Up @@ -162,6 +163,26 @@ class WindowManager {
frame.makeKeyAndOrderFront(nil)
}

func showJumpToLineWindow() -> JumpToLineWindowController {
if jumpToLineWindowController == nil {
jumpToLineWindowController = JumpToLineWindowController()
}
let ctl = jumpToLineWindowController!
ctl.window?.center()
ctl.window?.makeKeyAndOrderFront(nil)
ctl.showWindow(nil)
return ctl
}

func closeJumpToLineWindow() {
guard let ctl = jumpToLineWindowController else {
return
}
ctl.window?.close()
ctl.dismissController(nil)
jumpToLineWindowController = nil
}

// XXX: Kind of a strange place for this to be in.
func openManual() {
if let url = Bundle.main.url(forResource: "resources/manual/index", withExtension: "html") {
Expand Down

0 comments on commit 94b5d72

Please sign in to comment.