1#import "InstanceCheck.hpp"
2#import "InstanceCheckMac.h"
3#import "GUI_App.hpp"
4
5@implementation OtherInstanceMessageHandlerMac
6
7-(instancetype) init
8{
9	self = [super init];
10	return self;
11}
12-(void)add_observer:(NSString *)version_hash
13{
14	//NSLog(@"adding observer");
15	//NSString *nsver = @"OtherPrusaSlicerInstanceMessage" + version_hash;
16	NSString *nsver = [NSString stringWithFormat: @"%@%@", @"OtherPrusaSlicerInstanceMessage", version_hash];
17	[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(message_update:) name:nsver object:nil suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
18}
19
20-(void)message_update:(NSNotification *)msg
21{
22	[self bring_forward];
23	//pass message
24	Slic3r::GUI::wxGetApp().other_instance_message_handler()->handle_message(std::string([msg.userInfo[@"data"] UTF8String]));
25}
26
27-(void) bring_forward
28{
29	//demiaturize all windows
30	for(NSWindow* win in [NSApp windows])
31	{
32		if([win isMiniaturized])
33		{
34			[win deminiaturize:self];
35		}
36	}
37	//bring window to front
38	[[NSApplication sharedApplication] activateIgnoringOtherApps : YES];
39}
40
41@end
42
43namespace Slic3r {
44
45void send_message_mac(const std::string &msg, const std::string &version)
46{
47	NSString *nsmsg = [NSString stringWithCString:msg.c_str() encoding:[NSString defaultCStringEncoding]];
48	//NSString *nsver = @"OtherPrusaSlicerInstanceMessage" + [NSString stringWithCString:version.c_str() encoding:[NSString defaultCStringEncoding]];
49	NSString *nsver = [NSString stringWithCString:version.c_str() encoding:[NSString defaultCStringEncoding]];
50	NSString *notifname = [NSString stringWithFormat: @"%@%@", @"OtherPrusaSlicerInstanceMessage", nsver];
51	[[NSDistributedNotificationCenter defaultCenter] postNotificationName:notifname object:nil userInfo:[NSDictionary dictionaryWithObject:nsmsg forKey:@"data"] deliverImmediately:YES];
52}
53
54namespace GUI {
55void OtherInstanceMessageHandler::register_for_messages(const std::string &version_hash)
56{
57	m_impl_osx = [[OtherInstanceMessageHandlerMac alloc] init];
58	if(m_impl_osx) {
59		NSString *nsver = [NSString stringWithCString:version_hash.c_str() encoding:[NSString defaultCStringEncoding]];
60		[m_impl_osx add_observer:nsver];
61	}
62}
63
64void OtherInstanceMessageHandler::unregister_for_messages()
65{
66	//NSLog(@"unreegistering other instance messages");
67	if (m_impl_osx) {
68        [m_impl_osx release];
69        m_impl_osx = nullptr;
70    } else {
71		NSLog(@"warning: unregister instance InstanceCheck notifications not required");
72	}
73}
74
75void OtherInstanceMessageHandler::bring_instance_forward()
76{
77	if (m_impl_osx) {
78		[m_impl_osx bring_forward];
79	}
80}
81}//namespace GUI
82}//namespace Slicer
83
84
85