Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 13, 2022
1 parent 4eb0766 commit 218ef6c
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 49 deletions.
24 changes: 12 additions & 12 deletions Color Picker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
objects = {

/* Begin PBXBuildFile section */
E38D432F263AAE8500701B82 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E38D432E263AAE8500701B82 /* SettingsView.swift */; };
E38D432F263AAE8500701B82 /* SettingsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = E38D432E263AAE8500701B82 /* SettingsScreen.swift */; };
E38D4331263AAEA900701B82 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = E38D4330263AAEA900701B82 /* Constants.swift */; };
E38D4333263AB24E00701B82 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = E38D4332263AB24E00701B82 /* Utilities.swift */; };
E38D4335263AEE3700701B82 /* ColorPickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E38D4334263AEE3700701B82 /* ColorPickerView.swift */; };
E38D4335263AEE3700701B82 /* ColorPickerScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = E38D4334263AEE3700701B82 /* ColorPickerScreen.swift */; };
E390D734263B3C71005FCB34 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = E390D733263B3C71005FCB34 /* AppState.swift */; };
E390D736263C6ACD005FCB34 /* ColorPanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E390D735263C6ACD005FCB34 /* ColorPanel.swift */; };
E390D738263C74C2005FCB34 /* WelcomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E390D737263C74C2005FCB34 /* WelcomeView.swift */; };
E390D738263C74C2005FCB34 /* WelcomeScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = E390D737263C74C2005FCB34 /* WelcomeScreen.swift */; };
E394C2F62642BE4C006B51CF /* InternetAccessPolicy.json in Resources */ = {isa = PBXBuildFile; fileRef = E394C2F52642BE4C006B51CF /* InternetAccessPolicy.json */; };
E394DAAB263D73AD00F5B042 /* Regex in Frameworks */ = {isa = PBXBuildFile; productRef = E394DAAA263D73AD00F5B042 /* Regex */; };
E394DAAE263E95D900F5B042 /* LaunchAtLogin in Frameworks */ = {isa = PBXBuildFile; productRef = E394DAAD263E95D900F5B042 /* LaunchAtLogin */; };
Expand Down Expand Up @@ -57,13 +57,13 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
E38D432E263AAE8500701B82 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
E38D432E263AAE8500701B82 /* SettingsScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsScreen.swift; sourceTree = "<group>"; };
E38D4330263AAEA900701B82 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
E38D4332263AB24E00701B82 /* Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
E38D4334263AEE3700701B82 /* ColorPickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorPickerView.swift; sourceTree = "<group>"; };
E38D4334263AEE3700701B82 /* ColorPickerScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorPickerScreen.swift; sourceTree = "<group>"; };
E390D733263B3C71005FCB34 /* AppState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = "<group>"; };
E390D735263C6ACD005FCB34 /* ColorPanel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorPanel.swift; sourceTree = "<group>"; };
E390D737263C74C2005FCB34 /* WelcomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeView.swift; sourceTree = "<group>"; };
E390D737263C74C2005FCB34 /* WelcomeScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeScreen.swift; sourceTree = "<group>"; };
E394C2F52642BE4C006B51CF /* InternetAccessPolicy.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = InternetAccessPolicy.json; sourceTree = "<group>"; };
E3A3B11925904E7B001B4D0C /* Color Picker.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Color Picker.app"; sourceTree = BUILT_PRODUCTS_DIR; };
E3A3B11C25904E7B001B4D0C /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -150,10 +150,10 @@
E38D4330263AAEA900701B82 /* Constants.swift */,
E390D733263B3C71005FCB34 /* AppState.swift */,
E3DFA8C82662514800D2623E /* Events.swift */,
E38D4334263AEE3700701B82 /* ColorPickerView.swift */,
E38D4334263AEE3700701B82 /* ColorPickerScreen.swift */,
E390D735263C6ACD005FCB34 /* ColorPanel.swift */,
E38D432E263AAE8500701B82 /* SettingsView.swift */,
E390D737263C74C2005FCB34 /* WelcomeView.swift */,
E38D432E263AAE8500701B82 /* SettingsScreen.swift */,
E390D737263C74C2005FCB34 /* WelcomeScreen.swift */,
E38D4332263AB24E00701B82 /* Utilities.swift */,
E3A3B12025904E7D001B4D0C /* Assets.xcassets */,
E3E7D7A827218959009D71F4 /* Intents.intentdefinition */,
Expand Down Expand Up @@ -332,15 +332,15 @@
buildActionMask = 2147483647;
files = (
E3A3B11D25904E7B001B4D0C /* App.swift in Sources */,
E38D432F263AAE8500701B82 /* SettingsView.swift in Sources */,
E38D432F263AAE8500701B82 /* SettingsScreen.swift in Sources */,
E38D4333263AB24E00701B82 /* Utilities.swift in Sources */,
E390D738263C74C2005FCB34 /* WelcomeView.swift in Sources */,
E390D738263C74C2005FCB34 /* WelcomeScreen.swift in Sources */,
E390D734263B3C71005FCB34 /* AppState.swift in Sources */,
E38D4331263AAEA900701B82 /* Constants.swift in Sources */,
E3DFA8C92662514800D2623E /* Events.swift in Sources */,
E390D736263C6ACD005FCB34 /* ColorPanel.swift in Sources */,
E3E7D7A927218959009D71F4 /* Intents.intentdefinition in Sources */,
E38D4335263AEE3700701B82 /* ColorPickerView.swift in Sources */,
E38D4335263AEE3700701B82 /* ColorPickerScreen.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion Color Picker/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct AppMain: App {
}
}
Settings {
SettingsView()
SettingsScreen()
}
}

Expand Down
16 changes: 7 additions & 9 deletions Color Picker/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class AppState: ObservableObject {
]
colorPanel.makeMain()

let view = ColorPickerView(colorPanel: colorPanel)
let view = ColorPickerScreen(colorPanel: colorPanel)
.environmentObject(self)
let accessoryView = NSHostingView(rootView: view)
colorPanel.accessoryView = accessoryView
Expand Down Expand Up @@ -93,30 +93,28 @@ final class AppState: ObservableObject {

let item = $0

$0.button!.onAction { [self] in
let event = NSApp.currentEvent!
$0.button!.onAction { [self] event in
let isAlternative = event.isAlternateClickForStatusItem

let showMenu = {
item.menu = createMenu()
item.button!.performClick(nil)
item.menu = nil
item.showMenu(createMenu())
}

switch Defaults[.menuBarItemClickAction] {
case .showMenu:
if event.type == .rightMouseUp {
if isAlternative {
pickColor()
} else {
showMenu()
}
case .showColorSampler:
if event.type == .rightMouseUp {
if isAlternative {
showMenu()
} else {
pickColor()
}
case .toggleWindow:
if event.type == .rightMouseUp {
if isAlternative {
showMenu()
} else {
colorPanel.toggle()
Expand Down
1 change: 1 addition & 0 deletions Color Picker/ColorPanel.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Cocoa
import Defaults

@MainActor
final class ColorPanel: NSColorPanel, NSWindowDelegate {
override var canBecomeMain: Bool { true }
override var canBecomeKey: Bool { true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private struct BarView: View {
}
}

struct ColorPickerView: View {
struct ColorPickerScreen: View {
@Default(.uppercaseHexColor) private var uppercaseHexColor
@Default(.hashPrefixInHexColor) private var hashPrefixInHexColor
@Default(.legacyColorSyntax) private var legacyColorSyntax
Expand Down Expand Up @@ -369,8 +369,8 @@ struct ColorPickerView: View {
}
}

struct ColorPickerView_Previews: PreviewProvider {
struct ColorPickerScreen_Previews: PreviewProvider {
static var previews: some View {
ColorPickerView(colorPanel: .shared)
ColorPickerScreen(colorPanel: .shared)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private struct MenuBarItemClickActionSetting: View {
}
.fixedSize()
Text(menuBarItemClickAction.tip)
.offset(x: 2)
.settingSubtitleTextStyle()
.frame(maxWidth: .infinity, alignment: .trailing)
}
Expand Down Expand Up @@ -197,7 +198,7 @@ private struct AdvancedSettings: View {
}
}

struct SettingsView: View {
struct SettingsScreen: View {
var body: some View {
TabView {
GeneralSettings()
Expand All @@ -216,8 +217,8 @@ struct SettingsView: View {
}
}

struct SettingsView_Previews: PreviewProvider {
struct SettingsScreen_Previews: PreviewProvider {
static var previews: some View {
SettingsView()
SettingsScreen()
}
}
83 changes: 62 additions & 21 deletions Color Picker/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,10 @@ extension NSColor {
color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)

return .init(
red: red.double,
green: green.double,
blue: blue.double,
alpha: alpha.double
red: red,
green: green,
blue: blue,
alpha: alpha
)
}
}
Expand Down Expand Up @@ -1191,18 +1191,6 @@ struct NativeTextField: NSViewRepresentable {
}


extension NSColorPanel {
/**
Show the color sampler.
*/
func showColorSampler() {
// "_magnify:"
let selector = String(":yfingam_".reversed())
perform(NSSelectorFromString(selector))
}
}


