Changeset - r26143:a4ef11add553
[Not reviewed]
master
0 2 0
Michael Lutz - 2 years ago 2021-12-30 23:29:51
michi@icosahedron.de
Change: [OSX] Allow touchbar usage on all supported OS versions.

Touchbar support was introduced in 10.12.2. There's no need to limit
support to 10.15+, as the convenience class NSButtonTouchBarItem is
easily replicated.
2 files changed with 41 insertions and 36 deletions:
0 comments (0 inline, 0 general)
src/video/cocoa/cocoa_wnd.h
Show inline comments
 
@@ -14,6 +14,10 @@
 
#include "toolbar_gui.h"
 
#include "table/sprites.h"
 

	
 
#ifdef MAC_OS_X_VERSION_10_12_2
 
#	define HAVE_TOUCHBAR_SUPPORT
 
#endif
 

	
 
class VideoDriver_Cocoa;
 

	
 
/* Right Mouse Button Emulation enum */
 
@@ -30,7 +34,7 @@ extern NSString *OTTDMainLaunchGameEngin
 
+ (NSCursor *) clearCocoaCursor;
 
@end
 

	
 
#ifdef HAVE_OSX_1015_SDK
 
#ifdef HAVE_TOUCHBAR_SUPPORT
 
/* 9 items can be displayed on the touch bar when using default buttons. */
 
static NSArray *touchBarButtonIdentifiers = @[
 
	@"openttd.pause",
 
@@ -83,12 +87,11 @@ static NSDictionary *touchBarFallbackTex
 
#endif
 

	
 
/** Subclass of NSWindow to cater our special needs */
 
#ifdef HAVE_OSX_1015_SDK
 
@interface OTTD_CocoaWindow : NSWindow <NSTouchBarDelegate>
 
@property (strong) NSSet *touchbarItems;
 
- (NSImage*)generateImage:(int)spriteId;
 
#else
 
@interface OTTD_CocoaWindow : NSWindow
 
#ifdef HAVE_TOUCHBAR_SUPPORT
 
	<NSTouchBarDelegate>
 

	
 
- (NSImage *)generateImage:(int)spriteId;
 
#endif
 

	
 
- (instancetype)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag driver:(VideoDriver_Cocoa *)drv;
src/video/cocoa/cocoa_wnd.mm
Show inline comments
 
@@ -405,18 +405,25 @@ void CocoaDialog(const char *title, cons
 
	return self;
 
}
 

	
 
#ifdef HAVE_OSX_1015_SDK
 
/**
 
 * Define the rectangle we draw our window in
 
 */
 
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag
 
{
 
	[ super setFrame:frameRect display:flag ];
 

	
 
	driver->AllocateBackingStore();
 
}
 

	
 
#ifdef HAVE_TOUCHBAR_SUPPORT
 

	
 
- (void)touchBarButtonAction:(id)sender
 
{
 
	if (@available(macOS 10.15, *)) {
 
		NSButtonTouchBarItem *btn = (NSButtonTouchBarItem *)sender;
 
		NSNumber *hotkeyIndex = [ touchBarButtonActions objectForKey:btn.identifier ];
 
		HandleToolbarHotkey(hotkeyIndex.intValue);
 
	}
 
	NSButton *btn = (NSButton *)sender;
 
	NSNumber *hotkeyIndex = [ touchBarButtonActions objectForKey:btn.identifier ];
 
	if (hotkeyIndex != nil) HandleToolbarHotkey(hotkeyIndex.intValue);
 
}
 

	
 
#pragma mark NSTouchBarProvider
 
- (nullable NSTouchBar *)makeTouchBar
 
{
 
	NSTouchBar *bar = [ [ NSTouchBar alloc ] init ];
 
@@ -464,38 +471,33 @@ void CocoaDialog(const char *title, cons
 
	return outImage;
 
}
 

	
 
#pragma mark NSTouchBarDelegate
 
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
 
{
 
	if (@available(macOS 10.15, *)) {
 
		NSButtonTouchBarItem *button = [ [ NSButtonTouchBarItem alloc ] initWithIdentifier:identifier ];
 
		button.target = self;
 
		button.action = @selector(touchBarButtonAction:);
 
	NSNumber *num = touchBarButtonSprites[identifier];
 
	NSImage *image = [ self generateImage:num.unsignedIntValue ];
 

	
 
		NSNumber *num = touchBarButtonSprites[identifier];
 
		NSImage *generatedImage = [ self generateImage:num.unsignedIntValue ];
 
		if (generatedImage != nullptr) {
 
			button.image = generatedImage;
 
		} else {
 
			button.title = NSLocalizedString(touchBarFallbackText[identifier], @"");
 
	NSButton *button;
 
	if (image != nil) {
 
		/* Human Interface Guidelines: Maximum touch bar glyph size 22 pt. */
 
		CGFloat max_dim = std::max(image.size.width, image.size.height);
 
		if (max_dim > 0.0) {
 
			CGFloat scale = 22.0 / max_dim;
 
			image.size = NSMakeSize(image.size.width * scale, image.size.height * scale);
 
		}
 
		return button;
 

	
 
		button = [ NSButton buttonWithImage:image target:self action:@selector(touchBarButtonAction:) ];
 
	} else {
 
		return nullptr;
 
		button = [ NSButton buttonWithTitle:touchBarFallbackText[identifier] target:self action:@selector(touchBarButtonAction:) ];
 
	}
 
	button.identifier = identifier;
 
	button.imageScaling = NSImageScaleProportionallyDown;
 

	
 
	NSCustomTouchBarItem *tb_item = [ [ NSCustomTouchBarItem alloc] initWithIdentifier:identifier ];
 
	tb_item.view = button;
 
	return tb_item;
 
}
 
#endif
 

	
 
/**
 
 * Define the rectangle we draw our window in
 
 */
 
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag
 
{
 
	[ super setFrame:frameRect display:flag ];
 

	
 
	driver->AllocateBackingStore();
 
}
 

	
 
@end
 

	
 
@implementation OTTD_CocoaView {
0 comments (0 inline, 0 general)