Skip to content

Commit

Permalink
Merge branch 'sindresorhus:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
godbout authored Oct 23, 2024
2 parents 60076d6 + 9203a8d commit 645d89a
Show file tree
Hide file tree
Showing 31 changed files with 396 additions and 104 deletions.
43 changes: 35 additions & 8 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
only_rules:
- anyobject_protocol
- accessibility_trait_for_button
- array_init
- blanket_disable_command
- block_based_kvo
- class_delegate_protocol
- closing_brace
Expand All @@ -10,6 +11,7 @@ only_rules:
- collection_alignment
- colon
- comma
- comma_inheritance
- compiler_protocol_init
- computed_accessors_order
- conditional_returns_on_newline
Expand All @@ -20,13 +22,15 @@ only_rules:
- control_statement
- custom_rules
- deployment_target
- direct_return
- discarded_notification_center_observer
- discouraged_assert
- discouraged_direct_init
- discouraged_none_name
- discouraged_object_literal
- discouraged_optional_boolean
- discouraged_optional_collection
- duplicate_conditions
- duplicate_enum_cases
- duplicate_imports
- duplicated_key_in_dictionary_literal
Expand All @@ -42,6 +46,7 @@ only_rules:
- explicit_init
- fallthrough
- fatal_error_message
- final_test_case
- first_where
- flatmap_over_map_reduce
- for_where
Expand All @@ -52,7 +57,7 @@ only_rules:
- implicit_getter
- implicit_return
- inclusive_language
- inert_defer
- invalid_swiftlint_command
- is_disjoint
- joined_default_parameter
- last_where
Expand All @@ -69,6 +74,7 @@ only_rules:
- mark
- modifier_order
- multiline_arguments
- multiline_arguments_brackets
- multiline_function_chains
- multiline_literal_brackets
- multiline_parameters
Expand All @@ -77,19 +83,22 @@ only_rules:
- no_extension_access_modifier
- no_fallthrough_only
- no_space_in_method_call
- non_optional_string_data_conversion
- non_overridable_class_declaration
- notification_center_detachment
- ns_number_init_as_function_reference
- nsobject_prefer_isequal
- number_separator
- opening_brace
- operator_usage_whitespace
- operator_whitespace
- orphaned_doc_comment
- overridden_super_call
- prefer_self_in_static_references
- prefer_self_type_over_type_of_self
- prefer_zero_over_explicit_init
- private_action
- private_outlet
- private_subject
- private_swiftui_state
- private_unit_test
- prohibited_super_call
- protocol_property_accessors_order
Expand All @@ -105,12 +114,18 @@ only_rules:
- redundant_void_return
- required_enum_case
- return_arrow_whitespace
- return_value_from_void_function
- self_binding
- self_in_property_initialization
- shorthand_operator
- shorthand_optional_binding
- sorted_first_last
- statement_position
- static_operator
- static_over_final_class
- strong_iboutlet
- superfluous_disable_command
- superfluous_else
- switch_case_alignment
- switch_case_on_newline
- syntactic_sugar
Expand All @@ -121,12 +136,13 @@ only_rules:
- trailing_newline
- trailing_semicolon
- trailing_whitespace
- unavailable_condition
- unavailable_function
- unneeded_break_in_switch
- unneeded_override
- unneeded_parentheses_in_closure_argument
- unowned_variable_capture
- untyped_error_in_catch
- unused_capture_list
- unused_closure_parameter
- unused_control_flow_label
- unused_enumerated
Expand All @@ -137,14 +153,19 @@ only_rules:
- vertical_parameter_alignment_on_call
- vertical_whitespace_closing_braces
- vertical_whitespace_opening_braces
- void_function_in_ternary
- void_return
- xct_specific_matcher
- xctfail_message
- yoda_condition
analyzer_rules:
- capture_variable
- typesafe_array_init
- unneeded_synthesized_initializer
- unused_declaration
- unused_import
for_where:
allow_for_as_filter: true
number_separator:
minimum_length: 5
identifier_name:
Expand All @@ -154,7 +175,6 @@ identifier_name:
min_length:
warning: 2
error: 2
validates_start_with_lowercase: false
allowed_symbols:
- '_'
excluded:
Expand All @@ -168,6 +188,10 @@ identifier_name:
- 'y1'
- 'y2'
- 'z2'
redundant_type_annotation:
consider_default_literal_types_redundant: true
unneeded_override:
affect_initializers: true
deployment_target:
macOS_deployment_target: '10.11'
custom_rules:
Expand All @@ -191,11 +215,14 @@ custom_rules:
regex: '\bCGFloat\('
message: 'Use Double instead of CGFloat'
swiftui_state_private:
regex: '@(State|StateObject|ObservedObject|EnvironmentObject)\s+var'
message: 'SwiftUI @State/@StateObject/@ObservedObject/@EnvironmentObject properties should be private'
regex: '@(ObservedObject|EnvironmentObject)\s+var'
message: 'SwiftUI @ObservedObject and @EnvironmentObject properties should be private'
swiftui_environment_private:
regex: '@Environment\(\\\.\w+\)\s+var'
message: 'SwiftUI @Environment properties should be private'
final_class:
regex: '^class [a-zA-Z\d]+[^{]+\{'
message: 'Classes should be marked as final whenever possible. If you actually need it to be subclassable, just add `// swiftlint:disable:next final_class`.'
no_alignment_center:
regex: '\b\(alignment: .center\b'
message: 'This alignment is the default.'
6 changes: 4 additions & 2 deletions Example/KeyboardShortcutsExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 12.3;
MACOSX_DEPLOYMENT_TARGET = 14.4;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -290,7 +290,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 12.3;
MACOSX_DEPLOYMENT_TARGET = 14.4;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = macosx;
Expand All @@ -315,6 +315,7 @@
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
Expand Down Expand Up @@ -343,6 +344,7 @@
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
Expand Down
4 changes: 1 addition & 3 deletions Example/KeyboardShortcutsExample/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import SwiftUI