extension NSColorPanel {
/**
Publishes when the color in the color panel changes.
Expand Down Expand Up @@ -1333,15 +1321,15 @@ protocol ControlActionClosureProtocol: NSObjectProtocol {
}

private final class ActionTrampoline: NSObject {
private let action: () -> Void
private let action: (NSEvent) -> Void

init(action: @escaping () -> Void) {
init(action: @escaping (NSEvent) -> Void) {
self.action = action
}

@objc
fileprivate func handleAction(_ sender: AnyObject) {
action()
action(NSApp.currentEvent!)
}
}

Expand All @@ -1352,12 +1340,12 @@ extension ControlActionClosureProtocol {
```
let button = NSButton(title: "Unicorn", target: nil, action: nil)
button.onAction {
button.onAction { _ in
print("Button action")
}
```
*/
func onAction(_ action: @escaping () -> Void) {
func onAction(_ action: @escaping (NSEvent) -> Void) {
let trampoline = ActionTrampoline(action: action)
target = trampoline
self.action = #selector(ActionTrampoline.handleAction)
Expand Down Expand Up @@ -2772,6 +2760,7 @@ extension Shape where Self == RoundedRectangle {


extension View {
@available(macOS, obsoleted: 12)
@ViewBuilder
func menuIndicatorHidden() -> some View {
if #available(macOS 12, *) {
Expand Down Expand Up @@ -2806,3 +2795,55 @@ extension Task where Success == Never, Failure == Never {
try await sleep(nanoseconds: UInt64(seconds * Double(NSEC_PER_SEC)))
}
}


extension NSStatusItem {
/**
Show a one-time menu from the status item.
*/
func showMenu(_ menu: NSMenu) {
self.menu = menu
button!.performClick(nil)
self.menu = nil
}
}


extension NSEvent {
/**
Real modifiers.
- Note: Prefer this over `.modifierFlags`.
```
// Check if Command is one of possible more modifiers keys
event.modifiers.contains(.command)
// Check if Command is the only modifier key
event.modifiers == .command
// Check if Command and Shift are the only modifiers
event.modifiers == [.command, .shift]
```
*/
var modifiers: ModifierFlags {
modifierFlags
.intersection(.deviceIndependentFlagsMask)
// We remove `capsLock` as it shouldn't affect the modifiers.
// We remove `numericPad`/`function` as arrow keys trigger it, use `event.specialKeys` instead.
.subtracting([.capsLock, .numericPad, .function])
}
}


extension NSEvent {
var isAlternateMouseUp: Bool {
type == .rightMouseUp
|| (type == .leftMouseUp && modifiers == .control)
}

var isAlternateClickForStatusItem: Bool {
isAlternateMouseUp
|| (type == .leftMouseUp && modifiers == .option)
}
}
File renamed without changes.

0 comments on commit 218ef6c

Please sign in to comment.