From edd113548ab4a40ce037794a897a2dc23d59ee4e Mon Sep 17 00:00:00 2001 From: Armagan Amcalar Date: Sun, 22 Oct 2017 01:00:27 +0200 Subject: [PATCH] Make ScrubberItem width dynamic Depending on whether a ScrubberItem has text or an icon, this changeset calculates the actual width and sizes the TouchBar items accordingly. Previously, all ScrubberItems, regardless of their content, had a static width of 50px. This commit also fixes #10539. --- atom/browser/ui/cocoa/atom_touch_bar.h | 2 +- atom/browser/ui/cocoa/atom_touch_bar.mm | 36 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/atom/browser/ui/cocoa/atom_touch_bar.h b/atom/browser/ui/cocoa/atom_touch_bar.h index 26662cf485..d36e45898e 100644 --- a/atom/browser/ui/cocoa/atom_touch_bar.h +++ b/atom/browser/ui/cocoa/atom_touch_bar.h @@ -17,7 +17,7 @@ #include "native_mate/constructor.h" #include "native_mate/persistent_dictionary.h" -@interface AtomTouchBar : NSObject { +@interface AtomTouchBar : NSObject { @protected std::vector ordered_settings_; std::map settings_; diff --git a/atom/browser/ui/cocoa/atom_touch_bar.mm b/atom/browser/ui/cocoa/atom_touch_bar.mm index 4c5c99a250..927c8d1572 100644 --- a/atom/browser/ui/cocoa/atom_touch_bar.mm +++ b/atom/browser/ui/cocoa/atom_touch_bar.mm @@ -667,4 +667,40 @@ static NSString* const ImageScrubberItemIdentifier = @"scrubber.image.item"; return itemView; } +- (NSSize)scrubber:(NSScrubber *)scrubber layout:(NSScrubberFlowLayout *)layout sizeForItemAtIndex:(NSInteger)itemIndex +{ + NSInteger width = 50; + NSInteger height = 30; + NSInteger margin = 15; + NSSize defaultSize = NSMakeSize(width, height); + + std::string s_id([[scrubber identifier] UTF8String]); + if (![self hasItemWithID:s_id]) return defaultSize; + + mate::PersistentDictionary settings = settings_[s_id]; + std::vector items; + if (!settings.Get("items", &items)) return defaultSize; + + if (itemIndex >= static_cast(items.size())) return defaultSize; + + mate::PersistentDictionary item = items[itemIndex]; + std::string title; + + if (item.Get("label", &title)) { + NSSize size = NSMakeSize(CGFLOAT_MAX, CGFLOAT_MAX); + NSRect textRect = [base::SysUTF8ToNSString(title) boundingRectWithSize:size + options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading + attributes:@{ NSFontAttributeName: [NSFont systemFontOfSize:0]}]; + + width = textRect.size.width + margin; + } else { + gfx::Image image; + if (item.Get("icon", &image)) { + width = image.AsNSImage().size.width; + } + } + + return NSMakeSize(width, height); +} + @end