How to add watermark to Screen (NSScreen) in macOS - macos

I am making an application that will add a watermark to the live screen on Mac. Which API should I choose? I tried NSScreen but I didn't find any method that could add a view to NSScreen.
Such app like Sakura in Mac Appstore.Please check it out,i have no idea which API should i use.

The easiest solution is to define a custom, transparent, window.
When you create the window, you specify the special BorderlessWindowMask. This creates a window that is a simple rectangular area on the screen with no titlebar, edges, etc.
Then you set up a bunch of properties so that
the window floats above the other windows
it doesn't respond to events
it doesn't have a shadow
its background is transparent
And so on
#implementation WatermarkOverlayWindow
- (id)initWithContentRect:(NSRect)contentRect
{
self = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
if (self!=nil)
{
self.level = NSFloatingWindowLevel;
self.ignoresMouseEvents = YES;
self.releasedWhenClosed = NO;
self.movableByWindowBackground = NO;
self.alphaValue = 1.0f;
self.backgroundColor = NSColor.clearColor;
self.opaque = NO;
self.hasShadow = NO;
}
return self;
}
...
Now you can add semi-transparent views to this window and those views will appear to float on the screen. Alternatively, you can place opaque views in this window and then change the overall alphaValue of the window to something less than 1.0.

Related

How to repaint window after hide or show control

