Fullscreen NSWindow on Secondary Monitor - Swift - xcode

I have seen many posts on this in Obj C. Not too many in swift, and I just can't get it to work. I want to be able to make a window fullscreen on a specific NSScreen. The 'ToggleFullscreen' method isn't the best way because there aren't many options (for external displays). I tried:
// screen is my variable already set
outputWindow!.window!.setFrame(screen.frame, display: true, animate: true)
outputWindow!.window!.styleMask = NSFullScreenWindowMask
outputWindow!.window!.level = Int(CGShieldingWindowLevel())
// the above one doesn't make it fullscreen.
// it has a title bar and shows the menu on the screen.
// then i tried....
fullscreenWindow = NSWindow(contentRect: screen.frame, styleMask:NSBorderlessWindowMask, backing: NSBackingStoreType.Buffered, defer: false, screen: screen)
fullscreenWindow.level = Int(CGShieldingWindowLevel())
fullscreenWindow.makeKeyAndOrderFront(nil)
//that one works on my main display (somewhat). and does nothing on externals.
One thing I noticed with making my own fullscreen is that I get stuck into it. It's not like the OS X fullscreen where you can press esc to escape it. Are there any tricks to this? Thanks

I struggled with getting stuck on fullscreen as well. The reason behind that is using NSBorderlessWindowMask prevents the window from becoming key regardless of sending the makeKeyAndOrderFront: or makeKeyWindow messages to it. In Objective-C, the solution was to implement canBecomeKeyWindow in the subclassed NSWindow.
- (BOOL)canBecomeKeyWindow {
return YES;
}
Now it receives key events after going full screen as well.

This is a pseudo answer:
[in a inherited class of NSWondowController]
var fullscreen:Bool {
set {
if newValue == true {
if fullscreen == true {
return
}
frameOrig = self.window?.frame
self.window?.setFrame((NSScreen.main()?.frame)!, display: true)
}
else {
self.window?.setFrame(frameOrig!, display: true)
}
}
get {
return self.window?.frame == NSScreen.main()?.frame
}
}
I also trapped key events on both the calling-window and fullscreen window and point them to the above function so that toggling is possible. Like so:
[in some viewcontroller]:
override func keyUp(with event: NSEvent) {
switch event.keyCode {
case 53:
debugPrint("ESC - randomviewcontroller")
(randomView.window?.windowController as! RandomWindowController).fullscreen = !(RandomView.window?.windowController as! RandomWindowController).fullscreen;
break
default:
super.keyUp(with: event)
}
}
I think the above can be done much better & smarter... (picked up Swift a month ago...) but as the saying goes: "It worked on my computer!"
Hope it helps.

Related

SwiftUI animation - toggled Boolean always ends up as true

