Skip to content

Commit

Permalink
Add menu button on all rows
Browse files Browse the repository at this point in the history
  • Loading branch information
SokoloffA committed May 26, 2024
1 parent 55f43a3 commit 76f0d98
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
34 changes: 29 additions & 5 deletions Radiola/Controls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
import AppKit
import Cocoa

extension NSControl {
func setFontWeight(_ weight: NSFont.Weight) {
if let font = font {
self.font = NSFont.systemFont(ofSize: font.pointSize, weight: weight)
}
}
}

// MARK: - NSWindow

public extension NSWindow {
Expand Down Expand Up @@ -259,11 +267,27 @@ class ImageButton: NSButton {
}
}

extension NSControl {
func setFontWeight(_ weight: NSFont.Weight) {
if let font = font {
self.font = NSFont.systemFont(ofSize: font.pointSize, weight: weight)
}
// MARK: - MenuButton

class MenuButton: NSButton {
init() {
super.init(frame: NSRect())
bezelStyle = .shadowlessSquare
isBordered = false
image = NSImage(systemSymbolName: NSImage.Name("ellipsis"), accessibilityDescription: "Context menu")?.tint(color: .lightGray)
// image = NSImage(systemSymbolName: NSImage.Name("ellipsis.circle"), accessibilityDescription: "Context menu")?.tint(color: .lightGray)
menu = NSMenu()
target = self
action = #selector(onClicked)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

@objc private func onClicked() {
let p = NSPoint(x: frame.width / 2, y: frame.height / 2)
menu!.popUp(positioning: nil, at: p, in: self)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class InternetStationRow: NSView, NSTextFieldDelegate {
var qualityText = Label()
var voteText = Label()
var actionButton = ImageButton()
let menuButton = MenuButton()
let separator = Separator()

private let actionButtonIcons = [
Expand All @@ -39,6 +40,7 @@ class InternetStationRow: NSView, NSTextFieldDelegate {
super.init(frame: NSRect())
addSubview(nameEdit)
addSubview(actionButton)
addSubview(menuButton)
addSubview(urlEdit)
addSubview(separator)
addSubview(qualityText)
Expand Down Expand Up @@ -75,13 +77,19 @@ class InternetStationRow: NSView, NSTextFieldDelegate {
urlEdit.translatesAutoresizingMaskIntoConstraints = false
qualityText.translatesAutoresizingMaskIntoConstraints = false
voteText.translatesAutoresizingMaskIntoConstraints = false
menuButton.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
actionButton.trailingAnchor.constraint(equalTo: trailingAnchor),
actionButton.widthAnchor.constraint(equalToConstant: 16),
nameEdit.leadingAnchor.constraint(equalTo: leadingAnchor),
nameEdit.trailingAnchor.constraint(equalTo: actionButton.leadingAnchor, constant: -8),

menuButton.leadingAnchor.constraint(equalTo: actionButton.trailingAnchor, constant: 8),
menuButton.trailingAnchor.constraint(equalTo: trailingAnchor),
menuButton.widthAnchor.constraint(equalTo: actionButton.widthAnchor),
menuButton.heightAnchor.constraint(equalTo: actionButton.heightAnchor),
menuButton.centerYAnchor.constraint(equalTo: actionButton.centerYAnchor),

urlEdit.leadingAnchor.constraint(equalTo: nameEdit.leadingAnchor),
voteText.leadingAnchor.constraint(equalTo: urlEdit.trailingAnchor, constant: 8),
qualityText.leadingAnchor.constraint(equalTo: voteText.trailingAnchor, constant: 8),
Expand All @@ -99,6 +107,9 @@ class InternetStationRow: NSView, NSTextFieldDelegate {
separator.alignBottom(of: self)

refreshActionButton()

menuButton.menu?.addItem(withTitle: "Copy station title", action: #selector(copyTitleToClipboard), keyEquivalent: "").target = self
menuButton.menu?.addItem(withTitle: "Copy station URL", action: #selector(copyUrlToClipboard), keyEquivalent: "").target = self
}

/* ****************************************
Expand Down Expand Up @@ -191,4 +202,22 @@ class InternetStationRow: NSView, NSTextFieldDelegate {
}
return res
}

/* ****************************************
*
* ****************************************/
@objc private func copyTitleToClipboard() {
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([.string], owner: nil)
pasteboard.setString(station.title, forType: .string)
}

/* ****************************************
*
* ****************************************/
@objc private func copyUrlToClipboard() {
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([.string], owner: nil)
pasteboard.setString(station.url, forType: .string)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ extension LocalStationDelegate {
outlineView.expandItem(newItem.parent)

// Select new item
var row = outlineView.row(forItem: newItem)
let row = outlineView.row(forItem: newItem)

if row > -1 {
outlineView.selectRowIndexes(IndexSet(integer: row), byExtendingSelection: false)
Expand Down
54 changes: 52 additions & 2 deletions Radiola/StationsWindow/LocalStations/LocalStationRows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class LocalGroupRow: NSView {

private var nameEdit = TextField()
private let separator = Separator()
let menuButton = MenuButton()

/* ****************************************
*
Expand All @@ -26,6 +27,7 @@ class LocalGroupRow: NSView {
super.init(frame: NSRect())
addSubview(nameEdit)
addSubview(separator)
addSubview(menuButton)

nameEdit.placeholderString = "Group name"
nameEdit.isBordered = false
Expand All @@ -41,10 +43,18 @@ class LocalGroupRow: NSView {

nameEdit.translatesAutoresizingMaskIntoConstraints = false
nameEdit.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
nameEdit.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8).isActive = true
nameEdit.trailingAnchor.constraint(equalTo: menuButton.leadingAnchor, constant: -8).isActive = true
nameEdit.topAnchor.constraint(equalTo: topAnchor, constant: 10.0).isActive = true

menuButton.translatesAutoresizingMaskIntoConstraints = false
menuButton.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
menuButton.topAnchor.constraint(equalTo: topAnchor, constant: 8).isActive = true
menuButton.widthAnchor.constraint(equalToConstant: 16).isActive = true
menuButton.heightAnchor.constraint(equalToConstant: 16).isActive = true

separator.alignBottom(of: self)

menuButton.menu?.addItem(withTitle: "Copy group title", action: #selector(copyTitleToClipboard), keyEquivalent: "").target = self
}

/* ****************************************
Expand All @@ -61,6 +71,15 @@ class LocalGroupRow: NSView {
group.title = sender.stringValue
list.save()
}

/* ****************************************
*
* ****************************************/
@objc private func copyTitleToClipboard() {
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([.string], owner: nil)
pasteboard.setString(group.title, forType: .string)
}
}

// MARK: - LocalStationRow
Expand All @@ -72,8 +91,11 @@ class LocalStationRow: NSView, NSTextFieldDelegate {
var nameEdit = TextField()
var urlEdit = TextField()
var favoriteButton = ImageButton()
let menuButton = MenuButton()
let separator = Separator()

private let contextMenu = NSMenu(title: "Context")

private let favoriteIcons = [
false: NSImage(systemSymbolName: NSImage.Name("star"), accessibilityDescription: "Favorite")?.tint(color: .lightGray),
true: NSImage(systemSymbolName: NSImage.Name("star.fill"), accessibilityDescription: "Favorite")?.tint(color: .systemYellow),
Expand All @@ -89,6 +111,7 @@ class LocalStationRow: NSView, NSTextFieldDelegate {
super.init(frame: NSRect())
addSubview(nameEdit)
addSubview(favoriteButton)
addSubview(menuButton)
addSubview(urlEdit)
addSubview(separator)

Expand All @@ -112,11 +135,17 @@ class LocalStationRow: NSView, NSTextFieldDelegate {
favoriteButton.action = #selector(favClicked(sender:))

favoriteButton.translatesAutoresizingMaskIntoConstraints = false
favoriteButton.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
favoriteButton.topAnchor.constraint(equalTo: topAnchor, constant: 8).isActive = true
favoriteButton.widthAnchor.constraint(equalToConstant: 16).isActive = true
favoriteButton.heightAnchor.constraint(equalToConstant: 16).isActive = true

menuButton.translatesAutoresizingMaskIntoConstraints = false
menuButton.leadingAnchor.constraint(equalTo: favoriteButton.trailingAnchor, constant: 8).isActive = true
menuButton.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
menuButton.centerYAnchor.constraint(equalTo: favoriteButton.centerYAnchor).isActive = true
menuButton.widthAnchor.constraint(equalTo: favoriteButton.widthAnchor).isActive = true
menuButton.heightAnchor.constraint(equalTo: favoriteButton.heightAnchor).isActive = true

nameEdit.translatesAutoresizingMaskIntoConstraints = false
nameEdit.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
nameEdit.trailingAnchor.constraint(equalTo: favoriteButton.leadingAnchor, constant: -8).isActive = true
Expand All @@ -131,6 +160,9 @@ class LocalStationRow: NSView, NSTextFieldDelegate {
separator.alignBottom(of: self)

refreshFavoriteButton()

menuButton.menu?.addItem(withTitle: "Copy station title", action: #selector(copyTitleToClipboard), keyEquivalent: "").target = self
menuButton.menu?.addItem(withTitle: "Copy station URL", action: #selector(copyUrlToClipboard), keyEquivalent: "").target = self
}

/* ****************************************
Expand Down Expand Up @@ -173,4 +205,22 @@ class LocalStationRow: NSView, NSTextFieldDelegate {
update()
return true
}

/* ****************************************
*
* ****************************************/
@objc private func copyTitleToClipboard() {
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([.string], owner: nil)
pasteboard.setString(station.title, forType: .string)
}

/* ****************************************
*
* ****************************************/
@objc private func copyUrlToClipboard() {
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([.string], owner: nil)
pasteboard.setString(station.url, forType: .string)
}
}

0 comments on commit 76f0d98

Please sign in to comment.