Skip to content

Commit

Permalink
feat: add second shortcut to active the app (closes #237)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Jul 20, 2020
1 parent cf7b076 commit a6285ba
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 42 deletions.
21 changes: 14 additions & 7 deletions src/logic/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Preferences {
"fontHeight": "15",
"holdShortcut": "",
"nextWindowShortcut": "",
"nextWindowShortcut2": "`",
"previousWindowShortcut": "",
"cancelShortcut": "",
"closeWindowShortcut": "W",
Expand All @@ -20,17 +21,23 @@ class Preferences {
"arrowKeysEnabled": "true",
"mouseHoverEnabled": "true",
"showFullscreenWindows": "true",
"showFullscreenWindows2": "true",
"showMinimizedWindows": "true",
"showMinimizedWindows2": "true",
"showHiddenWindows": "true",
"showHiddenWindows2": "true",
"showTabsAsWindows": "false",
"windowDisplayDelay": "0",
"theme": "0",
"showOnScreen": "0",
"titleTruncation": "0",
"alignThumbnails": "0",
"appsToShow": "0",
"appsToShow2": "1",
"spacesToShow": "0",
"spacesToShow2": "0",
"screensToShow": "0",
"screensToShow2": "0",
"fadeOutAnimation": "false",
"hideSpaceNumberLabels": "false",
"startAtLogin": "true",
Expand All @@ -57,7 +64,7 @@ class Preferences {
static var iconSize: CGFloat { defaults.cgfloat("iconSize") }
static var fontHeight: CGFloat { defaults.cgfloat("fontHeight") }
static var holdShortcut: String { defaults.string("holdShortcut") }
static var nextWindowShortcut: String { defaults.string("nextWindowShortcut") }
static var nextWindowShortcut: [String] { ["nextWindowShortcut", "nextWindowShortcut2"].map { defaults.string($0) } }
static var previousWindowShortcut: String { defaults.string("previousWindowShortcut") }
static var cancelShortcut: String { defaults.string("cancelShortcut") }
static var closeWindowShortcut: String { defaults.string("closeWindowShortcut") }
Expand All @@ -66,9 +73,9 @@ class Preferences {
static var hideShowAppShortcut: String { defaults.string("hideShowAppShortcut") }
static var arrowKeysEnabled: Bool { defaults.bool("arrowKeysEnabled") }
static var mouseHoverEnabled: Bool { defaults.bool("mouseHoverEnabled") }
static var showFullscreenWindows: Bool { defaults.bool("showFullscreenWindows") }
static var showMinimizedWindows: Bool { defaults.bool("showMinimizedWindows") }
static var showHiddenWindows: Bool { defaults.bool("showHiddenWindows") }
static var showFullscreenWindows: [Bool] { ["showFullscreenWindows", "showFullscreenWindows2"].map { defaults.bool($0) } }
static var showMinimizedWindows: [Bool] { ["showMinimizedWindows", "showMinimizedWindows2"].map { defaults.bool($0) } }
static var showHiddenWindows: [Bool] { ["showHiddenWindows", "showHiddenWindows2"].map { defaults.bool($0) } }
static var showTabsAsWindows: Bool { defaults.bool("showTabsAsWindows") }
static var windowDisplayDelay: DispatchTimeInterval { DispatchTimeInterval.milliseconds(defaults.int("windowDisplayDelay")) }
static var fadeOutAnimation: Bool { defaults.bool("fadeOutAnimation") }
Expand All @@ -84,9 +91,9 @@ class Preferences {
static var showOnScreen: ShowOnScreenPreference { defaults.macroPref("showOnScreen", ShowOnScreenPreference.allCases) }
static var titleTruncation: TitleTruncationPreference { defaults.macroPref("titleTruncation", TitleTruncationPreference.allCases) }
static var alignThumbnails: AlignThumbnailsPreference { defaults.macroPref("alignThumbnails", AlignThumbnailsPreference.allCases) }
static var appsToShow: AppsToShowPreference { defaults.macroPref("appsToShow", AppsToShowPreference.allCases) }
static var spacesToShow: SpacesToShowPreference { defaults.macroPref("spacesToShow", SpacesToShowPreference.allCases) }
static var screensToShow: ScreensToShowPreference { defaults.macroPref("screensToShow", ScreensToShowPreference.allCases) }
static var appsToShow: [AppsToShowPreference] { ["appsToShow", "appsToShow2"].map { defaults.macroPref($0, AppsToShowPreference.allCases) } }
static var spacesToShow: [SpacesToShowPreference] { ["spacesToShow", "spacesToShow2"].map { defaults.macroPref($0, SpacesToShowPreference.allCases) } }
static var screensToShow: [ScreensToShowPreference] { ["screensToShow", "screensToShow2"].map { defaults.macroPref($0, ScreensToShowPreference.allCases) } }

// derived values
static var cellBorderWidth: CGFloat { theme.themeParameters.cellBorderWidth }
Expand Down
12 changes: 6 additions & 6 deletions src/logic/Windows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ class Windows {

static func refreshIfWindowShouldBeShownToTheUser(_ window: Window, _ screen: NSScreen) {
window.shouldShowTheUser =
!(!Preferences.showFullscreenWindows && window.isFullscreen) &&
!(!Preferences.showMinimizedWindows && window.isMinimized) &&
!(!Preferences.showHiddenWindows && window.isHidden) &&
!(Preferences.appsToShow == .active && window.application.runningApplication != NSWorkspace.shared.frontmostApplication) &&
!(Preferences.spacesToShow == .active && window.spaceId != Spaces.currentSpaceId) &&
!(Preferences.screensToShow == .showingAltTab && !isOnScreen(window, screen)) &&
!(!Preferences.showFullscreenWindows[App.app.shortcutIndex] && window.isFullscreen) &&
!(!Preferences.showMinimizedWindows[App.app.shortcutIndex] && window.isMinimized) &&
!(!Preferences.showHiddenWindows[App.app.shortcutIndex] && window.isHidden) &&
!(Preferences.appsToShow[App.app.shortcutIndex] == .active && window.application.runningApplication != NSWorkspace.shared.frontmostApplication) &&
!(Preferences.spacesToShow[App.app.shortcutIndex] == .active && window.spaceId != Spaces.currentSpaceId) &&
!(Preferences.screensToShow[App.app.shortcutIndex] == .showingAltTab && !isOnScreen(window, screen)) &&
(Preferences.showTabsAsWindows || !window.isTabbed) &&
!Preferences.dontShowBlacklist.contains(window.application.runningApplication.bundleIdentifier ?? "")
}
Expand Down
12 changes: 10 additions & 2 deletions src/ui/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class App: NSApplication, NSApplicationDelegate {
var isFirstSummon = true
var appIsBeingUsed = false
var shortcutsShouldBeDisabled = false
var shortcutIndex = 0

override init() {
super.init()
Expand All @@ -33,6 +34,9 @@ class App: NSApplication, NSApplicationDelegate {
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
#if DEBUG
UserDefaults.standard.set(true, forKey: "NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints")
#endif
#if !DEBUG
PFMoveToApplicationsFolderIfNecessary()
#endif
Expand All @@ -53,6 +57,9 @@ class App: NSApplication, NSApplicationDelegate {
// TODO: undeterministic; events in the queue may still be processing; good enough for now
DispatchQueue.main.async { () -> () in Windows.sortByLevel() }
self.preloadWindows()
#if DEBUG
self.preferencesWindowController.show()
#endif
}
}

Expand Down Expand Up @@ -164,7 +171,7 @@ class App: NSApplication, NSApplicationDelegate {

@objc func showUi() {
appIsBeingUsed = true
DispatchQueue.main.async { () -> () in self.showUiOrCycleSelection() }
DispatchQueue.main.async { () -> () in self.showUiOrCycleSelection(0) }
}

func cycleSelection(_ direction: Direction) {
Expand Down Expand Up @@ -215,7 +222,7 @@ class App: NSApplication, NSApplicationDelegate {
}
}

func showUiOrCycleSelection() {
func showUiOrCycleSelection(_ shortcutIndex: Int) {
debugPrint("showUiOrCycleSelection")
if isFirstSummon {
debugPrint("showUiOrCycleSelection: isFirstSummon")
Expand All @@ -231,6 +238,7 @@ class App: NSApplication, NSApplicationDelegate {
Spaces.idsAndIndexes = Spaces.allIdsAndIndexes()
Windows.updateSpaces()
let screen = Screen.preferred()
self.shortcutIndex = shortcutIndex
Windows.refreshWhichWindowsToShowTheUser(screen)
if (!Windows.list.contains { $0.shouldShowTheUser }) { hideUi(); return }
Windows.updateFocusedWindowIndex(0)
Expand Down
1 change: 1 addition & 0 deletions src/ui/generic-components/CustomRecorderControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class CustomRecorderControl: RecorderControl, RecorderControlDelegate {

convenience init(_ shortcutString: String, _ clearable: Bool) {
self.init(frame: .zero)
translatesAutoresizingMaskIntoConstraints = false
self.clearable = clearable
delegate = self
allowsEscapeToCancelRecording = false
Expand Down
15 changes: 9 additions & 6 deletions src/ui/generic-components/StackView.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import Cocoa

class StackView: NSStackView {
convenience init(_ views: [NSView], _ orientation: NSUserInterfaceLayoutOrientation = .horizontal, top: CGFloat = 0, right: CGFloat = 0, bottom: CGFloat = 0, left: CGFloat = 0) {
convenience init(_ views: [NSView], _ orientation: NSUserInterfaceLayoutOrientation = .horizontal, _ shouldFit: Bool = true, top: CGFloat = 0, right: CGFloat = 0, bottom: CGFloat = 0, left: CGFloat = 0) {
self.init(views: views)
translatesAutoresizingMaskIntoConstraints = false
edgeInsets = NSEdgeInsets(top: top, left: left, bottom: bottom, right: right)
alignment = orientation == .horizontal ? .firstBaseline : .leading
// workaround: for some reason, horizontal stackviews with a RecorderControl have extra fittingSize.height
if orientation == .horizontal && (views.contains { $0 is CustomRecorderControl }) {
fit(fittingSize.width, fittingSize.height - 7)
} else {
fit()
if shouldFit {
// workaround: for some reason, horizontal stackviews with a RecorderControl have extra fittingSize.height
if orientation == .horizontal && (views.contains { $0 is CustomRecorderControl }) {
fit(fittingSize.width, fittingSize.height - 7)
} else {
fit()
}
}
}
}
11 changes: 8 additions & 3 deletions src/ui/generic-components/text/TextField.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import Cocoa

class TextField: NSTextField {
// NSTextField has 2px insets left and right by default; we remove those
let insets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
override var alignmentRectInsets: NSEdgeInsets { insets }

convenience init(_ labelWithString: String) {
self.init(labelWithString: labelWithString)
translatesAutoresizingMaskIntoConstraints = false
}

convenience init(_ attributedString: NSAttributedString) {
self.init(labelWithAttributedString: TextField.forceLeftToRight(attributedString))
translatesAutoresizingMaskIntoConstraints = false
}

// we know the content to display should be left-to-right, so we force it to avoid displayed it right-to-left
Expand All @@ -16,7 +24,4 @@ class TextField: NSTextField {
forced.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: forced.length))
return forced
}

// NSTextField has 2px insets left and right by default; we remove those
override var alignmentRectInsets: NSEdgeInsets { insets }
}
15 changes: 14 additions & 1 deletion src/ui/preferences-window/LabelAndControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class LabelAndControl: NSObject {

static func makeLabelWithCheckbox(_ labelText: String, _ rawName: String, extraAction: ActionClosure? = nil, labelPosition: LabelPosition = .leftWithSeparator) -> [NSView] {
let checkbox = NSButton(checkboxWithTitle: labelPosition == .right ? labelText : "", target: nil, action: nil)
checkbox.translatesAutoresizingMaskIntoConstraints = false
checkbox.state = (Preferences.getString(rawName)! as NSString).boolValue ? .on : .off
let views = makeLabelWithProvidedControl(labelText, rawName, checkbox, labelPosition: labelPosition, extraAction: extraAction)
return views
Expand All @@ -39,6 +40,7 @@ class LabelAndControl: NSObject {

static func makeDropDown(_ rawName: String, _ macroPreferences: [MacroPreference]) -> NSPopUpButton {
let popUp = NSPopUpButton()
popUp.translatesAutoresizingMaskIntoConstraints = false
popUp.addItems(withTitles: macroPreferences.map { $0.localizedString })
popUp.selectItem(at: Int(Preferences.getString(rawName)!)!)
return popUp
Expand Down Expand Up @@ -97,7 +99,7 @@ class LabelAndControl: NSObject {
}

static func makeLabel(_ labelText: String, _ labelPosition: LabelPosition = .leftWithoutSeparator) -> NSTextField {
let label = NSTextField(wrappingLabelWithString: labelText)
let label = TextField(labelText)
label.isSelectable = false
label.usesSingleLineMode = true
label.alignment = .right
Expand Down Expand Up @@ -150,3 +152,14 @@ class LabelAndControl: NSObject {
enum ControlIdentifierDiscriminator: String {
case SUFFIX = "_suffix"
}

class TabView: NSTabView {
// without the left/right value, the whole view is shifted to the right for some reason
// let insets = NSEdgeInsets(top: 0, left: 7, bottom: 0, right: 7)
// override var alignmentRectInsets: NSEdgeInsets { get { insets } }

convenience init() {
self.init(frame: .zero)
translatesAutoresizingMaskIntoConstraints = false
}
}
Loading

0 comments on commit a6285ba

Please sign in to comment.