I'm trying to create an animation in my app when a particular action happens which will essentially make the background of a given element change colour and back x number of times to create a kind of 'pulse' effect. The application itself is quite large, but I've managed to re-create the issue in a very basic app.
So the ContentView is as follows:
struct ContentView: View {
struct Constants {
static let animationDuration = 1.0
static let backgroundAlpha: CGFloat = 0.6
}
#State var isAnimating = false
#ObservedObject var viewModel = ContentViewViewModel()
private let animation = Animation.easeInOut(duration: Constants.animationDuration).repeatCount(6, autoreverses: false)
var body: some View {
VStack {
Text("Hello, world!")
.padding()
Button(action: {
animate()
}) {
Text("Button")
.foregroundColor(Color.white)
}
}
.background(isAnimating ? Color.red : Color.blue)
.onReceive(viewModel.$shouldAnimate, perform: { _ in
if viewModel.shouldAnimate {
withAnimation(self.animation, {
self.isAnimating.toggle()
})
}
})
}
func animate() {
self.viewModel.isNew = true
}
}
And then my viewModel is:
import Combine
import SwiftUI
class ContentViewViewModel: ObservableObject {
#Published var shouldAnimate = false
#Published var isNew = false
var cancellables = Set<AnyCancellable>()
init() {
$isNew
.sink { result in
if result {
self.shouldAnimate = true
}
}
.store(in: &cancellables)
}
}
So the logic I am following is that when the button is tapped, we set 'isNew' to true. This in turn is a publisher which, when set to true, sets 'shouldAnimate' to true. In the ContentView, when shouldAnimate is received and is true, we toggle the background colour of the VStack x number of times.
The reason I am using this 'shouldAnimate' published property is because in the actual app, there are several different actions which may need to trigger the animation, and so it feels simpler to have this tied to one variable which we can listen for in the ContentView.
So in the code above, we should be toggling the isAnimating bool 6 times. So, we start with false then toggle as follows:
1: true, 2: false, 3: true, 4: false, 5: true, 6: false
So I would expect to end up on false and therefore have the background white. However, this is what I am getting:
I tried changing the repeatCount (in case I was misunderstanding how the count works):
private let animation = Animation.easeInOut(duration: Constants.animationDuration).repeatCount(7, autoreverses: false)
And I get the following:
No matter the count, I always end on true.
Update:
I have now managed to get the effect I am looking for by using the following loop:
for i in 0...5 {
DispatchQueue.main.asyncAfter(deadline: .now() + Double(i), execute: {
withAnimation(self.animation, {
self.isAnimating.toggle()
})
})
}
Not sure this is the best way to go though....
To understand what is going on, it would help to understand CALayer property animations.
When you define an animation the system captures the state of a Layer and watches for changes in the animatable properties of that layer. It records property changes for playback during the animation. To present the animation, it create a copy of the layer in its initial state (the presentationLayer). It then substitutes the copy in place of the actual layers on screen and runs the animation by manipulating the animatable properties of the presentation layer.
I this case, when you begin the animation, the system watches what happens to the CALayer that backs your view and captures the changes to any animatable properties (in this case the background color). It then creates a presentationLayer and replays those property changes repeatedly. It's not running your code repeatedly - it's changing the properties of the presentation Layer.
In other words the animation the system knows the layer's background color property should toggle back and forth because of the example you set in your animation block, but the animation toggles the background color back and forth without running your code again.

Animating Auto Layout constraints with NSView.layoutSubtreeIfNeeded() not working on macOS High Sierra

I have a basic Mac app with a view animation done through Auto Layout:
I add a new view to the right of the current view
I update the constraints so that the new view ends up filling the window
→ The animation will make it appear as if the view slides in from the right.
The recommended way for animating Auto Layout changes is:
Update the constraints
Use NSAnimationContext.runAnimationGroup()
Set allowsImplicitAnimation to true inside the animation block
Call view.layoutSubtreeIfNeeded() inside the animation block
I followed this recommendation and everything worked fine on macOS Sierra, but on macOS High Sierra, the animation does not take place anymore. Instead the view shows up at its final position without the animation.
I found a workaround: I schedule the animation on the next runloop cycle using DispatchQueue.main.async. However, that seems like a hack and I'm wondering if there is something else I'm missing here.
Here is my actual code:
private func appendSlideViewControllerAnimated(_ viewController:NSViewController, to viewToTheLeft:NSView)
{
// Insert the new view on the very right, just outside the parent:
viewController.view.frame = self.view.bounds
viewController.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(viewController.view)
viewController.view.topAnchor.constraint( equalTo: view.topAnchor ).isActive = true
viewController.view.bottomAnchor.constraint( equalTo: view.bottomAnchor ).isActive = true
viewController.view.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
viewController.view.leadingAnchor.constraint(equalTo: viewToTheLeft.trailingAnchor).isActive = true
// Update the layout after we just added the view on the right:
view.layoutSubtreeIfNeeded()
// Starting with macOS High Sierra, animating constraint changes for the newly inserted view
// only works if scheduled on the next runloop:
//DispatchQueue.main.async {
// Update the constraints to pin the view to the left:
self.view.removeConstraint(self.activeSlideLeadingConstraint!)
self.activeSlideLeadingConstraint = viewController.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor)
self.activeSlideLeadingConstraint?.constant = 0
self.activeSlideLeadingConstraint?.isActive = true
NSAnimationContext.runAnimationGroup( { context in
self.isAnimating = true
context.duration = self.slidingAnimationDuration
context.allowsImplicitAnimation = true
self.view.layoutSubtreeIfNeeded()
}, completionHandler: {
viewToTheLeft.removeFromSuperview()
self.clearUndoHistory()
self.updateFirstResponder()
self.isAnimating = false
})
//}
}
Enable Core Animation backing for root view you trying to animate. It can be done in Interface Builder or programmatically:
override func viewDidLoad()
{
super.viewDidLoad()
view.wantsLayer = true
}

