Skip to main content

hyprforge_ui/widgets/
change.rs

1//! A line of a "before → after" preview.
2//!
3//! Written for Files' bulk rename, and here rather than in Files because
4//! nothing about it is about files: any preview of a change to many
5//! things at once — Settings importing a hand-written config, a batch of
6//! retitled media — is the same two columns and the same three states.
7//! See `DESIGN-SYSTEM.md`'s rule about the second user.
8
9use super::scaled_text;
10use crate::density;
11use crate::theme::{self, spacing, FontScale};
12use iced::widget::{column, container, row, text::IntoFragment};
13use iced::{Element, Length};
14
15/// What a [`change_row`] is saying about its thing.
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum Change {
18    /// Nothing happens to it. Both sides dim, so the eye skips to the
19    /// lines that do change — the row stays, because a preview that
20    /// hides rows makes a person wonder whether the rule saw them.
21    Unchanged,
22    /// It changes, and can.
23    Changes,
24    /// It would change and cannot; the reason goes under it.
25    Refused,
26}
27
28/// One thing in a preview: what it is now on the left, what it would be
29/// on the right, and — when it cannot — the reason under the right-hand
30/// side in the error colour.
31///
32/// Two equal columns rather than "old → new" run together, so a list of
33/// them reads down each side; a long value wraps within its own column,
34/// never under the other one. The arrow is text, in the dim colour, at
35/// the metadata size: it separates, it does not say anything.
36pub fn change_row<'a, Message: 'a>(
37    before: impl IntoFragment<'a>,
38    after: impl IntoFragment<'a>,
39    change: Change,
40    reason: Option<&'a str>,
41    scale: FontScale,
42) -> Element<'a, Message> {
43    let (before_colour, after_colour) = match change {
44        Change::Unchanged => (theme::text_dim(), theme::text_dim()),
45        Change::Changes => (theme::text_dim(), theme::text()),
46        Change::Refused => (theme::text_dim(), theme::error()),
47    };
48    let size = density::ROW_TEXT_BASE * 0.9;
49    let wrap = iced::widget::text::Wrapping::WordOrGlyph;
50    let mut right = column![scaled_text(after, size, scale).color(after_colour).wrapping(wrap)].spacing(2.0);
51    if let (Change::Refused, Some(reason)) = (change, reason) {
52        right = right.push(
53            scaled_text(reason, density::META_TEXT_BASE * 0.9, scale).color(theme::error()).wrapping(wrap),
54        );
55    }
56    row![
57        container(scaled_text(before, size, scale).color(before_colour).wrapping(wrap)).width(Length::FillPortion(1)),
58        scaled_text("\u{2192}", density::META_TEXT_BASE, scale).color(theme::text_dim()),
59        container(right).width(Length::FillPortion(1)),
60    ]
61    .spacing(spacing::SM)
62    .align_y(iced::Alignment::Start)
63    .into()
64}