Skip to content

Commit

Permalink
Improves ShowSettingsAction.
Browse files Browse the repository at this point in the history
- Rename action name.
- Define Mac-specific action.
  • Loading branch information
mntone committed Jan 23, 2024
1 parent ff26d2f commit e6a15ea
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 59 deletions.
6 changes: 3 additions & 3 deletions src/App/Views/Modifiers/SharedGameListModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import SwiftUI
struct SharedGameListModifier: ViewModifier {
let viewModel: HomeViewModel

@Environment(\.presentSettingsSheetAction)
private var presentSettingsSheetAction
@Environment(\.showSettings)
private var showSettings

func body(content: Content) -> some View {
content
#if !os(macOS)
.toolbarItemBackport(alignment: .leading) {
Button("Settings",
systemImage: "gearshape",
action: presentSettingsSheetAction.callAsFunction)
action: showSettings.callAsFunction)
#if !os(watchOS)
.keyboardShortcut(",", modifiers: [.command])
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/App/Views/Navigations/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct ContentView: View {
.sheet(isPresented: $isSettingsPresented) {
SettingsContainer()
}
.setPresentSettingsSheetAction(isPresented: $isSettingsPresented)
.setShowSettings(isPresented: $isSettingsPresented)
#endif
#if os(iOS)
.environment(\.mobileMetrics, DynamicMobileMetrics(sceneDelegate.window))
Expand Down

This file was deleted.

6 changes: 3 additions & 3 deletions src/App/Views/Utils/EnvironmentValues+Settings.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import class MonsterAnalyzerCore.Settings
import SwiftUI

struct SettingsEnvironmentKey: EnvironmentKey {
private struct _SettingsEnvironmentKey: EnvironmentKey {
static var defaultValue: MonsterAnalyzerCore.Settings? = nil
}

extension EnvironmentValues {
var settings: MonsterAnalyzerCore.Settings? {
get { self[SettingsEnvironmentKey.self] }
set { self[SettingsEnvironmentKey.self] = newValue }
get { self[_SettingsEnvironmentKey.self] }
set { self[_SettingsEnvironmentKey.self] = newValue }
}
}
58 changes: 58 additions & 0 deletions src/App/Views/Utils/EnvironmentValues+ShowSettingsAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import SwiftUI

public protocol ShowSettingsAction {
func callAsFunction()
}

#if os(macOS)

private struct _MacShowSettingsAction: ShowSettingsAction {
func callAsFunction() {
if #available(macOS 13.0, *) {
NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil)
} else {
NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil)
}
}
}

#else

private struct _ShowSettingsActionImpl: ShowSettingsAction {
@Binding
private(set) var isPresented: Bool

func callAsFunction() {
isPresented = true
}
}

@available(macOS, unavailable)
public extension View {
func setShowSettings(isPresented: Binding<Bool>) -> some View {
environment(\.showSettings, _ShowSettingsActionImpl(isPresented: isPresented))
}
}

private struct _ShowSettingsNoAction: ShowSettingsAction {
func callAsFunction() {}
}

#endif

private struct _ShowSettingsActionKey: EnvironmentKey {
static var defaultValue: ShowSettingsAction {
#if os(macOS)
_MacShowSettingsAction()
#else
_ShowSettingsNoAction()
#endif
}
}

public extension EnvironmentValues {
var showSettings: ShowSettingsAction {
get { self[_ShowSettingsActionKey.self] }
set { self[_ShowSettingsActionKey.self] = newValue }
}
}
12 changes: 6 additions & 6 deletions src/MonsterAnalyzer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@
EB8778232B26B28500B3359C /* MonsterListColumn.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB8778222B26B28500B3359C /* MonsterListColumn.swift */; };
EB8778252B26B38200B3359C /* MonsterListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB8778242B26B38200B3359C /* MonsterListItem.swift */; };
EB8778262B26B38200B3359C /* MonsterListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB8778242B26B38200B3359C /* MonsterListItem.swift */; };
EB8778282B26BDEF00B3359C /* EnvironmentValues+PresentSettingsSheetAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB8778272B26BDEF00B3359C /* EnvironmentValues+PresentSettingsSheetAction.swift */; };
EB8778292B26BDEF00B3359C /* EnvironmentValues+PresentSettingsSheetAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB8778272B26BDEF00B3359C /* EnvironmentValues+PresentSettingsSheetAction.swift */; };
EB8778282B26BDEF00B3359C /* EnvironmentValues+ShowSettingsAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB8778272B26BDEF00B3359C /* EnvironmentValues+ShowSettingsAction.swift */; };
EB8778292B26BDEF00B3359C /* EnvironmentValues+ShowSettingsAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB8778272B26BDEF00B3359C /* EnvironmentValues+ShowSettingsAction.swift */; };
EB87782E2B26E4F200B3359C /* View+OnChangeBackport.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB87782D2B26E4F200B3359C /* View+OnChangeBackport.swift */; };
EB87782F2B26E4F200B3359C /* View+OnChangeBackport.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB87782D2B26E4F200B3359C /* View+OnChangeBackport.swift */; };
EB8AD06B2B42944800F1E96F /* Backport+ShapeStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB8AD06A2B42944800F1E96F /* Backport+ShapeStyle.swift */; };
Expand Down Expand Up @@ -493,7 +493,7 @@
EB8778202B26B1E600B3359C /* ColumnContentView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColumnContentView.swift; sourceTree = "<group>"; };
EB8778222B26B28500B3359C /* MonsterListColumn.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonsterListColumn.swift; sourceTree = "<group>"; };
EB8778242B26B38200B3359C /* MonsterListItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonsterListItem.swift; sourceTree = "<group>"; };
EB8778272B26BDEF00B3359C /* EnvironmentValues+PresentSettingsSheetAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "EnvironmentValues+PresentSettingsSheetAction.swift"; sourceTree = "<group>"; };
EB8778272B26BDEF00B3359C /* EnvironmentValues+ShowSettingsAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "EnvironmentValues+ShowSettingsAction.swift"; sourceTree = "<group>"; };
EB87782D2B26E4F200B3359C /* View+OnChangeBackport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+OnChangeBackport.swift"; sourceTree = "<group>"; };
EB8AD06A2B42944800F1E96F /* Backport+ShapeStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Backport+ShapeStyle.swift"; sourceTree = "<group>"; };
EB8AD06D2B42946A00F1E96F /* ShapeStyle+Hierarchical.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ShapeStyle+Hierarchical.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1066,8 +1066,8 @@
EB8AD06A2B42944800F1E96F /* Backport+ShapeStyle.swift */,
EB42495E2B332A230086E255 /* Backport.swift */,
EBC85C7C2B209D65008A16C6 /* EdgeInsets+Extensions.swift */,
EB8778272B26BDEF00B3359C /* EnvironmentValues+PresentSettingsSheetAction.swift */,
EB6437962B4702DD00E8D02B /* EnvironmentValues+Settings.swift */,
EB8778272B26BDEF00B3359C /* EnvironmentValues+ShowSettingsAction.swift */,
EB4426AD2B58AB1C0074B1FF /* HorizontalLayoutMargin.swift */,
EB71C1412B5BBEBF0013FBBC /* ScrollViewOffsetDetector.swift */,
EB8AD06D2B42946A00F1E96F /* ShapeStyle+Hierarchical.swift */,
Expand Down Expand Up @@ -1552,8 +1552,8 @@
EBC85C7E2B209D65008A16C6 /* EdgeInsets+Extensions.swift in Sources */,
EBB4B20E2B175DE2004D63EA /* Effectiveness+Extensions.swift in Sources */,
EB71C13C2B5B6FF10013FBBC /* EnvironmentValues+MAForm.swift in Sources */,
EB8778292B26BDEF00B3359C /* EnvironmentValues+PresentSettingsSheetAction.swift in Sources */,
EB6437982B4702DD00E8D02B /* EnvironmentValues+Settings.swift in Sources */,
EB8778292B26BDEF00B3359C /* EnvironmentValues+ShowSettingsAction.swift in Sources */,
EB560BCF2B42F48800E91E01 /* FavoriteButton.swift in Sources */,
EBBF86252B46D4F000F76D03 /* FavoriteSwipeButton.swift in Sources */,
EBBBAABA2B3FE27000BBAB23 /* GameGroupViewModel.swift in Sources */,
Expand Down Expand Up @@ -1740,8 +1740,8 @@
EBC85C7D2B209D65008A16C6 /* EdgeInsets+Extensions.swift in Sources */,
EBB4B1D52B161691004D63EA /* Effectiveness+Extensions.swift in Sources */,
EB71F8722B5A0D4300999269 /* EnvironmentValues+MAForm.swift in Sources */,
EB8778282B26BDEF00B3359C /* EnvironmentValues+PresentSettingsSheetAction.swift in Sources */,
EB6437972B4702DD00E8D02B /* EnvironmentValues+Settings.swift in Sources */,
EB8778282B26BDEF00B3359C /* EnvironmentValues+ShowSettingsAction.swift in Sources */,
EB560BCE2B42F48800E91E01 /* FavoriteButton.swift in Sources */,
EBFEAB842B45A5DC005363F7 /* FavoriteContextMenuButton.swift in Sources */,
EBBF86242B46D4F000F76D03 /* FavoriteSwipeButton.swift in Sources */,
Expand Down

0 comments on commit e6a15ea

Please sign in to comment.