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