How Can I change a toolbar icon in a safari app extension at runtime? - swift

I have found this in the documentation.
https://developer.apple.com/documentation/safariservices/sfsafaritoolbaritem
which lead me to be able to use the window variable in the toolbarItemClicked function to get a reference to the toolbar item like so.
var toolbaritemretrieved = false
window.getToolbarItem { (item) in
toolbaritem = item! as SFSafariToolbarItem;
toolbaritemretrieved = true;
}
once I had a reference to the toolbar item I could then run this code and supposedly change the image of the icon.
let path = Bundle.main.path(forResource: "newIcon", ofType: "png")!;
let icon = NSImage(path);
while(!toolbaritemretrieved){
//wait for toolbar item to be retrieved
}
toolbaritem?.setImage(icon);
SFSafariApplication.setToolbarItemsNeedUpdate();
However this doesn't work. There are no visible errors but for some reason the icon does not display the new image.

Okay it turns out that there was actually nothing wrong with my code at this point and I had a forgotten breakpoint.

Related

How to set an action to menubar icon on swipe gesture in swift/SwiftUI

I'm trying to get a small test app I'm working on to pop up a mini window with a swiftUI view when the menubar icon is swipe down on. Rather than clicking on the button for the window to pop up, a two finger swipe down as if the user pulled down on the icon for the window to show up.
I'm still a bit new to swift but I have the following so far in the AppDelegate.
if let button = statusBar.button{
let iconImage = NSImage(named: NSImage.Name(("iconPic")))
button.image = iconImage
button.action = #selector(self.handleOpenPlayerDescriptionWindow(_:))
button.sendAction(on: .swipe)
// button.sendAction(on: .leftMouseDown)
}
#objc func handleOpenPlayerDescriptionWindow(_ sender: NSMenuItem){
//code to show swiftUI Window
...
}
The pop-up window is simply just a placeholder Text view with an NBA player's game stats. I've commented out the sendAction with 'leftMouseDown' in the code above because that works perfectly fine, but when I change it to 'swipe' as a sendAction, it does nothing. I've also tried 'beginGesture', 'endGesture', and 'gesture' but all didn't work, the closest success was 'leftMouseDragged' but not really what I'm going for.
I'm using SwiftUI and not storyboard if maybe that's the issue
There is limitation of this method, as documented only specific masks are checked by this method, swipe is not.
> Declaration
>
> func sendAction(on mask: NSEvent.EventTypeMask) -> Int Discussion
>
> You use this method during mouse tracking when the mouse button
> changes state, the mouse moves, or if the cell is marked to send its
> action continuously while tracking. Because of this, the only bits
> checked in mask are NSLeftMouseDownMask, NSLeftMouseUpMask,
> NSLeftMouseDraggedMask, and NSPeriodicMask, which are declared in the
> NSEvent class reference.

How to Remove windows from UIApplication in Swift

While clicking on the button , i am moving to another view controller using the following code.
var window: UIWindow?
window = UIWindow.init(frame: UIScreen.main.bounds)
window?.autoresizesSubviews = true
window?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
let trackingViewController = LoginCameraViewController.init(screen:
.main)
window?.rootViewController = trackingViewController
window?.addSubview((trackingViewController?.view)!)
window?.makeKeyAndVisible()
window?.layoutSubviews()
For every button click, a new window is added to the application.I want to remove the latest window added.
The number of windows present in the application can be known by using following code.
let windowz = UIApplication.shared.windows
print("subviews",windowz)
I think you get the wrong concept of navigation in iOS. Window is like a root object in which ViewControllers appear. So probably the solution you're looking in a first place is UINavigationController.
Apple Documentation on Navigation
For iOS 13 i was able to do it this way
I created array which contains the window using which this new viewController is being presented,
var arrWindow = [UIWindow]()
arrWindow.append(yourNewWindow)
// Note: This will be stored as strong reference so need to remove it.
Also store your original window in variable
let originalWindow = yourOriginalWindow
// Note: Same goes for this as well ,this will be stored as strong reference so need to remove it.
At the time of removing there are many ways to do it but this was the most suited way for me,
func removeAppendedWindow() {
for window in arrWindow {
if window != originalWindow {
if let index = arrWindow.index(of: window) {
window.isHidden = true
arrWindow.remove(at: index)
}
}
}
}
In the below code windowz is normal array.
let windowz = UIApplication.shared.windows
You can remove last by using
windowz.removeLast()
You should use View Controller instead of adding windows and pop it instead where you are removing the window.
Window is only one object for app and will contain the views.
Please correct your understanding and use View controllers.

Swift Sidebar on right

Since i need to have the sidebar open on the right, how do i change it from the left to right side? the code i use is simple:
if revealViewController() != nil {
Menubutton.target = revealViewController()
Menubutton.action = #selector(SWRevealViewController.revealToggle(_:))
}
Also when the sidebar menu opens up it doesn't show the cells i have ( i've put both image and text on the different cells). But if i put a label in the Table view controller it shows the label when i open the sidebar menu.
To reveal from the right side, change the selector to rightRevealToggle
Menubutton.action = #selector(SWRevealViewController.rightRevealToggle(_:))

How to observe other application icon in OS X?

I need to know current shown icon of another running application, I have tried this code:
var icon = NSRunningApplication.runningApplicationsWithBundleIdentifier("some.app.dentifier").first?.icon
but this returns a fixed app icon and if the other app changed its icon this code still returns same icon before change.
Is there a way to return current shown icon of another app or icon identifier ?
First add this to applicationDidFinishLaunching
let appChanged = Notification.Name("NSWorkspaceDidActivateApplicationNotification")
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(self.appChanged), name: appChanged, object: nil)
Then this function will get you the image
#objc func appChanged(notification:NSNotification){
if let userInfo = notification.userInfo![NSWorkspace.applicationUserInfoKey] as? NSRunningApplication {
let icon = userInfo.icon
}
}

Tab Bar - custom images without titles

I want to set my tab bar to have custom images but XCode insists in leaving the space for the tab bar item title text leaving my images positioned too high in the tab bar. I've tried the following code in my TabBarController.swift viewDidLoad function:
let tabItems = tabBar.items as [UITabBarItem]
tabItems[0].title = nil
tabItems[0].selectedImage = UIImage(named: "HomeWhiteIcon")
tabItems[1].title = "Database"
tabItems[1].selectedImage = UIImage(named: "Second")
tabItems[2].title = nil
tabItems[2].selectedImage = UIImage(named: "SettingsWhiteIcon")
tabItems[3].title = nil
tabItems[3].selectedImage = UIImage(named: "ReportsWhiteIcon")
However, although no title is displayed the images are positioned too high as pic below (please ignore database icon - I have not set this icon yet.
Here's a random stab as swift is not my strong suit, but perhaps the database icon having a title is causing the entire title row to not collapse. Try removing the title from the database icon.
Otherwise have you seen the setTitlePositionAdjustment method? Perhaps adjust the title's position up into the icon and the space below will go away.