Xcode Playground Timeline empty - xcode

I am trying to use the timeline assistant editor for playground in xcode version 7.3.1, it is always empty.
Timeline assistant editor
I think the error is from xcode, however from search it doesn't look like anyone got the same error so i am confused.

To display the result of print you need to open the "debug area" by going to menu
View > Debug Area > Show Debug Area
or click on the button in the lower left part:
To display the timeline graph, you could use XCPCaptureValue:
import XCPlayground
var x = 0
for i in 0...10 {
x += i
print(x)
XCPCaptureValue("Value for x", value: x)
}
but XCPCaptureValue has been deprecated and won't be available in the future (there's no available replacement).
The alternative is to display the graph inline by clicking on the "+" button on the right:
Do a right click on the graphs and you can choose to display value history instead:

I was just getting started in Playgrounds myself, and came across the same problem of not being able to print to Timeline.
This Medium article explains how to show or render things in the timeline as of Xcode 8 and Swift 3. Basically, you have to create a view and assign it to the PlaygroundPage.current.liveView:
import UIKit
import PlaygroundSupport
let contentView = UIView(frame: CGRect(x: 0, y: 0, width: 320.0, height: 600.0))
contentView.backgroundColor = UIColor.white
PlaygroundPage.current.liveView = contentView
Afterwards, you can add anything to your contentView to be displayed in the timeline. The PlaygroundPage.current.liveView can receive any UIView, such as UILabel, UITextField, etc.
However, sometimes the created view defaults to a black background, so you have to remember to set the .backgroundColor to UIColor.white to see it's info/child views.

Related

SwiftUI macOS Disable Scroll Indicator Background

I'm looking for a way to disable scroll indicator background(of a TextEditor) in SwiftUI, macOS to only show the moving part of the indicator(like in popovers) look at examples below:
What I currently have:
What I'm looking for:
Updated to include feedback from OP that original soln proposed doesn't quite completely remove tint.
On Ventura with Xcode 14 beta 1 it's possible to come very close [0] to achieving the desired effect by adding a background modifier after the .scrollContentBackground(.hidden) extends the background under the scrollbar,
e.g.
TextEditor(text: $text)
.scrollContentBackground(.hidden)
.background(.cyan)
Where $text is a little snippet of Under Milk Wood gives
Beyond this, afaik SwiftUI's TextEditor doesnt have native api to get closer at the moment. As an alternative though - if working with underlying AppKit components and its trade-offs is acceptable - then nicer styling might be an option.
For example using the Introspect library to enable the more modern overlay scrollerStyle:
import Introspect // from https://github.com/siteline/SwiftUI-Introspect
struct ContentView: View {
#State var text = demoText
var body: some View {
TextEditor(text: $text)
.scrollContentBackground(.hidden)
.background(.cyan)
.introspectTextView { (nsV: NSTextView) in
DispatchQueue.main.async {
nsV.enclosingScrollView?.scrollerStyle = .overlay
}
}
}
}
Seems to give quite a nice effect
[0] The subtle tint that remains, is as can be seen - missable - or at least was for me :-/

Voice over do not work in NSMenu in status bar with custom view for NSMenuItem

