From 92fb3e17aa413046dff0a004ab5d790d45ef90f5 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sat, 3 Aug 2024 12:20:54 +0200 Subject: add cursor shape handling --- src/state/editor/mod.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/state/editor/mod.rs') diff --git a/src/state/editor/mod.rs b/src/state/editor/mod.rs index 47d11bc..d9c3ab5 100644 --- a/src/state/editor/mod.rs +++ b/src/state/editor/mod.rs @@ -1,18 +1,20 @@ use crate::cursor::CursorMove; -use self::{bar::EditorBarState, buffer::Buffer}; +use self::{bar::EditorBarState, buffer::Buffer, cursorshape::CursorShape}; -use super::{GlobalState, DUMMY_STATE}; +use super::{window::Window, GlobalState, DUMMY_STATE}; use mlua::{Lua, Table, UserData}; pub mod bar; pub mod buffer; +pub mod cursorshape; #[derive(Default, Debug)] pub struct EditorState { pub visible: bool, pub buffer: Buffer, pub bar: EditorBarState, + pub cursor_shape: CursorShape, } macro_rules! cfg { @@ -33,6 +35,7 @@ impl EditorState { visible: false, buffer: Buffer::new(), bar: EditorBarState::new(), + cursor_shape: CursorShape::Bar, } } @@ -48,18 +51,31 @@ impl UserData for EditorState { fn add_fields<'lua, F: mlua::prelude::LuaUserDataFields<'lua, Self>>(fields: &mut F) { fields.add_field_function_get("visible", |_, _| Ok(cfg!().visible)); fields.add_field_function_set("visible", |_, _, visible: bool| { - cfg_mut!().visible = visible; + let mut state = GlobalState::instance_mut(); + state.editor.visible = visible; + + if let Window::Editor = state.active_window { + if !visible { + state.set_focus(Window::View); + } + } + Ok(()) }); - fields.add_field_function_get("bar", |_, _| Ok(DUMMY_STATE.editor.bar)); - fields.add_field_function_get("content", |_, _| Ok(cfg!().buffer.as_string())); fields.add_field_function_set("content", |_, _, content: String| { cfg_mut!().buffer.set_lines_from_string(content); Ok(()) }); + fields.add_field_function_get("cursor_shape", |_, _| Ok(cfg!().cursor_shape)); + fields.add_field_function_set("cursor_shape", |_, _, shape: CursorShape| { + cfg_mut!().cursor_shape = shape; + Ok(()) + }); + + fields.add_field_function_get("lines", |lua, _| { let table = lua.create_table()?; @@ -80,6 +96,8 @@ impl UserData for EditorState { cfg_mut!().buffer.set_lines(vec); Ok(()) }); + + fields.add_field_function_get("bar", |_, _| Ok(DUMMY_STATE.editor.bar)); } fn add_methods<'lua, M: mlua::prelude::LuaUserDataMethods<'lua, Self>>(methods: &mut M) { -- cgit v1.2.3-70-g09d2