Skip to content

Commit

Permalink
Merge pull request #251 from wakatime/bugfix/fix-settings-dynamic
Browse files Browse the repository at this point in the history
Fix showing and hiding browser settings
  • Loading branch information
alanhamlett authored Mar 27, 2024
2 parents 3ec4aa2 + 18a8045 commit fdf2747
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
1 change: 1 addition & 0 deletions WakaTime/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {

private func showSettings() {
NSApp.activate(ignoringOtherApps: true)
settingsWindowController.settingsView.setBrowserVisibility()
settingsWindowController.showWindow(self)
}

Expand Down
65 changes: 35 additions & 30 deletions WakaTime/Views/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,40 +167,32 @@ class SettingsView: NSView, NSTextFieldDelegate, NSTextViewDelegate {
}()

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

let stackView = NSStackView(views: views)
checkboxesStackView,
browserLabel,
domainStackView,
filterStackView,
versionLabel
])
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

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)
)
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 @@ -212,11 +204,9 @@ class SettingsView: NSView, NSTextFieldDelegate, NSTextViewDelegate {

addSubview(stackView)
setupConstraints()

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

required init?(coder: NSCoder) {
Expand Down Expand Up @@ -282,6 +272,21 @@ class SettingsView: NSView, NSTextFieldDelegate, NSTextViewDelegate {
])
}

func setBrowserVisibility() {
if MonitoringManager.isMonitoringBrowsing {
browserLabel.isHidden = false
domainStackView.isHidden = false
filterStackView.isHidden = false
versionLabel.isHidden = false
} else {
browserLabel.isHidden = true
domainStackView.isHidden = true
filterStackView.isHidden = true
versionLabel.isHidden = true
}
adjustWindowSize(animate: false)
}

// MARK: State Helpers

private func updateDomainPreference(animate: Bool) {
Expand Down
22 changes: 12 additions & 10 deletions WakaTime/Watchers/MonitoredApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ enum MonitoredApp: String, CaseIterable {
}

switch self {
case .canva:
// Canva obviously implements tabs in a different way than the tab content UI.
// Due to this circumstance, it's possible to just sample an element from the
// Canva window which is positioned underneath the tab bar and trace to the
// web area root which appears to be properly titled. All the UI zoom settings
// in Canva only change the tab content or sub content of the tab content, hence
// this should be relatively safe. In cases where this fails, nil should be
// returned as a consequence of the web area not being found.
let someElem = element.elementAtPositionRelativeToWindow(x: 10, y: 60)
let webArea = someElem?.firstAncestorWhere { $0.role == "AXWebArea" }
return webArea?.rawTitle
case .notes:
// There's apparently two text editor implementations in Apple Notes. One uses a web view,
// the other appears to be a native implementation based on the `ICTK2MacTextView` class.
Expand Down Expand Up @@ -235,16 +246,7 @@ enum MonitoredApp: String, CaseIterable {
case .brave:
fatalError("\(self.rawValue) should never use window title as entity")
case .canva:
// Canva obviously implements tabs in a different way than the tab content UI.
// Due to this circumstance, it's possible to just sample an element from the
// Canva window which is positioned underneath the tab bar and trace to the
// web area root which appears to be properly titled. All the UI zoom settings
// in Canva only change the tab content or sub content of the tab content, hence
// this should be relatively safe. In cases where this fails, nil should be
// returned as a consequence of the web area not being found.
let someElem = element.elementAtPositionRelativeToWindow(x: 10, y: 60)
let webArea = someElem?.firstAncestorWhere { $0.role == "AXWebArea" }
return webArea?.rawTitle
fatalError("\(self.rawValue) should never use window title as entity")
case .chrome:
fatalError("\(self.rawValue) should never use window title as entity")
case .figma:
Expand Down

0 comments on commit fdf2747

Please sign in to comment.