Skip to content

Instantly share code, notes, and snippets.

@kopyl
Created April 28, 2025 22:51
Show Gist options
  • Save kopyl/a9c976a18159403f9c1dd599f4ef9dc7 to your computer and use it in GitHub Desktop.
Save kopyl/a9c976a18159403f9c1dd599f4ef9dc7 to your computer and use it in GitHub Desktop.
final class TabItemView: NSView {
let tab: Tab
var onTabHover: ((Int) -> Void)?
private var firstColumnLabel: NSTextField
private var seconColumnLabel: NSTextField
public var swipeActionViewCenterYAnchorConstraint = NSLayoutConstraint()
private var isRunningFullSwipe = false
private var isRunningPartialFullSwipe = false
private var isRunningAnyAnimation = false
private var totalSwipeDistance: CGFloat = 0
/// need to lock on swipe-to-close-tab scroll and avoid letting user to swipe vertically
private var isUserTryingToSwipeToCloseTab = false
private var textLabelForSwipeViewXConstraint = NSLayoutConstraint()
init(tab: Tab) {
self.tab = tab
switch appState.columnOrder {
case .host_title:
self.firstColumnLabel = NSTextField(labelWithString: tab.host)
self.seconColumnLabel = NSTextField(labelWithString: tab.title)
case .title_host:
self.firstColumnLabel = NSTextField(labelWithString: tab.title)
self.seconColumnLabel = NSTextField(labelWithString: tab.host)
}
let textLabelForSwipeView = NSTextField(labelWithString: Copy.TabsPanel.closeButtonTitle)
let stackView = NSStackView()
super.init(frame: .zero)
self.clipsToBounds = true
self.wantsLayer = true
self.layer?.cornerRadius = 6
textLabelForSwipeView.font = .systemFont(ofSize: 13, weight: .regular)
textLabelForSwipeView.textColor = .white
firstColumnLabel.lineBreakMode = .byTruncatingTail
firstColumnLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
firstColumnLabel.font = .systemFont(ofSize: 18)
firstColumnLabel.textColor = .tabFg
stackView.orientation = .horizontal
stackView.edgeInsets = .init(top: 0, left: 57, bottom: 0, right: 50)
stackView.addArrangedSubview(firstColumnLabel)
stackView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
stackView.topAnchor.constraint(equalTo: topAnchor),
stackView.bottomAnchor.constraint(equalTo: bottomAnchor),
])
}
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
@kopyl
Copy link
Author

kopyl commented Apr 28, 2025

Here is the TabHistoryView.swift which works with this TabItemView class:

https://gist.github.com/kopyl/693773388d4e944bdc401561d665b215

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment