aboutsummaryrefslogtreecommitdiff
path: root/Map/Presentation/Preferences/EditorPreferencesView.swift
blob: 91b332f065216e039a7400984ca1711094048b82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (C) 2024 Rubén Beltrán del Río

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see https://map.tranquil.systems.

import SwiftUI

struct EditorPreferencesView: View {

  @AppStorage("useSmartEditor") private var useSmartEditor = UserPreferences.Defaults.useSmartEditor
  @AppStorage("useCustomEditorFont") private var useCustomEditorFont = UserPreferences.Defaults
    .useCustomEditorFont
  @AppStorage("customEditorFontName") private var customEditorFontName = UserPreferences.Defaults
    .customEditorFontName
  @AppStorage("editorFontSize") private var editorFontSize: Double = UserPreferences.Defaults
    .editorFontSize

  var monospacedFonts: [String] {
    let fontManager = NSFontManager.shared
    let monospaceTrait = NSFontTraitMask.fixedPitchFontMask

    return fontManager.availableFontFamilies.filter { familyName in
      let members = fontManager.availableMembers(ofFontFamily: familyName) ?? []
      return members.contains { member in
        if let traits = member[3] as? NSNumber {
          return NSFontTraitMask(rawValue: traits.uintValue).contains(monospaceTrait)
        }
        return false
      }
    }
  }

  var body: some View {
    VStack(alignment: .center, spacing: Dimensions.Spacing.loose) {
      HStack(alignment: .center, spacing: Dimensions.Spacing.regular) {
        Button(action: {
          if editorFontSize > 7 {
            editorFontSize -= 1
          }
        }) {
          Image(systemName: "textformat.size.smaller")
        }
        .buttonStyle(BorderlessButtonStyle())
        .disabled(editorFontSize <= 7)
        .help("preferences.editor.font_size.decrease.help")

        Slider(value: $editorFontSize, in: 7...21, step: 1)
          .frame(width: 250)

        Button(action: {
          if editorFontSize < 21 {
            editorFontSize += 1
          }
        }) {
          Image(systemName: "textformat.size.larger")
        }
        .buttonStyle(BorderlessButtonStyle())
        .disabled(editorFontSize >= 21)
        .help("preferences.editor.font_size.increase.help")
      }

      Divider()

      HStack(alignment: .firstTextBaseline, spacing: Dimensions.Spacing.regular) {
        Text("preferences.editor.editor_style.title")
          .font(.Theme.Body.emphasized)
          .frame(maxWidth: 250, alignment: .trailing)

        VStack(alignment: .leading, spacing: Dimensions.Spacing.cozy) {

          Toggle(
            isOn: $useCustomEditorFont,
            label: {
              Text("preferences.editor.editor_style.use_custom_font")
                .font(.Theme.Body.regular)
            })

          Picker("", selection: $customEditorFontName) {
            ForEach(monospacedFonts, id: \.self) { fontFamily in
              Text(fontFamily).tag(fontFamily)
            }
          }
          .pickerStyle(MenuPickerStyle())
          .frame(maxWidth: 250)
          .padding(.leading, -Dimensions.Spacing.regular)
          .disabled(!useCustomEditorFont)
        }
        .frame(maxWidth: .infinity, alignment: .leading)
      }

      /*
      
       Disabled because we haven't written the smart editor yet.
       Call it aspirational preferences.
      
      Divider()
      
      HStack(alignment: .firstTextBaseline, spacing: Dimensions.Spacing.regular) {
        Text("preferences.editor.smart_editor.title")
          .font(.Theme.Body.emphasized)
          .frame(maxWidth: 250, alignment: .trailing)
      
        VStack(alignment: .leading, spacing: Dimensions.Spacing.cozy) {
          Toggle(
            String(localized: "preferences.editor.smart_editor.enabled"), isOn: $useSmartEditor)
        }
        .frame(maxWidth: .infinity, alignment: .leading)
      }
       */

      Spacer()
    }.padding(.vertical, Dimensions.Spacing.loose)
      .padding(.horizontal, Dimensions.Spacing.indulgent)
  }
}

#Preview {
  EditorPreferencesView()
}