remove Ui::set_layout
This commit is contained in:
parent
d8021843f8
commit
aad0391fef
8 changed files with 67 additions and 49 deletions
|
@ -58,8 +58,7 @@ pub fn combo_box(
|
||||||
let frame_margin = frame.margin;
|
let frame_margin = frame.margin;
|
||||||
frame.show(ui, |ui| {
|
frame.show(ui, |ui| {
|
||||||
ui.set_min_width(button_response.rect.width() - 2.0 * frame_margin.x);
|
ui.set_min_width(button_response.rect.width() - 2.0 * frame_margin.x);
|
||||||
ui.set_layout(Layout::justified(Direction::Vertical));
|
ui.with_layout(Layout::justified(Direction::Vertical), menu_contents);
|
||||||
menu_contents(ui);
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -83,7 +82,7 @@ fn button_frame(
|
||||||
let outer_rect_bounds = ui.available();
|
let outer_rect_bounds = ui.available();
|
||||||
let inner_rect = outer_rect_bounds.shrink2(margin);
|
let inner_rect = outer_rect_bounds.shrink2(margin);
|
||||||
let where_to_put_background = ui.painter().add(PaintCmd::Noop);
|
let where_to_put_background = ui.painter().add(PaintCmd::Noop);
|
||||||
let mut content_ui = ui.child_ui(inner_rect);
|
let mut content_ui = ui.child_ui(inner_rect, *ui.layout());
|
||||||
add_contents(&mut content_ui);
|
add_contents(&mut content_ui);
|
||||||
|
|
||||||
let outer_rect = Rect::from_min_max(
|
let outer_rect = Rect::from_min_max(
|
||||||
|
|
|
@ -81,7 +81,7 @@ impl Frame {
|
||||||
let outer_rect_bounds = ui.available();
|
let outer_rect_bounds = ui.available();
|
||||||
let inner_rect = outer_rect_bounds.shrink2(self.margin);
|
let inner_rect = outer_rect_bounds.shrink2(self.margin);
|
||||||
let where_to_put_background = ui.painter().add(PaintCmd::Noop);
|
let where_to_put_background = ui.painter().add(PaintCmd::Noop);
|
||||||
let content_ui = ui.child_ui(inner_rect);
|
let content_ui = ui.child_ui(inner_rect, *ui.layout());
|
||||||
Prepared {
|
Prepared {
|
||||||
frame: self,
|
frame: self,
|
||||||
outer_rect_bounds,
|
outer_rect_bounds,
|
||||||
|
|
|
@ -203,7 +203,7 @@ impl Resize {
|
||||||
|
|
||||||
content_clip_rect = content_clip_rect.intersect(ui.clip_rect()); // Respect parent region
|
content_clip_rect = content_clip_rect.intersect(ui.clip_rect()); // Respect parent region
|
||||||
|
|
||||||
let mut content_ui = ui.child_ui(inner_rect);
|
let mut content_ui = ui.child_ui(inner_rect, *ui.layout());
|
||||||
content_ui.set_clip_rect(content_clip_rect);
|
content_ui.set_clip_rect(content_clip_rect);
|
||||||
|
|
||||||
Prepared {
|
Prepared {
|
||||||
|
|
|
@ -100,10 +100,13 @@ impl ScrollArea {
|
||||||
let inner_size = outer_size - vec2(current_scroll_bar_width, 0.0);
|
let inner_size = outer_size - vec2(current_scroll_bar_width, 0.0);
|
||||||
let inner_rect = Rect::from_min_size(ui.available().min, inner_size);
|
let inner_rect = Rect::from_min_size(ui.available().min, inner_size);
|
||||||
|
|
||||||
let mut content_ui = ui.child_ui(Rect::from_min_size(
|
let mut content_ui = ui.child_ui(
|
||||||
inner_rect.min - state.offset,
|
Rect::from_min_size(
|
||||||
vec2(inner_size.x, f32::INFINITY),
|
inner_rect.min - state.offset,
|
||||||
));
|
vec2(inner_size.x, f32::INFINITY),
|
||||||
|
),
|
||||||
|
*ui.layout(),
|
||||||
|
);
|
||||||
let mut content_clip_rect = inner_rect.expand(ui.style().visuals.clip_rect_margin);
|
let mut content_clip_rect = inner_rect.expand(ui.style().visuals.clip_rect_margin);
|
||||||
content_clip_rect = content_clip_rect.intersect(ui.clip_rect());
|
content_clip_rect = content_clip_rect.intersect(ui.clip_rect());
|
||||||
content_clip_rect.max.x = ui.clip_rect().max.x - current_scroll_bar_width; // Nice handling of forced resizing beyond the possible
|
content_clip_rect.max.x = ui.clip_rect().max.x - current_scroll_bar_width; // Nice handling of forced resizing beyond the possible
|
||||||
|
|
|
@ -389,13 +389,15 @@ fn show_menu_bar(ui: &mut Ui, windows: &mut OpenWindows, env: &DemoEnvironment)
|
||||||
(time.rem_euclid(60.0)).floor(),
|
(time.rem_euclid(60.0)).floor(),
|
||||||
(time.rem_euclid(1.0) * 100.0).floor()
|
(time.rem_euclid(1.0) * 100.0).floor()
|
||||||
);
|
);
|
||||||
ui.set_layout(Layout::horizontal(Align::Max).reverse());
|
|
||||||
if ui
|
ui.with_layout(Layout::horizontal(Align::Max).reverse(), |ui| {
|
||||||
.add(Button::new(time).text_style(TextStyle::Monospace))
|
if ui
|
||||||
.clicked
|
.add(Button::new(time).text_style(TextStyle::Monospace))
|
||||||
{
|
.clicked
|
||||||
windows.fractal_clock = !windows.fractal_clock;
|
{
|
||||||
}
|
windows.fractal_clock = !windows.fractal_clock;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -360,20 +360,24 @@ impl Default for LayoutDemo {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LayoutDemo {
|
impl LayoutDemo {
|
||||||
|
fn layout(&self) -> Layout {
|
||||||
|
let layout = Layout::from_dir_align(self.dir, self.align);
|
||||||
|
if self.reversed {
|
||||||
|
layout.reverse()
|
||||||
|
} else {
|
||||||
|
layout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn ui(&mut self, ui: &mut Ui) {
|
pub fn ui(&mut self, ui: &mut Ui) {
|
||||||
Resize::default()
|
Resize::default()
|
||||||
.default_size([200.0, 100.0])
|
.default_size([200.0, 100.0])
|
||||||
.show(ui, |ui| self.content_ui(ui));
|
.show(ui, |ui| {
|
||||||
|
ui.with_layout(self.layout(), |ui| self.content_ui(ui))
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn content_ui(&mut self, ui: &mut Ui) {
|
pub fn content_ui(&mut self, ui: &mut Ui) {
|
||||||
let layout = Layout::from_dir_align(self.dir, self.align);
|
|
||||||
if self.reversed {
|
|
||||||
ui.set_layout(layout.reverse());
|
|
||||||
} else {
|
|
||||||
ui.set_layout(layout);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ui.add(label!("Available space: {:?}", ui.available().size()));
|
// ui.add(label!("Available space: {:?}", ui.available().size()));
|
||||||
if ui.add(Button::new("Reset")).clicked {
|
if ui.add(Button::new("Reset")).clicked {
|
||||||
*self = Default::default();
|
*self = Default::default();
|
||||||
|
|
|
@ -111,8 +111,7 @@ fn menu_impl<'c>(
|
||||||
style.visuals.widgets.inactive.bg_fill = TRANSPARENT;
|
style.visuals.widgets.inactive.bg_fill = TRANSPARENT;
|
||||||
style.visuals.widgets.inactive.bg_stroke = Stroke::none();
|
style.visuals.widgets.inactive.bg_stroke = Stroke::none();
|
||||||
ui.set_style(style);
|
ui.set_style(style);
|
||||||
ui.set_layout(Layout::justified(Direction::Vertical));
|
ui.with_layout(Layout::justified(Direction::Vertical), add_contents);
|
||||||
add_contents(ui)
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -67,17 +67,24 @@ impl Ui {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn child_ui(&mut self, child_rect: Rect) -> Self {
|
pub fn child_ui(&mut self, child_rect: Rect, layout: Layout) -> Self {
|
||||||
let id = self.make_position_id(); // TODO: is this a good idea?
|
let id = self.make_position_id(); // TODO: is this a good idea?
|
||||||
self.child_count += 1;
|
self.child_count += 1;
|
||||||
|
|
||||||
|
let cursor = if layout.is_reversed() {
|
||||||
|
child_rect.max
|
||||||
|
} else {
|
||||||
|
child_rect.min
|
||||||
|
};
|
||||||
|
|
||||||
Ui {
|
Ui {
|
||||||
id,
|
id,
|
||||||
painter: self.painter.clone(),
|
painter: self.painter.clone(),
|
||||||
desired_rect: child_rect,
|
desired_rect: child_rect,
|
||||||
child_bounds: Rect::from_min_size(child_rect.min, Vec2::zero()), // TODO: Rect::nothing() ?
|
child_bounds: Rect::from_min_size(child_rect.min, Vec2::zero()), // TODO: Rect::nothing() ?
|
||||||
style: self.style().clone(),
|
style: self.style().clone(),
|
||||||
layout: self.layout,
|
layout,
|
||||||
cursor: child_rect.min,
|
cursor,
|
||||||
child_count: 0,
|
child_count: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -275,16 +282,6 @@ impl Ui {
|
||||||
&self.layout
|
&self.layout
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remove
|
|
||||||
pub fn set_layout(&mut self, layout: Layout) {
|
|
||||||
self.layout = layout;
|
|
||||||
|
|
||||||
// TODO: remove this HACK:
|
|
||||||
if layout.is_reversed() {
|
|
||||||
self.cursor = self.rect_finite().max;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
pub fn contains_mouse(&self, rect: Rect) -> bool {
|
pub fn contains_mouse(&self, rect: Rect) -> bool {
|
||||||
|
@ -613,7 +610,7 @@ impl Ui {
|
||||||
pub fn add_custom_contents(&mut self, size: Vec2, add_contents: impl FnOnce(&mut Ui)) -> Rect {
|
pub fn add_custom_contents(&mut self, size: Vec2, add_contents: impl FnOnce(&mut Ui)) -> Rect {
|
||||||
let size = size.at_most(self.available().size());
|
let size = size.at_most(self.available().size());
|
||||||
let child_rect = Rect::from_min_size(self.cursor, size);
|
let child_rect = Rect::from_min_size(self.cursor, size);
|
||||||
let mut child_ui = self.child_ui(child_rect);
|
let mut child_ui = self.child_ui(child_rect, self.layout);
|
||||||
add_contents(&mut child_ui);
|
add_contents(&mut child_ui);
|
||||||
self.allocate_space(child_ui.bounding_size())
|
self.allocate_space(child_ui.bounding_size())
|
||||||
}
|
}
|
||||||
|
@ -621,7 +618,7 @@ impl Ui {
|
||||||
/// Create a child ui. You can use this to temporarily change the Style of a sub-region, for instance.
|
/// Create a child ui. You can use this to temporarily change the Style of a sub-region, for instance.
|
||||||
pub fn add_custom<R>(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Rect) {
|
pub fn add_custom<R>(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Rect) {
|
||||||
let child_rect = self.available();
|
let child_rect = self.available();
|
||||||
let mut child_ui = self.child_ui(child_rect);
|
let mut child_ui = self.child_ui(child_rect, self.layout);
|
||||||
let r = add_contents(&mut child_ui);
|
let r = add_contents(&mut child_ui);
|
||||||
let size = child_ui.bounding_size();
|
let size = child_ui.bounding_size();
|
||||||
(r, self.allocate_space(size))
|
(r, self.allocate_space(size))
|
||||||
|
@ -641,7 +638,7 @@ impl Ui {
|
||||||
let child_rect = Rect::from_min_max(self.cursor + indent, self.bottom_right());
|
let child_rect = Rect::from_min_max(self.cursor + indent, self.bottom_right());
|
||||||
let mut child_ui = Ui {
|
let mut child_ui = Ui {
|
||||||
id: self.id.with(id_source),
|
id: self.id.with(id_source),
|
||||||
..self.child_ui(child_rect)
|
..self.child_ui(child_rect, self.layout)
|
||||||
};
|
};
|
||||||
let ret = add_contents(&mut child_ui);
|
let ret = add_contents(&mut child_ui);
|
||||||
let size = child_ui.bounding_size();
|
let size = child_ui.bounding_size();
|
||||||
|
@ -677,10 +674,13 @@ impl Ui {
|
||||||
Align::Center => self.available().width() / 2.0 - width / 2.0,
|
Align::Center => self.available().width() / 2.0 - width / 2.0,
|
||||||
Align::Max => self.available().width() - width,
|
Align::Max => self.available().width() - width,
|
||||||
};
|
};
|
||||||
self.child_ui(Rect::from_min_size(
|
self.child_ui(
|
||||||
self.cursor + vec2(x, 0.0),
|
Rect::from_min_size(
|
||||||
vec2(width, self.available().height()),
|
self.cursor + vec2(x, 0.0),
|
||||||
))
|
vec2(width, self.available().height()),
|
||||||
|
),
|
||||||
|
self.layout,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start a ui with horizontal layout.
|
/// Start a ui with horizontal layout.
|
||||||
|
@ -716,8 +716,19 @@ impl Ui {
|
||||||
add_contents: impl FnOnce(&mut Self) -> R,
|
add_contents: impl FnOnce(&mut Self) -> R,
|
||||||
) -> (R, Rect) {
|
) -> (R, Rect) {
|
||||||
let child_rect = Rect::from_min_size(self.cursor, initial_size);
|
let child_rect = Rect::from_min_size(self.cursor, initial_size);
|
||||||
let mut child_ui = self.child_ui(child_rect);
|
let mut child_ui = self.child_ui(child_rect, layout);
|
||||||
child_ui.set_layout(layout); // HACK: need a separate call right now
|
let ret = add_contents(&mut child_ui);
|
||||||
|
let size = child_ui.bounding_size();
|
||||||
|
let rect = self.allocate_space(size);
|
||||||
|
(ret, rect)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_layout<R>(
|
||||||
|
&mut self,
|
||||||
|
layout: Layout,
|
||||||
|
add_contents: impl FnOnce(&mut Self) -> R,
|
||||||
|
) -> (R, Rect) {
|
||||||
|
let mut child_ui = self.child_ui(self.rect(), layout);
|
||||||
let ret = add_contents(&mut child_ui);
|
let ret = add_contents(&mut child_ui);
|
||||||
let size = child_ui.bounding_size();
|
let size = child_ui.bounding_size();
|
||||||
let rect = self.allocate_space(size);
|
let rect = self.allocate_space(size);
|
||||||
|
@ -749,7 +760,7 @@ impl Ui {
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
id: self.make_child_id(&("column", col_idx)),
|
id: self.make_child_id(&("column", col_idx)),
|
||||||
..self.child_ui(child_rect)
|
..self.child_ui(child_rect, self.layout)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
Loading…
Reference in a new issue