rsvim_core 0.1.3-alpha.2

The core library for RSVIM text editor.
Documentation
//! Tree utils for testing.

use crate::buf::BufferManagerArc;
use crate::cmdltext::CmdlineTextArc;
use crate::evloop::ui as evloop_ui;
use crate::prelude::*;
use crate::ui::canvas::CursorStyle;
use crate::ui::tree::*;
use crate::ui::widget::cursor::BLINKING;
use crate::ui::widget::cursor::CURSOR_STYLE;
use crate::ui::widget::cursor::HIDDEN;
use crate::ui::widget::window::opt::WindowOptions;
use std::sync::Arc;
use taffy::Style;

/// Create tree with 1 window and 1 buffer, the buffer is in buffers manager.
pub fn make_tree_with_buffers(
  canvas_size: U16Size,
  window_local_opts: WindowOptions,
  buffers_manager: BufferManagerArc,
) -> TreeArc {
  let tree_style = Style {
    size: taffy::Size {
      width: taffy::prelude::length(canvas_size.width()),
      height: taffy::prelude::length(canvas_size.height()),
    },
    ..Default::default()
  };
  let window_style = Style {
    size: taffy::Size {
      width: taffy::prelude::percent(1.0_f32),
      height: taffy::prelude::percent(1.0_f32),
    },
    ..Default::default()
  };

  let tree_arc = Tree::to_arc(Tree::new(tree_style).unwrap());
  let buffer_manager = lock!(buffers_manager);

  let mut tree = lock!(tree_arc);
  tree.set_global_local_options(window_local_opts);
  let tree_root_id = tree.root_id();

  // Window
  let (_, buf) = buffer_manager.first_key_value().unwrap();
  let window_id = tree
    .new_window_with_parent(
      tree_root_id,
      window_style,
      window_local_opts,
      Arc::downgrade(buf),
    )
    .unwrap();
  let window_content_id = tree.window(window_id).unwrap().content_id();
  tree.set_current_window_id(Some(window_id));

  // Cursor.
  let _cursor_id = tree
    .new_cursor_with_parent(
      window_content_id,
      false,
      false,
      CursorStyle::SteadyBlock,
    )
    .unwrap();

  tree_arc.clone()
}

/// Create tree with 1 window, 1 buffer, and 1 command-line, the buffer is in buffers manager, the
/// command-line is in the text contents.
pub fn make_tree_with_buffers_cmdline(
  canvas_size: U16Size,
  window_local_opts: WindowOptions,
  buffers_manager: BufferManagerArc,
  cmdline_text: CmdlineTextArc,
) -> TreeArc {
  let tree_style = Style {
    display: taffy::Display::Grid,
    grid_template_rows: vec![
      taffy::prelude::fr(1_u16),
      taffy::prelude::length(1_u16),
    ],
    size: taffy::Size {
      width: taffy::prelude::length(canvas_size.width()),
      height: taffy::prelude::length(canvas_size.height()),
    },
    ..Default::default()
  };
  let tree_arc = Tree::to_arc(Tree::new(tree_style).unwrap());
  let buffer_manager = lock!(buffers_manager);
  let (_, buf) = buffer_manager.first_key_value().unwrap();
  let buf = Arc::downgrade(buf);
  let cmdline_text = Arc::downgrade(&cmdline_text);

  let mut tree = lock!(tree_arc);
  tree.set_global_local_options(window_local_opts);

  evloop_ui::init_default_window(
    &mut tree,
    buf,
    cmdline_text,
    BLINKING,
    HIDDEN,
    CURSOR_STYLE,
  );

  tree_arc.clone()
}