Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic dimming functionality #2751

Closed
wants to merge 12 commits into from
Prev Previous commit
Next Next commit
simplifys implementation & removes palette dependency.
  • Loading branch information
farwyler committed Jul 2, 2022
commit 68d76a27303018f0a58eb83a69c1d49e5f99ba45
117 changes: 0 additions & 117 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions helix-term/src/ui/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ fn clip_rect_relative(rect: Rect, percent_horizontal: u8, percent_vertical: u8)

impl<T: Component + 'static> Component for Overlay<T> {
fn render(&mut self, area: Rect, frame: &mut Buffer, ctx: &mut Context) {
if let Some(op) = &ctx.editor.config().dim.overlay_backdrops {
if let Some(shade) = ctx.editor.config().dim.overlay_backdrops {
// TODO: optimise. we don't need to dim behind the child content
frame.dim(area, op);
frame.dim(area, shade);
}
let dimensions = (self.calc_child_size)(area);
self.content.render(dimensions, frame, ctx)
Expand Down
1 change: 0 additions & 1 deletion helix-tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ include = ["src/**/*", "README.md"]
default = ["crossterm"]

[dependencies]
palette = "0.6"
bitflags = "1.3"
cassowary = "0.3"
unicode-segmentation = "1.9"
Expand Down
52 changes: 24 additions & 28 deletions helix-tui/src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::text::{Span, Spans};
use helix_core::unicode::width::UnicodeWidthStr;
use helix_view::graphics::{Color, DimFunction, DimOperation, Modifier, Rect, Style};
use palette::Shade;
use helix_view::graphics::{Color, Modifier, Rect, Style};
use std::cmp::min;
use std::collections::HashMap;
use unicode_segmentation::UnicodeSegmentation;
Expand Down Expand Up @@ -585,26 +584,26 @@ impl Buffer {
updates
}

/// Apply DimOperation to all cells in area
pub fn dim(&mut self, area: Rect, op: &DimOperation) {
use DimFunction::*;

type Srgb8 = palette::rgb::Rgb<palette::encoding::Srgb, u8>;
/// Apply shade to all cells in area.
///
/// shade = 0 -> set text modifier DIM
/// shade < 0 -> darken rgb color
/// shade > 0 -> lighten rgb color
farwyler marked this conversation as resolved.
Show resolved Hide resolved
pub fn dim(&mut self, area: Rect, shade: i8) {
let shade_amount = shade.unsigned_abs().saturating_mul(2);
let shade_fn = if shade < 0 {
u8::saturating_sub
} else {
u8::saturating_add
};
// TODO: replace with saturating_add_signed when mixed_integer_ops (https://github.com/rust-lang/rust/issues/87840) get stabilized.
let shaded = |v| shade_fn(v, shade_amount);
farwyler marked this conversation as resolved.
Show resolved Hide resolved

let mut cache: HashMap<Color, Color> = HashMap::new();
let intensity = op.intensity as f32 / 100.0; // TODO: clamp

let mut modify = |col| {
let result = cache.entry(col).or_insert_with(|| {
if let Color::Rgb(r, g, b) = col {
let mut rgb = Srgb8::new(r, g, b).into_format().into_linear();
rgb = match op.func {
Lighten => rgb.lighten(intensity),
Darken => rgb.darken(intensity),
_ => unimplemented!(),
};
let rgb = Srgb8::from(rgb).into_components();
Color::Rgb(rgb.0, rgb.1, rgb.2)
Color::Rgb(shaded(r), shaded(g), shaded(b))
} else {
col
}
Expand All @@ -615,17 +614,14 @@ impl Buffer {
for x in area.left()..area.right() {
let cell = &mut self[(x, y)];

match op.func {
Dim => {
cell.modifier.insert(Modifier::DIM);
}
Lighten | Darken => {
if let Color::Rgb(..) = cell.fg {
cell.fg = modify(cell.fg);
};
if let Color::Rgb(..) = cell.bg {
cell.bg = modify(cell.bg);
}
if shade == 0 {
cell.modifier.insert(Modifier::DIM);
} else {
if let Color::Rgb(..) = cell.fg {
cell.fg = modify(cell.fg);
};
if let Color::Rgb(..) = cell.bg {
cell.bg = modify(cell.bg);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
clipboard::{get_clipboard_provider, ClipboardProvider},
document::{Mode, SCRATCH_BUFFER_NAME},
graphics::{CursorKind, DimOperation, Rect},
graphics::{CursorKind, Rect},
info::Info,
input::KeyEvent,
theme::{self, Theme},
Expand Down Expand Up @@ -112,10 +112,10 @@ impl Default for FilePickerConfig {
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
pub struct DimConfig {
/// dim function for margin around content of dimmable overlays
pub overlay_backdrops: Option<DimOperation>,
/// dim function for unfocused editor views
pub unfocused_views: Option<DimOperation>,
/// shade for margin around content of overlays
pub overlay_backdrops: Option<i8>,
/// shade for unfocused editor views
pub unfocused_views: Option<i8>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down
29 changes: 0 additions & 29 deletions helix-view/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,6 @@ use std::{
str::FromStr,
};

#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum DimFunction {
// Set text modifier DIM
Dim,
// Lighten the shade of an RGB color
Lighten,
// Darken the shade of an RGB color
Darken,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
pub struct DimOperation {
/// Function to perform on cells
pub func: DimFunction,
/// 0-100 Instensity of the applied function. Has no effect with DimFunction.Dim
pub intensity: u8,
}

impl Default for DimOperation {
fn default() -> Self {
Self {
func: DimFunction::Darken,
intensity: 50,
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
/// UNSTABLE
Expand Down