Valid way to hide QLPreviewController RightBarButtonItem/RightBarButtonItems Xamarin iOS

My app supports iOS8+ devices. I want to hide right Action button from navigation bar. By research I found following few workarounds:
1. Create Sub class of QLPreviewController and in ViewDidAppear SetRightBarButtonItems to zero.
public class PdfViewController : QLPreviewController
{
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
NavigationItem.SetRightBarButtonItems (new UIKit.UIBarButtonItem[0], false);
}
}
In this case problem is RightBarButtonItem appears and then disappears. In mean while user is able to click on that RightBarButtonItem button. I don't want this behavior.
2. Create UIViewController and add QLPreviewController as child ViewController.
void BtnShowPdf_Clicked (object sender, EventArgs e) {
var dummyVC = new UIViewController ();
var pdfVC = new PdfViewController ();
dummyVC.AddChildViewController (pdfVC);
dummyVC.View.AddSubview (pdfVC.View);
dummyVC.NavigationItem.SetRightBarButtonItems (new UIBarButtonItem[0], false);
dummyVC.EdgesForExtendedLayout = UIRectEdge.None;
dummyVC.AutomaticallyAdjustsScrollViewInsets = false;
dummyVC.View.BackgroundColor = UIColor.Clear;
pdfVC.EdgesForExtendedLayout = UIRectEdge.None;
pdfVC.AutomaticallyAdjustsScrollViewInsets = false;
pdfVC.View.BackgroundColor = UIColor.Clear;
}
In this case If I set QLPreviewController it works as expected. But NavigationBar becomes more darker than default ViewController background color.
Dark Bar:
http://screencast.com/t/bqVMv5qqGz
Needed clear background bar like:
http://screencast.com/t/MUwE2VnxJ7
My questions are:
A) I would like to know which is the correct way to hide right
navigation bar button as per Apple guidelines ? If you have correct
solution then those are also appreciated.
B) Also Can you please suggest solution(s) for #1 Or #2 ?
Pretty sure you can do this:
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
self.navigationItem.rightBarButtonItems = nil
}

Drag and drop function swift OSX

This is a bit complex but I hope that someone can help me.
I am trying to build a drag and drop function for my OSX application.
This is how it is looking at the moment.
So there is just a single textfield which the user can drag and drop around the view. It is simple enough with just one textfield but if there are several textfields it is getting complicated and I don't know how to approach.
This is what I currently have:
#IBOutlet weak var test: NSTextField!
#IBAction override func mouseDragged(theEvent: NSEvent) {
NSCursor.closedHandCursor().set()
var event_location = theEvent.locationInWindow
test.frame.origin.x = event_location.x - 192
test.frame.origin.y = event_location.y
}
Test is the name of my NSTextField. I know the name of it so it is simple to move it arround. But if the user adds more textfields (see on the left pane) then I don't know how to address this textfield because I have no name of it (like "test" for the first input).
I am adding the textfields via this code:
let input = NSTextField(frame: CGRectMake(width, height, 100, 22))
self.MainView.addSubview(input)
How can I determine which textfield (if there are multiple on the view) was selected and then move the appropriate via drag and drop?
The drag and drop is working for that single static textfield
I have prepared a sample app, so consider this:
https://github.com/melifaro-/DraggableNSTextFieldSample
The idea is to introduce SelectableTextField which inherits NSTextField. SelectableTextField provides facility for subscription of interested listener on text field selection event. It has didSelectCallback block variable, where you need to set you handling code. Something like this:
textField.didSelectCallback = { (textField) in
//this peace of code will be performed once mouse down event
//was detected on the text field
self.currentTextField = textField
}
By using mentioned callback mechanism, once text field selected, we can store it in currentTextField variable. So that when mouseDragged function of ViewController is called we are aware of currentTextField and we can handle it appropriatelly. In case of sample app we need adjust currentTextField origin according drag event shift. Hope it became better now.
P.S. NSTextField is opened for inheriting from it, so you can freely use our SelectableTextField everywhere where you use NSTextField, including Interface Builder.
EDIT
I have checked out your sample. Unfortuantly I am not able to commit /create pull request into you repository, so find my suggestion here:
override func viewDidLoad() {
super.viewDidLoad()
didButtonSelectCallback = { (button) in
if let currentButton = self.currentButton {
currentButton.highlighted = !currentButton.highlighted
if currentButton == button {
self.currentButton = nil
} else {
self.currentButton = button
}
} else {
self.currentButton = button
}
button.highlighted = !button.highlighted
}
addButtonAtRandomePlace()
addButtonAtRandomePlace()
didButtonSelectCallback(button: addButtonAtRandomePlace())
}
override func mouseDragged(theEvent: NSEvent) {
guard let button = currentButton else {
return
}
NSCursor.closedHandCursor().set()
button.frame.origin.x += theEvent.deltaX
button.frame.origin.y -= theEvent.deltaY
}
private func addButtonAtRandomePlace() -> SelectableButton {
let viewWidth = self.view.bounds.size.width
let viewHeight = self.view.bounds.size.height
let x = CGFloat(rand() % Int32((viewWidth - ButtonWidth)))
let y = CGFloat(rand() % Int32((viewHeight - ButtonHeight)))
let button = SelectableButton(frame: CGRectMake(x, y, ButtonWidth, ButtonHeight))
button.setButtonType(NSButtonType.ToggleButton)
button.alignment = NSCenterTextAlignment
button.bezelStyle = NSBezelStyle.RoundedBezelStyle
button.didSelectCallback = didButtonSelectCallback
self.view.addSubview(button)
return button
}

