Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(298)

Side by Side Diff: Source/core/layout/LayoutGrid.cpp

Issue 1317643005: [css-grid] Fix track sizing algo w/ size restrictions and intrinsic sizes (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Simplified code Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Apple Inc. All rights reserved. 2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 384
385 minLogicalWidth += minTrackBreadth; 385 minLogicalWidth += minTrackBreadth;
386 maxLogicalWidth += maxTrackBreadth; 386 maxLogicalWidth += maxTrackBreadth;
387 } 387 }
388 388
389 LayoutUnit scrollbarWidth = intrinsicScrollbarLogicalWidth(); 389 LayoutUnit scrollbarWidth = intrinsicScrollbarLogicalWidth();
390 minLogicalWidth += scrollbarWidth; 390 minLogicalWidth += scrollbarWidth;
391 maxLogicalWidth += scrollbarWidth; 391 maxLogicalWidth += scrollbarWidth;
392 } 392 }
393 393
394 bool LayoutGrid::gridElementIsShrinkToFit() 394 static bool heightSizedUnderMinContentConstraint(const ComputedStyle& style)
cbiesinger 2015/09/30 15:49:35 I'm somewhat skeptical about this design -- does t
395 { 395 {
396 return isFloatingOrOutOfFlowPositioned(); 396 return style.logicalMinHeight().isMinContent() || style.logicalHeight().isMi nContent() || style.logicalMaxHeight().isMinContent();
397 }
398
399 static bool heightSizedUnderMaxContentConstraint(const ComputedStyle& style)
400 {
401 return style.logicalMinHeight().isMaxContent() || style.logicalHeight().isMa xContent() || style.logicalMaxHeight().isMaxContent();
402 }
403
404 LayoutUnit LayoutGrid::spaceToDistributeForMaximizeTracks(GridTrackSizingDirecti on direction, bool hasDefiniteAvailableSpace, const LayoutUnit &tracksBreadth, c onst LayoutUnit& freeSpace) const
405 {
406 if (direction == ForColumns)
407 return freeSpace;
408
409 // Under max-content the freeSpace is infinity unless we have a max-height r estriction.
410 if (heightSizedUnderMaxContentConstraint(styleRef())) {
411 if (!styleRef().logicalMaxHeight().isMaxSizeNone())
412 return std::max<LayoutUnit>(0, computeLogicalHeightUsing(MaxSize, st yleRef().logicalMaxHeight(), contentLogicalHeight()) - tracksBreadth);
413
414 return infinity;
415 }
416
417 // Under min-content the freeSpace is 0 unless we have a min-height restrict ion.
418 if (heightSizedUnderMinContentConstraint(styleRef())) {
419 const Length& logicalMinSize = styleRef().logicalMinHeight();
420 if (!logicalMinSize.isZero() || logicalMinSize.isIntrinsic())
421 return std::max<LayoutUnit>(0, computeLogicalHeightUsing(MinSize, st yleRef().logicalMinHeight(), contentLogicalHeight()) - tracksBreadth);
422
423 return 0;
424 }
425
426 if (!hasDefiniteAvailableSpace)
427 return infinity;
428
429 return freeSpace;
430 }
431
432 void LayoutGrid::maximizeTracks(Vector<GridTrack>& tracks, GridSizingData& sizin gData, LayoutUnit& spaceToDistribute)
433 {
434 if (spaceToDistribute == infinity) {
435 for (auto& track : tracks)
436 track.setBaseSize(track.growthLimit());
437 return;
438 }
439
440 ASSERT(spaceToDistribute >= 0);
441 if (spaceToDistribute == 0)
442 return;
443
444 size_t tracksSize = tracks.size();
445 Vector<GridTrack*> tracksForDistribution(tracksSize);
446 for (size_t i = 0; i < tracksSize; ++i) {
447 tracksForDistribution[i] = tracks.data() + i;
448 tracksForDistribution[i]->setPlannedSize(tracksForDistribution[i]->baseS ize());
449 }
450
451 distributeSpaceToTracks<MaximizeTracks>(tracksForDistribution, nullptr, sizi ngData, spaceToDistribute);
452
453 for (auto* track : tracksForDistribution)
454 track->setBaseSize(track->plannedSize());
397 } 455 }
398 456
399 void LayoutGrid::computeUsedBreadthOfGridTracks(GridTrackSizingDirection directi on, GridSizingData& sizingData, LayoutUnit& freeSpace) 457 void LayoutGrid::computeUsedBreadthOfGridTracks(GridTrackSizingDirection directi on, GridSizingData& sizingData, LayoutUnit& freeSpace)
400 { 458 {
401 const LayoutUnit initialFreeSpace = freeSpace; 459 const LayoutUnit initialFreeSpace = freeSpace;
402 Vector<GridTrack>& tracks = (direction == ForColumns) ? sizingData.columnTra cks : sizingData.rowTracks; 460 Vector<GridTrack>& tracks = (direction == ForColumns) ? sizingData.columnTra cks : sizingData.rowTracks;
403 Vector<size_t> flexibleSizedTracksIndex; 461 Vector<size_t> flexibleSizedTracksIndex;
404 sizingData.contentSizedTracksIndex.shrink(0); 462 sizingData.contentSizedTracksIndex.shrink(0);
405 463
406 // 1. Initialize per Grid track variables. 464 // 1. Initialize per Grid track variables.
(...skipping 15 matching lines...) Expand all
422 480
423 // 2. Resolve content-based TrackSizingFunctions. 481 // 2. Resolve content-based TrackSizingFunctions.
424 if (!sizingData.contentSizedTracksIndex.isEmpty()) 482 if (!sizingData.contentSizedTracksIndex.isEmpty())
425 resolveContentBasedTrackSizingFunctions(direction, sizingData); 483 resolveContentBasedTrackSizingFunctions(direction, sizingData);
426 484
427 for (const auto& track: tracks) { 485 for (const auto& track: tracks) {
428 ASSERT(!track.infiniteGrowthPotential()); 486 ASSERT(!track.infiniteGrowthPotential());
429 freeSpace -= track.baseSize(); 487 freeSpace -= track.baseSize();
430 } 488 }
431 489
432 const bool hasUndefinedRemainingSpace = (direction == ForRows) ? style()->lo gicalHeight().isAuto() : gridElementIsShrinkToFit(); 490 // For columns we already have the right available space because the intrins ic sizes
433 491 // were previously computed by computeIntrinsicLogicalWidths().
434 if (!hasUndefinedRemainingSpace && freeSpace <= 0) 492 const bool hasDefiniteAvailableSpace = (direction == ForRows) ? hasDefiniteL ogicalHeight() : true;
493 if (hasDefiniteAvailableSpace && freeSpace <= 0)
435 return; 494 return;
436 495
437 // 3. Grow all Grid tracks in GridTracks from their baseSize up to their gro wthLimit value until freeSpace is exhausted. 496 // 3. Grow all Grid tracks in GridTracks from their baseSize up to their gro wthLimit value until freeSpace is exhausted.
438 const size_t tracksSize = tracks.size(); 497 LayoutUnit tracksBreadth = initialFreeSpace - freeSpace;
439 if (!hasUndefinedRemainingSpace) { 498 LayoutUnit spaceToDistribute = spaceToDistributeForMaximizeTracks(direction, hasDefiniteAvailableSpace, tracksBreadth, freeSpace);
440 Vector<GridTrack*> tracksForDistribution(tracksSize); 499 maximizeTracks(tracks, sizingData, spaceToDistribute);
441 for (size_t i = 0; i < tracksSize; ++i) {
442 tracksForDistribution[i] = tracks.data() + i;
443 tracksForDistribution[i]->setPlannedSize(tracksForDistribution[i]->b aseSize());
444 }
445
446 distributeSpaceToTracks<MaximizeTracks>(tracksForDistribution, nullptr, sizingData, freeSpace);
447
448 for (auto* track : tracksForDistribution)
449 track->setBaseSize(track->plannedSize());
450 } else {
451 for (auto& track : tracks)
452 track.setBaseSize(track.growthLimit());
453 }
454 500
455 if (flexibleSizedTracksIndex.isEmpty()) 501 if (flexibleSizedTracksIndex.isEmpty())
456 return; 502 return;
457 503
458 // 4. Grow all Grid tracks having a fraction as the MaxTrackSizingFunction. 504 // 4. Grow all Grid tracks having a fraction as the MaxTrackSizingFunction.
459 double normalizedFractionBreadth = 0; 505 double normalizedFractionBreadth = 0;
460 if (!hasUndefinedRemainingSpace) { 506 if (hasDefiniteAvailableSpace) {
461 normalizedFractionBreadth = computeNormalizedFractionBreadth(tracks, Gri dSpan(0, tracks.size() - 1), direction, initialFreeSpace); 507 normalizedFractionBreadth = computeNormalizedFractionBreadth(tracks, Gri dSpan(0, tracks.size() - 1), direction, initialFreeSpace);
462 } else { 508 } else {
463 for (const auto& trackIndex : flexibleSizedTracksIndex) { 509 for (const auto& trackIndex : flexibleSizedTracksIndex) {
464 GridTrackSize trackSize = gridTrackSize(direction, trackIndex); 510 GridTrackSize trackSize = gridTrackSize(direction, trackIndex);
465 normalizedFractionBreadth = std::max(normalizedFractionBreadth, trac ks[trackIndex].baseSize() / trackSize.maxTrackBreadth().flex()); 511 normalizedFractionBreadth = std::max(normalizedFractionBreadth, trac ks[trackIndex].baseSize() / trackSize.maxTrackBreadth().flex());
466 } 512 }
467 513
468 for (size_t i = 0; i < flexibleSizedTracksIndex.size(); ++i) { 514 for (size_t i = 0; i < flexibleSizedTracksIndex.size(); ++i) {
469 GridIterator iterator(m_grid, direction, flexibleSizedTracksIndex[i] ); 515 GridIterator iterator(m_grid, direction, flexibleSizedTracksIndex[i] );
470 while (LayoutBox* gridItem = iterator.nextGridItem()) { 516 while (LayoutBox* gridItem = iterator.nextGridItem()) {
(...skipping 1449 matching lines...) Expand 10 before | Expand all | Expand 10 after
1920 1966
1921 return LayoutPoint(rowAxisOffset, columnAxisOffsetForChild(child)); 1967 return LayoutPoint(rowAxisOffset, columnAxisOffsetForChild(child));
1922 } 1968 }
1923 1969
1924 void LayoutGrid::paintChildren(const PaintInfo& paintInfo, const LayoutPoint& pa intOffset) 1970 void LayoutGrid::paintChildren(const PaintInfo& paintInfo, const LayoutPoint& pa intOffset)
1925 { 1971 {
1926 GridPainter(*this).paintChildren(paintInfo, paintOffset); 1972 GridPainter(*this).paintChildren(paintInfo, paintOffset);
1927 } 1973 }
1928 1974
1929 } // namespace blink 1975 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698