| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#![forbid(unsafe_code)] |
| 67 |
|
| 68 |
use egui::{ |
| 69 |
Color32, ComboBox, CornerRadius, Margin, Painter, Rect, Response, RichText, Shape, Stroke, |
| 70 |
TextEdit, Ui, |
| 71 |
}; |
| 72 |
use makeover_layout::{Bevel, Choice, Depth, Edge, Field, FieldKind, Fill, State}; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 81 |
pub struct Palette { |
| 82 |
|
| 83 |
pub page: Color32, |
| 84 |
|
| 85 |
pub raised: Color32, |
| 86 |
|
| 87 |
pub overlay: Color32, |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
pub well: Color32, |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
pub sunken: Color32, |
| 106 |
|
| 107 |
pub bevel_light: Color32, |
| 108 |
|
| 109 |
pub bevel_dark: Color32, |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
pub content: Color32, |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
pub content_muted: Color32, |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
pub danger: Color32, |
| 130 |
} |
| 131 |
|
| 132 |
impl Palette { |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
#[must_use] |
| 146 |
pub const fn fill(&self, fill: Fill) -> Option<Color32> { |
| 147 |
match fill { |
| 148 |
Fill::Page => Some(self.page), |
| 149 |
Fill::Raised => Some(self.raised), |
| 150 |
Fill::Overlay => Some(self.overlay), |
| 151 |
Fill::Well => Some(self.well), |
| 152 |
Fill::Sunken => Some(self.sunken), |
| 153 |
_ => None, |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
#[must_use] |
| 159 |
pub const fn edge(&self, edge: Edge) -> Color32 { |
| 160 |
match edge { |
| 161 |
Edge::Light => self.bevel_light, |
| 162 |
Edge::Dark => self.bevel_dark, |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
#[derive(Debug, Clone, Copy, PartialEq)] |
| 173 |
pub struct FrameStyle { |
| 174 |
|
| 175 |
pub radius: CornerRadius, |
| 176 |
|
| 177 |
pub margin: Margin, |
| 178 |
|
| 179 |
pub stroke: f32, |
| 180 |
} |
| 181 |
|
| 182 |
impl Default for FrameStyle { |
| 183 |
|
| 184 |
fn default() -> Self { |
| 185 |
Self { |
| 186 |
radius: CornerRadius::ZERO, |
| 187 |
margin: Margin::ZERO, |
| 188 |
stroke: 1.0, |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
pub fn paint_bevel(painter: &Painter, rect: Rect, bevel: Bevel, palette: &Palette, stroke: f32) { |
| 212 |
let (top_left, bottom_right) = bevel.edges(); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
let r = rect.shrink(stroke / 2.0); |
| 218 |
|
| 219 |
painter.add(Shape::line( |
| 220 |
vec![r.left_bottom(), r.left_top(), r.right_top()], |
| 221 |
Stroke::new(stroke, palette.edge(top_left)), |
| 222 |
)); |
| 223 |
painter.add(Shape::line( |
| 224 |
vec![r.right_top(), r.right_bottom(), r.left_bottom()], |
| 225 |
Stroke::new(stroke, palette.edge(bottom_right)), |
| 226 |
)); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
pub fn frame<R>( |
| 236 |
ui: &mut Ui, |
| 237 |
depth: Depth, |
| 238 |
palette: &Palette, |
| 239 |
style: FrameStyle, |
| 240 |
add_contents: impl FnOnce(&mut Ui) -> R, |
| 241 |
) -> R { |
| 242 |
let mut f = egui::Frame::new() |
| 243 |
.corner_radius(style.radius) |
| 244 |
.inner_margin(style.margin); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
if let Some(fill) = depth.fill().and_then(|f| palette.fill(f)) { |
| 251 |
f = f.fill(fill); |
| 252 |
} |
| 253 |
let framed = f.show(ui, add_contents); |
| 254 |
if let Some(bevel) = depth.bevel() { |
| 255 |
paint_bevel( |
| 256 |
ui.painter(), |
| 257 |
framed.response.rect, |
| 258 |
bevel, |
| 259 |
palette, |
| 260 |
style.stroke, |
| 261 |
); |
| 262 |
} |
| 263 |
framed.inner |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
#[derive(Debug, Clone, Copy, PartialEq)] |
| 271 |
pub struct FieldStyle { |
| 272 |
|
| 273 |
pub frame: FrameStyle, |
| 274 |
|
| 275 |
|
| 276 |
pub gap: f32, |
| 277 |
|
| 278 |
pub group_gap: f32, |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
pub required_marker: &'static str, |
| 287 |
} |
| 288 |
|
| 289 |
impl Default for FieldStyle { |
| 290 |
|
| 291 |
fn default() -> Self { |
| 292 |
Self { |
| 293 |
frame: FrameStyle::default(), |
| 294 |
gap: 0.0, |
| 295 |
group_gap: 0.0, |
| 296 |
required_marker: "*", |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
#[derive(Debug, Default)] |
| 312 |
pub enum Filling<'a> { |
| 313 |
|
| 314 |
#[default] |
| 315 |
Absent, |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
Text(&'a mut String), |
| 321 |
|
| 322 |
On(&'a mut bool), |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
fn label_text(field: &Field<'_>, style: &FieldStyle) -> String { |
| 327 |
if field.required { |
| 328 |
format!("{} {}", field.label, style.required_marker) |
| 329 |
} else { |
| 330 |
field.label.to_owned() |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 342 |
enum Control { |
| 343 |
|
| 344 |
Typed, |
| 345 |
|
| 346 |
|
| 347 |
Chosen, |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
Listed, |
| 355 |
|
| 356 |
Toggled, |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
const fn control_shape(kind: FieldKind) -> Control { |
| 365 |
match kind { |
| 366 |
FieldKind::Select => Control::Chosen, |
| 367 |
FieldKind::Radio => Control::Listed, |
| 368 |
FieldKind::Checkbox => Control::Toggled, |
| 369 |
_ => Control::Typed, |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
fn shown_label<'a>(options: &'a [Choice<'a>], value: &'a str) -> &'a str { |
| 381 |
options |
| 382 |
.iter() |
| 383 |
.find(|opt| opt.value == value) |
| 384 |
.map_or(value, |opt| opt.label) |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
fn control( |
| 389 |
ui: &mut Ui, |
| 390 |
field: &Field<'_>, |
| 391 |
filling: Filling<'_>, |
| 392 |
palette: &Palette, |
| 393 |
style: &FieldStyle, |
| 394 |
) -> Response { |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
let mut discard = String::new(); |
| 400 |
let mut off = false; |
| 401 |
|
| 402 |
match control_shape(field.kind) { |
| 403 |
Control::Typed => { |
| 404 |
let text = match filling { |
| 405 |
Filling::Text(text) => text, |
| 406 |
_ => &mut discard, |
| 407 |
}; |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
let mut edit = if matches!(field.kind, FieldKind::Textarea) { |
| 412 |
TextEdit::multiline(text) |
| 413 |
} else { |
| 414 |
TextEdit::singleline(text) |
| 415 |
} |
| 416 |
.frame(egui::Frame::NONE) |
| 417 |
.margin(Margin::ZERO) |
| 418 |
.text_color(palette.content) |
| 419 |
.password(field.kind.confidential()); |
| 420 |
if let Some(ghost) = field.placeholder { |
| 421 |
edit = edit.hint_text(RichText::new(ghost).color(palette.content_muted)); |
| 422 |
} |
| 423 |
frame(ui, Depth::Well, palette, style.frame, |ui| ui.add(edit)) |
| 424 |
} |
| 425 |
Control::Toggled => { |
| 426 |
let on = match filling { |
| 427 |
Filling::On(on) => on, |
| 428 |
_ => &mut off, |
| 429 |
}; |
| 430 |
ui.checkbox(on, RichText::new(field.label).color(palette.content)) |
| 431 |
} |
| 432 |
Control::Listed => { |
| 433 |
let value = match filling { |
| 434 |
Filling::Text(text) => text, |
| 435 |
_ => &mut discard, |
| 436 |
}; |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
let group = ui.vertical(|ui| { |
| 442 |
let mut answered: Option<Response> = None; |
| 443 |
for opt in field.options { |
| 444 |
let picked = ui.radio_value( |
| 445 |
value, |
| 446 |
opt.value.to_owned(), |
| 447 |
RichText::new(opt.label).color(palette.content), |
| 448 |
); |
| 449 |
answered = Some(match answered { |
| 450 |
Some(prev) => prev.union(picked), |
| 451 |
None => picked, |
| 452 |
}); |
| 453 |
} |
| 454 |
answered |
| 455 |
}); |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
group.inner.unwrap_or(group.response) |
| 461 |
} |
| 462 |
Control::Chosen => { |
| 463 |
let value = match filling { |
| 464 |
Filling::Text(text) => text, |
| 465 |
_ => &mut discard, |
| 466 |
}; |
| 467 |
let shown = shown_label(field.options, value); |
| 468 |
ComboBox::from_id_salt(field.name) |
| 469 |
.selected_text(RichText::new(shown).color(palette.content)) |
| 470 |
.show_ui(ui, |ui| { |
| 471 |
for opt in field.options { |
| 472 |
ui.selectable_value( |
| 473 |
value, |
| 474 |
opt.value.to_owned(), |
| 475 |
RichText::new(opt.label).color(palette.content), |
| 476 |
); |
| 477 |
} |
| 478 |
}) |
| 479 |
.response |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
pub fn field( |
| 503 |
ui: &mut Ui, |
| 504 |
field: &Field<'_>, |
| 505 |
filling: Filling<'_>, |
| 506 |
state: Option<State>, |
| 507 |
palette: &Palette, |
| 508 |
style: &FieldStyle, |
| 509 |
) -> Option<Response> { |
| 510 |
if !field.kind.visible() { |
| 511 |
return None; |
| 512 |
} |
| 513 |
let enabled = !state.is_some_and(State::suppresses_interaction); |
| 514 |
let text = if enabled { |
| 515 |
palette.content |
| 516 |
} else { |
| 517 |
palette.content_muted |
| 518 |
}; |
| 519 |
|
| 520 |
let response = ui |
| 521 |
.vertical(|ui| { |
| 522 |
ui.spacing_mut().item_spacing.y = style.gap; |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
if !field.kind.labels_itself() { |
| 528 |
ui.label(RichText::new(label_text(field, style)).color(text)); |
| 529 |
} |
| 530 |
|
| 531 |
let response = ui |
| 532 |
.add_enabled_ui(enabled, |ui| control(ui, field, filling, palette, style)) |
| 533 |
.inner; |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
if let Some(hint) = field.hint { |
| 540 |
ui.label(RichText::new(hint).color(palette.content_muted)); |
| 541 |
} |
| 542 |
if let Some(error) = field.error { |
| 543 |
ui.label(RichText::new(error).color(palette.danger)); |
| 544 |
} |
| 545 |
response |
| 546 |
}) |
| 547 |
.inner; |
| 548 |
|
| 549 |
Some(response) |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
pub fn group<'a>( |
| 566 |
ui: &mut Ui, |
| 567 |
fields: &'a [Field<'a>], |
| 568 |
show_extended: bool, |
| 569 |
style: &FieldStyle, |
| 570 |
mut draw: impl FnMut(&mut Ui, &'a Field<'a>), |
| 571 |
) { |
| 572 |
ui.vertical(|ui| { |
| 573 |
ui.spacing_mut().item_spacing.y = style.group_gap; |
| 574 |
for f in fields { |
| 575 |
if f.extended && !show_extended { |
| 576 |
continue; |
| 577 |
} |
| 578 |
draw(ui, f); |
| 579 |
} |
| 580 |
}); |
| 581 |
} |
| 582 |
|
| 583 |
#[cfg(test)] |
| 584 |
mod tests { |
| 585 |
use super::*; |
| 586 |
|
| 587 |
fn palette(well: Color32) -> Palette { |
| 588 |
Palette { |
| 589 |
page: Color32::from_rgb(1, 1, 1), |
| 590 |
raised: Color32::from_rgb(2, 2, 2), |
| 591 |
overlay: Color32::from_rgb(3, 3, 3), |
| 592 |
well, |
| 593 |
sunken: Color32::from_rgb(4, 4, 4), |
| 594 |
bevel_light: Color32::WHITE, |
| 595 |
bevel_dark: Color32::BLACK, |
| 596 |
content: Color32::from_rgb(5, 5, 5), |
| 597 |
content_muted: Color32::from_rgb(6, 6, 6), |
| 598 |
danger: Color32::from_rgb(7, 7, 7), |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
#[test] |
| 603 |
fn a_well_resolves_to_its_own_token() { |
| 604 |
|
| 605 |
|
| 606 |
let w = Color32::from_rgb(9, 9, 9); |
| 607 |
let p = palette(w); |
| 608 |
assert_eq!(p.fill(Fill::Well), Some(w)); |
| 609 |
assert_ne!(p.fill(Fill::Well), Some(p.page)); |
| 610 |
} |
| 611 |
|
| 612 |
#[test] |
| 613 |
fn every_intent_is_a_plain_lookup() { |
| 614 |
let p = palette(Color32::from_rgb(9, 9, 9)); |
| 615 |
assert_eq!(p.fill(Fill::Page), Some(p.page)); |
| 616 |
assert_eq!(p.fill(Fill::Raised), Some(p.raised)); |
| 617 |
assert_eq!(p.fill(Fill::Overlay), Some(p.overlay)); |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
#[test] |
| 624 |
fn sunken_is_neither_the_well_nor_the_page() { |
| 625 |
let p = palette(Color32::from_rgb(9, 9, 9)); |
| 626 |
assert_eq!(p.fill(Fill::Sunken), Some(p.sunken)); |
| 627 |
assert_ne!(p.fill(Fill::Sunken), p.fill(Fill::Well)); |
| 628 |
assert_ne!(p.fill(Fill::Sunken), p.fill(Fill::Page)); |
| 629 |
} |
| 630 |
|
| 631 |
#[test] |
| 632 |
fn a_raised_region_never_resolves_to_the_well_fill() { |
| 633 |
|
| 634 |
let p = palette(Color32::from_rgb(9, 9, 9)); |
| 635 |
let raised = Depth::Raised.fill().and_then(|f| p.fill(f)); |
| 636 |
let well = Depth::Well.fill().and_then(|f| p.fill(f)); |
| 637 |
assert_eq!(raised, Some(p.raised)); |
| 638 |
assert_ne!(raised, well); |
| 639 |
} |
| 640 |
|
| 641 |
#[test] |
| 642 |
fn the_lit_edge_swaps_when_a_card_is_pressed() { |
| 643 |
let p = palette(Color32::from_rgb(9, 9, 9)); |
| 644 |
let (tl, _) = Depth::Raised.bevel().unwrap().edges(); |
| 645 |
let (ptl, _) = Depth::Raised.pressed().bevel().unwrap().edges(); |
| 646 |
assert_eq!(p.edge(tl), p.bevel_light); |
| 647 |
assert_eq!(p.edge(ptl), p.bevel_dark); |
| 648 |
} |
| 649 |
|
| 650 |
#[test] |
| 651 |
fn flat_asks_for_neither_fill_nor_edge() { |
| 652 |
assert!(Depth::Flat.fill().is_none()); |
| 653 |
assert!(Depth::Flat.bevel().is_none()); |
| 654 |
} |
| 655 |
|
| 656 |
#[test] |
| 657 |
fn a_select_keeps_a_value_none_of_its_options_carries() { |
| 658 |
|
| 659 |
|
| 660 |
let options = [ |
| 661 |
Choice::plain("1"), |
| 662 |
Choice::plain("3"), |
| 663 |
Choice::plain("7"), |
| 664 |
Choice::plain("14"), |
| 665 |
]; |
| 666 |
assert_eq!(shown_label(&options, "10"), "10"); |
| 667 |
|
| 668 |
let spelled = [Choice { |
| 669 |
value: "7", |
| 670 |
label: "One week", |
| 671 |
}]; |
| 672 |
assert_eq!(shown_label(&spelled, "7"), "One week"); |
| 673 |
} |
| 674 |
|
| 675 |
#[test] |
| 676 |
fn only_a_required_field_is_marked() { |
| 677 |
let style = FieldStyle::default(); |
| 678 |
let plain = Field::new(FieldKind::Text, "title", "Title"); |
| 679 |
assert_eq!(label_text(&plain, &style), "Title"); |
| 680 |
|
| 681 |
let required = Field { |
| 682 |
required: true, |
| 683 |
..plain |
| 684 |
}; |
| 685 |
assert_eq!(label_text(&required, &style), "Title *"); |
| 686 |
|
| 687 |
|
| 688 |
let house = FieldStyle { |
| 689 |
required_marker: "(required)", |
| 690 |
..style |
| 691 |
}; |
| 692 |
assert_eq!(label_text(&required, &house), "Title (required)"); |
| 693 |
} |
| 694 |
|
| 695 |
#[test] |
| 696 |
fn a_select_and_a_checkbox_are_pressed_and_everything_else_is_typed_into() { |
| 697 |
|
| 698 |
|
| 699 |
assert_eq!(control_shape(FieldKind::Select), Control::Chosen); |
| 700 |
assert_eq!(control_shape(FieldKind::Radio), Control::Listed); |
| 701 |
assert_eq!(control_shape(FieldKind::Checkbox), Control::Toggled); |
| 702 |
for k in [ |
| 703 |
FieldKind::Text, |
| 704 |
FieldKind::Secret, |
| 705 |
FieldKind::Number, |
| 706 |
FieldKind::Email, |
| 707 |
FieldKind::Url, |
| 708 |
FieldKind::Tel, |
| 709 |
FieldKind::Textarea, |
| 710 |
] { |
| 711 |
assert_eq!(control_shape(k), Control::Typed, "{k:?} is typed into"); |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
#[test] |
| 716 |
fn the_two_option_taking_kinds_are_drawn_differently_on_purpose() { |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
assert!(FieldKind::Select.offers_options()); |
| 723 |
assert!(FieldKind::Radio.offers_options()); |
| 724 |
assert_ne!( |
| 725 |
control_shape(FieldKind::Select), |
| 726 |
control_shape(FieldKind::Radio) |
| 727 |
); |
| 728 |
} |
| 729 |
|
| 730 |
#[test] |
| 731 |
fn a_hidden_field_draws_nothing_and_answers_nothing() { |
| 732 |
|
| 733 |
|
| 734 |
let f = Field::new(FieldKind::Hidden, "id", "Id"); |
| 735 |
let p = palette(Color32::from_rgb(9, 9, 9)); |
| 736 |
egui::__run_test_ui(|ui| { |
| 737 |
let drawn = field(ui, &f, Filling::Absent, None, &p, &FieldStyle::default()); |
| 738 |
assert!(drawn.is_none()); |
| 739 |
}); |
| 740 |
} |
| 741 |
|
| 742 |
#[test] |
| 743 |
fn a_disabled_field_stops_answering_and_a_focused_one_does_not() { |
| 744 |
let f = Field::new(FieldKind::Text, "title", "Title"); |
| 745 |
let p = palette(Color32::from_rgb(9, 9, 9)); |
| 746 |
let style = FieldStyle::default(); |
| 747 |
egui::__run_test_ui(|ui| { |
| 748 |
let mut text = String::from("x"); |
| 749 |
let disabled = field( |
| 750 |
ui, |
| 751 |
&f, |
| 752 |
Filling::Text(&mut text), |
| 753 |
Some(State::Disabled), |
| 754 |
&p, |
| 755 |
&style, |
| 756 |
) |
| 757 |
.unwrap(); |
| 758 |
assert!(!disabled.enabled()); |
| 759 |
|
| 760 |
let mut text = String::from("x"); |
| 761 |
let focused = field( |
| 762 |
ui, |
| 763 |
&f, |
| 764 |
Filling::Text(&mut text), |
| 765 |
Some(State::Focus), |
| 766 |
&p, |
| 767 |
&style, |
| 768 |
) |
| 769 |
.unwrap(); |
| 770 |
assert!(focused.enabled(), "focus is a thing you can still click"); |
| 771 |
}); |
| 772 |
} |
| 773 |
|
| 774 |
#[test] |
| 775 |
fn a_field_described_one_way_and_filled_another_is_drawn_inert() { |
| 776 |
|
| 777 |
|
| 778 |
let f = Field::new(FieldKind::Checkbox, "done", "Done"); |
| 779 |
let p = palette(Color32::from_rgb(9, 9, 9)); |
| 780 |
let mut text = String::from("untouched"); |
| 781 |
egui::__run_test_ui(|ui| { |
| 782 |
let drawn = field( |
| 783 |
ui, |
| 784 |
&f, |
| 785 |
Filling::Text(&mut text), |
| 786 |
None, |
| 787 |
&p, |
| 788 |
&FieldStyle::default(), |
| 789 |
); |
| 790 |
assert!(drawn.is_some()); |
| 791 |
}); |
| 792 |
assert_eq!(text, "untouched"); |
| 793 |
} |
| 794 |
|
| 795 |
#[test] |
| 796 |
fn the_disclosure_belongs_to_the_form_and_not_to_the_field() { |
| 797 |
let fields = [ |
| 798 |
Field::new(FieldKind::Text, "title", "Title"), |
| 799 |
Field { |
| 800 |
extended: true, |
| 801 |
..Field::new(FieldKind::Text, "notes", "Notes") |
| 802 |
}, |
| 803 |
]; |
| 804 |
let style = FieldStyle::default(); |
| 805 |
|
| 806 |
let mut closed = Vec::new(); |
| 807 |
egui::__run_test_ui(|ui| { |
| 808 |
group(ui, &fields, false, &style, |_, f| closed.push(f.name)); |
| 809 |
}); |
| 810 |
assert_eq!(closed, ["title"]); |
| 811 |
|
| 812 |
let mut open = Vec::new(); |
| 813 |
egui::__run_test_ui(|ui| { |
| 814 |
group(ui, &fields, true, &style, |_, f| open.push(f.name)); |
| 815 |
}); |
| 816 |
assert_eq!(open, ["title", "notes"]); |
| 817 |
} |
| 818 |
|
| 819 |
#[test] |
| 820 |
fn the_default_frame_is_square_and_one_point() { |
| 821 |
let d = FrameStyle::default(); |
| 822 |
assert_eq!(d.radius, CornerRadius::ZERO); |
| 823 |
assert_eq!(d.margin, Margin::ZERO); |
| 824 |
assert!((d.stroke - 1.0).abs() < f32::EPSILON); |
| 825 |
} |
| 826 |
} |
| 827 |
|