Skip to content

Commit fabd2f7

Browse files
committed
(#3) Added macOS implementation of configurable, transparent highlight window
1 parent 5ae5a57 commit fabd2f7

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/window.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
#ifndef WINDOW_H
3+
#define WINDOW_H
4+
5+
class Window {
6+
public:
7+
Window(int x, int y, int width, int height);
8+
void show(int duration, float opacity);
9+
10+
private:
11+
int _x;
12+
int _y;
13+
int _width;
14+
int _height;
15+
};
16+
17+
#endif

src/window_macos.mm

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "window.h"
2+
#import <Cocoa/Cocoa.h>
3+
4+
5+
Window::Window(int x, int y, int width, int height) {
6+
this->_x = x;
7+
this->_y = y;
8+
this->_width = width;
9+
this->_height = height;
10+
}
11+
12+
void Window::show(int duration, float opacity) {
13+
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
14+
[NSApplication sharedApplication];
15+
16+
NSRect frame = NSMakeRect(this->_x, this->_y, this->_width, this->_height);
17+
NSUInteger styleMask = NSWindowStyleMaskBorderless;
18+
NSWindow * window = [[[NSWindow alloc] initWithContentRect:frame styleMask:styleMask backing:NSBackingStoreBuffered defer:NO] autorelease];
19+
[window setBackgroundColor:[NSColor redColor]];
20+
[window setAlphaValue:opacity];
21+
[window orderFrontRegardless];
22+
23+
[NSTimer scheduledTimerWithTimeInterval: duration
24+
repeats: NO
25+
block: ^(NSTimer * timer){
26+
[NSApp stop:0];
27+
NSRect f = [window frame];
28+
f.size = CGSizeMake(0, 0);
29+
[window setFrame: f display: YES animate: NO];
30+
}];
31+
[NSApp run];
32+
[pool release];
33+
}

0 commit comments

Comments
 (0)