summaryrefslogtreecommitdiff
path: root/src/panes/code_view.rs
blob: b7aa7603f9c02abe6c0ec074eff0bd1e59355ece (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use iced::advanced::text::highlighter::Format;
use iced::border::Radius;
use iced::widget::{button, pane_grid, row, text, text_editor};
use iced::{Alignment, Border, Font, Length};
use iced_custom_highlighter::{Highlight, Highlighter, Scope, Settings};
use material_theme::Theme;

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

// TODO: implement a highlight style for the material theme
fn highlight_style(theme: &Theme, scope: &Scope) -> Format<Font> {
    let theme = if theme.is_dark() {
        iced::Theme::SolarizedDark
    } else {
        iced::Theme::SolarizedLight
    };

    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,
    is_focused: bool,
) -> pane_grid::Content<'_, Message, Theme> {
    let title_bar = pane_grid::TitleBar::new(text("Generated Code").center())
        .controls(pane_grid::Controls::dynamic(
            row![
                tip(
                    button(icon::copy())
                        .on_press(Message::CopyCode)
                        .padding([2, 7])
                        .style(material_theme::button::text),
                    "Copy",
                    tip::Position::FollowCursor
                ),
                button("Switch to Designer view")
                    .on_press(DesignerPane::DesignerView.into())
            ]
            .spacing(20)
            .align_y(Alignment::Center),
            row![
                tip(
                    button(icon::copy())
                        .on_press(Message::CopyCode)
                        .padding([2, 7])
                        .style(material_theme::button::text),
                    "Copy",
                    tip::Position::FollowCursor
                ),
                button(icon::switch())
                    .on_press(DesignerPane::DesignerView.into())
            ]
            .spacing(20)
            .align_y(Alignment::Center),
        ))
        .padding(10)
        .style(style::title_bar);

    pane_grid::Content::new(
        text_editor(editor_content)
            .on_action(Message::EditorAction)
            .font(Font::MONOSPACE)
            .highlight_with::<Highlighter<Theme>>(
                Settings::new(vec![], highlight_style, "rs"),
                Highlight::to_format,
            )
            .style(|theme, _| {
                let style = material_theme::text_editor::default(
                    theme,
                    text_editor::Status::Active,
                );

                text_editor::Style {
                    border: Border {
                        radius: Radius::default(),
                        ..style.border
                    },
                    ..style
                }
            })
            .height(Length::Fill)
            .padding(20),
    )
    .title_bar(title_bar)
    .style(if is_focused {
        style::pane_focused
    } else {
        style::pane_active
    })
}