Skip to content

Commit

Permalink
Merge branch 'main' into deployment-target-macOS-12
Browse files Browse the repository at this point in the history
  • Loading branch information
F1248 committed Sep 19, 2024
2 parents f666c84 + db665f2 commit df8c7c4
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 32 deletions.
1 change: 1 addition & 0 deletions Genius/Localization/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
}
},
"Home" : {
"extractionState" : "manual",
"localizations" : {
"de" : {
"stringUnit" : {
Expand Down
15 changes: 15 additions & 0 deletions Genius/Types/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,19 @@ import ObjectiveC
class AppDelegate: NSObject, NSApplicationDelegate {

func applicationShouldTerminateAfterLastWindowClosed(_: NSApplication) -> Bool { true }

func applicationDockMenu(_: NSApplication) -> NSMenu? {
let dockMenu = NSMenu()
for tab in ContentViewTab.allCases {
let menuItem = NSMenuItem(title: tab.localizedString, action: #selector(changeTab(_:)), keyEquivalent: "")
menuItem.tag = tab.tag
dockMenu.addItem(menuItem)
}
return dockMenu
}

@objc
func changeTab(_ sender: NSMenuItem) {
sharedData.selectedTabIndex = sender.tag
}
}
15 changes: 15 additions & 0 deletions Genius/Types/SharedData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// SharedData.swift
// Genius
//
// Created by F1248.
//

import Foundation

class SharedData: ObservableObject {

@Published var selectedTabIndex = 0 // swiftlint:disable:this explicit_type_interface
}

nonisolated(unsafe) let sharedData = SharedData()
4 changes: 4 additions & 0 deletions Genius/Views/Common/TabContentBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
@resultBuilder
enum TabContentBuilder {

static func buildBlock(_ tabs: [TabLegacy]) -> [TabLegacy] {
tabs
}

static func buildBlock(_ tabs: TabLegacy...) -> [TabLegacy] {
tabs
}
Expand Down
14 changes: 8 additions & 6 deletions Genius/Views/Common/TabViewLegacy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@ struct TabViewLegacy: View {

let entireWindow: Bool
let tabs: [TabLegacy]
var selectedTabIndexParameter: Binding<Int>?

@State private var selectedTab: TabLegacy
@State private var selectedTabIndexPrivate = 0 // swiftlint:disable:this explicit_type_interface

var selectedTab: TabLegacy { tabs[selectedTabIndexParameter?.wrappedValue ?? selectedTabIndexPrivate] }

// swiftlint:disable:next type_contents_order
init?(entireWindow: Bool = false, @TabContentBuilder content: () -> [TabLegacy]) {
init?(selection: Binding<Int>? = nil, entireWindow: Bool = false, @TabContentBuilder content: () -> [TabLegacy]) {
self.selectedTabIndexParameter = selection
self.entireWindow = entireWindow
self.tabs = content()
guard let firstTab = tabs.first else { return nil }
self.selectedTab = firstTab
}

var picker: some View {
Picker(selection: $selectedTab) {
Picker(selection: selectedTabIndexParameter ?? $selectedTabIndexPrivate) {
ForEach(tabs) { tab in
tab.title
.tag(tab)
.tag(tabs.firstIndex(of: tab)!) // swiftlint:disable:this force_unwrapping
}
}
.pickerStyle(.segmented)
Expand Down
40 changes: 16 additions & 24 deletions Genius/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,31 @@ import SwiftUICore

struct ContentView: View {

@ObservedObject var observedSharedData: SharedData = sharedData
@AppStorage("interfaceMode")
var interfaceMode = Settings.InterfaceMode()

var body: some View {
if #available(macOS 15, *) {
TabView {
Tab("Home") {
HomeView()
}
Tab("System Information", variesByInterfaceMode: true, viewInvalidator: interfaceMode) {
SystemInformationView()
}
Tab("Maintenance") {
MaintenanceView()
}
Tab("Settings") {
SettingsView()
TabView(selection: $observedSharedData.selectedTabIndex) {
ForEach(ContentViewTab.allCases) { tab in
Tab(
tab.localizedStringKey,
value: tab.tag,
variesByInterfaceMode: tab.variesByInterfaceMode,
viewInvalidator: tab.variesByInterfaceMode ? interfaceMode : nil
) { tab.content }
}
}
.frame(minWidth: 686, minHeight: 256)
} else {
TabViewLegacy(entireWindow: true) {
TabLegacy("Home") {
HomeView()
}
TabLegacy("System Information", variesByInterfaceMode: true, viewInvalidator: interfaceMode) {
SystemInformationView()
}
TabLegacy("Maintenance") {
MaintenanceView()
}
TabLegacy("Settings") {
SettingsView()
TabViewLegacy(selection: $observedSharedData.selectedTabIndex, entireWindow: true) {
ContentViewTab.allCases.map { tab in
TabLegacy(
tab.localizedStringKey,
variesByInterfaceMode: tab.variesByInterfaceMode,
viewInvalidator: tab.variesByInterfaceMode ? interfaceMode : nil
) { tab.content }
}
}
.frame(minWidth: 686, minHeight: 256)
Expand Down
5 changes: 3 additions & 2 deletions Genius/Views/Extensions/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import SwiftUI
import SwiftUICore

@available(macOS 15, *)
extension Tab where Value == Never, Content: View, Label == SwiftUI.Label<Text, EmptyView> {
extension Tab where Value: Hashable, Content: View, Label == SwiftUI.Label<Text, EmptyView> {

init(
_ titleKey: LocalizedStringKey,
value: Value,
variesByInterfaceMode: Bool = false,
viewInvalidator _: Any? = nil,
content: () -> Content
) {
self.init(content: content) {
self.init(value: value, content: content) {
Label {
Text(titleKey, variesByInterfaceMode: variesByInterfaceMode)
} icon: {
Expand Down
37 changes: 37 additions & 0 deletions Genius/Views/Types/ContentViewTab.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// ContentViewTab.swift
// Genius
//
// Created by F1248.
//

import SwiftUICore

enum ContentViewTab: String, CaseIterable, Identifiable {

case home = "Home"
case systemInformation = "System Information"
case maintenance = "Maintenance"
case settings = "Settings"

var id: Self { self }
var tag: Int { Self.allCases.firstIndex(of: self)! } // swiftlint:disable:this force_unwrapping
var localizedString: String { rawValue.localized }
var localizedStringKey: LocalizedStringKey { LocalizedStringKey(rawValue) }

var content: AnyView {
switch self {
case .home: AnyView(HomeView())
case .systemInformation: AnyView(SystemInformationView())
case .maintenance: AnyView(MaintenanceView())
case .settings: AnyView(SettingsView())
}
}

var variesByInterfaceMode: Bool {
switch self {
case .systemInformation: true
default: false
}
}
}

0 comments on commit df8c7c4

Please sign in to comment.