# CutBox.app

## Coverage Report

#### Created: 2024-03-12 03:40

    .../Source/App/Preferences/PreferencesService/CutBoxPreferencesService.swift

    Line

    Count

    Source (jump to first uncovered line)

    1

    //

    2

    //  CutBoxPreferences.swift

    3

    //  CutBox

    4

    //

    5

    //  Created by Jason Milkins on 27/3/18.

    6

    //  Copyright © 2018-2023 ocodo. All rights reserved.

    7

    //

    8

    9

    import Cocoa

    10

    import Magnet

    11

    import RxSwift

    12

    13

    enum CutBoxPreferencesEvent: Equatable {

    14

        case historyLimitChanged(limit: Int)

    15

        case compactUISettingChanged(isOn: Bool)

    16

        case hidePreviewSettingChanged(isOn: Bool)

    17

        case protectFavoritesChanged(isOn: Bool)

    18

        case javascriptReload

    19

        case javascriptReloaded

    20

        case themeChanged

    21

        case themesReloaded

    22

        case historyClearByOffset(offset: TimeInterval)

    23

    }

    24

    25

    5

    func notifyUser(title: String, info: String) {

    26

    5

        let notification = NSUserNotification()

    27

    5

        notification.title = title

    28

    5

        notification.informativeText = info

    29

    5

        NSUserNotificationCenter.default

    30

    5

            .deliver(notification)

    31

    5

    }

    32

    33

    class CutBoxPreferencesService {

    34

    35

        var events: PublishSubject<CutBoxPreferencesEvent>!

    36

    37

        var defaults: UserDefaults!

    38

    39

        var js: JSFuncService!

    40

    41

    151

        init(defaults: UserDefaults = UserDefaults.standard) {

    42

    151

            self.events = PublishSubject<CutBoxPreferencesEvent>()

    43

    151

            self.defaults = defaults

    44

    151

            self.js = JSFuncService()

    45

    151

            self.themes = []

    46

    151

            js.prefs = self

    47

    151

            loadThemes()

    48

    151

        }

    49

    50

    153

        func loadThemes() {

    51

    153

            let themeLoader = CutBoxThemeLoader()

    52

    153

            themeLoader.cutBoxUserThemesLocation = cutBoxUserThemesLocation

    53

    153

            let bundledThemes = themeLoader.getBundledThemes()

    54

    153

            let userThemes = themeLoader.getUserThemes()

    55

    153

            self.themes = bundledThemes + userThemes

    56

    153

            events.onNext(.themesReloaded)

    57

    153

        }

    58

    59

        static let shared = CutBoxPreferencesService()

    60

    61

        var themes: [CutBoxColorTheme]

    62

    63

        var minItemSize: CGFloat = 12

    64

        var maxItemSize: CGFloat = 20

    65

    66

        var minPreviewSize: CGFloat = 12

    67

        var maxPreviewSize: CGFloat = 20

    68

    69

    151

        var searchViewTextFieldFont = NSFont(

    70

    151

            name: "Helvetica Neue",

    71

    151

            size: 28)

    72

    73

    151

        var searchViewClipTextFieldFont = NSFont(

    74

    151

            name: "Helvetica Neue",

    75

    151

            size: 12)

    76

    77

    151

        var searchViewClipPreviewFont = NSFont(

    78

    151

            name: "Menlo",

    79

    151

            size: 12)

    80

    81

        var savedTimeFilterValue: String {

    82

    1

            get {

    83

    1

                return defaults.string(forKey: Constants.kSavedTimeFilterValue) ?? ""

    84

    1

            }

    85

    2

            set {

    86

    2

                defaults.set(newValue, forKey: Constants.kSavedTimeFilterValue)

    87

    2

            }

    88

        }

    89

    90

        var useJoinString: Bool {

    91

    116

            get {

    92

    116

                return defaults.bool(forKey: Constants.kUseJoinSeparator)

    93

    116

            }

    94

    9

            set {

    95

    9

                defaults.set(newValue, forKey: Constants.kUseJoinSeparator)

    96

    9

            }

    97

        }

    98

    99

        var multiJoinString: String? {

    100

    53

            get {

    101

    53

                return useJoinString ?

    102

    53

                defaults.string(forKey: Constants.kMultiJoinSeparator) : nil

    103

    53

            }

    104

    6

            set {

    105

    6

                defaults.set(newValue, forKey: Constants.kMultiJoinSeparator)

    106

    6

            }

    107

        }

    108

    109

        var useWrappingStrings: Bool {

    110

    115

            get {

    111

    115

                return defaults.bool(forKey: Constants.kUseWrappingStrings)

    112

    115

            }

    113

    58

            set {

    114

    58

                defaults.set(newValue, forKey: Constants.kUseWrappingStrings)

    115

    58

            }

    116

        }

    117

    118

        var wrappingStrings: (String?, String?) {

    119

    53

            get {

    120

    53

                return useWrappingStrings ?

    121

    53

                    (

    122

    3

                        defaults.string(forKey: Constants.kWrapStringStart),

    123

    3

                        defaults.string(forKey: Constants.kWrapStringEnd)

    124

    50

                    ) : (nil, nil)

    125

    53

            }

    126

    7

            set {

    127

    7

                defaults.set(newValue.0, forKey: Constants.kWrapStringStart)

    128

    7

                defaults.set(newValue.1, forKey: Constants.kWrapStringEnd)

    129

    7

            }

    130

        }

    131

    132

        var historyLimited: Bool {

    133

    50

            get {

    134

    50

                return defaults.bool(forKey: Constants.kHistoryLimited)

    135

    50

            }

    136

    52

            set {

    137

    52

                defaults.set(newValue, forKey: Constants.kHistoryLimited)

    138

    52

                if !newValue {

    139

    51

                    historyLimit = 0

    140

    52

                }

    141

    52

                events.onNext(.historyLimitChanged(limit: historyLimit))

    142

    52

            }

    143

        }

    144

    145

        var historyLimit: Int {

    146

    106

            get {

    147

    106

                return defaults.integer(forKey: Constants.kHistoryLimit)

    148

    106

            }

    149

    55

            set {

    150

    55

                defaults.set(newValue, forKey: Constants.kHistoryLimit)

    151

    55

                events.onNext(.historyLimitChanged(limit: newValue))

    152

    55

            }

    153

        }

    154

    155

        var useCompactUI: Bool {

    156

    61

            get {

    157

    61

                return defaults.bool(forKey: Constants.kUseCompactUI)

    158

    61

            }

    159

    52

            set {

    160

    52

                defaults.set(newValue, forKey: Constants.kUseCompactUI)

    161

    52

                events.onNext(.compactUISettingChanged(isOn: newValue))

    162

    52

            }

    163

        }

    164

    165

        var hidePreview: Bool {

    166

    61

            get {

    167

    61

                return defaults.bool(forKey: Constants.kHidePreview)

    168

    61

            }

    169

    53

            set {

    170

    53

                defaults.set(newValue, forKey: Constants.kHidePreview)

    171

    53

                events.onNext(.hidePreviewSettingChanged(isOn: newValue))

    172

    53

            }

    173

        }

    174

    175

        var protectFavorites: Bool {

    176

    56

            get {

    177

    56

                return defaults.bool(forKey: Constants.kProtectFavorites)

    178

    56

            }

    179

    53

            set {

    180

    53

                defaults.set(newValue, forKey: Constants.kProtectFavorites)

    181

    53

                events.onNext(.protectFavoritesChanged(isOn: newValue))

    182

    53

            }

    183

        }

    184

    185

        var cutBoxUserThemesLocation: String {

    186

    155

            get {

    187

    155

                if let userThemesLocation = defaults.string(forKey: Constants.kCutBoxUserThemesLocation) {

    188

    19

                    return userThemesLocation

    189

    136

                }

    190

    136

                let defaultLocation = "~/.config/cutbox"

    191

    136

                self.cutBoxUserThemesLocation = defaultLocation

    192

    136

                return defaultLocation

    193

    155

            }

    194

    195

    137

            set {

    196

    137

                defaults.set(newValue, forKey: Constants.kCutBoxUserThemesLocation)

    197

    137

            }

    198

        }

    199

    200

        var cutBoxJSLocation: String {

    201

    7

            get {

    202

    7

                if let jsLocation = defaults.string(forKey: Constants.kCutBoxJSLocation) {

    203

    4

                    return jsLocation

    204

    4

                }

    205

    3

                let defaultLocation = "~/.cutbox.js"

    206

    3

                self.cutBoxJSLocation = defaultLocation

    207

    3

                return defaultLocation

    208

    7

            }

    209

    210

    7

            set {

    211

    7

                defaults.set(newValue, forKey: Constants.kCutBoxJSLocation)

    212

    7

            }

    213

        }

    214

    215

    3

        func loadJavascript() {

    216

    3

            js.reload()

    217

    3

            events.onNext(.javascriptReloaded)

    218

    3

        }

    219

    }