How to hide a divider of nssplitview?

Now I want to hide or show with my condition a divider when my app run. used this delegate method:
- (BOOL)splitView:(NSSplitView *)splitView shouldHideDividerAtIndex:(NSInteger)dividerIndex
{
if (A)
return YES;
else
return NO;
}
but it didn't work , why? How to used this method? Thank you very much!
Further to #carmin’s note, above, overriding the NSSplitView dividerThickness property is the only thing that worked for me (specifically, returning NSRectZero from the splitView:effectiveRect:forDrawnRect:ofDividerAtIndex: NSSplitView delegate method — as detailed here – didn’t work and resulted in floating dividers disjointed from the views themselves).
Here’s the code in Swift:
override var dividerThickness:CGFloat
{
get { return 0.0 }
}
The split view sends that message to its delegate, to ask the delegate whether it should hide that divider. So, be the delegate, and answer the split view's question.
Be sure to check out the documentation. It's possible that that message won't accomplish what you want it to. The documentation lists everything you can do by responding to that message.
You can overload NSSplitView-dividerThickness and return 0 to hide all of the dividers. You can overload NSSplitView-drawDividerInRect: to have individual control over the dividers (choosing to allow super to draw the divider or not). These choices work even when the subviews are visible.
Here's how to do it in Obj-C that doesn't involve subclassing. Make sure that you've got the SplitView delegate in IB connected.
Then in your delegate class:
-(NSRect)splitView:(NSSplitView *)splitView effectiveRect:(NSRect)proposedEffectiveRect forDrawnRect:(NSRect)drawnRect ofDividerAtIndex:(NSInteger)dividerIndex
{
if ( [_splitView subviews][1].isHidden ==YES || [[_splitView subviews][1] frame].size.height < 50) //closed or almost closed
{
return NSZeroRect;
}
return proposedEffectiveRect;
}
- (BOOL)splitView:(NSSplitView *)splitView shouldHideDividerAtIndex:(NSInteger)dividerIndex
{
if ( [_splitView subviews][1].isHidden ==YES || [[_splitView subviews][1] frame].size.height < 50)
{
return YES;
}
return NO;
}
This will hide the divider when the split view is closed, but show it when it is open.
If you don't want them to be able to drag it even when its open, just cut out all the code in the first method and return only NSZeroRect. Do the same in the second method and only return YES.
For the sake of posterity, working with Swift you can call the delegate function splitView(_:effectiveRect:forDrawnRect:ofDividerAtIndex:) and just have it return an empty NSRect
override func splitView(_ splitView: NSSplitView, effectiveRect proposedEffectiveRect: NSRect, forDrawnRect drawnRect: NSRect, ofDividerAt dividerIndex: Int) -> NSRect {
if dividerIndex == 1 {
return NSRect()
}
return super.splitView(splitView, effectiveRect: proposedEffectiveRect, forDrawnRect: drawnRect, ofDividerAt: dividerIndex)
}
Use this class in Custom class of NSSplitView:
class customSplitView: NSSplitView {
override var dividerThickness: CGFloat {
return 0
}
}
For me it worked!

Resources