Browse Source

Merge pull request #11141 from electron/backport-scrubber-width-fix

Backport scrubber width fix to 1-7-x
Charles Kerr 7 years ago
parent
commit
75ff5d2a45

+ 44 - 0
Jenkinsfile

@@ -0,0 +1,44 @@
+pipeline {
+  agent none
+  stages {
+    stage('Build') {
+      parallel {
+        stage('electron-osx-x64') {
+            agent {
+              label 'osx'
+            }
+            steps {
+              sh 'script/bootstrap.py --target_arch=x64 --dev'
+              sh 'npm run lint'
+              sh 'script/build.py -c D'
+              sh 'script/test.py --ci --rebuild_native_modules'
+            }
+            post {
+              always {
+                cleanWs()
+              }
+            }
+        }
+        stage('electron-mas-x64') {
+          agent {
+            label 'osx'
+          }
+          environment {
+            MAS_BUILD = '1'
+          }
+          steps {
+            sh 'script/bootstrap.py --target_arch=x64 --dev'
+            sh 'npm run lint'
+            sh 'script/build.py -c D'
+            sh 'script/test.py --ci --rebuild_native_modules'
+          }
+          post {
+            always {
+              cleanWs()
+            }
+          }
+        }
+      }
+    }
+  }
+}

+ 1 - 1
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<NSScrubberDelegate, NSScrubberDataSource> {
+@interface AtomTouchBar : NSObject<NSScrubberDelegate, NSScrubberDataSource, NSScrubberFlowLayoutDelegate> {
  @protected
   std::vector<mate::PersistentDictionary> ordered_settings_;
   std::map<std::string, mate::PersistentDictionary> settings_;

+ 36 - 0
atom/browser/ui/cocoa/atom_touch_bar.mm

@@ -666,4 +666,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<mate::PersistentDictionary> items;
+  if (!settings.Get("items", &items)) return defaultSize;
+
+  if (itemIndex >= static_cast<NSInteger>(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

+ 10 - 1
atom/browser/ui/cocoa/touch_bar_forward_declarations.h

@@ -15,7 +15,7 @@
 
 @class NSTouchBar, NSTouchBarItem;
 @class NSScrubber, NSScrubberItemView, NSScrubberArrangedView, NSScrubberTextItemView, NSScrubberImageItemView, NSScrubberSelectionStyle;
-@protocol NSTouchBarDelegate, NSScrubberDelegate, NSScrubberDataSource;
+@protocol NSTouchBarDelegate, NSScrubberDelegate, NSScrubberDataSource, NSScrubberFlowLayoutDelegate, NSScrubberFlowLayout;
 
 typedef float NSTouchBarItemPriority;
 static const NSTouchBarItemPriority NSTouchBarItemPriorityHigh = 1000;
@@ -149,6 +149,9 @@ static const NSTouchBarItemIdentifier NSTouchBarItemIdentifierOtherItemsProxy =
 
 @end
 
+@interface NSScrubberFlowLayout: NSObject
+@end
+
 @interface NSScrubberSelectionStyle : NSObject<NSCoding>
 
 @property(class, strong, readonly) NSScrubberSelectionStyle* outlineOverlayStyle;
@@ -229,6 +232,12 @@ static const NSTouchBarItemIdentifier NSTouchBarItemIdentifierOtherItemsProxy =
 
 @end
 
+@protocol NSScrubberFlowLayoutDelegate<NSObject>
+
+- (NSSize)scrubber:(NSScrubber *)scrubber layout:(NSScrubberFlowLayout *)layout sizeForItemAtIndex:(NSInteger)itemIndex;
+
+@end
+
 #pragma clang assume_nonnull end
 
 #elif MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12_1