@main
struct AppMain: App {
@StateObject private var state = AppState()

var body: some Scene {
WindowGroup {
MainScreen()
.task {
state.createMenus()
AppState.shared.createMenus()
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions Example/KeyboardShortcutsExample/AppState.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import SwiftUI

@MainActor
final class AppState: ObservableObject {
final class AppState {
static let shared = AppState()

private init() {}

func createMenus() {
let testMenuItem = NSMenuItem()
NSApp.mainMenu?.addItem(testMenuItem)
Expand All @@ -13,22 +17,22 @@ final class AppState: ObservableObject {
testMenu.addCallbackItem("Shortcut 1") { [weak self] in
self?.alert(1)
}
.setShortcut(for: .testShortcut1)
.setShortcut(for: .testShortcut1)

testMenu.addCallbackItem("Shortcut 2") { [weak self] in
self?.alert(2)
}
.setShortcut(for: .testShortcut2)
.setShortcut(for: .testShortcut2)

testMenu.addCallbackItem("Shortcut 3") { [weak self] in
self?.alert(3)
}
.setShortcut(for: .testShortcut3)
.setShortcut(for: .testShortcut3)

testMenu.addCallbackItem("Shortcut 4") { [weak self] in
self?.alert(4)
}
.setShortcut(for: .testShortcut4)
.setShortcut(for: .testShortcut4)
}

private func alert(_ number: Int) {
Expand Down
64 changes: 33 additions & 31 deletions Example/KeyboardShortcutsExample/MainScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ private struct DynamicShortcutRecorder: View {
Text("Pressed? \(isPressed ? "👍" : "👎")")
.frame(width: 100, alignment: .leading)
}
.onChange(of: name) { _ in
isFocused = true
}
.onChange(of: name) {
isFocused = true
}
}
}

Expand Down Expand Up @@ -57,13 +57,18 @@ private struct DynamicShortcut: View {
Divider()
DynamicShortcutRecorder(name: $shortcut.name, isPressed: $isPressed)
}
}
.frame(maxWidth: 300)
.padding()
.padding(.bottom, 20)
.onChange(of: shortcut) { [oldValue = shortcut] in
onShortcutChange(oldValue: oldValue, newValue: $0)
Divider()
.padding(.vertical)
Button("Reset All") {
KeyboardShortcuts.resetAll()
}
}
.frame(maxWidth: 300)
.padding()
.padding(.bottom, 20)
.onChange(of: shortcut) { oldValue, newValue in
onShortcutChange(oldValue: oldValue, newValue: newValue)
}
}

private func onShortcutChange(oldValue: Shortcut, newValue: Shortcut) {
Expand Down Expand Up @@ -93,30 +98,27 @@ private struct DoubleShortcut: View {
KeyboardShortcuts.Recorder(for: .testShortcut2) {
Text("Shortcut 2:") // Intentionally using the verbose initializer for testing.
}
.overlay(alignment: .trailing) {
Text("Pressed? \(isPressed2 ? "👍" : "👎")")
.offset(x: 90)
}
Spacer()
Button("Reset All") {
KeyboardShortcuts.reset(.testShortcut1, .testShortcut2)
.overlay(alignment: .trailing) {
Text("Pressed? \(isPressed2 ? "👍" : "👎")")
.offset(x: 90)
}
Spacer()
}
.offset(x: -40)
.frame(maxWidth: 300)
.padding()
.padding()
.onKeyboardShortcut(.testShortcut1) {
isPressed1 = $0 == .keyDown
}
.onKeyboardShortcut(.testShortcut2, type: .keyDown) {
isPressed2 = true
}
.task {
KeyboardShortcuts.onKeyUp(for: .testShortcut2) {
isPressed2 = false
}
.offset(x: -40)
.frame(maxWidth: 300)
.padding()
.padding()
.onGlobalKeyboardShortcut(.testShortcut1) {
isPressed1 = $0 == .keyDown
}
.onGlobalKeyboardShortcut(.testShortcut2, type: .keyDown) {
isPressed2 = true
}
.task {
KeyboardShortcuts.onKeyUp(for: .testShortcut2) {
isPressed2 = false
}
}
}
}

Expand All @@ -127,7 +129,7 @@ struct MainScreen: View {
Divider()
DynamicShortcut()
}
.frame(width: 400, height: 320)
.frame(width: 400, height: 320)
}
}

Expand Down
5 changes: 2 additions & 3 deletions Example/KeyboardShortcutsExample/Utilities.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftUI


@MainActor
final class CallbackMenuItem: NSMenuItem {
private static var validateCallback: ((NSMenuItem) -> Bool)?

Expand Down Expand Up @@ -49,7 +49,6 @@ extension CallbackMenuItem: NSMenuItemValidation {
}
}


extension NSMenuItem {
convenience init(
_ title: String,
Expand Down Expand Up @@ -80,8 +79,8 @@ extension NSMenuItem {
}
}


extension NSMenu {
@MainActor
@discardableResult
func addCallbackItem(
_ title: String,
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.9
// swift-tools-version:5.11
import PackageDescription

let package = Package(
Expand Down
Loading

0 comments on commit 645d89a

Please sign in to comment.