use std::collections::HashMap;
use std::num::NonZeroU32;
use std::ops::Range;
use std::sync::Arc;
use std::time::{Duration, Instant};
use lune_terminal::{
CellPosition, CursorAppearance, Palette, PaletteEntry, PixelPosition, PixelSize, Progress,
ScreenBuffer,
};
use winit::dpi::PhysicalSize;
use winit::window::Window;
use crate::gpu::linear_colors;
use crate::{
CellBackgroundRenderer, ImageRenderer, PixelRect, PresentationFrame, PresentationTracker,
PresentedCursor, RowDamage,
};
use super::bg_image::{BackgroundImageConfig, BackgroundImageRenderer};
use super::blocks;
use super::confirm;
use super::context_menu::ContextMenu;
use super::cursor::{CursorMotion, CursorTarget};
use super::font::{Font, FontConfig, FontMetrics};
use super::hints::HintMode;
use super::ime::Preedit;
use super::palette::CommandPalette;
use super::scrollbar::ScrollbarView;
use super::search::SearchBar;
use super::selection::Selection;
use super::split::Divider;
use super::text::TextRenderer;
use super::url::HoveredLink;
use super::{PaneId, Result, ssh, tab_bar, titlebar, vi};
const RETRY_DELAY: Duration = Duration::from_millis(16);
const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(530);
pub(super) struct PaneOverlay<'a> {
pub(super) selection: Option<&'a Selection>,
pub(super) hovered_link: Option<&'a HoveredLink>,
pub(super) progress: Progress,
pub(super) progress_phase: f32,
pub(super) preedit: Option<&'a Preedit>,
pub(super) vi: Option<&'a vi::Mode>,
pub(super) search: Option<&'a SearchBar>,
pub(super) hints: Option<&'a HintMode>,
pub(super) zoomed: bool,
pub(super) scrollbar: Option<ScrollbarView>,
pub(super) command_blocks: bool,
pub(super) hovered_block: Option<blocks::HoveredBlock>,
pub(super) scale: f64,
pub(super) dim_alpha: f32,
}
pub(super) struct PaneDraw<'a> {
pub(super) id: PaneId,
pub(super) rect: PixelRect,
pub(super) text_origin: PixelPosition,
pub(super) frame: PresentationFrame<'a>,
pub(super) selection: Option<&'a Selection>,
pub(super) hovered_link: Option<&'a HoveredLink>,
pub(super) progress: Progress,
pub(super) preedit: Option<&'a Preedit>,
pub(super) vi: Option<&'a vi::Mode>,
pub(super) search: Option<&'a SearchBar>,
pub(super) hints: Option<&'a HintMode>,
pub(super) focused: bool,
pub(super) zoomed: bool,
pub(super) scrollbar: Option<ScrollbarView>,
pub(super) command_blocks: bool,
pub(super) hovered_block: Option<blocks::HoveredBlock>,
pub(super) dim_alpha: f32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum NoticeKind {
Success,
Warning,
Error,
}
#[derive(Clone, Copy)]
pub(super) struct NoticeView<'a> {
pub(super) kind: NoticeKind,
pub(super) text: &'a str,
}
pub(super) struct WindowChrome<'a> {
pub(super) palette: &'a Palette,
pub(super) reverse_video: bool,
pub(super) dividers: &'a [Divider],
pub(super) focused_rect: Option<PixelRect>,
pub(super) notice: Option<NoticeView<'a>>,
pub(super) confirm: Option<&'a confirm::Dialog>,
pub(super) command_palette: Option<&'a CommandPalette>,
pub(super) context_menu: Option<&'a ContextMenu>,
pub(super) scale: f64,
pub(super) titlebar: Option<titlebar::View<'a>>,
pub(super) ssh_browser: Option<ssh::BrowserView<'a>>,
pub(super) tab_bar: tab_bar::View<'a>,
}
struct PaneGpu {
backgrounds: CellBackgroundRenderer,
images: ImageRenderer,
graphics_prepare_failed: bool,
}
pub(super) struct Painter {
instance: wgpu::Instance,
surface: wgpu::Surface<'static>,
config: wgpu::SurfaceConfiguration,
alpha_modes: Vec<wgpu::CompositeAlphaMode>,
background_opacity: f32,
device: wgpu::Device,
queue: wgpu::Queue,
gpus: HashMap<PaneId, PaneGpu>,
trackers: Vec<(PaneId, PresentationTracker)>,
background_image: BackgroundImageRenderer,
text: TextRenderer,
pane_bg_ranges: Vec<(PaneId, Range<u32>)>,
pane_ranges: Vec<(PaneId, Range<u32>)>,
chrome_range: Range<u32>,
titlebar_range: Range<u32>,
sidebar_range: Range<u32>,
ssh_browser_range: Range<u32>,
tab_bar_range: Range<u32>,
modal_range: Range<u32>,
cursor_pane: Option<PaneId>,
cursor_motion: CursorMotion,
cursor_buffer: ScreenBuffer,
cursor_blink: bool,
cursor_animation: bool,
progress_animation: Option<Instant>,
retry: Option<Instant>,
force_present: bool,
dirty: bool,
}
pub(super) struct PainterOptions {
pub(super) physical_font_size: f32,
pub(super) scale_factor: f64,
pub(super) background_opacity: f32,
pub(super) minimum_contrast: f32,
pub(super) cursor_blink: bool,
pub(super) cursor_animation: bool,
}
impl Painter {
pub(super) async fn new(
window: Arc<Window>,
mut font: Font,
options: PainterOptions,
) -> Result<Self> {
let PainterOptions {
physical_font_size,
scale_factor,
background_opacity,
minimum_contrast,
cursor_blink,
cursor_animation,
} = options;
let mut descriptor = wgpu::InstanceDescriptor::new_without_display_handle();
descriptor.backends = wgpu::Backends::VULKAN;
let instance = wgpu::Instance::new(descriptor);
let surface = instance.create_surface(window.clone())?;
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
compatible_surface: Some(&surface),
..Default::default()
})
.await?;
let (device, queue) = adapter
.request_device(&wgpu::DeviceDescriptor {
memory_hints: wgpu::MemoryHints::MemoryUsage,
..Default::default()
})
.await?;
let size = window.inner_size();
let mut config = surface
.get_default_config(&adapter, size.width.max(1), size.height.max(1))
.ok_or_else(|| std::io::Error::other("No compatible Wayland surface format"))?;
let capabilities = surface.get_capabilities(&adapter);
config.format = capabilities
.formats
.into_iter()
.find(|format| format.is_srgb())
.ok_or_else(|| std::io::Error::other("The Wayland surface has no sRGB format"))?;
let background_opacity = background_opacity.clamp(0.0, 1.0);
let alpha_modes = capabilities.alpha_modes;
config.alpha_mode = choose_alpha_mode(&alpha_modes, background_opacity);
if background_opacity < 1.0 && config.alpha_mode == wgpu::CompositeAlphaMode::Opaque {
log::warn!(
"Wayland surface does not support alpha compositing; rendering opaque background"
);
}
surface.configure(&device, &config);
let metrics = font.measure(physical_font_size, scale_factor);
let background_image = BackgroundImageRenderer::new(&device, config.format);
let text = TextRenderer::new(
&device,
&queue,
config.format,
font,
metrics,
minimum_contrast,
);
log::debug!(
"Vulkan renderer ready with surface format {:?} and alpha mode {:?}",
config.format,
config.alpha_mode
);
Ok(Self {
instance,
surface,
config,
alpha_modes,
background_opacity,
device,
queue,
gpus: HashMap::new(),
trackers: Vec::new(),
background_image,
text,
pane_bg_ranges: Vec::new(),
pane_ranges: Vec::new(),
chrome_range: 0..0,
titlebar_range: 0..0,
sidebar_range: 0..0,
ssh_browser_range: 0..0,
tab_bar_range: 0..0,
modal_range: 0..0,
cursor_pane: None,
cursor_motion: CursorMotion::default(),
cursor_buffer: ScreenBuffer::Primary,
cursor_blink,
cursor_animation,
progress_animation: None,
retry: None,
force_present: false,
dirty: true,
})
}
pub(super) fn resize(&mut self, size: PhysicalSize<u32>) {
if size.width == self.config.width && size.height == self.config.height {
return;
}
if size.width == 0 || size.height == 0 {
return;
}
self.config.width = size.width;
self.config.height = size.height;
self.surface.configure(&self.device, &self.config);
self.reset_cursor_motion();
self.dirty = true;
self.force_present = true;
}
pub(super) fn metrics(&self) -> FontMetrics {
self.text.metrics()
}
pub(super) fn chrome_metrics(&self) -> FontMetrics {
self.text.chrome_metrics()
}
pub(super) fn size(&self) -> PhysicalSize<u32> {
PhysicalSize::new(self.config.width, self.config.height)
}
pub(super) fn background_opacity(&self) -> f32 {
self.background_opacity
}
pub(super) fn measure(&mut self, size: f32, scale_factor: f64) -> FontMetrics {
self.text.measure(size, scale_factor)
}
pub(super) fn measure_chrome(&mut self, base: FontMetrics) -> FontMetrics {
self.text.measure_chrome(base)
}
pub(super) fn set_font(&mut self, config: &FontConfig, size: f32, scale_factor: f64) -> bool {
if !self.text.set_font(config) {
return false;
}
let metrics = self.text.measure(size, scale_factor);
self.text.set_metrics(metrics);
self.reset_cursor_motion();
self.dirty = true;
self.force_present = true;
true
}
pub(super) fn set_minimum_contrast(&mut self, value: f32) {
if self.text.set_minimum_contrast(value) {
self.dirty = true;
self.force_present = true;
}
}
pub(super) fn set_background_image(
&mut self,
config: BackgroundImageConfig,
force_reload: bool,
) -> std::result::Result<(), String> {
let result =
self.background_image
.configure(&self.device, &self.queue, config, force_reload);
match result {
Ok(true) => {
self.dirty = true;
self.force_present = true;
Ok(())
}
Ok(false) => Ok(()),
Err(error) => {
self.dirty = true;
self.force_present = true;
Err(error)
}
}
}
pub(super) fn set_background_opacity(&mut self, opacity: f32) {
let opacity = opacity.clamp(0.0, 1.0);
if self.background_opacity == opacity {
return;
}
self.background_opacity = opacity;
let alpha_mode = choose_alpha_mode(&self.alpha_modes, opacity);
if opacity < 1.0 && alpha_mode == wgpu::CompositeAlphaMode::Opaque {
log::warn!(
"Wayland surface does not support alpha compositing; rendering opaque background"
);
}
if alpha_mode != self.config.alpha_mode {
self.config.alpha_mode = alpha_mode;
self.surface.configure(&self.device, &self.config);
}
self.dirty = true;
self.force_present = true;
}
pub(super) fn set_metrics(&mut self, metrics: FontMetrics) {
if self.text.metrics() != metrics {
self.reset_cursor_motion();
}
self.text.set_metrics(metrics);
self.dirty = true;
self.force_present = true;
}
pub(super) fn set_cursor_blink(&mut self, enabled: bool) {
if self.cursor_blink == enabled {
return;
}
self.cursor_blink = enabled;
let now = Instant::now();
let interval = if enabled {
CURSOR_BLINK_INTERVAL
} else {
Duration::ZERO
};
for (_, tracker) in &mut self.trackers {
tracker.set_cursor_blink_interval(interval, now);
}
self.dirty = true;
self.force_present = true;
}
pub(super) fn set_cursor_animation(&mut self, enabled: bool) {
if self.cursor_animation == enabled {
return;
}
self.cursor_animation = enabled;
if !enabled {
self.reset_cursor_motion();
}
}
pub(super) fn invalidate_overlay(&mut self) {
self.dirty = true;
}
pub(super) fn remove_pane(&mut self, id: PaneId) {
self.gpus.remove(&id);
self.trackers.retain(|(pane_id, _)| *pane_id != id);
if self.cursor_pane == Some(id) {
self.cursor_pane = None;
self.reset_cursor_motion();
}
self.dirty = true;
self.force_present = true;
}
pub(super) fn switch_session(&mut self) {
self.cursor_pane = None;
self.reset_cursor_motion();
self.retry = None;
self.dirty = true;
self.force_present = true;
}
pub(super) fn reset_cursor_motion(&mut self) -> bool {
let moving = self.cursor_motion.moving();
self.cursor_motion = CursorMotion::default();
self.dirty |= moving;
moving
}
pub(super) fn restart_blink(&mut self, id: PaneId, now: Instant) -> bool {
let changed = self
.trackers
.iter_mut()
.find(|(pane_id, _)| *pane_id == id)
.is_some_and(|(_, tracker)| tracker.restart_cursor_blink(now).is_some());
self.dirty |= changed;
changed
}
pub(super) fn deadline(&self) -> Option<Instant> {
let progress_deadline = self
.progress_animation
.map(|_| Instant::now() + RETRY_DELAY);
let cursor_deadline = self.cursor_pane.and_then(|id| {
self.trackers
.iter()
.find(|(pane_id, _)| *pane_id == id)
.and_then(|(_, tracker)| tracker.cursor_deadline())
});
self.retry
.into_iter()
.chain(cursor_deadline)
.chain(progress_deadline)
.min()
}
pub(super) fn tick(&mut self, now: Instant) -> bool {
let retry = self.retry.is_some_and(|retry| retry <= now);
if retry {
self.retry = None;
self.force_present = true;
}
let cursor_changed = self.cursor_pane.is_some_and(|id| {
self.trackers
.iter_mut()
.find(|(pane_id, _)| *pane_id == id)
.is_some_and(|(_, tracker)| tracker.tick_cursor(now).is_some())
});
let progress_changed = self.progress_animation.is_some();
self.dirty |= cursor_changed || progress_changed;
retry || cursor_changed || progress_changed
}
fn ensure_panes(&mut self, panes: &[PaneDraw<'_>], now: Instant) {
for pane in panes {
self.gpus.entry(pane.id).or_insert_with(|| PaneGpu {
backgrounds: CellBackgroundRenderer::new(&self.device, self.config.format),
images: ImageRenderer::new(&self.device, self.config.format),
graphics_prepare_failed: false,
});
if !self.trackers.iter().any(|(id, _)| *id == pane.id) {
let mut tracker = PresentationTracker::default();
let interval = if self.cursor_blink {
CURSOR_BLINK_INTERVAL
} else {
Duration::ZERO
};
tracker.set_cursor_blink_interval(interval, now);
self.trackers.push((pane.id, tracker));
}
}
}
pub(super) fn draw(
&mut self,
window: &Arc<Window>,
panes: &[PaneDraw<'_>],
chrome: WindowChrome<'_>,
) -> Result<()> {
if panes.is_empty() {
return Ok(());
}
let now = Instant::now();
let background_opacity = if self.config.alpha_mode == wgpu::CompositeAlphaMode::Opaque {
1.0
} else {
self.background_opacity
};
let top_bar_height = chrome
.titlebar
.as_ref()
.map_or(0, |view| view.layout.height);
let titlebar_width = chrome
.titlebar
.as_ref()
.map_or(0, |view| view.layout.header_width);
let content_top_inset = chrome
.titlebar
.as_ref()
.map_or(0, |view| view.layout.content_top_inset);
let sidebar_width = chrome
.titlebar
.as_ref()
.map_or(0, |view| view.layout.sidebar_width);
let indeterminate = panes
.iter()
.any(|pane| pane.progress == Progress::Indeterminate)
|| ((chrome.tab_bar.layout.height != 0 || top_bar_height != 0)
&& chrome
.tab_bar
.tabs
.iter()
.any(|tab| tab.progress() == Progress::Indeterminate));
let progress_phase = if indeterminate {
let start = *self.progress_animation.get_or_insert(now);
(now.duration_since(start).as_secs_f32() * 0.75).fract()
} else {
self.progress_animation = None;
0.0
};
let bar_height = chrome.tab_bar.layout.height;
let content_height = self.config.height.saturating_sub(bar_height);
for pane in panes {
let area = pane.frame.geometry.text_area_pixels().unwrap();
if pane.text_origin.x + area.width.get() > self.config.width
|| pane.text_origin.y + area.height.get() > content_height
{
return Ok(());
}
}
self.ensure_panes(panes, now);
let mut updates = Vec::with_capacity(panes.len());
let mut any_rows_changed = false;
let mut any_cursor_changed = false;
let mut any_graphics_changed = false;
let mut focused_index: Option<usize> = None;
for (tracker_id, tracker) in &mut self.trackers {
let Some(pane_index) = panes.iter().position(|pane| pane.id == *tracker_id) else {
continue;
};
let pane = &panes[pane_index];
let update = tracker.prepare(&pane.frame, now)?;
let damage = update.damage();
let rows_changed = match damage.rows {
RowDamage::All => true,
RowDamage::Rows(rows) => !rows.is_empty(),
};
let cursor = if !pane.focused {
None
} else if let Some(vi) = pane.vi {
let rows = pane.frame.viewport.range();
rows.contains(&vi.cursor.row).then(|| PresentedCursor {
position: CellPosition {
row: vi.cursor.row - rows.start,
column: vi.cursor.column,
},
appearance: CursorAppearance {
blinking: false,
..CursorAppearance::default()
},
color: pane.frame.palette.color(PaletteEntry::Cursor),
})
} else {
damage.displayed_cursor
};
any_rows_changed |= rows_changed;
any_cursor_changed |= damage.cursor.is_some();
any_graphics_changed |= damage.graphics_changed;
if pane.focused {
focused_index = Some(pane_index);
}
updates.push((pane_index, cursor, update));
}
updates.sort_unstable_by_key(|(pane_index, _, _)| *pane_index);
let focused_pane = focused_index.map(|index| &panes[index]);
let focused_cursor = focused_index.and_then(|index| updates[index].1);
let focused_id = focused_pane.map(|pane| pane.id);
let buffer = focused_pane
.map(|pane| pane.frame.graphics_buffer())
.unwrap_or(ScreenBuffer::Primary);
let mut motion = if self.cursor_pane == focused_id && self.cursor_buffer == buffer {
self.cursor_motion
} else {
CursorMotion::default()
};
let animation_active = self.cursor_animation
&& focused_pane.is_some_and(|pane| {
pane.focused
&& pane.vi.is_none()
&& pane.search.is_none()
&& pane.hints.is_none()
&& pane.preedit.is_none()
&& pane.frame.viewport.offset() == 0
});
let target = if animation_active && let Some(pane) = focused_pane {
focused_cursor.and_then(|cursor| cursor_origin(&pane.frame, cursor.position))
} else {
None
};
let cursor_input = if !animation_active {
CursorTarget::Reset
} else if let Some(origin) = target {
CursorTarget::Visible(origin)
} else {
CursorTarget::Hidden
};
let position = motion.advance(cursor_input, now);
let moving = position.is_some();
let panes_layout_changed = self.pane_ranges.len() != panes.len()
|| self
.pane_ranges
.iter()
.zip(panes)
.any(|((id, _), pane)| *id != pane.id);
let text_changed = any_rows_changed
|| self.dirty
|| panes_layout_changed
|| moving != self.cursor_motion.moving()
|| (!moving && any_cursor_changed);
let prepare_layers = text_changed || any_graphics_changed || self.force_present;
if !prepare_layers && !moving {
for (_, _, update) in updates {
update.commit();
}
self.cursor_pane = focused_id;
self.cursor_motion = motion;
self.cursor_buffer = buffer;
return Ok(());
}
use wgpu::CurrentSurfaceTexture as Acquired;
let (texture, suboptimal) = match self.surface.get_current_texture() {
Acquired::Success(texture) => (texture, false),
Acquired::Suboptimal(texture) => (texture, true),
Acquired::Timeout => {
self.retry = Some(Instant::now() + RETRY_DELAY);
return Ok(());
}
Acquired::Occluded => return Ok(()),
Acquired::Outdated => {
self.surface.configure(&self.device, &self.config);
self.retry = Some(Instant::now() + RETRY_DELAY);
return Ok(());
}
Acquired::Lost => {
log::debug!("Recreating the lost Wayland surface");
self.surface = self.instance.create_surface(window.clone())?;
self.surface.configure(&self.device, &self.config);
self.retry = Some(Instant::now() + RETRY_DELAY);
return Ok(());
}
Acquired::Validation => {
return Err(std::io::Error::other("Invalid Wayland surface").into());
}
};
if text_changed {
self.text.begin_frame();
self.pane_bg_ranges.clear();
self.pane_ranges.clear();
for (index, pane) in panes.iter().enumerate() {
let cursor = updates[index].1;
let pane_moving = moving && focused_index == Some(index);
let overlay = PaneOverlay {
selection: pane.selection,
hovered_link: pane.hovered_link,
progress: pane.progress,
progress_phase,
preedit: pane.preedit,
vi: pane.vi,
search: pane.search,
hints: pane.hints,
zoomed: pane.zoomed,
scrollbar: pane.scrollbar,
command_blocks: pane.command_blocks,
hovered_block: pane.hovered_block,
scale: chrome.scale,
dim_alpha: pane.dim_alpha,
};
let pane_blocks = if pane.command_blocks {
let first_row = pane.frame.viewport.range().start;
let rows = usize::from(pane.frame.geometry.pty_size().rows);
blocks::blocks_in_viewport(
pane.frame.viewport.source(),
first_row..first_row + rows,
)
} else {
Vec::new()
};
let bg_range = self.text.prepare_pane_blocks_bg(
pane.rect,
pane.text_origin,
&pane.frame,
&overlay,
&pane_blocks,
);
self.pane_bg_ranges.push((pane.id, bg_range));
let range = self.text.prepare_pane(
&pane.frame,
cursor.filter(|_| !pane_moving),
&overlay,
&pane_blocks,
)?;
self.pane_ranges.push((pane.id, range));
}
if let (Some(width), Some(height)) = (
NonZeroU32::new(self.config.width),
NonZeroU32::new(content_height),
) {
self.chrome_range = self.text.prepare_chrome(
PixelSize { width, height },
chrome.palette,
chrome.dividers,
chrome.focused_rect,
chrome.notice,
content_top_inset,
);
} else {
self.chrome_range = 0..0;
}
self.titlebar_range = match &chrome.titlebar {
Some(view) => self.text.prepare_titlebar(view, progress_phase),
None => 0..0,
};
self.sidebar_range = match &chrome.titlebar {
Some(view) if view.layout.sidebar_width != 0 => {
self.text.prepare_sidebar(view, progress_phase)
}
_ => 0..0,
};
self.ssh_browser_range = match &chrome.ssh_browser {
Some(view) if view.browser.layout.rect.is_some() => {
self.text.prepare_ssh_browser(view)
}
_ => 0..0,
};
self.tab_bar_range =
self.text
.prepare_tab_bar(&chrome.tab_bar, progress_phase, background_opacity);
if let Some(dialog) = chrome.confirm
&& let (Some(width), Some(height)) = (
NonZeroU32::new(self.config.width),
NonZeroU32::new(self.config.height),
)
{
self.modal_range = self.text.prepare_modal(
PixelSize { width, height },
chrome.palette,
dialog,
chrome.scale,
background_opacity,
);
} else if let Some(cmd_palette) = chrome.command_palette
&& let (Some(width), Some(height)) = (
NonZeroU32::new(self.config.width),
NonZeroU32::new(self.config.height),
)
{
self.modal_range = self.text.prepare_command_palette(
PixelSize { width, height },
chrome.palette,
cmd_palette,
chrome.scale,
background_opacity,
);
} else if let Some(menu) = chrome.context_menu
&& let (Some(width), Some(height)) = (
NonZeroU32::new(self.config.width),
NonZeroU32::new(self.config.height),
)
{
self.modal_range = self.text.prepare_context_menu(
PixelSize { width, height },
chrome.palette,
menu,
chrome.scale,
background_opacity,
);
} else {
self.modal_range = 0..0;
}
self.text.finish_frame()?;
}
let mut draw_moving_cursor = false;
if let Some(position) = position
&& let Some(target) = target
&& let Some(pane) = focused_pane
&& let Some(cursor) = focused_cursor
&& let Some(paint) = pane
.frame
.cursor_paint(cursor, NonZeroU32::new(2).unwrap())?
{
let origin = [
position[0] + (paint.rect.origin.x as f32 - target.x as f32),
position[1] + (paint.rect.origin.y as f32 - target.y as f32),
];
let area = pane.frame.geometry.text_area_pixels().unwrap();
self.text
.prepare_moving_cursor(area, origin, paint.rect.size, paint.color);
draw_moving_cursor = true;
}
let mut total_failures = 0usize;
let prepare_backgrounds = any_rows_changed || panes_layout_changed || self.force_present;
let prepare_images = prepare_backgrounds || any_graphics_changed;
if prepare_backgrounds || prepare_images {
for pane in panes {
let gpu = self.gpus.get_mut(&pane.id).unwrap();
if prepare_backgrounds {
gpu.backgrounds
.prepare(&self.device, &self.queue, &pane.frame)
.map_err(|error| {
std::io::Error::other(format!(
"Failed to prepare terminal backgrounds: {error:?}"
))
})?;
}
if prepare_images {
let failures = gpu.images.prepare(&self.device, &self.queue, &pane.frame);
if failures != 0 {
if !gpu.graphics_prepare_failed {
log::warn!("Failed to prepare {failures} terminal graphics placements");
}
total_failures += failures;
}
gpu.graphics_prepare_failed = failures != 0;
}
}
}
let view = texture
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
let background = chrome.palette.color(if chrome.reverse_video {
PaletteEntry::Foreground
} else {
PaletteEntry::Background
});
let lut = linear_colors();
let clear_alpha = if self.config.alpha_mode == wgpu::CompositeAlphaMode::Opaque {
1.0
} else {
f64::from(self.background_opacity)
};
let clear_scale = if self.config.alpha_mode == wgpu::CompositeAlphaMode::PreMultiplied {
clear_alpha
} else {
1.0
};
{
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: f64::from(lut[background.red as usize]) * clear_scale,
g: f64::from(lut[background.green as usize]) * clear_scale,
b: f64::from(lut[background.blue as usize]) * clear_scale,
a: clear_alpha,
}),
store: wgpu::StoreOp::Store,
},
})],
..Default::default()
});
pass.set_viewport(
0.0,
0.0,
self.config.width as f32,
self.config.height as f32,
0.0,
1.0,
);
pass.set_scissor_rect(0, 0, self.config.width, self.config.height);
self.background_image.draw(
&mut pass,
&self.queue,
(self.config.width, self.config.height),
background,
clear_scale as f32,
clear_alpha as f32,
);
for (index, pane) in panes.iter().enumerate() {
let Some(gpu) = self.gpus.get(&pane.id) else {
continue;
};
let bg_range = self.pane_bg_ranges[index].1.clone();
if !bg_range.is_empty() {
let bx = pane.rect.origin.x;
let by = pane.rect.origin.y;
let bw = pane.rect.size.width.get();
let bh = pane.rect.size.height.get();
pass.set_viewport(bx as f32, by as f32, bw as f32, bh as f32, 0.0, 1.0);
pass.set_scissor_rect(bx, by, bw, bh);
self.text.draw_range(&mut pass, bg_range, false);
}
let range = self.pane_ranges[index].1.clone();
let area = pane.frame.geometry.text_area_pixels().unwrap();
let (x, y) = (pane.text_origin.x, pane.text_origin.y);
let (width, height) = (area.width.get(), area.height.get());
let is_moving = draw_moving_cursor && focused_index == Some(index);
pass.set_viewport(x as f32, y as f32, width as f32, height as f32, 0.0, 1.0);
pass.set_scissor_rect(x, y, width, height);
gpu.images.draw_frame(&mut pass, &gpu.backgrounds, |pass| {
self.text.draw_range(pass, range, is_moving)
});
}
if top_bar_height != 0 && titlebar_width != 0 && !self.titlebar_range.is_empty() {
pass.set_viewport(
0.0,
0.0,
titlebar_width as f32,
top_bar_height as f32,
0.0,
1.0,
);
pass.set_scissor_rect(0, 0, titlebar_width, top_bar_height);
self.text
.draw_range(&mut pass, self.titlebar_range.clone(), false);
}
if sidebar_width != 0
&& !self.sidebar_range.is_empty()
&& self.config.height > top_bar_height
{
let sidebar_height = self.config.height - top_bar_height;
pass.set_viewport(
0.0,
top_bar_height as f32,
sidebar_width as f32,
sidebar_height as f32,
0.0,
1.0,
);
pass.set_scissor_rect(0, top_bar_height, sidebar_width, sidebar_height);
self.text
.draw_range(&mut pass, self.sidebar_range.clone(), false);
}
if let Some(view) = &chrome.ssh_browser
&& let Some(rect) = view.browser.layout.rect
&& !self.ssh_browser_range.is_empty()
{
let dx = rect.origin.x;
let dy = rect.origin.y;
let dw = rect.size.width.get();
let dh = rect.size.height.get();
pass.set_viewport(dx as f32, dy as f32, dw as f32, dh as f32, 0.0, 1.0);
pass.set_scissor_rect(dx, dy, dw, dh);
self.text
.draw_range(&mut pass, self.ssh_browser_range.clone(), false);
}
if !self.chrome_range.is_empty() && content_height != 0 {
pass.set_viewport(
0.0,
0.0,
self.config.width as f32,
content_height as f32,
0.0,
1.0,
);
pass.set_scissor_rect(0, 0, self.config.width, content_height);
self.text
.draw_range(&mut pass, self.chrome_range.clone(), false);
}
if bar_height != 0 && !self.tab_bar_range.is_empty() {
let top = chrome.tab_bar.layout.top;
pass.set_viewport(
0.0,
top as f32,
self.config.width as f32,
bar_height as f32,
0.0,
1.0,
);
pass.set_scissor_rect(0, top, self.config.width, bar_height);
self.text
.draw_range(&mut pass, self.tab_bar_range.clone(), false);
}
if !self.modal_range.is_empty() {
pass.set_viewport(
0.0,
0.0,
self.config.width as f32,
self.config.height as f32,
0.0,
1.0,
);
pass.set_scissor_rect(0, 0, self.config.width, self.config.height);
self.text
.draw_range(&mut pass, self.modal_range.clone(), false);
}
}
self.queue.submit([encoder.finish()]);
window.pre_present_notify();
self.queue.present(texture);
for (_, _, update) in updates {
update.commit();
}
self.cursor_pane = focused_id;
self.cursor_motion = motion;
self.cursor_buffer = buffer;
self.force_present = false;
self.dirty = false;
if suboptimal {
self.surface.configure(&self.device, &self.config);
}
if total_failures != 0 {
self.retry = Some(Instant::now() + Duration::from_millis(100));
}
if moving || self.progress_animation.is_some() {
window.request_redraw();
}
Ok(())
}
}
fn choose_alpha_mode(modes: &[wgpu::CompositeAlphaMode], opacity: f32) -> wgpu::CompositeAlphaMode {
use wgpu::CompositeAlphaMode as Mode;
let preferred: &[Mode] = if opacity >= 1.0 {
&[
Mode::Opaque,
Mode::PreMultiplied,
Mode::PostMultiplied,
Mode::Auto,
]
} else {
&[
Mode::PreMultiplied,
Mode::PostMultiplied,
Mode::Auto,
Mode::Opaque,
]
};
preferred
.iter()
.copied()
.find(|mode| modes.contains(mode))
.or_else(|| modes.first().copied())
.unwrap_or(Mode::Auto)
}
fn cursor_origin(frame: &PresentationFrame<'_>, position: CellPosition) -> Option<PixelPosition> {
let row = frame.viewport.row(position.row)?;
if row.sized_text_at(position.column).is_some() {
return None;
}
let cell = row.cells().get(position.column)?;
let leading = position.column - usize::from(cell.is_continuation());
frame
.geometry
.row_rect(position.row, leading..leading + 1)
.map(|rect| rect.origin)
}
#[cfg(test)]
mod tests {
use wgpu::CompositeAlphaMode as Mode;
use super::choose_alpha_mode;
#[test]
fn alpha_mode_prefers_opaque_at_full_opacity_and_premultiplied_when_translucent() {
let all = [Mode::Opaque, Mode::PreMultiplied, Mode::PostMultiplied];
assert_eq!(choose_alpha_mode(&all, 1.0), Mode::Opaque);
assert_eq!(choose_alpha_mode(&all, 0.85), Mode::PreMultiplied);
assert_eq!(
choose_alpha_mode(&[Mode::Opaque, Mode::PostMultiplied], 0.5),
Mode::PostMultiplied
);
assert_eq!(choose_alpha_mode(&[Mode::Opaque], 0.5), Mode::Opaque);
}
}