Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add search bar #2278

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/logic/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class Preferences {
"nextWindowShortcut4": "",
"nextWindowShortcut5": "",
"focusWindowShortcut": "Space",
"openNextWindowShortcut": "",
"previousWindowShortcut": "⇧",
"previousWindowShortcut2": "",
"cancelShortcut": "⎋",
"closeWindowShortcut": "W",
"minDeminWindowShortcut": "M",
Expand Down Expand Up @@ -111,7 +113,9 @@ class Preferences {
static var holdShortcut: [String] { ["holdShortcut", "holdShortcut2", "holdShortcut3", "holdShortcut4", "holdShortcut5"].map { defaults.string($0) } }
static var nextWindowShortcut: [String] { ["nextWindowShortcut", "nextWindowShortcut2", "nextWindowShortcut3", "nextWindowShortcut4", "nextWindowShortcut5"].map { defaults.string($0) } }
static var focusWindowShortcut: String { defaults.string("focusWindowShortcut") }
static var openNextWindowShortcut: String { defaults.string("openNextWindowShortcut") }
static var previousWindowShortcut: String { defaults.string("previousWindowShortcut") }
static var previousWindowShortcut2: String { defaults.string("previousWindowShortcut2") }
static var cancelShortcut: String { defaults.string("cancelShortcut") }
static var closeWindowShortcut: String { defaults.string("closeWindowShortcut") }
static var minDeminWindowShortcut: String { defaults.string("minDeminWindowShortcut") }
Expand Down
5 changes: 4 additions & 1 deletion src/logic/Windows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Windows {
let focusedView = ThumbnailsView.recycledViews[focusedWindowIndex]
focusedView.highlight(true)
App.app.thumbnailsPanel.thumbnailsView.scrollView.contentView.scrollToVisible(focusedView.frame)
voiceOverFocusedWindow()
// voiceOverFocusedWindow()
}

static func voiceOverFocusedWindow() {
Expand Down Expand Up @@ -220,6 +220,7 @@ class Windows {
}

static func refreshIfWindowShouldBeShownToTheUser(_ window: Window, _ screen: NSScreen) {
let searchText = App.app.thumbnailsPanel.thumbnailsView.searchField.stringValue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be lowercased().

window.shouldShowTheUser =
!(window.application.runningApplication.bundleIdentifier.flatMap { id in
Preferences.blacklist.contains {
Expand All @@ -229,6 +230,8 @@ class Windows {
} ?? false) &&
!(Preferences.appsToShow[App.app.shortcutIndex] == .active && window.application.runningApplication.processIdentifier != NSWorkspace.shared.frontmostApplication?.processIdentifier) &&
!(!(Preferences.showHiddenWindows[App.app.shortcutIndex] != .hide) && window.isHidden) &&
(searchText.isEmpty || (window.title?.lowercased().contains(searchText) ?? true) ||
(window.application.runningApplication.localizedName?.lowercased().contains(searchText)) ?? true) &&
((!Preferences.hideWindowlessApps && window.isWindowlessApp) ||
!window.isWindowlessApp &&
!(!(Preferences.showFullscreenWindows[App.app.shortcutIndex] != .hide) && window.isFullscreen) &&
Expand Down
8 changes: 7 additions & 1 deletion src/ui/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ class App: AppCenterApplication, NSApplicationDelegate {
}
}

func nextWindowShortcutWithRepeatingKey() {
cycleSelection(.leading)
KeyRepeatTimer.toggleRepeatingKeyNextWindow()
}

func previousWindowShortcutWithRepeatingKey() {
cycleSelection(.trailing)
KeyRepeatTimer.toggleRepeatingKeyPreviousWindow()
Expand Down Expand Up @@ -238,7 +243,7 @@ class App: AppCenterApplication, NSApplicationDelegate {
thumbnailsPanel.display()
guard appIsBeingUsed else { return }
currentScreen.repositionPanel(thumbnailsPanel, .appleCentered)
Windows.voiceOverFocusedWindow() // at this point ThumbnailViews are assigned to the window, and ready
// Windows.voiceOverFocusedWindow() // at this point ThumbnailViews are assigned to the window, and ready
}

private func refreshSpecificWindows(_ windowsToUpdate: [Window]?, _ currentScreen: NSScreen) -> ()? {
Expand Down Expand Up @@ -269,6 +274,7 @@ class App: AppCenterApplication, NSApplicationDelegate {
Windows.updateSpaces()
let screen = NSScreen.preferred()
self.shortcutIndex = shortcutIndex
thumbnailsPanel.thumbnailsView.searchField.stringValue = ""
Windows.refreshWhichWindowsToShowTheUser(screen)
Windows.reorderList()
if (!Windows.list.contains { $0.shouldShowTheUser }) { hideUi(); return }
Expand Down
7 changes: 5 additions & 2 deletions src/ui/generic-components/CustomRecorderControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ class CustomRecorderControl: RecorderControl, RecorderControlDelegate {
|| ((id.starts(with: "holdShortcut") || id.starts(with: "nextWindowShortcut")) && id2 == comboShortcutName) {
return false
}
if comboShortcutName?.starts(with: "holdShortcut") ?? false {
// you can have command tab open and close the flow for example
return false
}
if shortcut2.keyCode != (id.starts(with: "holdShortcut") ? comboShortcut?.keyCode : shortcut.keyCode) {
return false
}
Expand All @@ -111,7 +115,6 @@ class CustomRecorderControl: RecorderControl, RecorderControlDelegate {
return false
}
return true
})?
.value
})?.value
}
}
49 changes: 46 additions & 3 deletions src/ui/main-window/ThumbnailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Cocoa

class ThumbnailsView: NSVisualEffectView {
let scrollView = ScrollView()
let searchField = SearchField()
static var recycledViews = [ThumbnailView]()
var rows = [[ThumbnailView]]()

Expand All @@ -11,6 +12,7 @@ class ThumbnailsView: NSVisualEffectView {
state = .active
wantsLayer = true
updateRoundedCorners(Preferences.windowCornerRadius)
addSubview(searchField)
addSubview(scrollView)
// TODO: think about this optimization more
(1...100).forEach { _ in ThumbnailsView.recycledViews.append(ThumbnailView()) }
Expand Down Expand Up @@ -67,9 +69,17 @@ class ThumbnailsView: NSVisualEffectView {
}
}

var lastOpenMaxX: CGFloat = 0
var lastOpenMaxY: CGFloat = 0
func updateItemsAndLayout(_ screen: NSScreen) {
let widthMax = ThumbnailsPanel.widthMax(screen).rounded()
if let (maxX, maxY) = layoutThumbnailViews(screen, widthMax) {
if let (maxXThumbnails, maxYThumbnails) = layoutThumbnailViews(screen, widthMax) {
if !searchField.isTextChangeCallback {
Copy link
Contributor

@decodism decodism Jan 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check that searchField.stringValue is empty.

lastOpenMaxX = maxXThumbnails
lastOpenMaxY = maxYThumbnails
}
let maxX = searchField.isTextChangeCallback ? lastOpenMaxX : maxXThumbnails
let maxY = searchField.isTextChangeCallback ? lastOpenMaxY : maxYThumbnails
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not needed. You can just reuse lastOpenMaxX and lastOpenMaxY.

layoutParentViews(screen, maxX, widthMax, maxY)
if Preferences.alignThumbnails == .center {
centerRows(maxX)
Expand Down Expand Up @@ -143,8 +153,12 @@ class ThumbnailsView: NSVisualEffectView {

private func layoutParentViews(_ screen: NSScreen, _ maxX: CGFloat, _ widthMax: CGFloat, _ maxY: CGFloat) {
let heightMax = ThumbnailsPanel.heightMax(screen).rounded()
frame.size = NSSize(width: min(maxX, widthMax) + Preferences.windowPadding * 2, height: min(maxY, heightMax) + Preferences.windowPadding * 2)
scrollView.frame.size = NSSize(width: min(maxX, widthMax), height: min(maxY, heightMax))
frame.size = NSSize(width: min(maxX, widthMax) + Preferences.windowPadding * 2, height: min(maxY + 40, heightMax) + Preferences.windowPadding * 2)

searchField.frame.size = NSSize(width: min(maxX, widthMax), height: 40)
searchField.frame.origin = CGPoint(x: Preferences.windowPadding, y: min(maxY + 40, heightMax) - Preferences.windowPadding)

scrollView.frame.size = NSSize(width: min(maxX, widthMax), height: min(maxY, heightMax - 40))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a constant for the height of the search field.

scrollView.frame.origin = CGPoint(x: Preferences.windowPadding, y: Preferences.windowPadding)
scrollView.contentView.frame.size = scrollView.frame.size
if App.shared.userInterfaceLayoutDirection == .rightToLeft {
Expand Down Expand Up @@ -296,3 +310,32 @@ enum Direction {
return self == .leading ? 1 : -1
}
}

class SearchField: NSTextField {
convenience init() {
self.init(string: "")
placeholderString = "Search"
drawsBackground = false
isBezeled = false
font = NSFont.systemFont(ofSize: 30)
focusRingType = .none

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could set textColor to Preferences.fontColor.

}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)

let borderColor = NSColor(red: 1, green: 1, blue: 1, alpha: 0.2)
let borderRect = NSMakeRect(0, 39, dirtyRect.size.width, 1)

borderColor.set()
NSBezierPath.fill(borderRect)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could set currentEditor().insertionPointColor to Preferences.fontColor like this:

override func becomeFirstResponder() -> Bool {
    let success = super.becomeFirstResponder()
    if success {
        (currentEditor() as? NSTextView)?.insertionPointColor = Preferences.fontColor
    }
    return success
}

override func textDidChange(_ notification: Notification) {
let screen = NSScreen.preferred()
Windows.refreshWhichWindowsToShowTheUser(screen)
isTextChangeCallback = true
App.app.refreshOpenUi()
isTextChangeCallback = false
}
var isTextChangeCallback = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed.

}
6 changes: 5 additions & 1 deletion src/ui/preferences-window/tabs/ControlsTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class ControlsTab {
"nextWindowShortcut3": { App.app.showUiOrCycleSelection(2) },
"nextWindowShortcut4": { App.app.showUiOrCycleSelection(3) },
"nextWindowShortcut5": { App.app.showUiOrCycleSelection(4) },
"openNextWindowShortcut": { App.app.nextWindowShortcutWithRepeatingKey() },
"previousWindowShortcut": { App.app.previousWindowShortcutWithRepeatingKey() },
"previousWindowShortcut2": { App.app.previousWindowShortcutWithRepeatingKey() },
"→": { App.app.cycleSelection(.right) },
"←": { App.app.cycleSelection(.left) },
"↑": { App.app.cycleSelection(.up) },
Expand All @@ -31,7 +33,9 @@ class ControlsTab {

static func initTab() -> NSView {
let focusWindowShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Focus selected window", comment: ""), "focusWindowShortcut", Preferences.focusWindowShortcut, labelPosition: .right)
let openNextWindowShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Select next window", comment: ""), "openNextWindowShortcut", Preferences.openNextWindowShortcut, labelPosition: .right)
let previousWindowShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Select previous window", comment: ""), "previousWindowShortcut", Preferences.previousWindowShortcut, labelPosition: .right)
let previousWindowShortcut2 = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Select previous window", comment: ""), "previousWindowShortcut2", Preferences.previousWindowShortcut2, labelPosition: .right)
let cancelShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Cancel and hide", comment: ""), "cancelShortcut", Preferences.cancelShortcut, labelPosition: .right)
let closeWindowShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Close window", comment: ""), "closeWindowShortcut", Preferences.closeWindowShortcut, labelPosition: .right)
let minDeminWindowShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Minimize/Deminimize window", comment: ""), "minDeminWindowShortcut", Preferences.minDeminWindowShortcut, labelPosition: .right)
Expand All @@ -45,7 +49,7 @@ class ControlsTab {
let selectWindowCheckboxes = StackView([StackView(enableArrows), StackView(enableMouse)], .vertical)
let miscCheckboxesExplanations = LabelAndControl.makeLabel(NSLocalizedString("Miscellaneous:", comment: ""))
let miscCheckboxes = StackView([StackView(enableCursorFollowFocus)], .vertical)
let shortcuts = StackView([focusWindowShortcut, previousWindowShortcut, cancelShortcut, closeWindowShortcut, minDeminWindowShortcut, quitAppShortcut, hideShowAppShortcut].map { (view: [NSView]) in StackView(view) }, .vertical)
let shortcuts = StackView([focusWindowShortcut, openNextWindowShortcut, previousWindowShortcut, previousWindowShortcut2, cancelShortcut, closeWindowShortcut, minDeminWindowShortcut, quitAppShortcut, hideShowAppShortcut].map { (view: [NSView]) in StackView(view) }, .vertical)
let orPress = LabelAndControl.makeLabel(NSLocalizedString("While open, press:", comment: ""), shouldFit: false)
let (holdShortcut, nextWindowShortcut, tab1View) = toShowSection(0)
let (holdShortcut2, nextWindowShortcut2, tab2View) = toShowSection(1)
Expand Down