Skip to content

Commit

Permalink
Fix OpenTTD#9743: [OSX] Only (re-)create touchbar sprites when reques…
Browse files Browse the repository at this point in the history
…ted by the main loop.
  • Loading branch information
michicc committed Dec 31, 2021
1 parent 7feb98f commit e0d125b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/video/cocoa/cocoa_ogl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,15 @@ - (void)viewDidChangeBackingProperties

void VideoDriver_CocoaOpenGL::PopulateSystemSprites()
{
VideoDriver_Cocoa::PopulateSystemSprites();

OpenGLBackend::Get()->PopulateCursorCache();
}

void VideoDriver_CocoaOpenGL::ClearSystemSprites()
{
VideoDriver_Cocoa::ClearSystemSprites();

CGLSetCurrentContext(this->gl_context);
OpenGLBackend::Get()->ClearCursorCache();
}
Expand Down
6 changes: 5 additions & 1 deletion src/video/cocoa/cocoa_v.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ extern bool _cocoa_video_started;

class VideoDriver_Cocoa : public VideoDriver {
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:
bool setup; ///< Window is currently being created.
Expand All @@ -45,6 +46,9 @@ class VideoDriver_Cocoa : public VideoDriver {
bool ChangeResolution(int w, int h) override;
bool ToggleFullscreen(bool fullscreen) override;

void ClearSystemSprites() override;
void PopulateSystemSprites() override;

void EditBoxLostFocus() override;

std::vector<int> GetListOfMonitorRefreshRates() override;
Expand Down
15 changes: 15 additions & 0 deletions src/video/cocoa/cocoa_v.mm
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
this->setup = false;
this->buffer_locked = false;

this->refresh_sys_sprites = true;

this->window = nil;
this->cocoaview = nil;
this->delegate = nil;
Expand Down Expand Up @@ -221,6 +223,19 @@
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.
* @return True if no error.
Expand Down
2 changes: 2 additions & 0 deletions src/video/cocoa/cocoa_wnd.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ static NSDictionary *touchBarFallbackText = @{
- (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)refreshSystemSprites;
@end

/** Subclass of NSView to support mouse awareness and text input. */
Expand Down
50 changes: 33 additions & 17 deletions src/video/cocoa/cocoa_wnd.mm
Original file line number Diff line number Diff line change
Expand Up @@ -466,30 +466,46 @@ - (nullable NSTouchBar *)makeTouchBar

- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
{
NSNumber *num = touchBarButtonSprites[identifier];
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:) ];
}
NSButton *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

#endif /* HAVE_TOUCHBAR_SUPPORT */

- (void)refreshSystemSprites
{
#ifdef HAVE_TOUCHBAR_SUPPORT
if (![ 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

Expand Down

0 comments on commit e0d125b

Please sign in to comment.