I have added NSMenu in NSStatusBar. I have used custom views for first NSMenuItem with the setView: method to include progressIndicator. All other items are default.
Issue here is with VO (voice over for disabled people). When I enable VO from Setting -> Accessibility -> VoiceOver and press option+command+M+M it focus on menus in status bar. Now using left and right keys I navigate to my app and then press down key of open NSMenu.
I can got down and select my options but cannot change to other app (wifi, date) from here. When I remove this custom view item it works fine. I also observed same with one another app.
Do I need to set any properties for custom views.
Edit
I created sample app in swift.
let menu = NSMenu()
let menuitem1 = NSMenuItem()
let view1 = NSView(frame: NSRect(x: 0, y: 0, width: 100, height: 30))
menuitem1.view = view1
menu.addItem(menuitem1)
menu.addItem(NSMenuItem(title: "Print Quote", action: #selector(AppDelegate.printQuote(_:)), keyEquivalent: "P"))
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(title: "Quit Quotes", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))
Even in this simple app I cannot get VO working properly.
Steps
VO (control+option) + M + M
VO + space to select our app options.
VO + down key to navigate inside.
Then try to go left or right in other apps. It do not switch to other app.
Edit 2
Observation is when I choose custom view first and try to switch to other app it works. But once I choose normal menuitem from custom view it falls in some issue. Only solution I find is either have all default menu items or all items with custom view.
For my NSMenuItems displayed in a table, I can add the accessibilityLabel on the newAutoLayoutView.
NSView *cellView = [NSView newAutoLayoutView];
cellView.accessibilityLabel = #"Voiceover Tab Name";

NSPopover application does not always appear where it should

I am working on an NSPopover application. I am using raywenderlich tutorial as a starting place.
The issue I am having is that when the popover opens and the system status bar is closed (such as when using multiple full-screen apps on a laptop) the popover shows in the bottom left of the screen.
Is there a way to force the system status bar to open and stay open while the popover is open?
We had a similar issue, and ended up detecting when the system menubar was minimized:
[NSMenu menuBarVisible]
As far as keeping your window visible, you might look into sharing your window style on the fly using NSBorderlessWindowMask | NSNonactivatingPanelMask
The problem is that when the status bar isn't visible the statusItem / button has a weird position so it's in the left side of the screen.
A possibly solution to this could be to save the position when the popover is first opened and keep showing it relative to that point. In this answer they place the popover relative to an invisible window. This we need because when we display the popover relative to the statusItem / button, the position is weird if the status is not visible.
So if you save the window as a variable and show the popover relative to this you will end up with something like this:
static let popover = NSPopover()
var invisibleWindow: NSWindow!
func showPopover(sender: Any?) {
if let button = AppDelegate.statusItem.button {
if (invisibleWindow == nil) {
invisibleWindow = NSWindow(contentRect: NSMakeRect(0, 0, 20, 1), styleMask: .borderless, backing: .buffered, defer: false)
invisibleWindow.backgroundColor = .red
invisibleWindow.alphaValue = 0
// find the coordinates of the statusBarItem in screen space
let buttonRect:NSRect = button.convert(button.bounds, to: nil)
let screenRect:NSRect = button.window!.convertToScreen(buttonRect)
// calculate the bottom center position (10 is the half of the window width)
let posX = screenRect.origin.x + (screenRect.width / 2) - 10
let posY = screenRect.origin.y
// position and show the window
invisibleWindow.setFrameOrigin(NSPoint(x: posX, y: posY))
invisibleWindow.makeKeyAndOrderFront(self)
}
AppDelegate.popover.show(relativeTo: invisibleWindow.contentView!.frame, of: invisibleWindow.contentView!, preferredEdge: NSRectEdge.minY)
NSApp.activate(ignoringOtherApps: true)
}
}

Cocoa osx NSButton show text when mouse over

In my application for Mac I want to show some info text when the users moves the mouse pointer over a button.
Something like this:
How can I achieve this correctly?
Thanks in advance.
This works for me in Xcode 6.2:
In the Identity Inspector(the pane on the right hand side in the image below), in the Tool Tip section enter "Sad face":
In Interface-Builder you can set a 'tooltip' for most objects, including NSButton (Open the Inspector, then choose the "Help" section).
However, if you're using a NSToolbar, this also has tooltips; you may choose to do this programmatically. Try typing setToolTip in your source, then option-double-click it for more information. (option=alternate).
To programmatically add a custom tooltip in Swift, subclass the corresponding view
var trackingArea: NSTrackingArea!
Add a tracking area for the view
let opts: NSTrackingAreaOptions = ([NSTrackingAreaOptions.MouseEnteredAndExited, NSTrackingAreaOptions.ActiveAlways])
trackingArea = NSTrackingArea(rect: bounds, options: opts, owner: self, userInfo: nil)
self.addTrackingArea(trackingArea)
Mouse entered Event
override func mouseEntered(theEvent: NSEvent) {
self.tooltip = "Sad face : Select the option for very poor"
}
Or you can make a separate tooltip for each range of a string: https://stackoverflow.com/a/18814112/308315
You can also do it programmatically.
(Assume someButton is your NSButton object)
[someButton setToolTip:#"Sad face: Select this option for \"Very poor\""];

How do you create a new NSWindow using Swift in XCode and add content to it programatically?

I come from a background in Java, and I'm trying out using swift for creating OSX and iOS applications. My current project is essentially a flashcard application, and it needs to be able to create a popup window for text-based user prompts (ie, to ask what the question is for the card, or to add String tags for sorting the flashcards by type). Here is the code that I put together so far:
//Pulls up a prompt box to add tags
#IBAction func AddTagButton(sender: AnyObject) {
//Declare new subwindow
var win = NSWindow(contentRect: NSMakeRect(100, 100, 400, 150),
styleMask: 1 | 2 | 4 | 8,
backing: NSBackingStoreType.Buffered, defer: true);
win.title = "Tag Adder";
win.center();
//Add the window to the main viewer
window.addChildWindow(win, ordered:NSWindowOrderingMode.Above);
var controller = NSWindowController(window: win);
controller.showWindow(self);
}
This pulls up a new window with the ability to close, resize, minimize, and so on. I need to add a WrappedTextField to this window programmatically, but I couldn't find any resources on how to do so. In Java, the closest analogy would be something along the lines of
JFrame frame = new Jframe();
JLabel label = new JLabel("Sample text");
frame.add(label); //How is this done in Swift?
frame.setVisible(true);
I wrote the main NSWindow by modifying the .xib in XCode (Xcode 6, Beta version 6), but I can't figure out for the life of me how to use the WYSIWYG editor to make a window appear at the push of a button. The best I could do was to make another NSWindow that was minimized/hidden by default, but would show itself when you pushed the button (which isn't exactly a very good solution). The other feature I found was an NSAlert, but that doesn't have a text field for users to input data. My question is how do you add content to an NSWindow that pops up at the push of a button, either by modifying the above method, or by using the .xib GUI editor that XCode provides?
You should add content to the contentView of NSWindow.
let textField =. NSTextView()
textView.stringvalue = "Some string"
textView.frame = CGRectMake(10,20,50,400)
mywindow.contentView.addSubview(textView)

Resources