From 76806e0c1e76b11ec66f646e494230015258ef55 Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Wed, 23 Jun 2021 22:30:10 -0700 Subject: [PATCH] reverse the dependency between helix-tui and helix-view by moving a fiew types to view --- Cargo.lock | 3 +- helix-term/Cargo.toml | 4 +- helix-term/src/application.rs | 8 +- helix-term/src/compositor.rs | 3 +- helix-term/src/ui/completion.rs | 11 +- helix-term/src/ui/editor.rs | 9 +- helix-term/src/ui/markdown.rs | 7 +- helix-term/src/ui/menu.rs | 7 +- helix-term/src/ui/mod.rs | 8 +- helix-term/src/ui/picker.rs | 10 +- helix-term/src/ui/popup.rs | 11 +- helix-term/src/ui/prompt.rs | 16 +- helix-term/src/ui/text.rs | 11 +- helix-tui/Cargo.toml | 1 + helix-tui/src/backend/crossterm.rs | 30 +- helix-tui/src/backend/mod.rs | 4 +- helix-tui/src/backend/test.rs | 3 +- helix-tui/src/buffer.rs | 8 +- helix-tui/src/layout.rs | 164 +--- helix-tui/src/lib.rs | 1 - helix-tui/src/terminal.rs | 16 +- helix-tui/src/text.rs | 2 +- helix-tui/src/widgets/block.rs | 3 +- helix-tui/src/widgets/mod.rs | 4 +- helix-tui/src/widgets/paragraph.rs | 4 +- helix-tui/src/widgets/table.rs | 4 +- helix-view/Cargo.toml | 10 +- helix-view/src/editor.rs | 9 +- .../style.rs => helix-view/src/graphics.rs | 764 +++++++++++------- helix-view/src/lib.rs | 1 + helix-view/src/theme.rs | 81 +- helix-view/src/tree.rs | 6 +- helix-view/src/view.rs | 6 +- 33 files changed, 576 insertions(+), 653 deletions(-) rename helix-tui/src/style.rs => helix-view/src/graphics.rs (56%) diff --git a/Cargo.lock b/Cargo.lock index 2a90b539cc15d..d1e32ad894188 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -371,6 +371,7 @@ dependencies = [ "cassowary", "crossterm", "helix-core", + "helix-view", "serde", "unicode-segmentation", "unicode-width", @@ -381,13 +382,13 @@ name = "helix-view" version = "0.2.0" dependencies = [ "anyhow", + "bitflags", "chardetng", "crossterm", "encoding_rs", "futures-util", "helix-core", "helix-lsp", - "helix-tui", "log", "once_cell", "serde", diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml index 385af64c28630..2334c34af3af4 100644 --- a/helix-term/Cargo.toml +++ b/helix-term/Cargo.toml @@ -23,8 +23,8 @@ path = "src/main.rs" [dependencies] helix-core = { version = "0.2", path = "../helix-core" } -helix-view = { version = "0.2", path = "../helix-view", features = ["term"]} -helix-lsp = { version = "0.2", path = "../helix-lsp"} +helix-view = { version = "0.2", path = "../helix-view" } +helix-lsp = { version = "0.2", path = "../helix-lsp" } anyhow = "1" once_cell = "1.8" diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 6f2f824be642d..dc89e876b2721 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -1,6 +1,10 @@ use helix_core::syntax; use helix_lsp::{lsp, LspProgressMap}; -use helix_view::{document::Mode, theme, Document, Editor, Theme, View}; +use helix_view::{ + document::Mode, + graphics::Rect, + theme, Document, Editor, Theme, View +}; use crate::{ args::Args, @@ -29,8 +33,6 @@ use crossterm::{ execute, terminal, }; -use tui::layout::Rect; - use futures_util::{future, stream::FuturesUnordered}; type BoxFuture = Pin + Send>>; diff --git a/helix-term/src/compositor.rs b/helix-term/src/compositor.rs index b04d4588b0fb8..cd0a12b5ad0e9 100644 --- a/helix-term/src/compositor.rs +++ b/helix-term/src/compositor.rs @@ -3,9 +3,10 @@ // cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), ) use helix_core::Position; use helix_lsp::LspProgressMap; +use helix_view::graphics::{CursorKind, Rect}; use crossterm::event::Event; -use tui::{buffer::Buffer as Surface, layout::Rect, terminal::CursorKind}; +use tui::buffer::Buffer as Surface; pub type Callback = Box; diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs index 8554361042152..2ab2b4b5daa03 100644 --- a/helix-term/src/ui/completion.rs +++ b/helix-term/src/ui/completion.rs @@ -1,15 +1,14 @@ use crate::compositor::{Component, Compositor, Context, EventResult}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; -use tui::{ - buffer::Buffer as Surface, - layout::Rect, - style::{Color, Style}, -}; +use tui::buffer::Buffer as Surface; use std::borrow::Cow; use helix_core::{Position, Transaction}; -use helix_view::Editor; +use helix_view::{ + graphics::{Rect, Color, Style}, + Editor +}; use crate::commands; use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent}; diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index c4322de0d5557..b44436b2c35b3 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -12,7 +12,11 @@ use helix_core::{ LineEnding, Position, Range, }; use helix_lsp::LspProgressMap; -use helix_view::{document::Mode, Document, Editor, Theme, View}; +use helix_view::{ + document::Mode, + graphics::{CursorKind, Rect, Color, Modifier, Style}, + Document, Editor, Theme, View +}; use std::borrow::Cow; use crossterm::{ @@ -22,9 +26,6 @@ use crossterm::{ use tui::{ backend::CrosstermBackend, buffer::Buffer as Surface, - layout::Rect, - style::{Color, Modifier, Style}, - terminal::CursorKind, }; pub struct EditorView { diff --git a/helix-term/src/ui/markdown.rs b/helix-term/src/ui/markdown.rs index 72a3e4ff8b672..4256b082790bb 100644 --- a/helix-term/src/ui/markdown.rs +++ b/helix-term/src/ui/markdown.rs @@ -2,15 +2,16 @@ use crate::compositor::{Component, Compositor, Context, EventResult}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use tui::{ buffer::Buffer as Surface, - layout::Rect, - style::{Color, Style}, text::Text, }; use std::{borrow::Cow, sync::Arc}; use helix_core::{syntax, Position}; -use helix_view::{Editor, Theme}; +use helix_view::{ + graphics::{Rect, Color, Style}, + Editor, Theme +}; pub struct Markdown { contents: String, diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs index 0a264f7c64c8f..eaca73c2b8a12 100644 --- a/helix-term/src/ui/menu.rs +++ b/helix-term/src/ui/menu.rs @@ -2,8 +2,6 @@ use crate::compositor::{Component, Compositor, Context, EventResult}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use tui::{ buffer::Buffer as Surface, - layout::Rect, - style::{Color, Style}, widgets::Table, }; @@ -15,7 +13,10 @@ use fuzzy_matcher::skim::SkimMatcherV2 as Matcher; use fuzzy_matcher::FuzzyMatcher; use helix_core::Position; -use helix_view::Editor; +use helix_view::{ + graphics::{Rect, Color, Style}, + Editor, +}; pub trait Item { // TODO: sort_text diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 29d555ac132f1..4a66c030c5839 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -18,12 +18,12 @@ pub use prompt::{Prompt, PromptEvent}; pub use spinner::{ProgressSpinners, Spinner}; pub use text::Text; -pub use tui::layout::Rect; -pub use tui::style::{Color, Modifier, Style}; - use helix_core::regex::Regex; use helix_core::register::Registers; -use helix_view::{Document, Editor, View}; +use helix_view::{ + graphics::{Rect, Color, Modifier, Style}, + Document, Editor, View +}; use std::path::{Path, PathBuf}; diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 82543bc5e864c..507110eb46617 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -2,8 +2,6 @@ use crate::compositor::{Component, Compositor, Context, EventResult}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use tui::{ buffer::Buffer as Surface, - layout::Rect, - style::{Color, Style}, widgets::{Block, BorderType, Borders}, }; @@ -14,9 +12,11 @@ use std::borrow::Cow; use crate::ui::{Prompt, PromptEvent}; use helix_core::Position; -use helix_view::editor::Action; -use helix_view::Editor; -use tui::terminal::CursorKind; +use helix_view::{ + editor::Action, + graphics::{CursorKind, Rect, Color, Style}, + Editor +}; pub struct Picker { options: Vec, diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index 8488d1c6f32b8..aba715ae1e633 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -1,15 +1,14 @@ use crate::compositor::{Component, Compositor, Context, EventResult}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; -use tui::{ - buffer::Buffer as Surface, - layout::Rect, - style::{Color, Style}, -}; +use tui::buffer::Buffer as Surface; use std::borrow::Cow; use helix_core::Position; -use helix_view::Editor; +use helix_view::{ + graphics::{Rect, Color, Style}, + Editor +}; // TODO: share logic with Menu, it's essentially Popup(render_fn), but render fn needs to return // a width/height hint. maybe Popup(Box) diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index 63078c3997b6e..fa61036773cce 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -1,15 +1,18 @@ use crate::compositor::{Component, Compositor, Context, EventResult}; use crate::ui; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; -use helix_core::Position; -use helix_view::{Editor, Theme}; use std::{borrow::Cow, ops::RangeFrom}; -use tui::terminal::CursorKind; +use tui::buffer::Buffer as Surface; use helix_core::{ + Position, unicode::segmentation::{GraphemeCursor, GraphemeIncomplete}, unicode::width::UnicodeWidthStr, }; +use helix_view::{ + graphics::{CursorKind, Margin, Rect, Color, Modifier, Style}, + Editor, Theme +}; pub type Completion = (RangeFrom, Cow<'static, str>); @@ -251,12 +254,6 @@ impl Prompt { } } -use tui::{ - buffer::Buffer as Surface, - layout::Rect, - style::{Color, Modifier, Style}, -}; - const BASE_WIDTH: u16 = 30; impl Prompt { @@ -343,7 +340,6 @@ impl Prompt { let background = theme.get("ui.help"); surface.clear_with(area, background); - use tui::layout::Margin; text.render( area.inner(&Margin { vertical: 1, diff --git a/helix-term/src/ui/text.rs b/helix-term/src/ui/text.rs index 6225e769004ff..5222e62f8fbd8 100644 --- a/helix-term/src/ui/text.rs +++ b/helix-term/src/ui/text.rs @@ -1,15 +1,14 @@ use crate::compositor::{Component, Compositor, Context, EventResult}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; -use tui::{ - buffer::Buffer as Surface, - layout::Rect, - style::{Color, Style}, -}; +use tui::buffer::Buffer as Surface; use std::borrow::Cow; use helix_core::Position; -use helix_view::Editor; +use helix_view::{ + graphics::{Rect, Color, Style}, + Editor +}; pub struct Text { contents: String, diff --git a/helix-tui/Cargo.toml b/helix-tui/Cargo.toml index 30e2374dd061f..bd0c269769535 100644 --- a/helix-tui/Cargo.toml +++ b/helix-tui/Cargo.toml @@ -22,4 +22,5 @@ unicode-segmentation = "1.2" unicode-width = "0.1" crossterm = { version = "0.20", optional = true } serde = { version = "1", "optional" = true, features = ["derive"]} +helix-view = { path = "../helix-view", features = ["term"] } helix-core = { version = "0.2", path = "../helix-core" } diff --git a/helix-tui/src/backend/crossterm.rs b/helix-tui/src/backend/crossterm.rs index 189f9dce2950a..673d5cd050b3c 100644 --- a/helix-tui/src/backend/crossterm.rs +++ b/helix-tui/src/backend/crossterm.rs @@ -1,9 +1,6 @@ use crate::{ backend::Backend, buffer::Cell, - layout::Rect, - style::{Color, Modifier}, - terminal::CursorKind, }; use crossterm::{ cursor::{CursorShape, Hide, MoveTo, SetCursorShape, Show}, @@ -15,6 +12,7 @@ use crossterm::{ terminal::{self, Clear, ClearType}, }; use std::io::{self, Write}; +use helix_view::graphics::{CursorKind, Rect, Color, Modifier}; pub struct CrosstermBackend { buffer: W, @@ -133,32 +131,6 @@ fn map_error(error: crossterm::Result<()>) -> io::Result<()> { error.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string())) } -impl From for CColor { - fn from(color: Color) -> Self { - match color { - Color::Reset => CColor::Reset, - Color::Black => CColor::Black, - Color::Red => CColor::DarkRed, - Color::Green => CColor::DarkGreen, - Color::Yellow => CColor::DarkYellow, - Color::Blue => CColor::DarkBlue, - Color::Magenta => CColor::DarkMagenta, - Color::Cyan => CColor::DarkCyan, - Color::Gray => CColor::Grey, - Color::DarkGray => CColor::DarkGrey, - Color::LightRed => CColor::Red, - Color::LightGreen => CColor::Green, - Color::LightBlue => CColor::Blue, - Color::LightYellow => CColor::Yellow, - Color::LightMagenta => CColor::Magenta, - Color::LightCyan => CColor::Cyan, - Color::White => CColor::White, - Color::Indexed(i) => CColor::AnsiValue(i), - Color::Rgb(r, g, b) => CColor::Rgb { r, g, b }, - } - } -} - #[derive(Debug)] struct ModifierDiff { pub from: Modifier, diff --git a/helix-tui/src/backend/mod.rs b/helix-tui/src/backend/mod.rs index 5cf2176829b61..c6c11019de8c8 100644 --- a/helix-tui/src/backend/mod.rs +++ b/helix-tui/src/backend/mod.rs @@ -1,8 +1,8 @@ use std::io; use crate::buffer::Cell; -use crate::layout::Rect; -use crate::terminal::CursorKind; + +use helix_view::graphics::{CursorKind, Rect}; #[cfg(feature = "crossterm")] mod crossterm; diff --git a/helix-tui/src/backend/test.rs b/helix-tui/src/backend/test.rs index 4681831d11cab..b9c37cc0ecec4 100644 --- a/helix-tui/src/backend/test.rs +++ b/helix-tui/src/backend/test.rs @@ -1,11 +1,10 @@ use crate::{ backend::Backend, buffer::{Buffer, Cell}, - layout::Rect, - terminal::CursorKind, }; use std::{fmt::Write, io}; use unicode_width::UnicodeWidthStr; +use helix_view::graphics::{CursorKind, Rect}; /// A backend used for the integration tests. #[derive(Debug)] diff --git a/helix-tui/src/buffer.rs b/helix-tui/src/buffer.rs index 0d1edc46d78a4..3d9b933a11d96 100644 --- a/helix-tui/src/buffer.rs +++ b/helix-tui/src/buffer.rs @@ -1,12 +1,10 @@ -use crate::{ - layout::Rect, - style::{Color, Modifier, Style}, - text::{Span, Spans}, -}; +use crate::text::{Span, Spans}; use std::cmp::min; use unicode_segmentation::UnicodeSegmentation; use unicode_width::UnicodeWidthStr; +use helix_view::graphics::{Rect, Color, Modifier, Style}; + /// A buffer cell #[derive(Debug, Clone, PartialEq)] pub struct Cell { diff --git a/helix-tui/src/layout.rs b/helix-tui/src/layout.rs index 5248f7bf9fe67..d7088e810fa5c 100644 --- a/helix-tui/src/layout.rs +++ b/helix-tui/src/layout.rs @@ -1,11 +1,12 @@ use std::cell::RefCell; -use std::cmp::{max, min}; use std::collections::HashMap; use cassowary::strength::{REQUIRED, WEAK}; use cassowary::WeightedRelation::*; use cassowary::{Constraint as CassowaryConstraint, Expression, Solver, Variable}; +use helix_view::graphics::{Margin, Rect}; + #[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)] pub enum Corner { TopLeft, @@ -45,12 +46,6 @@ impl Constraint { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Margin { - pub vertical: u16, - pub horizontal: u16, -} - #[derive(Debug, Clone, Copy, PartialEq)] pub enum Alignment { Left, @@ -348,117 +343,6 @@ impl Element { } } -/// A simple rectangle used in the computation of the layout and to give widgets an hint about the -/// area they are supposed to render to. -#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] -pub struct Rect { - pub x: u16, - pub y: u16, - pub width: u16, - pub height: u16, -} - -impl Default for Rect { - fn default() -> Rect { - Rect { - x: 0, - y: 0, - width: 0, - height: 0, - } - } -} - -impl Rect { - /// Creates a new rect, with width and height limited to keep the area under max u16. - /// If clipped, aspect ratio will be preserved. - pub fn new(x: u16, y: u16, width: u16, height: u16) -> Rect { - let max_area = u16::max_value(); - let (clipped_width, clipped_height) = - if u32::from(width) * u32::from(height) > u32::from(max_area) { - let aspect_ratio = f64::from(width) / f64::from(height); - let max_area_f = f64::from(max_area); - let height_f = (max_area_f / aspect_ratio).sqrt(); - let width_f = height_f * aspect_ratio; - (width_f as u16, height_f as u16) - } else { - (width, height) - }; - Rect { - x, - y, - width: clipped_width, - height: clipped_height, - } - } - - pub fn area(self) -> u16 { - self.width * self.height - } - - pub fn left(self) -> u16 { - self.x - } - - pub fn right(self) -> u16 { - self.x.saturating_add(self.width) - } - - pub fn top(self) -> u16 { - self.y - } - - pub fn bottom(self) -> u16 { - self.y.saturating_add(self.height) - } - - pub fn inner(self, margin: &Margin) -> Rect { - if self.width < 2 * margin.horizontal || self.height < 2 * margin.vertical { - Rect::default() - } else { - Rect { - x: self.x + margin.horizontal, - y: self.y + margin.vertical, - width: self.width - 2 * margin.horizontal, - height: self.height - 2 * margin.vertical, - } - } - } - - pub fn union(self, other: Rect) -> Rect { - let x1 = min(self.x, other.x); - let y1 = min(self.y, other.y); - let x2 = max(self.x + self.width, other.x + other.width); - let y2 = max(self.y + self.height, other.y + other.height); - Rect { - x: x1, - y: y1, - width: x2 - x1, - height: y2 - y1, - } - } - - pub fn intersection(self, other: Rect) -> Rect { - let x1 = max(self.x, other.x); - let y1 = max(self.y, other.y); - let x2 = min(self.x + self.width, other.x + other.width); - let y2 = min(self.y + self.height, other.y + other.height); - Rect { - x: x1, - y: y1, - width: x2 - x1, - height: y2 - y1, - } - } - - pub fn intersects(self, other: Rect) -> bool { - self.x < other.x + other.width - && self.x + self.width > other.x - && self.y < other.y + other.height - && self.y + self.height > other.y - } -} - #[cfg(test)] mod tests { use super::*; @@ -487,48 +371,4 @@ mod tests { assert_eq!(target.height, chunks.iter().map(|r| r.height).sum::()); chunks.windows(2).for_each(|w| assert!(w[0].y <= w[1].y)); } - - #[test] - fn test_rect_size_truncation() { - for width in 256u16..300u16 { - for height in 256u16..300u16 { - let rect = Rect::new(0, 0, width, height); - rect.area(); // Should not panic. - assert!(rect.width < width || rect.height < height); - // The target dimensions are rounded down so the math will not be too precise - // but let's make sure the ratios don't diverge crazily. - assert!( - (f64::from(rect.width) / f64::from(rect.height) - - f64::from(width) / f64::from(height)) - .abs() - < 1.0 - ) - } - } - - // One dimension below 255, one above. Area above max u16. - let width = 900; - let height = 100; - let rect = Rect::new(0, 0, width, height); - assert_ne!(rect.width, 900); - assert_ne!(rect.height, 100); - assert!(rect.width < width || rect.height < height); - } - - #[test] - fn test_rect_size_preservation() { - for width in 0..256u16 { - for height in 0..256u16 { - let rect = Rect::new(0, 0, width, height); - rect.area(); // Should not panic. - assert_eq!(rect.width, width); - assert_eq!(rect.height, height); - } - } - - // One dimension below 255, one above. Area below max u16. - let rect = Rect::new(0, 0, 300, 100); - assert_eq!(rect.width, 300); - assert_eq!(rect.height, 100); - } } diff --git a/helix-tui/src/lib.rs b/helix-tui/src/lib.rs index 05263bc85b4bc..2636b268f02e4 100644 --- a/helix-tui/src/lib.rs +++ b/helix-tui/src/lib.rs @@ -125,7 +125,6 @@ pub mod backend; pub mod buffer; pub mod layout; -pub mod style; pub mod symbols; pub mod terminal; pub mod text; diff --git a/helix-tui/src/terminal.rs b/helix-tui/src/terminal.rs index b9b2437a166f0..df729d5becf49 100644 --- a/helix-tui/src/terminal.rs +++ b/helix-tui/src/terminal.rs @@ -1,5 +1,6 @@ -use crate::{backend::Backend, buffer::Buffer, layout::Rect}; +use crate::{backend::Backend, buffer::Buffer}; use std::io; +use helix_view::graphics::{CursorKind, Rect}; #[derive(Debug, Clone, PartialEq)] /// UNSTABLE @@ -8,19 +9,6 @@ enum ResizeBehavior { Auto, } -#[derive(Debug)] -/// UNSTABLE -pub enum CursorKind { - /// █ - Block, - /// | - Bar, - /// _ - Underline, - /// Hidden cursor, can set cursor position with this to let IME have correct cursor position. - Hidden, -} - #[derive(Debug, Clone, PartialEq)] /// UNSTABLE pub struct Viewport { diff --git a/helix-tui/src/text.rs b/helix-tui/src/text.rs index b23bfd81d6989..5b2b6e91f63e6 100644 --- a/helix-tui/src/text.rs +++ b/helix-tui/src/text.rs @@ -46,8 +46,8 @@ //! Span::raw(" title"), //! ]); //! ``` -use crate::style::Style; use helix_core::line_ending::str_is_line_ending; +use helix_view::graphics::Style; use std::borrow::Cow; use unicode_segmentation::UnicodeSegmentation; use unicode_width::UnicodeWidthStr; diff --git a/helix-tui/src/widgets/block.rs b/helix-tui/src/widgets/block.rs index 2569c17dc2fbf..fca92d0a001fa 100644 --- a/helix-tui/src/widgets/block.rs +++ b/helix-tui/src/widgets/block.rs @@ -1,11 +1,10 @@ use crate::{ buffer::Buffer, - layout::Rect, - style::Style, symbols::line, text::{Span, Spans}, widgets::{Borders, Widget}, }; +use helix_view::graphics::{Rect, Style}; #[derive(Debug, Clone, Copy, PartialEq)] pub enum BorderType { diff --git a/helix-tui/src/widgets/mod.rs b/helix-tui/src/widgets/mod.rs index 484ad50e64a0c..e5608a79e8039 100644 --- a/helix-tui/src/widgets/mod.rs +++ b/helix-tui/src/widgets/mod.rs @@ -20,9 +20,11 @@ pub use self::block::{Block, BorderType}; pub use self::paragraph::{Paragraph, Wrap}; pub use self::table::{Cell, Row, Table, TableState}; -use crate::{buffer::Buffer, layout::Rect}; +use crate::buffer::Buffer; use bitflags::bitflags; +use helix_view::graphics::Rect; + bitflags! { /// Bitflags that can be composed to set the visible borders essentially on the block widget. pub struct Borders: u32 { diff --git a/helix-tui/src/widgets/paragraph.rs b/helix-tui/src/widgets/paragraph.rs index ecce85816040f..cf48e29255f8c 100644 --- a/helix-tui/src/widgets/paragraph.rs +++ b/helix-tui/src/widgets/paragraph.rs @@ -1,7 +1,6 @@ use crate::{ buffer::Buffer, - layout::{Alignment, Rect}, - style::Style, + layout::Alignment, text::{StyledGrapheme, Text}, widgets::{ reflow::{LineComposer, LineTruncator, WordWrapper}, @@ -10,6 +9,7 @@ use crate::{ }; use std::iter; use unicode_width::UnicodeWidthStr; +use helix_view::graphics::{Rect, Style}; fn get_line_offset(line_width: u16, text_area_width: u16, alignment: Alignment) -> u16 { match alignment { diff --git a/helix-tui/src/widgets/table.rs b/helix-tui/src/widgets/table.rs index d42d7d304c643..f3894bef52eca 100644 --- a/helix-tui/src/widgets/table.rs +++ b/helix-tui/src/widgets/table.rs @@ -1,7 +1,6 @@ use crate::{ buffer::Buffer, - layout::{Constraint, Rect}, - style::Style, + layout::Constraint, text::Text, widgets::{Block, Widget}, }; @@ -15,6 +14,7 @@ use std::{ iter::{self, Iterator}, }; use unicode_width::UnicodeWidthStr; +use helix_view::graphics::{Rect, Style}; /// A [`Cell`] contains the [`Text`] to be displayed in a [`Row`] of a [`Table`]. /// diff --git a/helix-view/Cargo.toml b/helix-view/Cargo.toml index cadbbdbd70eab..93dc29d54affb 100644 --- a/helix-view/Cargo.toml +++ b/helix-view/Cargo.toml @@ -9,20 +9,18 @@ categories = ["editor"] repository = "https://github.com/helix-editor/helix" homepage = "https://helix-editor.com" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [features] -term = ["tui", "crossterm"] -default = ["term"] +default = [] +term = ["crossterm"] [dependencies] +bitflags = "1.0" anyhow = "1" helix-core = { version = "0.2", path = "../helix-core" } helix-lsp = { version = "0.2", path = "../helix-lsp"} +crossterm = { version = "0.20", optional = true } # Conversion traits -tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"], optional = true } -crossterm = { version = "0.20", features = ["event-stream"], optional = true } once_cell = "1.8" url = "2" diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 7f910b8098213..ff61d1818b60b 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1,12 +1,10 @@ -use crate::clipboard::{get_clipboard_provider, ClipboardProvider}; use crate::{ + clipboard::{get_clipboard_provider, ClipboardProvider}, + graphics::{Rect, CursorKind}, theme::{self, Theme}, tree::Tree, Document, DocumentId, RegisterSelection, View, ViewId, }; -use helix_core::syntax; -use tui::layout::Rect; -use tui::terminal::CursorKind; use futures_util::future; use std::{path::PathBuf, sync::Arc, time::Duration}; @@ -18,6 +16,7 @@ use anyhow::Error; pub use helix_core::diagnostic::Severity; pub use helix_core::register::Registers; use helix_core::Position; +use helix_core::syntax; #[derive(Debug)] pub struct Editor { @@ -45,7 +44,7 @@ pub enum Action { impl Editor { pub fn new( - mut area: tui::layout::Rect, + mut area: Rect, themes: Arc, config_loader: Arc, ) -> Self { diff --git a/helix-tui/src/style.rs b/helix-view/src/graphics.rs similarity index 56% rename from helix-tui/src/style.rs rename to helix-view/src/graphics.rs index f322d304d54b3..bfc63a63d719b 100644 --- a/helix-tui/src/style.rs +++ b/helix-view/src/graphics.rs @@ -1,281 +1,483 @@ -//! `style` contains the primitives used to control how your user interface will look. - -use bitflags::bitflags; - -#[derive(Debug, Clone, Copy, PartialEq)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub enum Color { - Reset, - Black, - Red, - Green, - Yellow, - Blue, - Magenta, - Cyan, - Gray, - DarkGray, - LightRed, - LightGreen, - LightYellow, - LightBlue, - LightMagenta, - LightCyan, - White, - Rgb(u8, u8, u8), - Indexed(u8), -} - -bitflags! { - /// Modifier changes the way a piece of text is displayed. - /// - /// They are bitflags so they can easily be composed. - /// - /// ## Examples - /// - /// ```rust - /// # use helix_tui::style::Modifier; - /// - /// let m = Modifier::BOLD | Modifier::ITALIC; - /// ``` - #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] - pub struct Modifier: u16 { - const BOLD = 0b0000_0000_0001; - const DIM = 0b0000_0000_0010; - const ITALIC = 0b0000_0000_0100; - const UNDERLINED = 0b0000_0000_1000; - const SLOW_BLINK = 0b0000_0001_0000; - const RAPID_BLINK = 0b0000_0010_0000; - const REVERSED = 0b0000_0100_0000; - const HIDDEN = 0b0000_1000_0000; - const CROSSED_OUT = 0b0001_0000_0000; - } -} - -/// Style let you control the main characteristics of the displayed elements. -/// -/// ```rust -/// # use helix_tui::style::{Color, Modifier, Style}; -/// Style::default() -/// .fg(Color::Black) -/// .bg(Color::Green) -/// .add_modifier(Modifier::ITALIC | Modifier::BOLD); -/// ``` -/// -/// It represents an incremental change. If you apply the styles S1, S2, S3 to a cell of the -/// terminal buffer, the style of this cell will be the result of the merge of S1, S2 and S3, not -/// just S3. -/// -/// ```rust -/// # use helix_tui::style::{Color, Modifier, Style}; -/// # use helix_tui::buffer::Buffer; -/// # use helix_tui::layout::Rect; -/// let styles = [ -/// Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD | Modifier::ITALIC), -/// Style::default().bg(Color::Red), -/// Style::default().fg(Color::Yellow).remove_modifier(Modifier::ITALIC), -/// ]; -/// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1)); -/// for style in &styles { -/// buffer.get_mut(0, 0).set_style(*style); -/// } -/// assert_eq!( -/// Style { -/// fg: Some(Color::Yellow), -/// bg: Some(Color::Red), -/// add_modifier: Modifier::BOLD, -/// sub_modifier: Modifier::empty(), -/// }, -/// buffer.get(0, 0).style(), -/// ); -/// ``` -/// -/// The default implementation returns a `Style` that does not modify anything. If you wish to -/// reset all properties until that point use [`Style::reset`]. -/// -/// ``` -/// # use helix_tui::style::{Color, Modifier, Style}; -/// # use helix_tui::buffer::Buffer; -/// # use helix_tui::layout::Rect; -/// let styles = [ -/// Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD | Modifier::ITALIC), -/// Style::reset().fg(Color::Yellow), -/// ]; -/// let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 1)); -/// for style in &styles { -/// buffer.get_mut(0, 0).set_style(*style); -/// } -/// assert_eq!( -/// Style { -/// fg: Some(Color::Yellow), -/// bg: Some(Color::Reset), -/// add_modifier: Modifier::empty(), -/// sub_modifier: Modifier::empty(), -/// }, -/// buffer.get(0, 0).style(), -/// ); -/// ``` -#[derive(Debug, Clone, Copy, PartialEq)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct Style { - pub fg: Option, - pub bg: Option, - pub add_modifier: Modifier, - pub sub_modifier: Modifier, -} - -impl Default for Style { - fn default() -> Style { - Style { - fg: None, - bg: None, - add_modifier: Modifier::empty(), - sub_modifier: Modifier::empty(), - } - } -} - -impl Style { - /// Returns a `Style` resetting all properties. - pub fn reset() -> Style { - Style { - fg: Some(Color::Reset), - bg: Some(Color::Reset), - add_modifier: Modifier::empty(), - sub_modifier: Modifier::all(), - } - } - - /// Changes the foreground color. - /// - /// ## Examples - /// - /// ```rust - /// # use helix_tui::style::{Color, Style}; - /// let style = Style::default().fg(Color::Blue); - /// let diff = Style::default().fg(Color::Red); - /// assert_eq!(style.patch(diff), Style::default().fg(Color::Red)); - /// ``` - pub fn fg(mut self, color: Color) -> Style { - self.fg = Some(color); - self - } - - /// Changes the background color. - /// - /// ## Examples - /// - /// ```rust - /// # use helix_tui::style::{Color, Style}; - /// let style = Style::default().bg(Color::Blue); - /// let diff = Style::default().bg(Color::Red); - /// assert_eq!(style.patch(diff), Style::default().bg(Color::Red)); - /// ``` - pub fn bg(mut self, color: Color) -> Style { - self.bg = Some(color); - self - } - - /// Changes the text emphasis. - /// - /// When applied, it adds the given modifier to the `Style` modifiers. - /// - /// ## Examples - /// - /// ```rust - /// # use helix_tui::style::{Color, Modifier, Style}; - /// let style = Style::default().add_modifier(Modifier::BOLD); - /// let diff = Style::default().add_modifier(Modifier::ITALIC); - /// let patched = style.patch(diff); - /// assert_eq!(patched.add_modifier, Modifier::BOLD | Modifier::ITALIC); - /// assert_eq!(patched.sub_modifier, Modifier::empty()); - /// ``` - pub fn add_modifier(mut self, modifier: Modifier) -> Style { - self.sub_modifier.remove(modifier); - self.add_modifier.insert(modifier); - self - } - - /// Changes the text emphasis. - /// - /// When applied, it removes the given modifier from the `Style` modifiers. - /// - /// ## Examples - /// - /// ```rust - /// # use helix_tui::style::{Color, Modifier, Style}; - /// let style = Style::default().add_modifier(Modifier::BOLD | Modifier::ITALIC); - /// let diff = Style::default().remove_modifier(Modifier::ITALIC); - /// let patched = style.patch(diff); - /// assert_eq!(patched.add_modifier, Modifier::BOLD); - /// assert_eq!(patched.sub_modifier, Modifier::ITALIC); - /// ``` - pub fn remove_modifier(mut self, modifier: Modifier) -> Style { - self.add_modifier.remove(modifier); - self.sub_modifier.insert(modifier); - self - } - - /// Results in a combined style that is equivalent to applying the two individual styles to - /// a style one after the other. - /// - /// ## Examples - /// ``` - /// # use helix_tui::style::{Color, Modifier, Style}; - /// let style_1 = Style::default().fg(Color::Yellow); - /// let style_2 = Style::default().bg(Color::Red); - /// let combined = style_1.patch(style_2); - /// assert_eq!( - /// Style::default().patch(style_1).patch(style_2), - /// Style::default().patch(combined)); - /// ``` - pub fn patch(mut self, other: Style) -> Style { - self.fg = other.fg.or(self.fg); - self.bg = other.bg.or(self.bg); - - self.add_modifier.remove(other.sub_modifier); - self.add_modifier.insert(other.add_modifier); - self.sub_modifier.remove(other.add_modifier); - self.sub_modifier.insert(other.sub_modifier); - - self - } -} - -#[cfg(test)] -mod tests { - use super::*; - - fn styles() -> Vec