|
1 | 1 | //! The settings page UI |
2 | 2 |
|
| 3 | +use std::collections::HashMap; |
| 4 | + |
3 | 5 | use iced::widget::Slider; |
| 6 | +use iced::widget::TextInput; |
4 | 7 | use iced::widget::checkbox; |
5 | 8 | use iced::widget::text_input; |
6 | 9 |
|
| 10 | +use crate::app::Editable; |
7 | 11 | use crate::app::SetConfigBufferFields; |
8 | 12 | use crate::app::SetConfigThemeFields; |
| 13 | +use crate::styles::delete_button_style; |
| 14 | +use crate::styles::settings_add_button_style; |
9 | 15 | use crate::styles::settings_checkbox_style; |
10 | 16 | use crate::styles::settings_save_button_style; |
11 | 17 | use crate::styles::settings_slider_style; |
@@ -351,6 +357,10 @@ pub fn settings_page(config: Config) -> Element<'static, Message> { |
351 | 357 | font_family.into(), |
352 | 358 | text_clr.into(), |
353 | 359 | bg_clr.into(), |
| 360 | + settings_hint_text(theme.clone(), "Aliases"), |
| 361 | + aliases_item(config.aliases, &theme), |
| 362 | + settings_hint_text(theme.clone(), "Rustcast modes"), |
| 363 | + modes_item(config.modes, &theme), |
354 | 364 | Row::from_iter([ |
355 | 365 | savebutton(theme.clone()), |
356 | 366 | default_button(theme.clone()), |
@@ -451,3 +461,138 @@ fn notice_item(theme: Theme, notice: impl ToString) -> Element<'static, Message> |
451 | 461 | .align_x(Alignment::End) |
452 | 462 | .into() |
453 | 463 | } |
| 464 | + |
| 465 | +fn aliases_item(aliases: HashMap<String, String>, theme: &Theme) -> Element<'static, Message> { |
| 466 | + let theme_clone = theme.clone(); |
| 467 | + let mut aliases = aliases |
| 468 | + .iter() |
| 469 | + .map(|(k, v)| (k.to_owned(), v.to_owned())) |
| 470 | + .collect::<Vec<(String, String)>>(); |
| 471 | + aliases.sort_by_key(|x| x.0.len()); |
| 472 | + Column::from_iter([ |
| 473 | + container( |
| 474 | + Column::from_iter(aliases.iter().map(|(key, value)| { |
| 475 | + let key_clone = key.clone(); |
| 476 | + let val_clone = value.clone(); |
| 477 | + let key_clone_2 = key.clone(); |
| 478 | + let val_clone_2 = value.clone(); |
| 479 | + let theme_clone_2 = theme.clone(); |
| 480 | + Row::from_iter([ |
| 481 | + text_input_cell(key.to_owned(), &theme_clone, "Shorthand") |
| 482 | + .on_input(move |input| { |
| 483 | + Message::SetConfig(SetConfigFields::Aliases(Editable::Update { |
| 484 | + old: (key_clone.clone(), val_clone.clone()), |
| 485 | + new: (input.clone(), val_clone.clone()), |
| 486 | + })) |
| 487 | + }) |
| 488 | + .into(), |
| 489 | + text_input_cell(value.to_owned(), &theme_clone, "Term") |
| 490 | + .on_input(move |input| { |
| 491 | + Message::SetConfig(SetConfigFields::Aliases(Editable::Update { |
| 492 | + old: (key_clone_2.clone(), val_clone_2.clone()), |
| 493 | + new: (key_clone_2.clone(), input.clone()), |
| 494 | + })) |
| 495 | + }) |
| 496 | + .into(), |
| 497 | + Button::new("Delete") |
| 498 | + .on_press(Message::SetConfig(SetConfigFields::Aliases( |
| 499 | + Editable::Delete((key.clone(), value.clone())), |
| 500 | + ))) |
| 501 | + .style(move |_, _| delete_button_style(&theme_clone_2)) |
| 502 | + .into(), |
| 503 | + ]) |
| 504 | + .spacing(10) |
| 505 | + .into() |
| 506 | + })) |
| 507 | + .spacing(10), |
| 508 | + ) |
| 509 | + .height(Length::Fill) |
| 510 | + .width(Length::Fill) |
| 511 | + .into(), |
| 512 | + Button::new( |
| 513 | + Text::new("+") |
| 514 | + .align_x(Alignment::Center) |
| 515 | + .align_y(Alignment::Center), |
| 516 | + ) |
| 517 | + .style(move |_, _| settings_add_button_style(&theme_clone.clone())) |
| 518 | + .on_press(Message::SetConfig(SetConfigFields::Aliases( |
| 519 | + Editable::Create((String::new(), String::new())), |
| 520 | + ))) |
| 521 | + .into(), |
| 522 | + ]) |
| 523 | + .spacing(10) |
| 524 | + .width(Length::Fill) |
| 525 | + .align_x(Alignment::Center) |
| 526 | + .into() |
| 527 | +} |
| 528 | + |
| 529 | +fn text_input_cell(text: String, theme: &Theme, placeholder: &str) -> TextInput<'static, Message> { |
| 530 | + text_input(placeholder, &text) |
| 531 | + .font(theme.font()) |
| 532 | + .padding(5) |
| 533 | + .on_submit(Message::WriteConfig(false)) |
| 534 | +} |
| 535 | + |
| 536 | +fn modes_item(modes: HashMap<String, String>, theme: &Theme) -> Element<'static, Message> { |
| 537 | + let theme_clone = theme.clone(); |
| 538 | + let mut modes = modes |
| 539 | + .iter() |
| 540 | + .map(|(k, v)| (k.to_owned(), v.to_owned())) |
| 541 | + .collect::<Vec<(String, String)>>(); |
| 542 | + modes.sort_by_key(|x| x.0.len()); |
| 543 | + Column::from_iter([ |
| 544 | + container( |
| 545 | + Column::from_iter(modes.iter().map(|(key, value)| { |
| 546 | + let theme_clone_1 = theme_clone.clone(); |
| 547 | + let display_val = if value.is_empty() { |
| 548 | + "Pick a file".to_string() |
| 549 | + } else { |
| 550 | + value.replace(&std::env::var("HOME").unwrap_or("".to_string()), "~") |
| 551 | + }; |
| 552 | + let key_clone = key.clone(); |
| 553 | + let val_clone = value.clone(); |
| 554 | + let theme_clone_2 = theme.clone(); |
| 555 | + Row::from_iter([ |
| 556 | + text_input_cell(key.to_owned(), &theme_clone, "Shorthand") |
| 557 | + .on_input(move |input| { |
| 558 | + Message::SetConfig(SetConfigFields::Modes(Editable::Update { |
| 559 | + old: (key_clone.clone(), val_clone.clone()), |
| 560 | + new: (input.clone(), val_clone.clone()), |
| 561 | + })) |
| 562 | + }) |
| 563 | + .into(), |
| 564 | + Button::new(Text::new(display_val)) |
| 565 | + .on_press(Message::OpenFileDialogue(key.to_owned())) |
| 566 | + .style(move |_, _| settings_add_button_style(&theme_clone_1.clone())) |
| 567 | + .into(), |
| 568 | + Button::new("Delete") |
| 569 | + .on_press(Message::SetConfig(SetConfigFields::Modes( |
| 570 | + Editable::Delete((key.clone(), value.clone())), |
| 571 | + ))) |
| 572 | + .style(move |_, _| delete_button_style(&theme_clone_2)) |
| 573 | + .into(), |
| 574 | + ]) |
| 575 | + .spacing(10) |
| 576 | + .into() |
| 577 | + })) |
| 578 | + .spacing(10), |
| 579 | + ) |
| 580 | + .height(Length::Fill) |
| 581 | + .width(Length::Fill) |
| 582 | + .into(), |
| 583 | + Button::new( |
| 584 | + Text::new("+") |
| 585 | + .align_x(Alignment::Center) |
| 586 | + .align_y(Alignment::Center), |
| 587 | + ) |
| 588 | + .on_press(Message::SetConfig(SetConfigFields::Modes( |
| 589 | + Editable::Create((String::new(), String::new())), |
| 590 | + ))) |
| 591 | + .style(move |_, _| settings_add_button_style(&theme_clone.clone())) |
| 592 | + .into(), |
| 593 | + ]) |
| 594 | + .spacing(10) |
| 595 | + .width(Length::Fill) |
| 596 | + .align_x(Alignment::Center) |
| 597 | + .into() |
| 598 | +} |
0 commit comments