use iced::Size;
use iced::advanced::input_method;
use iced::widget::canvas::Canvas;
use iced::widget::{
Column, Row, Scrollable, Space, container, scrollable, text,
};
use iced::{Background, Border, Color, Element, Length, Rectangle, Shadow};
use iced_aw::ContextMenu;
use super::wrapping::{self, WrappingCalculator};
use crate::canvas_editor::features::context_menu;
use crate::canvas_editor::features::goto_line::dialog as goto_line_dialog;
use crate::canvas_editor::features::search::dialog as search_dialog;
use crate::canvas_editor::input::ime_requester::ImeRequester;
use crate::canvas_editor::{CodeEditor, GUTTER_WIDTH, Message};
use std::rc::Rc;
fn canvas_scrollbar_style(
scrollbar_bg: Color,
scroller_color: Color,
) -> scrollable::Style {
scrollable::Style {
container: container::Style {
background: Some(Background::Color(Color::TRANSPARENT)),
..container::Style::default()
},
vertical_rail: scrollable_rail(scrollbar_bg, scroller_color, 4.0),
horizontal_rail: scrollable_rail(scrollbar_bg, scroller_color, 4.0),
gap: None,
auto_scroll: scrollable::AutoScroll {
background: Color::TRANSPARENT.into(),
border: Border::default(),
shadow: Shadow::default(),
icon: Color::TRANSPARENT,
},
}
}
impl CodeEditor {
fn calculate_canvas_height(&self) -> (Rc<Vec<wrapping::VisualLine>>, f32) {
let visual_lines = self.visual_lines_cached(self.viewport_width);
let total_visual_lines = visual_lines.len();
let content_height = total_visual_lines as f32 * self.line_height;
let canvas_height = content_height.max(self.viewport_height);
(visual_lines, canvas_height)
}
fn create_scrollable_style(
&self,
) -> impl Fn(&iced::Theme, scrollable::Status) -> scrollable::Style {
let scrollbar_bg = self.style.scrollbar_background;
let scroller_color = self.style.scroller_color;
move |_theme, _status| {
canvas_scrollbar_style(scrollbar_bg, scroller_color)
}
}
fn create_canvas_with_scrollable(
&self,
canvas_height: f32,
) -> Scrollable<'_, Message> {
let canvas = Canvas::new(self)
.width(Length::Fill)
.height(Length::Fixed(canvas_height));
Scrollable::new(canvas)
.id(self.scrollable_id.clone())
.width(Length::Fill)
.height(Length::Fill)
.on_scroll(Message::Scrolled)
.style(self.create_scrollable_style())
}
fn create_horizontal_scrollbar(
&self,
max_content_width: f32,
) -> Option<Element<'_, Message>> {
if self.wrap_enabled || max_content_width <= self.viewport_width {
return None;
}
let scrollbar_bg = self.style.scrollbar_background;
let scroller_color = self.style.scroller_color;
let h_scrollable = Scrollable::new(
Space::new().width(Length::Fixed(max_content_width)).height(0.0),
)
.id(self.horizontal_scrollable_id.clone())
.width(Length::Fill)
.height(Length::Fixed(12.0))
.direction(scrollable::Direction::Horizontal(
scrollable::Scrollbar::new(),
))
.on_scroll(Message::HorizontalScrolled)
.style(move |_theme, _status| {
canvas_scrollbar_style(scrollbar_bg, scroller_color)
});
Some(h_scrollable.into())
}
fn create_gutter_container(
&self,
) -> Option<container::Container<'_, Message>> {
if self.line_numbers_enabled {
let gutter_background = self.style.gutter_background;
Some(
container(
Space::new().width(Length::Fill).height(Length::Fill),
)
.width(Length::Fixed(GUTTER_WIDTH))
.height(Length::Fill)
.style(move |_| container::Style {
background: Some(Background::Color(gutter_background)),
..container::Style::default()
}),
)
} else {
None
}
}
fn create_code_background_container(
&self,
) -> container::Container<'_, Message> {
let background_color = self.style.background;
container(Space::new().width(Length::Fill).height(Length::Fill))
.width(Length::Fill)
.height(Length::Fill)
.style(move |_| container::Style {
background: Some(Background::Color(background_color)),
..container::Style::default()
})
}
fn create_vim_status_bar(&self) -> Element<'_, Message> {
let (left_text, right_text) = self.vim_state.status_line_text();
let background = self.style.gutter_background;
let text_color = self.style.text_color;
container(
Row::new()
.push(
text(left_text).size(self.font_size).style(move |_| {
text::Style { color: Some(text_color) }
}),
)
.push(Space::new().width(Length::Fill))
.push(
text(right_text).size(self.font_size).style(move |_| {
text::Style { color: Some(text_color) }
}),
),
)
.padding([2, 8])
.width(Length::Fill)
.height(Length::Fixed(self.line_height.max(20.0)))
.style(move |_| container::Style {
background: Some(Background::Color(background)),
..container::Style::default()
})
.into()
}
fn create_background_layer(&self) -> Row<'_, Message> {
let gutter_container = self.create_gutter_container();
let code_background_container = self.create_code_background_container();
if let Some(gutter) = gutter_container {
Row::new().push(gutter).push(code_background_container)
} else {
Row::new().push(code_background_container)
}
}
fn calculate_ime_cursor_rect(
&self,
visual_lines: &[wrapping::VisualLine],
) -> Rectangle {
let ime_enabled = self.is_focused() && self.has_canvas_focus;
if !ime_enabled {
return Rectangle::new(
iced::Point::new(0.0, 0.0),
Size::new(0.0, 0.0),
);
}
if let Some(cursor_visual) = WrappingCalculator::logical_to_visual(
visual_lines,
self.cursors.primary_position().0,
self.cursors.primary_position().1,
) {
let vl = &visual_lines[cursor_visual];
let line_content = self.buffer.line(vl.logical_line);
let prefix_len =
self.cursors.primary_position().1.saturating_sub(vl.start_col);
let prefix_text: String = line_content
.chars()
.skip(vl.start_col)
.take(prefix_len)
.collect();
let cursor_x = self.gutter_width()
+ 5.0
+ crate::canvas_editor::measure_text_width(
&prefix_text,
self.full_char_width,
self.char_width,
)
- self.horizontal_scroll_offset;
let cursor_y = (cursor_visual as f32 * self.line_height)
- self.viewport_scroll;
Rectangle::new(
iced::Point::new(cursor_x, cursor_y + 2.0),
Size::new(2.0, self.line_height - 4.0),
)
} else {
Rectangle::new(iced::Point::new(0.0, 0.0), Size::new(0.0, 0.0))
}
}
fn create_ime_layer(&self, cursor_rect: Rectangle) -> Element<'_, Message> {
let ime_enabled = self.is_focused() && self.has_canvas_focus;
let preedit =
self.ime_preedit.as_ref().map(|p| input_method::Preedit {
content: p.content.clone(),
selection: p.selection.clone(),
text_size: None,
});
let ime_layer = ImeRequester::new(ime_enabled, cursor_rect, preedit);
iced::Element::new(ime_layer)
}
pub fn view(&self) -> Element<'_, Message> {
let (visual_lines, canvas_height) = self.calculate_canvas_height();
let scrollable = self.create_canvas_with_scrollable(canvas_height);
let background_row = self.create_background_layer();
let mut editor_stack =
iced::widget::Stack::new().push(background_row).push(scrollable);
let cursor_rect = self.calculate_ime_cursor_rect(visual_lines.as_ref());
let ime_layer = self.create_ime_layer(cursor_rect);
editor_stack = editor_stack.push(ime_layer);
if self.search_state.is_open {
let search_dialog =
search_dialog::view(&self.search_state, &self.translations);
let positioned_dialog = container(
Row::new()
.push(Space::new().width(Length::Fill))
.push(search_dialog),
)
.padding(20)
.width(Length::Fill)
.height(Length::Shrink);
editor_stack = editor_stack.push(positioned_dialog);
}
if self.goto_line_state.is_open {
let goto_line_dialog = goto_line_dialog::view(
&self.goto_line_state,
self.buffer.line_count(),
);
let positioned_dialog = container(
Row::new()
.push(Space::new().width(Length::Fill))
.push(goto_line_dialog)
.push(Space::new().width(Length::Fill)),
)
.padding(20)
.width(Length::Fill)
.height(Length::Shrink);
editor_stack = editor_stack.push(positioned_dialog);
}
let editor_container = container(editor_stack)
.width(Length::Fill)
.height(Length::Fill)
.clip(true);
let can_undo = self.history.can_undo();
let can_redo = self.history.can_redo();
let has_selection =
self.cursors.iter().any(|cursor| cursor.has_selection());
let has_content =
self.buffer.line_count() > 1 || self.buffer.line_len(0) > 0;
let custom_context_menu_entries =
self.custom_context_menu_entries().to_vec();
let default_context_menu_enabled = self.default_context_menu_enabled();
let reveal_in_file_manager_enabled =
self.reveal_in_file_manager_enabled();
let translations = self.translations;
let editor_container = ContextMenu::new(editor_container, move || {
context_menu::view(
&custom_context_menu_entries,
default_context_menu_enabled,
context_menu::MenuState {
can_undo,
can_redo,
has_selection,
has_content,
reveal_in_file_manager_enabled,
},
translations,
)
});
let editor_body: Element<'_, Message> = if self.wrap_enabled {
editor_container.into()
} else {
let max_content_width = self.max_content_width();
if let Some(h_scrollbar) =
self.create_horizontal_scrollbar(max_content_width)
{
Column::new().push(editor_container).push(h_scrollbar).into()
} else {
editor_container.into()
}
};
if self.vim_enabled {
Column::new()
.push(editor_body)
.push(self.create_vim_status_bar())
.width(Length::Fill)
.height(Length::Fill)
.into()
} else {
editor_body
}
}
}
pub(crate) fn scrollable_rail(
background: Color,
scroller: Color,
radius: f32,
) -> iced::widget::scrollable::Rail {
iced::widget::scrollable::Rail {
background: Some(background.into()),
border: iced::Border {
radius: radius.into(),
width: 0.0,
color: Color::TRANSPARENT,
},
scroller: iced::widget::scrollable::Scroller {
background: scroller.into(),
border: iced::Border {
radius: radius.into(),
width: 0.0,
color: Color::TRANSPARENT,
},
},
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_scrollable_rail_sets_background_scroller_and_radius() {
let bg = Color::from_rgb(0.1, 0.2, 0.3);
let scroller = Color::from_rgb(0.4, 0.5, 0.6);
let rail = scrollable_rail(bg, scroller, 4.0);
assert_eq!(rail.background, Some(iced::Background::Color(bg)));
assert_eq!(rail.scroller.background, iced::Background::Color(scroller));
assert_eq!(rail.border.radius, iced::border::Radius::from(4.0));
assert!(rail.border.width.abs() < f32::EPSILON);
assert_eq!(
rail.scroller.border.radius,
iced::border::Radius::from(4.0)
);
}
#[test]
fn test_canvas_scrollbar_style_uses_transparent_container() {
let bg = Color::from_rgb(0.1, 0.2, 0.3);
let scroller = Color::from_rgb(0.4, 0.5, 0.6);
let style = canvas_scrollbar_style(bg, scroller);
assert_eq!(
style.container.background,
Some(Background::Color(Color::TRANSPARENT))
);
let expected_rail = scrollable_rail(bg, scroller, 4.0);
assert_eq!(style.vertical_rail.background, expected_rail.background);
assert_eq!(style.horizontal_rail.background, expected_rail.background);
}
}