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

Side by Side Diff: Source/core/frame/FrameViewAutoSizeInfo.cpp

Issue 459633002: Autosizing storage doesnot belong on FrameView (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebased Created 6 years, 4 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
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "core/frame/FrameViewAutoSizeInfo.h"
7
8 #include "core/frame/FrameView.h"
9 #include "core/frame/LocalFrame.h"
10 #include "core/rendering/RenderBox.h"
11 #include "core/rendering/RenderView.h"
12
13 namespace blink {
14
15 FrameViewAutoSizeInfo::FrameViewAutoSizeInfo(FrameView* view)
16 : m_frameView(view)
17 , m_shouldAutoSize(false)
18 , m_inAutoSize(false)
19 , m_didRunAutosize(false)
20 {
21 ASSERT(m_frameView);
22 }
23
24 void FrameViewAutoSizeInfo::enableAutoSizeMode(bool enable, const IntSize& minSi ze, const IntSize& maxSize)
25 {
26 ASSERT(!enable || !minSize.isEmpty());
27 ASSERT(minSize.width() <= maxSize.width());
28 ASSERT(minSize.height() <= maxSize.height());
29
30 if (m_shouldAutoSize == enable && m_minAutoSize == minSize && m_maxAutoSize == maxSize)
31 return;
32
33 m_shouldAutoSize = enable;
34 m_minAutoSize = minSize;
35 m_maxAutoSize = maxSize;
36 m_didRunAutosize = false;
37
38 m_frameView->setLayoutSizeFixedToFrameSize(enable);
39 m_frameView->setNeedsLayout();
40 m_frameView->scheduleRelayout();
41 if (m_shouldAutoSize)
42 return;
43
44 // Since autosize mode forces the scrollbar mode, change them to being auto.
45 m_frameView->setVerticalScrollbarLock(false);
46 m_frameView->setHorizontalScrollbarLock(false);
47 m_frameView->setScrollbarModes(ScrollbarAuto, ScrollbarAuto);
48 }
49
50 void FrameViewAutoSizeInfo::autoSizeIfEnabled()
51 {
52 if (!m_shouldAutoSize)
53 return;
54
55 if (m_inAutoSize)
56 return;
57
58 TemporaryChange<bool> changeInAutoSize(m_inAutoSize, true);
59
60 Document* document = m_frameView->frame().document();
61 if (!document || !document->isActive())
62 return;
63
64 Element* documentElement = document->documentElement();
65 if (!documentElement)
66 return;
67
68 // If this is the first time we run autosize, start from small height and
69 // allow it to grow.
70 if (!m_didRunAutosize)
71 m_frameView->resize(m_frameView->frameRect().width(), m_minAutoSize.heig ht());
72
73 IntSize size = m_frameView->frameRect().size();
74
75 // Do the resizing twice. The first time is basically a rough calculation us ing the preferred width
76 // which may result in a height change during the second iteration.
77 for (int i = 0; i < 2; i++) {
78 // Update various sizes including contentsSize, scrollHeight, etc.
79 document->updateLayoutIgnorePendingStylesheets();
80
81 RenderView* renderView = document->renderView();
82 if (!renderView)
83 return;
84
85 int width = renderView->minPreferredLogicalWidth();
86
87 RenderBox* documentRenderBox = documentElement->renderBox();
88 if (!documentRenderBox)
89 return;
90
91 int height = documentRenderBox->scrollHeight();
92 IntSize newSize(width, height);
93
94 // Check to see if a scrollbar is needed for a given dimension and
95 // if so, increase the other dimension to account for the scrollbar.
96 // Since the dimensions are only for the view rectangle, once a
97 // dimension exceeds the maximum, there is no need to increase it furthe r.
98 if (newSize.width() > m_maxAutoSize.width()) {
99 RefPtr<Scrollbar> localHorizontalScrollbar = m_frameView->horizontal Scrollbar();
100 if (!localHorizontalScrollbar)
101 localHorizontalScrollbar = m_frameView->createScrollbar(Horizont alScrollbar);
102 if (!localHorizontalScrollbar->isOverlayScrollbar())
103 newSize.setHeight(newSize.height() + localHorizontalScrollbar->h eight());
104
105 // Don't bother checking for a vertical scrollbar because the width is at
106 // already greater the maximum.
107 } else if (newSize.height() > m_maxAutoSize.height()) {
108 RefPtr<Scrollbar> localVerticalScrollbar = m_frameView->verticalScro llbar();
109 if (!localVerticalScrollbar)
110 localVerticalScrollbar = m_frameView->createScrollbar(VerticalSc rollbar);
111 if (!localVerticalScrollbar->isOverlayScrollbar())
112 newSize.setWidth(newSize.width() + localVerticalScrollbar->width ());
113
114 // Don't bother checking for a horizontal scrollbar because the heig ht is
115 // already greater the maximum.
116 }
117
118 // Ensure the size is at least the min bounds.
119 newSize = newSize.expandedTo(m_minAutoSize);
120
121 // Bound the dimensions by the max bounds and determine what scrollbars to show.
122 ScrollbarMode horizonalScrollbarMode = ScrollbarAlwaysOff;
123 if (newSize.width() > m_maxAutoSize.width()) {
124 newSize.setWidth(m_maxAutoSize.width());
125 horizonalScrollbarMode = ScrollbarAlwaysOn;
126 }
127 ScrollbarMode verticalScrollbarMode = ScrollbarAlwaysOff;
128 if (newSize.height() > m_maxAutoSize.height()) {
129 newSize.setHeight(m_maxAutoSize.height());
130 verticalScrollbarMode = ScrollbarAlwaysOn;
131 }
132
133 if (newSize == size)
134 continue;
135
136 // While loading only allow the size to increase (to avoid twitching dur ing intermediate smaller states)
137 // unless autoresize has just been turned on or the maximum size is smal ler than the current size.
138 if (m_didRunAutosize && size.height() <= m_maxAutoSize.height() && size. width() <= m_maxAutoSize.width()
139 && !m_frameView->frame().document()->loadEventFinished() && (newSize .height() < size.height() || newSize.width() < size.width()))
140 break;
141
142 m_frameView->resize(newSize.width(), newSize.height());
143 // Force the scrollbar state to avoid the scrollbar code adding them and causing them to be needed. For example,
144 // a vertical scrollbar may cause text to wrap and thus increase the hei ght (which is the only reason the scollbar is needed).
145 m_frameView->setVerticalScrollbarLock(false);
146 m_frameView->setHorizontalScrollbarLock(false);
147 m_frameView->setScrollbarModes(horizonalScrollbarMode, verticalScrollbar Mode, true, true);
148 }
149 m_didRunAutosize = true;
150 }
151
152 } // namespace blink
OLDNEW
« Source/core/frame/FrameViewAutoSizeInfo.h ('K') | « Source/core/frame/FrameViewAutoSizeInfo.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698