From 922e4ebc0bede74a174b04c1fb1d34da72ac7ff4 Mon Sep 17 00:00:00 2001 From: MinusGix Date: Thu, 8 Feb 2024 02:15:06 -0600 Subject: [PATCH] Allow toggling editor gutter (#309) --- examples/editor/src/main.rs | 18 ++++++++++++++---- src/views/editor/mod.rs | 4 ++++ src/views/editor/view.rs | 31 +++++++++++++++---------------- src/views/text_editor.rs | 14 ++++++++++++++ 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 73eba8e8..14e507c8 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -23,24 +23,34 @@ fn app_view() -> impl View { } CommandExecuted::No }) + .gutter(false) .update(|_| { // This hooks up to both editors! println!("Editor changed"); }); let doc = editor_a.doc(); + let gutter_a = editor_a.editor().gutter; + let gutter_b = editor_b.editor().gutter; let view = stack(( editor_a, editor_b, - button(|| "Clear") - .on_click_stop(move |_| { + stack(( + button(|| "Clear").on_click_stop(move |_| { doc.edit_single( Selection::region(0, doc.text().len()), "", EditType::DeleteSelection, ); - }) - .style(|s| s.width_full()), + }), + button(|| "Flip Gutter").on_click_stop(move |_| { + let a = !gutter_a.get_untracked(); + let b = !gutter_b.get_untracked(); + gutter_a.set(a); + gutter_b.set(b); + }), + )) + .style(|s| s.width_full().flex_row().items_center().justify_center()), )) .style(|s| s.size_full().flex_col().items_center().justify_center()); diff --git a/src/views/editor/mod.rs b/src/views/editor/mod.rs index 3d63df1a..6e08cd10 100644 --- a/src/views/editor/mod.rs +++ b/src/views/editor/mod.rs @@ -74,6 +74,9 @@ pub struct Editor { pub active: RwSignal, + /// Whether the gutter is enabled + pub gutter: RwSignal, + /// Whether you can edit within this editor. pub read_only: RwSignal, /// Whether you can scroll beyond the last line of the document. @@ -193,6 +196,7 @@ impl Editor { effects_cx: Cell::new(cx.create_child()), id, active: cx.create_rw_signal(false), + gutter: cx.create_rw_signal(true), read_only: cx.create_rw_signal(false), scroll_beyond_last_line: cx.create_rw_signal(false), cursor_surrounding_lines: cx.create_rw_signal(1), diff --git a/src/views/editor/view.rs b/src/views/editor/view.rs index 10fb04b1..622cb719 100644 --- a/src/views/editor/view.rs +++ b/src/views/editor/view.rs @@ -1085,22 +1085,21 @@ pub fn editor_container_view( ) -> impl View { let editor_rect = create_rw_signal(Rect::ZERO); - stack(( - // editor_breadcrumbs(workspace, editor.get_untracked(), config), - container( - stack(( - editor_gutter(editor), - container(editor_content(editor, is_active, handle_key_event)) - .style(move |s| s.size_pct(100.0, 100.0)), - empty().style(move |s| s.absolute().width_pct(100.0)), - )) - .on_resize(move |rect| { - editor_rect.set(rect); - }) - .style(|s| s.absolute().size_pct(100.0, 100.0)), - ) - .style(|s| s.size_pct(100.0, 100.0)), - )) + stack((container( + stack(( + editor_gutter(editor).style(move |s| { + s.apply_if(editor.with_untracked(|ed| ed.gutter).get(), |s| s.hide()) + }), + container(editor_content(editor, is_active, handle_key_event)) + .style(move |s| s.size_pct(100.0, 100.0)), + empty().style(move |s| s.absolute().width_pct(100.0)), + )) + .on_resize(move |rect| { + editor_rect.set(rect); + }) + .style(|s| s.absolute().size_pct(100.0, 100.0)), + ) + .style(|s| s.size_pct(100.0, 100.0)),)) .on_cleanup(move || { // TODO: should we have some way for doc to tell us if we're allowed to cleanup the editor? let editor = editor.get_untracked(); diff --git a/src/views/text_editor.rs b/src/views/text_editor.rs index c33800bd..2232323c 100644 --- a/src/views/text_editor.rs +++ b/src/views/text_editor.rs @@ -66,6 +66,12 @@ impl View for TextEditor { } impl TextEditor { + /// Note: this requires that the document underlying it is a [`TextDocument`] for the use of + /// some logic. You should usually not swap this out without good reason. + pub fn editor(&self) -> &Editor { + &self.editor + } + /// Note: this requires that the document underlying it is a [`TextDocument`] for the use of /// some logic. You should usually not swap this out without good reason. pub fn with_editor(self, f: impl FnOnce(&Editor)) -> Self { @@ -166,6 +172,14 @@ impl TextEditor { self } + /// Enable or disable the gutter. + /// Equivalent to setting [`Editor::gutter`] + /// Default: `true` + pub fn gutter(self, gutter: bool) -> Self { + self.editor.gutter.set(gutter); + self + } + /// Allow scrolling beyond the last line of the document. /// Equivalent to setting [`Editor::scroll_beyond_last_line`] /// Default: `false`