Skip to content

Commit

Permalink
Merge pull request #248 from wakatime/misc/hide-browser-settings-when…
Browse files Browse the repository at this point in the history
…-not-monitored

Hide browser settings when no browser monitored
  • Loading branch information
alanhamlett authored Mar 27, 2024
2 parents f72a05f + 96f5b51 commit 4e6ce58
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
12 changes: 12 additions & 0 deletions WakaTime/Helpers/MonitoringManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ class MonitoringManager {
)
}

static var isMonitoringBrowsing: Bool {
for bundleId in MonitoredApp.browserAppIds {
guard
AppInfo.getAppName(bundleId: bundleId) != nil,
isAppMonitored(for: bundleId)
else { continue }

return true
}
return false
}

static func set(monitoringState: MonitoringState, for bundleId: String) {
UserDefaults.standard.set(monitoringState == .on, forKey: monitoredKey(bundleId: bundleId))
UserDefaults.standard.synchronize()
Expand Down
52 changes: 32 additions & 20 deletions WakaTime/Views/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class SettingsView: NSView, NSTextFieldDelegate, NSTextViewDelegate {
// MARK: Domain Preference

lazy var browserLabel: NSTextField = {
var label = NSTextField(labelWithString: "The settings below are only applicable when you’ve enabled " +
var label = NSTextField(labelWithString: "The settings below are only applicable because you’ve enabled " +
"monitoring a browser in the Monitored Apps menu.")
label.lineBreakMode = .byWordWrapping // Enable word wrapping
label.maximumNumberOfLines = 0 // Set to 0 to allow unlimited lines
Expand Down Expand Up @@ -167,31 +167,41 @@ class SettingsView: NSView, NSTextFieldDelegate, NSTextViewDelegate {
}()

lazy var stackView: NSStackView = {
let stackView = NSStackView(views: [
var views: [NSView] = [
apiKeyStackView,
checkboxesStackView,
browserLabel,
domainStackView,
filterStackView,
versionLabel
])
checkboxesStackView
]
if MonitoringManager.isMonitoringBrowsing {
views.append(contentsOf: [
browserLabel,
domainStackView,
filterStackView,
versionLabel
])
}

let stackView = NSStackView(views: views)
stackView.alignment = .leading
stackView.orientation = .vertical
stackView.spacing = 25
stackView.distribution = .equalSpacing
stackView.edgeInsets = NSEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.addConstraint(
NSLayoutConstraint(
item: filterStackView,
attribute: .width,
relatedBy: .equal,
toItem: stackView,
attribute: .width,
multiplier: 1,
constant: -(stackView.edgeInsets.left + stackView.edgeInsets.right)

if MonitoringManager.isMonitoringBrowsing {
stackView.addConstraint(
NSLayoutConstraint(
item: filterStackView,
attribute: .width,
relatedBy: .equal,
toItem: stackView,
attribute: .width,
multiplier: 1,
constant: -(stackView.edgeInsets.left + stackView.edgeInsets.right)
)
)
)
}

return stackView
}()

Expand All @@ -203,8 +213,10 @@ class SettingsView: NSView, NSTextFieldDelegate, NSTextViewDelegate {
addSubview(stackView)
setupConstraints()

updateDomainPreference(animate: false)
updateFilterControls(animate: false)
if MonitoringManager.isMonitoringBrowsing {
updateDomainPreference(animate: false)
updateFilterControls(animate: false)
}
}

required init?(coder: NSCoder) {
Expand Down

0 comments on commit 4e6ce58

Please sign in to comment.