Refactor mode indicator to remove itself

One of the problems we had is that the status_bar shows a gap between
items, and we want to not add an additional gap for an invisible status
indicator.
This commit is contained in:
Conrad Irwin 2023-07-24 09:37:48 -06:00
parent d14a484a20
commit 43d94e37ec
7 changed files with 142 additions and 41 deletions

View file

@ -1,30 +1,18 @@
use gpui::{
elements::{Empty, Label},
AnyElement, Element, Entity, View, ViewContext,
};
use gpui::{elements::Label, AnyElement, Element, Entity, View, ViewContext};
use workspace::{item::ItemHandle, StatusItemView};
use crate::{state::Mode, Vim};
use crate::state::Mode;
pub struct ModeIndicator {
mode: Option<Mode>,
pub mode: Mode,
}
impl ModeIndicator {
pub fn new(cx: &mut ViewContext<Self>) -> Self {
cx.observe_global::<Vim, _>(|this, cx| {
let vim = Vim::read(cx);
if vim.enabled {
this.set_mode(Some(Vim::read(cx).state.mode), cx)
} else {
this.set_mode(None, cx)
}
})
.detach();
Self { mode: None }
pub fn new(mode: Mode) -> Self {
Self { mode }
}
pub fn set_mode(&mut self, mode: Option<Mode>, cx: &mut ViewContext<Self>) {
pub fn set_mode(&mut self, mode: Mode, cx: &mut ViewContext<Self>) {
if mode != self.mode {
self.mode = mode;
cx.notify();
@ -38,22 +26,21 @@ impl Entity for ModeIndicator {
impl View for ModeIndicator {
fn ui_name() -> &'static str {
"ModeIndicator"
"ModeIndicatorView"
}
fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
if let Some(mode) = self.mode {
let theme = &theme::current(cx).workspace.status_bar;
let text = match mode {
Mode::Normal => "",
Mode::Insert => "--- INSERT ---",
Mode::Visual { line: false } => "--- VISUAL ---",
Mode::Visual { line: true } => "--- VISUAL LINE ---",
};
Label::new(text, theme.vim_mode.clone()).into_any()
} else {
Empty::new().into_any()
}
let theme = &theme::current(cx).workspace.status_bar;
// we always choose text to be 12 monospace characters
// so that as the mode indicator changes, the rest of the
// UI stays still.
let text = match self.mode {
Mode::Normal => "-- NORMAL --",
Mode::Insert => "-- INSERT --",
Mode::Visual { line: false } => "-- VISUAL --",
Mode::Visual { line: true } => "VISUAL LINE ",
};
Label::new(text, theme.vim_mode_indicator.clone()).into_any()
}
}