From 196d19a311f53a8828731ceacb258d7d91ed96cc Mon Sep 17 00:00:00 2001 From: Pu Xingyu Date: Wed, 20 Jul 2016 16:49:07 +0800 Subject: [PATCH] Add 'get_flex_line()' method to flex flow The method returns 'Option', can be used to obtain a line in flex container. --- components/layout/flex.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/components/layout/flex.rs b/components/layout/flex.rs index f185f586536..15685e7b69d 100644 --- a/components/layout/flex.rs +++ b/components/layout/flex.rs @@ -421,6 +421,42 @@ impl FlexFlow { } } + /// Returns a line start after the last item that is already in a line. + /// Note that when the container main size is infinite(i.e. A column flexbox with auto height), + /// we do not need to do flex resolving and this can be considered as a fast-path, so the + /// 'container_size' param does not need to be 'None'. A line has to contain at least one item; + /// (expect this) if the container can be multi-line the sum of outer main size of items should + /// be less than the container size; a line should be filled by items as much as possible. + /// After been collected in a line a item should have its main sizes initialized. + fn get_flex_line(&mut self, container_size: Au) -> Option { + let start = if self.lines.len() == 0 { + 0 + } else { + self.lines[self.lines.len()-1].range.end + }; + if start == self.items.len() { + return None; + } + let mut end = start; + let mut total_line_size = Au(0); + let mut margin_count = 0; + + let items = &mut self.items[start..]; + for mut item in items.iter_mut() { + item.init_sizes(container_size, self.main_mode); + let outer_main_size = item.outer_main_size(self.main_mode); + if total_line_size + outer_main_size > container_size && end != start && self.is_wrappable { + break; + } + margin_count += item.auto_margin_num(self.main_mode); + total_line_size += outer_main_size; + end += 1; + } + + let line = FlexLine::new(start..end, container_size - total_line_size, margin_count); + Some(line) + } + // TODO(zentner): This function should use flex-basis. // Currently, this is the core of BlockFlow::bubble_inline_sizes() with all float logic // stripped out, and max replaced with union_nonbreaking_inline.