Skip to content

Commit

Permalink
Managing shared UI properly
Browse files Browse the repository at this point in the history
  • Loading branch information
gershnik committed Jan 10, 2024
1 parent 2b12536 commit f0e0460
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Translit/src/AppDelegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

@property (nonatomic, readonly) NSMenu * menu;

-(void) displayMappingsForLanguage:(NSString *)language;
-(void) setMappingsLanguage:(NSString *)language;

@end

#endif
Expand Down
23 changes: 20 additions & 3 deletions Translit/src/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

#import "AppDelegate.hpp"
#import "MenuProtocol.hpp"
#include "MappingsWindowController.hpp"

@interface AppDelegate () {
NSWindow * _window;
IBOutlet NSMenu * _menu;
MappingsWindowController * _mappingsController;
}

@end
Expand Down Expand Up @@ -61,10 +63,25 @@ -(NSMenu *) menu {
return _menu;
}

-(void) displayMappingsForLanguage:(NSString *)language {
if (!_mappingsController) {
_mappingsController = [[MappingsWindowController alloc] initWithWindowNibName:@"mappings"];
[_mappingsController.window setLevel:NSMainMenuWindowLevel];
}
_mappingsController.language = language;
[_mappingsController showWindow:self];
[_mappingsController.window orderFrontRegardless];
}

-(void) setMappingsLanguage:(NSString *)language {
if (_mappingsController)
_mappingsController.language = language;
}


//- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app {
// return YES;
//}
- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app {
return YES;
}


@end
29 changes: 17 additions & 12 deletions Translit/src/InputController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "Transliterator.hpp"
#include "AppDelegate.hpp"
#include "MenuProtocol.hpp"
#include "MappingsWindowController.hpp"



@interface InputController : IMKInputController<MenuProtocol>
Expand All @@ -14,7 +14,6 @@ @interface InputController : IMKInputController<MenuProtocol>

@interface InputController() {
std::unique_ptr<Transliterator> _transliterator;
MappingsWindowController * _mappingsController;
NSString * _currentLanguage;
}

Expand Down Expand Up @@ -70,11 +69,8 @@ -(NSMenu*) menu
//MenuProtocol

-(void) displayMappings:(id)sender {
if (!_mappingsController)
_mappingsController = [[MappingsWindowController alloc] initWithWindowNibName:@"mappings"];
_mappingsController.language = _currentLanguage;
[_mappingsController showWindow:self];
[_mappingsController.window orderFrontRegardless];
auto del = (AppDelegate *)NSApp.delegate;
[del displayMappingsForLanguage:_currentLanguage];
}


Expand All @@ -97,16 +93,25 @@ -(void) setValue:(id)value forTag:(long)tag client:(id)sender
_currentLanguage = val.remove_prefix(prefix).ns_str();
os_log_info(OS_LOG_DEFAULT, "Setting language to %{public}@", _currentLanguage);
_transliterator = std::make_unique<Transliterator>(_currentLanguage);
if (_mappingsController)
_mappingsController.language = _currentLanguage;
auto del = (AppDelegate *)NSApp.delegate;
[del setMappingsLanguage:_currentLanguage];
}
}
[super setValue:value forTag:tag client:sender];
}

//-(void) deactivateServer:(id)sender {
// os_log_debug(OS_LOG_DEFAULT, "Deactivate");
//}
- (void)activateServer:(id)sender {
[super activateServer:sender];

auto del = (AppDelegate *)NSApp.delegate;
[del setMappingsLanguage:_currentLanguage];
}

-(void) deactivateServer:(id)sender {
auto del = (AppDelegate *)NSApp.delegate;
[del setMappingsLanguage:nil];
[super deactivateServer:sender];
}

//Private

Expand Down

0 comments on commit f0e0460

Please sign in to comment.