I have a NSTextField control on the bottom of the window.
I need to show them or hide according to some condition.
In the initial state the control is hidden.
The window shows the blank space on the control's position.
When in runtime the control should become visible
mpTxtCtrl.hidden = NO;
it does not happen.
The tasks:
1 Display/Hide control.
2 Resize the main window according to the control's state.
I can't find any tutorials how to manage layouts in the cocoa.
I would be grateful for any help!
When in runtime the control should become visible
mpTxtCtrl.hidden = NO;
it does not happen.
Make sure you connected the mpTxtCtrl outlet to the text field in your nib. Having forgotten to connect your outlet to anything is a leading cause of nothing happening.
I can't find any tutorials how to manage layouts in the cocoa.
Switch to the File Inspector while editing the nib and turn “Use Auto Layout” on. Xcode will create constraints whenever you place views along Aqua guides (the blue lines that appear when you place and size views correctly), and Cocoa will enforce these constraints as the sizes of views and the window change.
More info:
Cocoa Auto Layout Guide
WWDC 2012 videos — include several sessions on Auto Layout
OS X Human Interface Guidelines — Aqua guide lines appear to indicate conformance to the HIG
Check this code, if you will find some useful stuffs here:
-(void)awakeFromNib{
[self.label setHidden:YES];
}
- (IBAction)showHide:(id)sender {
NSLog(#"%#",[sender title]);
if ([[sender title] isEqualToString:#"Hide"]) {
[self.label setHidden:YES];
[sender setTitle:#"Show"];
}
else if ([[sender title] isEqualToString:#"Show"]){
[self.label setHidden:NO];
[sender setTitle:#"Hide"];
}
}
- (IBAction)maximize:(id)sender {
NSArray *screens = [NSScreen screens];
NSRect screenRect;
for (NSInteger index=0; index < [screens count]; index++) {
NSScreen *screen = screens[index];
screenRect = [screen visibleFrame];
}
[self.window setFrame:screenRect display:YES];
}

Two Finger Drag with IKImageView and NSScrollView in Mountain Lion

I have a Mac App that's been in the app store for a year or so now. It was first published with target SDK 10.7, Lion. Upon the update to Mountain Lion it no longer works.
The application displays large images in an IKImageView which is embedded in an NSScrollView. The purpose of putting it into a scrollview was to get two finger dragging working, rather than the user having to click to drag. Using ScrollViewWorkaround by Nicholas Riley, I was able to use two finger scrolling to show the clipped content after the user had zoomed in. Just like you see in the Preview app.
Nicholas Riley's Solution:
IKImageView and scroll bars
Now in Mountain Lion this doesn't work. After zooming in, pinch or zoom button, the image is locked in the lower left portion of the image. It won't scroll.
So the question is, what's the appropriate way to display a large image in IKImageView and have two finger dragging of the zoomed image?
Thank you,
Stateful
Well, Nicholas Riley's Solution is an ugly hack in that it addresses the wrong class; the issue isn't with NSClipView (which he subclassed, but which works just fine as is), but with IKImageView.
The issue with IKImageView is actually quite simple (God knows why Apple hasn't fixed this in what? … 7 years ...): Its size does not adjust to the size of the image it displays. Now, when you embed an IKImageView in an NSScrollView, the scroll view obviously can only adjust its scroll bars relative to the size of the embedded IKImageView, not to the image it contains. And since the size of the IKImageView always stays the same, the scroll bars won't work as expected.
The following code subclasses IKImageView and fixes this behavior. Alas, it won't fix the fact that IKImageView is crash-prone in Mountain Lion as soon as you zoom …
///////////////////// HEADER FILE - FixedIKImageView.h
#import <Quartz/Quartz.h>
#interface FixedIKImageView : IKImageView
#end
///////////////////// IMPLEMENTATION FILE - FixedIKImageView.m
#import "FixedIKImageView.h"
#implementation FixedIKImageView
- (void)awakeFromNib
{
[self setTranslatesAutoresizingMaskIntoConstraints:NO]; // compatibility with Auto Layout; without this, there could be Auto Layout error messages when we are resized (delete this line if your app does not use Auto Layout)
}
// FixedIKImageView must *only* be used embedded within an NSScrollView. This means that setFrame: should never be called explicitly from outside the scroll view. Instead, this method is overwritten here to provide the correct behavior within a scroll view. The new implementation ignores the frameRect parameter.
- (void)setFrame:(NSRect)frameRect
{
NSSize imageSize = [self imageSize];
CGFloat zoomFactor = [self zoomFactor];
NSSize clipViewSize = [[self superview] frame].size;
// The content of our scroll view (which is ourselves) should stay at least as large as the scroll clip view, so we make ourselves as large as the clip view in case our (zoomed) image is smaller. However, if our image is larger than the clip view, we make ourselves as large as the image, to make the scrollbars appear and scale appropriately.
CGFloat newWidth = (imageSize.width * zoomFactor < clipViewSize.width)? clipViewSize.width : imageSize.width * zoomFactor;
CGFloat newHeight = (imageSize.height * zoomFactor < clipViewSize.height)? clipViewSize.height : imageSize.height * zoomFactor;
[super setFrame:NSMakeRect(0, 0, newWidth - 2, newHeight - 2)]; // actually, the clip view is 1 pixel larger than the content view on each side, so we must take that into account
}
//// We forward size affecting messages to our superclass, but add [self setFrame:NSZeroRect] to update the scroll bars. We also add [self setAutoresizes:NO]. Since IKImageView, instead of using [self setAutoresizes:NO], seems to set the autoresizes instance variable to NO directly, the scrollers would not be activated again without invoking [self setAutoresizes:NO] ourselves when these methods are invoked.
- (void)setZoomFactor:(CGFloat)zoomFactor
{
[super setZoomFactor:zoomFactor];
[self setFrame:NSZeroRect];
[self setAutoresizes:NO];
}
- (void)zoomImageToRect:(NSRect)rect
{
[super zoomImageToRect:rect];
[self setFrame:NSZeroRect];
[self setAutoresizes:NO];
}
- (void)zoomIn:(id)sender
{
[super zoomIn:self];
[self setFrame:NSZeroRect];
[self setAutoresizes:NO];
}
- (void)zoomOut:(id)sender
{
[super zoomOut:self];
[self setFrame:NSZeroRect];
[self setAutoresizes:NO];
}
- (void)zoomImageToActualSize:(id)sender
{
[super zoomImageToActualSize:sender];
[self setFrame:NSZeroRect];
[self setAutoresizes:NO];
}
- (void)zoomImageToFit:(id)sender
{
[self setAutoresizes:YES]; // instead of invoking super's zoomImageToFit: method, which has problems of its own, we invoke setAutoresizes:YES, which does the same thing, but also makes sure the image stays zoomed to fit even if the scroll view is resized, which is the most intuitive behavior, anyway. Since there are no scroll bars in autoresize mode, we need not add [self setFrame:NSZeroRect].
}
- (void)setAutoresizes:(BOOL)autoresizes // As long as we autoresize, make sure that no scrollers flicker up occasionally during live update.
{
[self setHasHorizontalScroller:!autoresizes];
[self setHasVerticalScroller:!autoresizes];
[super setAutoresizes:autoresizes];
}
#end

Float an NSView above a window being used by DVDPlayback framework

I am trying to write a simple DVD player using Mac OS X's DVDPlayback.framework. I can get the DVD to play within a window, but ultimately I want this to run as a full-screen app.
I'm having difficulty adding a sub-view to display media controls whilst the DVD is playing (pause / play, progress slider to scroll through the movie, change chapter etc).
It seems that if I create a sub-view (NSView) within the window being used by the DVD framework, it always seems to fall behind the DVD content, even if I tell the NSView to be at the top-most level.
Here's the simplified code, which just tries to create a white sub-view within a region of the window:
(I've tried the code on 10.6 and 10.7 with the same results).
const BOOL PLAY_DVD = YES;
#interface ControlsView : NSView {
}
#end
#implementation ControlsView
- (void)drawRect:(NSRect)rect {
[[NSColor whiteColor] set];
NSRectFill(rect);
}
#end
#implementation AppDelegate
#synthesize window = _window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSView *view = self.window.contentView;
if (PLAY_DVD) {
NSLog(#"%# Playing DVD video", [self class]);
// Register error handler
OSStatus err;
err = DVDInitialize();
if (err != noErr) {
NSLog(#"DVDInitialise failed with error code %d", err);
[NSApp terminate:self];
}
// Set window to be the main window
err = DVDSetVideoWindowID([self.window windowNumber]);
// Set the display...
CGDirectDisplayID display = (CGDirectDisplayID) [[[[self.window screen] deviceDescription] valueForKey:#"NSScreenNumber"] intValue];
Boolean isSupported;
err = DVDSwitchToDisplay(display, &isSupported);
// Set video bounds
NSRect frame = [self.window frame];
CGRect rect = CGRectMake(0, 0, frame.size.width, frame.size.height);
err = DVDSetVideoCGBounds(&rect);
FSRef ref;
DVDOpenMediaFileWithURL([NSURL URLWithString:#"file:///Path/to/my/TestDVD/VIDEO_TS"]);
DVDOpenMediaFile(&ref);
DVDPlay();
}
// Attempt to add a subview to show the controls...
ControlsView *controls = [[ControlsView alloc] initWithFrame:NSMakeRect(20, 20, 100, 50)];
[view addSubview:controls positioned:NSWindowAbove relativeTo:nil];
}
#end
If PLAY_DVD is NO, the subview is correctly rendered (and I can create other sub-views and show that the ordering is correct).
If PLAY_DVD is YES, the media starts playing, but the sub-view is never visible because it always seems to fall behind the video.
The only examples of DVD playback I've been able to find have had the controls in a second window, but for a full-screen application I'd like the controls to be part of the full-screen view and to fade in/out when required.
Does anyone know how best to do this? Do my full-screen controls have to be in a separate window which floats above the full-screen window? I've not been able to find an example which has the controls and the DVD playback in the same window.
Thanks in advance for your help!

Changing alpha of window background, not the whole window

so i have a quick question i have the method below which sets the alpha value of a window depending on the value from a slider, however the content of the window also becomes translucent and eventually disappears with the window.
Is there a way to just change the alpha value of the window and not the content view inside it?
- (IBAction)changeTransparency:(id)sender {
// Set the window's alpha value. This will cause the views in the window to redraw.
[self.window setAlphaValue:[sender floatValue]];}
Thanks, Sami.
Apple's docs gives a way to do this. The key is to set the window's backgroundColor's alpha to the desired value. You must also make sure to set the window's opaque property to NO (which is YES by default.)
e.x.
// At some point in your code...
[window setOpaque:NO];
// Then in your changeTransparency: method...
NSColor *backgroundColor = [window backgroundColor];
backgroundColor = [backgroundColor colorWithAlphaComponent:[sender floatValue]];
[window setBackgroundColor:backgroundColor];
Here is another way.
Suppose,
self.window <--- base view and this alpha will be changed (but exacatly fake).
subView1, subView2 <-- these views are contents of self.window. and theier alpha should not be changed.
self.window.backgroundColor = [UIColor clearColor];
UIView* anAlphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.widht, self.window.frame.size.height)];
anAlphaView.backgroundColor = [UIColor blackColor]; // as you want
anAlphaView.alpha = 0.5f; // as you want.
[self.window addSubview:anAlphaView];
[anAlphaView release];
[self.window addSubview:subView1]; // you should add sub views to self.window
[self.window addSubview:subView2];
You can make a method using above code :)

Making NSScroller larger

I'm making a Cocoa application optimized for external touch sensitive screens, so I want to make a large scollbar in an NSScrollView. I've tried just to resize the scrollers frame, but both drawing and mouse events only happens in the scroll area, it doesn't get wider (it's the vertical scroller). This is the code I'm using (from the subclassed NSScrollView):
- (void)tile{
[super tile];
NSScroller *vertScroll = [self verticalScroller];
NSRect vertScrollFrame = [vertScroll frame];
NSView *content = [self contentView];
NSRect contentFrame = [content frame];
contentFrame.size.width -= 50;
vertScrollFrame.origin.x -= 50;
vertScrollFrame.size.width += 50;
[vertScroll setFrame:vertScrollFrame];
[content setFrame:contentFrame];
}
I've tried to subclass the NSScroller, but I don't know how I would extent it.
Does your customized scroll view exist in a nib/xib? If so, have you set the scroll view's class identity in Interface Builder to the name of your custom class? It's found in the "Identity" tab of the Inspector window.

Resources