Get the iOS safe area width/height in Nativescript - nativescript

I'm trying to get the width and height of the iOS safe area in Nativescript. I can't find any documentation on how this is done.
Can anybody help me please?

I found the solution already. I hope this helps somebody.
import * as application from "#nativescript/core/application";
import { Screen } from "#nativescript/core/platform";
let screenheight = Screen.mainScreen.heightDIPs;
let safeAreaTopInset = application.ios.window.safeAreaInsets.top;
let safeAreaBottomInset = application.ios.window.safeAreaInsets.bottom;
let safeAreaHeight = screenheight - safeAreaTopInset - safeAreaBottomInset;

Related

Panning AudioKit's AKMetronome

I am making a metronome app and I need to pan one metronome to the left and another to the right. I know AKMetronome class has no pan property, but can anyone suggest I can accomplish this?
let mixer = AKMixer(metronome, metronome2)
AudioKit.output = mixer
try AudioKit.start()
metronome.start()
metronome2.start()
Above code works but two metronomes are both centered. Thanks.
AKPanner will do the trick:
https://audiokit.io/docs/Classes/AKPanner.html
https://github.com/AudioKit/AudioKit/blob/master/AudioKit/Common/Nodes/Mixing/Panner/AKPanner.swift
let leftPan = AKPanner(metronome)
leftPan.pan = -1
let rightPan = AKPanner(metronome2)
rightPan.pan = 1
let mixer = AKMixer(leftPan, rightPan)

How to snap-to-grid in macOS via Swift?

I'm developing simple app for macOS using Swift 2. How can I snap a CGPoint to grid (5-pixel distance for example) pressing a keyboard shortcut? I need such kind of point snapping as it's found in AutoCAD.
I am looking for a solution for final project not Interface Builder.
this is the general way to do it, something like a pseudo code
var gridWidth = 16.0
var gridHeight = 16.0
object.Position.X = Math.Floor(object.Position.X / gridWidth) * gridWidth
object.Position.Y = Math.Floor(object.Position.Y / gridHeight) * gridHeight

Add circle on Bokeh image

I'm working with Bokeh and I want to add a circle on a specific position on my image.
For the moment, I create my image like this :
img = image(image=[data],
x_range=[0, x_range],
y_range=[0, y_range],
x=x,
y=y,
dw=dw,
dh=dh,
tools=TOOLS,
palette=["Greys-9"],
title=title,
plot_width=plot_width,
plot_height=plot_height,
)
circle(x=10,y=10,radius=100,fill_color="#df1c1c",line_color="#df1c1c")
resources = Resources("inline")
plot_script, plot_div = components(img, resources)
html_script = encode_utf8(plot_script)
html_div = encode_utf8(plot_div)
hold()
figure()
return html_script, html_div
and send this to my HTML page.
The problem is that the circle is not on the final display. Maybe on background ? I don't know...
I tryed add function, add_glyph function, add_layout... None of these are functionnal!
Thanks for helping guys
The above code did not work due to a bug in Bokeh. However, the bug has since been fixed, and that code and code similar to it will function as expected.

Firefox Addon API for Taking Screenshot

I am looking for firefox addon api to take screenshot of visible area of document.
Chrome and Safari have api's to achieve this. And they are pretty fast.
I could not find anything specific for firefox.
I found a workaround at How do I use the canvas drawWindow function in an addon created using the addon sdk? but this solution takes full page screenshot with scrolls including (hidden parts of document). There are 2 issues for this solution;
1- if page has long scroll, it takes long time to complete screenshot process. Because it is using canvas based drawing.
2- I would like to get screenshot of visible area of document, not whole document.
Is there any workaround for this?
Thanks.
Using the SDK you can do something like this:
const { window: { document } } = require('sdk/addon/window');
const { getTabContentWindow, getActiveTab } = require('sdk/tabs/utils');
const { getMostRecentBrowserWindow } = require('sdk/window/utils');
const canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas');
document.documentElement.appendChild(canvas);
function captureTab(tab=getActiveTab(getMostRecentBrowserWindow())) {
let contentWindow = getTabContentWindow(tab);
let w = contentWindow.innerWidth;
let h = contentWindow.innerHeight;
let x = contentWindow.scrollX;
let y = contentWindow.scrollY;
canvas.width = w;
canvas.height = h;
let ctx = canvas.getContext('2d');
ctx.drawWindow(contentWindow, x, y, w, h, '#000');
return canvas.toDataURL();
}
That should takes only the visible area. By default, it grabs the active tab, but you can pass any other tab – because is designed as low level API it takes a native tab, however, not a SDK tab.
You can put in a module and exports just the captureTab function.
Edit: e10s version
The code above is not currently compatible with Firefox with e10s available, as Ian Bicking noted in the comment. An easy way to workaround this issue, is create a temporary canvas in the same document and content process we want to capture the screenshot:
const { getTabContentWindow, getActiveTab } = require('sdk/tabs/utils');
const { getMostRecentBrowserWindow } = require('sdk/window/utils');
function captureTab(tab=getActiveTab(getMostRecentBrowserWindow())) {
let contentWindow = getTabContentWindow(tab);
let { document } = contentWindow;
let w = contentWindow.innerWidth;
let h = contentWindow.innerHeight;
let x = contentWindow.scrollX;
let y = contentWindow.scrollY;
let canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas');
canvas.width = w;
canvas.height = h;
let ctx = canvas.getContext('2d');
ctx.drawWindow(contentWindow, x, y, w, h, '#000');
let dataURL = canvas.toDataURL();
canvas = null;
return dataURL;
}
That works in both e10s and no-e10s FF version; the downside comparing to the previous one is creating a canvas every time we want to take a screenshot, but I think is acceptable.
Your assumption that taking a screenshot on Firefox with canvas is somehow slow is wrong.
I did a couple of screenshots and Firefox/canvas was faster than Chrome/captureVisibleTab.
Actually Firefox is better suited for as-fast-as-possible screenshots, since its canvas expose to privileged code the mozFetchAsStream method, allowing to bypass the actual bottleneck which is the base64 encoding of the image data.
Some numbers
Chrome: captureVisibleTab 200-205ms
Firefox: drawImage 20-25ms + toDataURL 125-130ms
The devtools screenshot command is a good example of how to capture just the visible part
In all fairness, to make a meaningful comparison one has to take into account whether Chrome's PNG encoder favors compression over speed. Still, this doesn't change the fact that Firefox's canvas is fine.
edit: OK, that base64 encoding remark is dumb, I don't know what I was thinking. Perhaps what I should write instead is that Firefox's canvas is not only fast but also versatile.

UIScrollView doesn't scroll with RubyMotion

I have a test application that I am trying to get the UIView to scroll and also move up when the keyboard comes on the screen. When I put the following code in and try to run the scroll through the simulator my UIScroll isn't working.
#scrollwindow = UIScrollView.alloc.initWithFrame(CGRect.new([0,0],[700,800]))
#scrollwindow.scrollEnabled = true
#scrollwindow.delegate
#window.addSubview(#scrollwindow)
#frame1 = UIView.alloc.initWithFrame(CGRect.new([10,10], [400, 200]))
#frame1.backgroundColor = UIColor.redColor
#scrollwindow.addSubview(#frame1)
#frame2 = UIView.alloc.initWithFrame(CGRect.new([20, 400], [600,700]))
#frame2.backgroundColor = UIColor.greenColor
#scrollwindow.addSubview(#frame2)
I am familiar with my understanding of Ruby but new to RubyMotion and the Objective-C community. Any help would be greatly appreciated.
You need to set the contentSize of the scroll view.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html

Resources