summaryrefslogtreecommitdiff
path: root/src/panes/code_view.rs
blob: 956eb871cf2273824c3c416fe611070ae2e5b3ff (plain)
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
67
68
69
70
71
72
73
74
75
76
use iced::advanced::text::highlighter::Format;
use iced::widget::{button, pane_grid, row, text, text_editor, Space};
use iced::{Alignment, Background, Border, Font, Length, Theme};
use iced_custom_highlighter::{Highlight, Highlighter, Scope, Settings};

use super::style;
use crate::icon::copy;
use crate::types::{DesignerPage, Message};
use crate::widget::tip;

fn highlight_style(theme: &Theme, scope: &Scope) -> Format<Font> {
    match scope {
        Scope::Custom { .. } | Scope::Other => Format {
            color: Some(theme.extended_palette().primary.strong.color),
            font: None,
        },
        _ => Highlight::default_style(theme, scope),
    }
}

pub fn view(
    editor_content: &text_editor::Content,
    theme: Theme,
    is_focused: bool,
) -> pane_grid::Content<'_, Message> {
    let title = row![
        text("Generated Code"),
        Space::with_width(Length::Fill),
        tip(
            button(copy()).on_press(Message::CopyCode),
            "Copy code to clipboard",
            tip::Position::FollowCursor
        ),
        Space::with_width(20),
        button("Switch to Designer view")
            .on_press(Message::SwitchPage(DesignerPage::DesignerView))
    ]
    .align_y(Alignment::Center);
    let title_bar = pane_grid::TitleBar::new(title)
        .padding(10)
        .style(style::title_bar);
    pane_grid::Content::new(
        text_editor(editor_content)
            .on_action(Message::EditorAction)
            .font(Font::MONOSPACE)
            .highlight_with::<Highlighter>(
                Settings::new(vec![], highlight_style, theme, "rs"),
                Highlight::to_format,
            )
            .style(|theme, _| {
                let palette = theme.extended_palette();
                text_editor::Style {
                    background: Background::Color(
                        palette.background.base.color,
                    ),
                    border: Border {
                        radius: 2.0.into(),
                        width: 1.0,
                        color: palette.background.strong.color,
                    },
                    icon: palette.background.weak.text,
                    placeholder: palette.background.strong.color,
                    value: palette.background.base.text,
                    selection: palette.primary.weak.color,
                }
            })
            .height(Length::Fill)
            .padding(20),
    )
    .title_bar(title_bar)
    .style(if is_focused {
        style::pane_focused
    } else {
        style::pane_active
    })
}