parley.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use crate::{
  19. gfx::Point,
  20. mesh::Color,
  21. prop::{PropertyAtomicGuard, PropertyColor, PropertyFloat32, PropertyStr},
  22. text2::{TextContext, FONT_STACK, TEXT_CTX},
  23. };
  24. macro_rules! t { ($($arg:tt)*) => { trace!(target: "text::editor", $($arg)*); } }
  25. pub struct Editor {
  26. editor: parley::PlainEditor<Color>,
  27. text: PropertyStr,
  28. font_size: PropertyFloat32,
  29. text_color: PropertyColor,
  30. window_scale: PropertyFloat32,
  31. lineheight: PropertyFloat32,
  32. }
  33. impl Editor {
  34. pub async fn new(
  35. text: PropertyStr,
  36. font_size: PropertyFloat32,
  37. text_color: PropertyColor,
  38. window_scale: PropertyFloat32,
  39. lineheight: PropertyFloat32,
  40. ) -> Self {
  41. let mut editor = parley::PlainEditor::new(1.);
  42. //let atxt = "A berry is a small, pulpy, and often edible fruit. Typically, berries are juicy, rounded, brightly colored, sweet, sour or tart, and do not have a stone or pit, although many pips or seeds may be present. Common examples of berries in the culinary sense are strawberries, raspberries, blueberries, blackberries, white currants, blackcurrants, and redcurrants. In Britain, soft fruit is a horticultural term for such fruits. The common usage of the term berry is different from the scientific or botanical definition of a berry, which refers to a fruit produced from the ovary of a single flower where the outer layer of the ovary wall develops into an edible fleshy portion (pericarp). The botanical definition includes many fruits that are not commonly known or referred to as berries, such as grapes, tomatoes, cucumbers, eggplants, bananas, and chili peppers.";
  43. //editor.set_text(atxt);
  44. let mut self_ = Self { text, editor, font_size, text_color, window_scale, lineheight };
  45. self_.refresh_layout().await;
  46. self_
  47. }
  48. pub fn init(&mut self) {}
  49. pub fn setup(&mut self) {}
  50. async fn refresh_layout(&mut self) {
  51. let font_size = self.font_size.get();
  52. let text_color = self.text_color.get();
  53. let window_scale = self.window_scale.get();
  54. let lineheight = self.lineheight.get();
  55. self.editor.set_scale(window_scale);
  56. let mut styles = parley::StyleSet::new(font_size);
  57. styles.insert(parley::StyleProperty::LineHeight(lineheight));
  58. styles.insert(parley::StyleProperty::FontStack(parley::FontStack::List(FONT_STACK.into())));
  59. styles.insert(parley::StyleProperty::Brush(text_color));
  60. *self.editor.edit_styles() = styles;
  61. let mut txt_ctx = TEXT_CTX.get().await;
  62. let (font_ctx, layout_ctx) = txt_ctx.borrow();
  63. self.editor.refresh_layout(font_ctx, layout_ctx);
  64. }
  65. pub async fn refresh(&mut self, atom: &mut PropertyAtomicGuard) {
  66. self.refresh_layout().await;
  67. self.text.set(atom, self.editor.raw_text());
  68. }
  69. pub fn layout(&self) -> &parley::Layout<Color> {
  70. self.editor.try_layout().unwrap()
  71. }
  72. pub fn move_to_pos(&self, pos: Point) {
  73. unimplemented!()
  74. }
  75. pub fn select_word_at_point(&self, pos: Point) {
  76. unimplemented!()
  77. }
  78. pub fn get_cursor_pos(&self) -> Option<Point> {
  79. let cursor_rect = self.editor.cursor_geometry(0.).unwrap();
  80. let cursor_pos = Point::new(cursor_rect.x0 as f32, cursor_rect.y0 as f32);
  81. Some(cursor_pos)
  82. }
  83. pub async fn driver<'a>(
  84. &'a mut self,
  85. txt_ctx: &'a mut TextContext,
  86. ) -> Option<parley::PlainEditorDriver<'a, Color>> {
  87. let (font_ctx, layout_ctx) = txt_ctx.borrow();
  88. Some(self.editor.driver(font_ctx, layout_ctx))
  89. }
  90. pub fn set_width(&mut self, w: f32) {
  91. self.editor.set_width(Some(w));
  92. }
  93. pub fn height(&self) -> f32 {
  94. self.layout().height()
  95. }
  96. pub fn selected_text(&self) -> Option<String> {
  97. self.editor.selected_text().map(str::to_string)
  98. }
  99. pub fn selection(&self) -> parley::Selection {
  100. *self.editor.raw_selection()
  101. }
  102. pub fn set_selection(&self, select_start: usize, select_end: usize) {}
  103. pub fn buffer(&self) -> String {
  104. self.editor.raw_text().to_string()
  105. }
  106. }