1
0
Fork 0

Fix #9743: [OSX] Only (re-)create touchbar sprites when requested by the main loop.

pull/9780/head
Michael Lutz 2021-12-31 01:04:35 +01:00
parent f40e82a19d
commit b351cbe490
5 changed files with 63 additions and 18 deletions

View File

@ -234,11 +234,15 @@ void VideoDriver_CocoaOpenGL::Stop()
void VideoDriver_CocoaOpenGL::PopulateSystemSprites() void VideoDriver_CocoaOpenGL::PopulateSystemSprites()
{ {
VideoDriver_Cocoa::PopulateSystemSprites();
OpenGLBackend::Get()->PopulateCursorCache(); OpenGLBackend::Get()->PopulateCursorCache();
} }
void VideoDriver_CocoaOpenGL::ClearSystemSprites() void VideoDriver_CocoaOpenGL::ClearSystemSprites()
{ {
VideoDriver_Cocoa::ClearSystemSprites();
CGLSetCurrentContext(this->gl_context); CGLSetCurrentContext(this->gl_context);
OpenGLBackend::Get()->ClearCursorCache(); OpenGLBackend::Get()->ClearCursorCache();
} }

View File

@ -22,7 +22,8 @@ extern bool _cocoa_video_started;
class VideoDriver_Cocoa : public VideoDriver { class VideoDriver_Cocoa : public VideoDriver {
private: private:
Dimension orig_res; ///< Saved window size for non-fullscreen mode. Dimension orig_res; ///< Saved window size for non-fullscreen mode.
bool refresh_sys_sprites; ///< System sprites need refreshing.
public: public:
bool setup; ///< Window is currently being created. bool setup; ///< Window is currently being created.
@ -45,6 +46,9 @@ public:
bool ChangeResolution(int w, int h) override; bool ChangeResolution(int w, int h) override;
bool ToggleFullscreen(bool fullscreen) override; bool ToggleFullscreen(bool fullscreen) override;
void ClearSystemSprites() override;
void PopulateSystemSprites() override;
void EditBoxLostFocus() override; void EditBoxLostFocus() override;
std::vector<int> GetListOfMonitorRefreshRates() override; std::vector<int> GetListOfMonitorRefreshRates() override;

View File

@ -99,6 +99,8 @@ VideoDriver_Cocoa::VideoDriver_Cocoa()
this->setup = false; this->setup = false;
this->buffer_locked = false; this->buffer_locked = false;
this->refresh_sys_sprites = true;
this->window = nil; this->window = nil;
this->cocoaview = nil; this->cocoaview = nil;
this->delegate = nil; this->delegate = nil;
@ -221,6 +223,19 @@ bool VideoDriver_Cocoa::ToggleFullscreen(bool full_screen)
return false; return false;
} }
void VideoDriver_Cocoa::ClearSystemSprites()
{
this->refresh_sys_sprites = true;
}
void VideoDriver_Cocoa::PopulateSystemSprites()
{
if (this->refresh_sys_sprites && this->window != nil) {
[ this->window refreshSystemSprites ];
this->refresh_sys_sprites = false;
}
}
/** /**
* Callback invoked after the blitter was changed. * Callback invoked after the blitter was changed.
* @return True if no error. * @return True if no error.

View File

@ -95,6 +95,8 @@ static NSDictionary *touchBarFallbackText = @{
- (instancetype)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag driver:(VideoDriver_Cocoa *)drv; - (instancetype)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag driver:(VideoDriver_Cocoa *)drv;
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag; - (void)setFrame:(NSRect)frameRect display:(BOOL)flag;
- (void)refreshSystemSprites;
@end @end
/** Subclass of NSView to support mouse awareness and text input. */ /** Subclass of NSView to support mouse awareness and text input. */

View File

@ -414,6 +414,7 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
@implementation OTTD_CocoaWindow { @implementation OTTD_CocoaWindow {
VideoDriver_Cocoa *driver; VideoDriver_Cocoa *driver;
bool touchbar_created;
} }
/** /**
@ -423,6 +424,7 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
{ {
if (self = [ super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag ]) { if (self = [ super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag ]) {
self->driver = drv; self->driver = drv;
self->touchbar_created = false;
[ self setContentMinSize:NSMakeSize(64.0f, 64.0f) ]; [ self setContentMinSize:NSMakeSize(64.0f, 64.0f) ];
@ -461,27 +463,14 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
bar.delegate = self; bar.delegate = self;
bar.defaultItemIdentifiers = touchBarButtonIdentifiers; bar.defaultItemIdentifiers = touchBarButtonIdentifiers;
self->touchbar_created = true;
return bar; return bar;
} }
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier - (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
{ {
NSNumber *num = touchBarButtonSprites[identifier]; NSButton *button = [ NSButton buttonWithTitle:touchBarFallbackText[identifier] target:self action:@selector(touchBarButtonAction:) ];
NSImage *image = NSImageFromSprite(num.unsignedIntValue, _settings_client.gui.zoom_min);
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);
}
button = [ NSButton buttonWithImage:image target:self action:@selector(touchBarButtonAction:) ];
} else {
button = [ NSButton buttonWithTitle:touchBarFallbackText[identifier] target:self action:@selector(touchBarButtonAction:) ];
}
button.identifier = identifier; button.identifier = identifier;
button.imageScaling = NSImageScaleProportionallyDown; button.imageScaling = NSImageScaleProportionallyDown;
@ -489,7 +478,38 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
tb_item.view = button; tb_item.view = button;
return tb_item; return tb_item;
} }
#endif
#endif /* HAVE_TOUCHBAR_SUPPORT */
- (void)refreshSystemSprites
{
#ifdef HAVE_TOUCHBAR_SUPPORT
if (!self->touchbar_created || ![ self respondsToSelector:@selector(touchBar) ] || self.touchBar == nil) return;
/* Re-create button images from OTTD sprites. */
for (NSTouchBarItemIdentifier ident in self.touchBar.itemIdentifiers) {
NSCustomTouchBarItem *tb_item = [ self.touchBar itemForIdentifier:ident ];
NSButton *button = tb_item.view;
NSNumber *num = touchBarButtonSprites[ident];
NSImage *image = NSImageFromSprite(num.unsignedIntValue, _settings_client.gui.zoom_min);
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);
}
button.image = image;
button.imagePosition = NSImageOnly;
} else {
button.image = nil;
button.imagePosition = NSNoImage;
}
}
#endif /* HAVE_TOUCHBAR_SUPPORT */
}
@end @end