Skip to content

Commit

Permalink
Add option to apply opacity to all background colors
Browse files Browse the repository at this point in the history
In some cases it could be desired to apply 'background_opacity'
to all background colors instead of just 'colors.primary.background',
thus adding an 'colors.opaque_background_colors' option to control that.

Fixes alacritty#741.
  • Loading branch information
kchibisov authored Aug 16, 2021
1 parent 9a8ae43 commit c24d7df
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## 0.10.0-dev

### Added

- Option `colors.transparent_background_colors` to allow applying opacity to all background colors

### Changed

- `ExpandSelection` is now a configurable mouse binding action
- Config option `background_opacity`, you should use `window.opacity` instead

## 0.9.0

Expand Down
19 changes: 13 additions & 6 deletions alacritty.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
# - buttonless: Title bar, transparent background and no title bar buttons
#decorations: full

# Background opacity
#
# Window opacity as a floating point number from `0.0` to `1.0`.
# The value `0.0` is completely transparent and `1.0` is opaque.
#opacity: 1.0

# Startup Mode (changes require restart)
#
# Values for `startup_mode`:
Expand Down Expand Up @@ -312,6 +318,13 @@
#
#indexed_colors: []

# Transparent cell backgrounds
#
# Whether or not `window.opacity` applies to all cell backgrounds or only to
# the default background. When set to `true` all cells will be transparent
# regardless of their background color.
#transparent_background_colors: false

# Bell
#
# The bell is rung every time the BEL control character is received.
Expand Down Expand Up @@ -353,12 +366,6 @@
#
#command: None

# Background opacity
#
# Window opacity as a floating point number from `0.0` to `1.0`.
# The value `0.0` is completely transparent and `1.0` is opaque.
#background_opacity: 1.0

#selection:
# This string contains all characters that are used as separators for
# "semantic words" in Alacritty.
Expand Down
2 changes: 1 addition & 1 deletion alacritty/res/text.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main() {
}

alphaMask = vec4(1.0);
color = vec4(bg.rgb, 1.0);
color = vec4(bg.rgb, bg.a);
} else if ((int(fg.a) & COLORED) != 0) {
// Color glyphs, like emojis.
vec4 glyphColor = texture(mask, TexCoords);
Expand Down
2 changes: 1 addition & 1 deletion alacritty/res/text.v.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ void main() {
TexCoords = uvOffset + position * uvSize;
}

bg = vec4(backgroundColor.rgb / 255.0, backgroundColor.a);
bg = backgroundColor / 255.0;
fg = vec4(textColor.rgb / 255.0, textColor.a);
}
1 change: 1 addition & 0 deletions alacritty/src/config/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Colors {
pub search: SearchColors,
pub line_indicator: LineIndicatorColors,
pub hints: HintColors,
pub transparent_background_colors: bool,
}

impl Colors {
Expand Down
9 changes: 5 additions & 4 deletions alacritty/src/config/ui_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ pub struct UiConfig {
mouse_bindings: MouseBindings,

/// Background opacity from 0.0 to 1.0.
background_opacity: Percentage,
#[config(deprecated = "use window.opacity instead")]
window_opacity: Option<Percentage>,
}

impl Default for UiConfig {
Expand All @@ -84,7 +85,7 @@ impl Default for UiConfig {
config_paths: Default::default(),
key_bindings: Default::default(),
mouse_bindings: Default::default(),
background_opacity: Default::default(),
window_opacity: Default::default(),
bell: Default::default(),
colors: Default::default(),
draw_bold_text_with_bright_colors: Default::default(),
Expand Down Expand Up @@ -115,8 +116,8 @@ impl UiConfig {
}

#[inline]
pub fn background_opacity(&self) -> f32 {
self.background_opacity.as_f32()
pub fn window_opacity(&self) -> f32 {
self.window_opacity.unwrap_or(self.window.opacity).as_f32()
}

#[inline]
Expand Down
8 changes: 6 additions & 2 deletions alacritty/src/config/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use serde::de::{self, MapAccess, Visitor};
use serde::{Deserialize, Deserializer};

use alacritty_config_derive::ConfigDeserialize;
use alacritty_terminal::config::LOG_TARGET_CONFIG;
use alacritty_terminal::config::{Percentage, LOG_TARGET_CONFIG};
use alacritty_terminal::index::Column;

use crate::config::ui_config::Delta;

/// Default Alacritty name, used for window title and class.
pub const DEFAULT_NAME: &str = "Alacritty";

#[derive(ConfigDeserialize, Debug, Clone, PartialEq, Eq)]
#[derive(ConfigDeserialize, Debug, Clone, PartialEq)]
pub struct WindowConfig {
/// Initial position.
pub position: Option<Delta<i32>>,
Expand Down Expand Up @@ -45,6 +45,9 @@ pub struct WindowConfig {
/// Window class.
pub class: Class,

/// Background opacity from 0.0 to 1.0.
pub opacity: Percentage,

/// Pixel padding.
padding: Delta<u8>,

Expand All @@ -64,6 +67,7 @@ impl Default for WindowConfig {
gtk_theme_variant: Default::default(),
dynamic_padding: Default::default(),
class: Default::default(),
opacity: Default::default(),
padding: Default::default(),
dimensions: Default::default(),
}
Expand Down
6 changes: 4 additions & 2 deletions alacritty/src/display/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl RenderableCell {
mem::swap(&mut fg, &mut bg);
1.0
} else {
Self::compute_bg_alpha(cell.bg)
Self::compute_bg_alpha(&content.config.ui_config, cell.bg)
};

let is_selected = content.terminal_content.selection.map_or(false, |selection| {
Expand Down Expand Up @@ -350,9 +350,11 @@ impl RenderableCell {
/// using the named input color, rather than checking the RGB of the background after its color
/// is computed.
#[inline]
fn compute_bg_alpha(bg: Color) -> f32 {
fn compute_bg_alpha(config: &UiConfig, bg: Color) -> f32 {
if bg == Color::Named(NamedColor::Background) {
0.
} else if config.colors.transparent_background_colors {
config.window_opacity()
} else {
1.
}
Expand Down
2 changes: 1 addition & 1 deletion alacritty/src/display/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl Display {

// Disable shadows for transparent windows on macOS.
#[cfg(target_os = "macos")]
window.set_has_shadow(config.ui_config.background_opacity() >= 1.0);
window.set_has_shadow(config.ui_config.window_opacity() >= 1.0);

// On Wayland we can safely ignore this call, since the window isn't visible until you
// actually draw something into it and commit those changes.
Expand Down
2 changes: 1 addition & 1 deletion alacritty/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@ impl<N: Notify + OnResize> Processor<N> {

// Disable shadows for transparent windows on macOS.
#[cfg(target_os = "macos")]
processor.ctx.window().set_has_shadow(config.ui_config.background_opacity() >= 1.0);
processor.ctx.window().set_has_shadow(config.ui_config.window_opacity() >= 1.0);

// Update hint keys.
processor.ctx.display.hint_state.update_alphabet(config.ui_config.hints.alphabet());
Expand Down
2 changes: 1 addition & 1 deletion alacritty/src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ impl Drop for QuadRenderer {
impl<'a> RenderApi<'a> {
pub fn clear(&self, color: Rgb) {
unsafe {
let alpha = self.config.background_opacity();
let alpha = self.config.window_opacity();
gl::ClearColor(
(f32::from(color.r) / 255.0).min(1.0) * alpha,
(f32::from(color.g) / 255.0).min(1.0) * alpha,
Expand Down

0 comments on commit c24d7df

Please sign in to comment.