1

enter image description here

As you can see in the image I would like to be able to do a similar one, to make a way that instead of showing only the icon of the sun, also showing a text.

As seen in the image below, an icon followed by a text.

enter image description here

But I only managed to do this:

enter image description here

The problem I would like to put the icon on the left or right of the text, not above it, can you give me a hand?

P.s.

The text must change accordingly, how can I make the StatusBarController receive the text changes.

import AppKit
import SwiftUI

class StatusBarController {
    @ObservedObject var userPreferences = UserPreferences.instance
    private var statusBar: NSStatusBar
    private var statusItem: NSStatusItem
    private var popover: NSPopover
    
    init(_ popover: NSPopover) {
        self.popover = popover
        statusBar = NSStatusBar.init()
        statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
        
        if let statusBarButton = statusItem.button {
             if let _ = userPreferences.$inDownload {
                statusItem.button?.title = userPreferences.$percentualDownload
            }
            statusBarButton.image = #imageLiteral(resourceName: "Weather")
            statusBarButton.image?.size = NSSize(width: 18.0, height: 18.0)
            statusBarButton.image?.isTemplate = true
            statusBarButton.action = #selector(togglePopover(sender:))
            statusBarButton.target = self
            statusBarButton.imagePosition = NSControl.ImagePosition.imageLeft
        }
    }
    
    @objc func togglePopover(sender: AnyObject) {
        if(popover.isShown) {
            hidePopover(sender)
        }
        else {
            showPopover(sender)
        }
    }
    
    func showPopover(_ sender: AnyObject) {
        if let statusBarButton = statusItem.button {
            popover.show(relativeTo: statusBarButton.bounds, of: statusBarButton, preferredEdge: NSRectEdge.maxY)
        }
    }
    
    func hidePopover(_ sender: AnyObject) {
        popover.performClose(sender)
    }
}

I'm thinking of using something like that:

import EventKit
import ServiceManagement

private struct PreferencesKeys {
    static let backgroundIsTransparent = "backgroundIsTransparent"
    static let inDownload = "inDownload"
    static let percentualDownload = "percentualDownload"
}

class UserPreferences: ObservableObject {
    static let instance = UserPreferences()
    
    private init() {
        // This prevents others from using the default '()' initializer for this class.
    }
    
    private static let defaults = UserDefaults.standard
    
    @Published var backgroundIsTransparent: Bool = {
        guard UserDefaults.standard.object(forKey: PreferencesKeys.backgroundIsTransparent) != nil else {
            return true
        }
        return UserDefaults.standard.bool(forKey: PreferencesKeys.backgroundIsTransparent)
    }() {
        didSet {
            UserPreferences.defaults.set(backgroundIsTransparent, forKey: PreferencesKeys.backgroundIsTransparent)
        }
    }
    
    @Published var inDownload: Bool = {
        guard UserDefaults.standard.object(forKey: PreferencesKeys.inDownload) != nil else {
            return true
        }
        return UserDefaults.standard.bool(forKey: PreferencesKeys.inDownload)
    }() {
        didSet {
            UserPreferences.defaults.set(inDownload, forKey: PreferencesKeys.inDownload)
        }
    }
    
    @Published var percentualDownload: String = {
        guard UserDefaults.standard.object(forKey: PreferencesKeys.percentualDownload) != nil else {
            return "0%"
        }
        return UserDefaults.standard.string(forKey: PreferencesKeys.percentualDownload)!
    }() {
        didSet {
            UserPreferences.defaults.set(percentualDownload, forKey: PreferencesKeys.percentualDownload)
        }
    }
}

but I get the following error:

enter image description here

Edit:

First problem solved I used:

statusBarButton.imagePosition = NSControl.ImagePosition.imageLeft
statusBarButton.imagePosition = NSControl.ImagePosition.imageRight

For the update text problem, what can I do?

Willeke
  • 14,578
  • 4
  • 19
  • 47
Paul
  • 3,644
  • 9
  • 47
  • 113

0 Answers0