Skip to content

Commit

Permalink
Accessibility improvements (#309)
Browse files Browse the repository at this point in the history
* Add accessibility support for Ice Bar

* Initial layout bar accessibility support
  • Loading branch information
jordanbaird authored Aug 10, 2024
1 parent 5a95e1b commit 83d6f50
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
55 changes: 38 additions & 17 deletions Ice/IceBar/IceBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class IceBarPanel: NSPanel {
defer: false
)
self.appState = appState
self.title = "Ice Bar"
self.titlebarAppearsTransparent = true
self.isMovableByWindowBackground = true
self.allowsToolTipsWhenApplicationIsInactive = true
Expand Down Expand Up @@ -336,10 +337,31 @@ private struct IceBarContentView: View {

private struct IceBarItemView: View {
@EnvironmentObject var imageCache: MenuBarItemImageCache
@EnvironmentObject var itemManager: MenuBarItemManager

let item: MenuBarItem
let closePanel: () -> Void

private var leftClickAction: () -> Void {
return { [weak itemManager] in
guard let itemManager else {
return
}
closePanel()
itemManager.tempShowItem(item, clickWhenFinished: true, mouseButton: .left)
}
}

private var rightClickAction: () -> Void {
return { [weak itemManager] in
guard let itemManager else {
return
}
closePanel()
itemManager.tempShowItem(item, clickWhenFinished: true, mouseButton: .right)
}
}

private var image: NSImage? {
guard
let image = imageCache.images[item.info],
Expand All @@ -359,8 +381,11 @@ private struct IceBarItemView: View {
Image(nsImage: image)
.contentShape(Rectangle())
.overlay {
IceBarItemClickView(item: item, closePanel: closePanel)
IceBarItemClickView(item: item, leftClickAction: leftClickAction, rightClickAction: rightClickAction)
}
.accessibilityLabel(item.displayName)
.accessibilityAction(named: "left click", leftClickAction)
.accessibilityAction(named: "right click", rightClickAction)
}
}
}
Expand All @@ -369,22 +394,22 @@ private struct IceBarItemView: View {

private struct IceBarItemClickView: NSViewRepresentable {
private class Represented: NSView {
private weak var itemManager: MenuBarItemManager?

let item: MenuBarItem
let closePanel: () -> Void

let leftClickAction: () -> Void
let rightClickAction: () -> Void

private var lastLeftMouseDownDate = Date.now
private var lastRightMouseDownDate = Date.now

private var lastLeftMouseDownLocation = CGPoint.zero
private var lastRightMouseDownLocation = CGPoint.zero

init(itemManager: MenuBarItemManager, item: MenuBarItem, closePanel: @escaping () -> Void) {
init(item: MenuBarItem, leftClickAction: @escaping () -> Void, rightClickAction: @escaping () -> Void) {
self.item = item
self.closePanel = closePanel
self.leftClickAction = leftClickAction
self.rightClickAction = rightClickAction
super.init(frame: .zero)
self.itemManager = itemManager
self.toolTip = item.displayName
}

Expand Down Expand Up @@ -412,37 +437,33 @@ private struct IceBarItemClickView: NSViewRepresentable {
override func mouseUp(with event: NSEvent) {
super.mouseUp(with: event)
guard
let itemManager,
Date.now.timeIntervalSince(lastLeftMouseDownDate) < 0.5,
absoluteDistance(lastLeftMouseDownLocation, NSEvent.mouseLocation) < 5
else {
return
}
closePanel()
itemManager.tempShowItem(item, clickWhenFinished: true, mouseButton: .left)
leftClickAction()
}

override func rightMouseUp(with event: NSEvent) {
super.rightMouseUp(with: event)
guard
let itemManager,
Date.now.timeIntervalSince(lastRightMouseDownDate) < 0.5,
absoluteDistance(lastRightMouseDownLocation, NSEvent.mouseLocation) < 5
else {
return
}
closePanel()
itemManager.tempShowItem(item, clickWhenFinished: true, mouseButton: .right)
rightClickAction()
}
}

@EnvironmentObject var itemManager: MenuBarItemManager

let item: MenuBarItem
let closePanel: () -> Void

let leftClickAction: () -> Void
let rightClickAction: () -> Void

func makeNSView(context: Context) -> NSView {
Represented(itemManager: itemManager, item: item, closePanel: closePanel)
Represented(item: item, leftClickAction: leftClickAction, rightClickAction: rightClickAction)
}

func updateNSView(_ nsView: NSView, context: Context) { }
Expand Down
2 changes: 2 additions & 0 deletions Ice/LayoutBar/LayoutBarItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ extension LayoutBarItemView: NSDraggingSource {
}
}

extension LayoutBarItemView: NSAccessibilityLayoutItem { }

// MARK: Layout Bar Item Pasteboard Type
extension NSPasteboard.PasteboardType {
static let layoutBarItem = Self("\(Constants.bundleIdentifier).layout-bar-item")
Expand Down
6 changes: 6 additions & 0 deletions Ice/LayoutBar/LayoutBarScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class LayoutBarScrollView: NSScrollView {
}
}

extension LayoutBarScrollView {
override func accessibilityChildren() -> [Any]? {
return arrangedViews
}
}

extension LayoutBarScrollView {
/// A custom scroller that overrides its knob slot to be transparent.
class HorizontalScroller: NSScroller {
Expand Down

0 comments on commit 83d6f50

Please sign in to comment.