1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 21:30:03 -04:00

Rename Region to Ui (shorter, sweeter)

This commit is contained in:
Emil Ernerfeldt
2020-05-08 22:42:31 +02:00
parent cbd51c3f43
commit fa82e8d806
22 changed files with 508 additions and 524 deletions

View File

@@ -99,9 +99,9 @@ impl<'a> Slider<'a> {
}
impl<'a> Widget for Slider<'a> {
fn ui(mut self, region: &mut Region) -> GuiResponse {
fn ui(mut self, ui: &mut Ui) -> GuiResponse {
let text_style = TextStyle::Button;
let font = &region.fonts()[text_style];
let font = &ui.fonts()[text_style];
if let Some(text) = &self.text {
if self.id.is_none() {
@@ -116,35 +116,35 @@ impl<'a> Widget for Slider<'a> {
let slider_sans_text = Slider { text: None, ..self };
if text_on_top {
// let (text, text_size) = font.layout_multiline(&full_text, region.available_width());
// let (text, text_size) = font.layout_multiline(&full_text, ui.available_width());
let (text, text_size) = font.layout_single_line(&full_text);
let pos = region.reserve_space(text_size, None).rect.min;
region.add_text(pos, text_style, text, text_color);
slider_sans_text.ui(region)
let pos = ui.reserve_space(text_size, None).rect.min;
ui.add_text(pos, text_style, text, text_color);
slider_sans_text.ui(ui)
} else {
region.columns(2, |columns| {
ui.columns(2, |columns| {
// Slider on the left:
let slider_response = columns[0].add(slider_sans_text);
// Place the text in line with the slider on the left:
columns[1].set_desired_height(slider_response.rect.height());
columns[1].horizontal(|region| {
region.set_align(Align::Center);
region.add(Label::new(full_text).multiline(false));
columns[1].horizontal(|ui| {
ui.set_align(Align::Center);
ui.add(Label::new(full_text).multiline(false));
});
slider_response
})
}
} else {
let height = font.line_spacing().max(region.style().clickable_diameter);
let height = font.line_spacing().max(ui.style().clickable_diameter);
let handle_radius = height / 2.5;
let id = self.id.unwrap_or_else(|| region.make_position_id());
let id = self.id.unwrap_or_else(|| ui.make_position_id());
let interact = region.reserve_space(
let interact = ui.reserve_space(
Vec2 {
x: region.available_width(),
x: ui.available_width(),
y: height,
},
Some(id),
@@ -156,7 +156,7 @@ impl<'a> Widget for Slider<'a> {
let range = self.range.clone();
debug_assert!(range.start() <= range.end());
if let Some(mouse_pos) = region.input().mouse_pos {
if let Some(mouse_pos) = ui.input().mouse_pos {
if interact.active {
self.set_value_f32(remap_clamp(mouse_pos.x, left..=right, range.clone()));
}
@@ -167,32 +167,32 @@ impl<'a> Widget for Slider<'a> {
let value = self.get_value_f32();
let rect = interact.rect;
let rail_radius = region.round_to_pixel((height / 8.0).max(2.0));
let rail_radius = ui.round_to_pixel((height / 8.0).max(2.0));
let rail_rect = Rect::from_min_max(
pos2(interact.rect.left(), rect.center().y - rail_radius),
pos2(interact.rect.right(), rect.center().y + rail_radius),
);
let marker_center_x = remap_clamp(value, range, left..=right);
region.add_paint_cmd(PaintCmd::Rect {
ui.add_paint_cmd(PaintCmd::Rect {
rect: rail_rect,
corner_radius: rail_radius,
fill_color: Some(region.style().background_fill_color()),
fill_color: Some(ui.style().background_fill_color()),
outline: Some(Outline::new(1.0, color::gray(200, 255))), // TODO
});
region.add_paint_cmd(PaintCmd::Circle {
ui.add_paint_cmd(PaintCmd::Circle {
center: pos2(marker_center_x, rail_rect.center().y),
radius: handle_radius,
fill_color: region.style().interact_fill_color(&interact),
fill_color: ui.style().interact_fill_color(&interact),
outline: Some(Outline::new(
region.style().interact_stroke_width(&interact),
region.style().interact_stroke_color(&interact),
ui.style().interact_stroke_width(&interact),
ui.style().interact_stroke_color(&interact),
)),
});
}
region.response(interact)
ui.response(interact)
}
}
}

View File

@@ -4,7 +4,7 @@ use crate::*;
pub struct TextEdit<'t> {
text: &'t mut String,
id: Option<Id>,
text_style: TextStyle, // TODO: Option<TextStyle>, where None means "use the default for the region"
text_style: TextStyle, // TODO: Option<TextStyle>, where None means "use the default for the current Ui"
text_color: Option<Color>,
}
@@ -35,29 +35,29 @@ impl<'t> TextEdit<'t> {
}
impl<'t> Widget for TextEdit<'t> {
fn ui(self, region: &mut Region) -> GuiResponse {
let id = region.make_child_id(self.id);
fn ui(self, ui: &mut Ui) -> GuiResponse {
let id = ui.make_child_id(self.id);
let font = &region.fonts()[self.text_style];
let font = &ui.fonts()[self.text_style];
let line_spacing = font.line_spacing();
let (text, text_size) = font.layout_multiline(self.text.as_str(), region.available_width());
let desired_size = text_size.max(vec2(region.available_width(), line_spacing));
let interact = region.reserve_space(desired_size, Some(id));
let (text, text_size) = font.layout_multiline(self.text.as_str(), ui.available_width());
let desired_size = text_size.max(vec2(ui.available_width(), line_spacing));
let interact = ui.reserve_space(desired_size, Some(id));
if interact.clicked {
region.request_kb_focus(id);
ui.request_kb_focus(id);
}
if interact.hovered {
region.output().cursor_icon = CursorIcon::Text;
ui.output().cursor_icon = CursorIcon::Text;
}
let has_kb_focus = region.has_kb_focus(id);
let has_kb_focus = ui.has_kb_focus(id);
if has_kb_focus {
for event in &region.input().events {
for event in &ui.input().events {
match event {
Event::Copy | Event::Cut => {
// TODO: cut
region.ctx().output().copied_text = self.text.clone();
ui.ctx().output().copied_text = self.text.clone();
}
Event::Text(text) => {
if text == "\u{7f}" {
@@ -76,35 +76,35 @@ impl<'t> Widget for TextEdit<'t> {
}
}
region.add_paint_cmd(PaintCmd::Rect {
ui.add_paint_cmd(PaintCmd::Rect {
rect: interact.rect,
corner_radius: 0.0,
// fill_color: Some(color::BLACK),
fill_color: region.style().interact_fill_color(&interact),
// fill_color: Some(region.style().background_fill_color()),
fill_color: ui.style().interact_fill_color(&interact),
// fill_color: Some(ui.style().background_fill_color()),
outline: None, //Some(Outline::new(1.0, color::WHITE)),
});
if has_kb_focus {
let cursor_blink_hz = region.style().cursor_blink_hz;
let cursor_blink_hz = ui.style().cursor_blink_hz;
let show_cursor =
(region.input().time * cursor_blink_hz as f64 * 3.0).floor() as i64 % 3 != 0;
(ui.input().time * cursor_blink_hz as f64 * 3.0).floor() as i64 % 3 != 0;
if show_cursor {
let cursor_pos = if let Some(last) = text.last() {
interact.rect.min + vec2(last.max_x(), last.y_offset)
} else {
interact.rect.min
};
region.add_paint_cmd(PaintCmd::line_segment(
ui.add_paint_cmd(PaintCmd::line_segment(
(cursor_pos, cursor_pos + vec2(0.0, line_spacing)),
color::WHITE,
region.style().text_cursor_width,
ui.style().text_cursor_width,
));
}
}
region.add_text(interact.rect.min, self.text_style, text, self.text_color);
ui.add_text(interact.rect.min, self.text_style, text, self.text_color);
region.response(interact)
ui.response(interact)
}
}