parley.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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::{PropertyColor, PropertyFloat32},
  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. font_size: PropertyFloat32,
  28. text_color: PropertyColor,
  29. window_scale: PropertyFloat32,
  30. lineheight: PropertyFloat32,
  31. }
  32. impl Editor {
  33. pub async fn new(
  34. font_size: PropertyFloat32,
  35. text_color: PropertyColor,
  36. window_scale: PropertyFloat32,
  37. lineheight: PropertyFloat32,
  38. ) -> Self {
  39. let editor = parley::PlainEditor::new(1.);
  40. let mut self_ = Self { editor, font_size, text_color, window_scale, lineheight };
  41. self_.refresh().await;
  42. self_
  43. }
  44. pub fn init(&mut self) {}
  45. pub fn setup(&mut self) {}
  46. pub async fn refresh(&mut self) {
  47. let font_size = self.font_size.get();
  48. let text_color = self.text_color.get();
  49. let window_scale = self.window_scale.get();
  50. let lineheight = self.lineheight.get();
  51. self.editor.set_scale(window_scale);
  52. let mut styles = parley::StyleSet::new(font_size);
  53. styles.insert(parley::StyleProperty::LineHeight(lineheight));
  54. styles.insert(parley::StyleProperty::FontStack(parley::FontStack::List(FONT_STACK.into())));
  55. styles.insert(parley::StyleProperty::Brush(text_color));
  56. *self.editor.edit_styles() = styles;
  57. let mut txt_ctx = TEXT_CTX.get().await;
  58. let (font_ctx, layout_ctx) = txt_ctx.borrow();
  59. self.editor.refresh_layout(font_ctx, layout_ctx);
  60. }
  61. pub fn layout(&self) -> &parley::Layout<Color> {
  62. self.editor.try_layout().unwrap()
  63. }
  64. pub fn move_to_pos(&self, pos: Point) {}
  65. pub fn get_cursor_pos(&self) -> Option<Point> {
  66. let cursor_rect = self.editor.cursor_geometry(0.).unwrap();
  67. let cursor_pos = Point::new(cursor_rect.x0 as f32, cursor_rect.y0 as f32);
  68. Some(cursor_pos)
  69. }
  70. pub async fn driver<'a>(
  71. &'a mut self,
  72. txt_ctx: &'a mut TextContext,
  73. ) -> Option<parley::PlainEditorDriver<'a, Color>> {
  74. let (font_ctx, layout_ctx) = txt_ctx.borrow();
  75. Some(self.editor.driver(font_ctx, layout_ctx))
  76. }
  77. pub fn set_width(&mut self, w: f32) {
  78. self.editor.set_width(Some(w));
  79. }
  80. pub fn height(&self) -> f32 {
  81. self.layout().height()
  82. }
  83. pub fn selected_text(&self) -> Option<String> {
  84. self.editor.selected_text().to_owned()
  85. }
  86. }