wezterm: allow adding images to termwiztermtab apps

Tidies up the plumbing around pixel dimensions so that ImageData
can be rendered via the termwiztermtab bits.

I put this together to play with sticking the wezterm logo in
the close confirmation dialogs.  I didn't end up using that though,
but have preserved the commented code for use in future hacking.
This commit is contained in:
Wez Furlong committed 2020-12-26 14:01:48 -08:00
1 parent 8649056ac0
commit b3ac77aa92
9 files changed
+84 -67

No files matched your search

Generated
+1
View File
@@ -4497,6 +4497,7 @@ dependencies = [
"notify-rust",
"open",
"openssl",
"ordered-float",
"palette",
"portable-pty",
"promise",
+5 -4
View File
@@ -1,6 +1,7 @@
use crate::termwiztermtab;
use anyhow::{anyhow, bail, Context as _};
use crossbeam::channel::{bounded, Receiver, Sender};
use portable_pty::PtySize;
use promise::spawn::block_on;
use promise::Promise;
use std::sync::Mutex;
@@ -239,12 +240,12 @@ pub struct ConnectionUI {
impl ConnectionUI {
pub fn new() -> Self {
let enable_close_delay = true;
Self::with_dimensions(80, 24, enable_close_delay)
Self::with_dimensions(PtySize::default(), enable_close_delay)
}
pub fn with_dimensions(width: usize, height: usize, enable_close_delay: bool) -> Self {
pub fn with_dimensions(size: PtySize, enable_close_delay: bool) -> Self {
let (tx, rx) = bounded(16);
promise::spawn::spawn_into_main_thread(termwiztermtab::run(width, height, move |term| {
promise::spawn::spawn_into_main_thread(termwiztermtab::run(size, move |term| {
let mut ui = ConnectionUIImpl { term, rx };
let status = ui.run().unwrap_or_else(|e| {
log::error!("while running ConnectionUI loop: {:?}", e);
@@ -266,7 +267,7 @@ impl ConnectionUI {
pub fn new_with_no_close_delay() -> Self {
let enable_close_delay = false;
Self::with_dimensions(80, 24, enable_close_delay)
Self::with_dimensions(PtySize::default(), enable_close_delay)
}
pub fn new_headless() -> Self {
+22 -53
View File
@@ -9,7 +9,7 @@ use crate::renderable::*;
use crate::tab::{SplitDirection, Tab, TabId};
use crate::window::WindowId;
use crate::Mux;
use anyhow::{bail, Error};
use anyhow::bail;
use async_trait::async_trait;
use crossbeam::channel::{unbounded as channel, Receiver, Sender};
use filedescriptor::{FileDescriptor, Pipe};
@@ -25,11 +25,10 @@ use std::sync::Arc;
use std::time::Duration;
use termwiz::caps::{Capabilities, ColorLevel, ProbeHints};
use termwiz::input::{InputEvent, KeyEvent, MouseEvent as TermWizMouseEvent};
use termwiz::lineedit::*;
use termwiz::render::terminfo::TerminfoRenderer;
use termwiz::surface::Change;
use termwiz::surface::Line;
use termwiz::terminal::{ScreenSize, Terminal, TerminalWaker};
use termwiz::terminal::{ScreenSize, TerminalWaker};
use url::Url;
use wezterm_term::color::ColorPalette;
use wezterm_term::{KeyCode, KeyModifiers, MouseEvent, StableRowIndex};
@@ -104,18 +103,17 @@ pub struct TermWizTerminalPane {
impl TermWizTerminalPane {
fn new(
domain_id: DomainId,
width: usize,
height: usize,
size: PtySize,
input_tx: Sender<InputEvent>,
render_rx: FileDescriptor,
) -> Self {
let pane_id = alloc_pane_id();
let terminal = RefCell::new(wezterm_term::Terminal::new(
height,
width,
0,
0,
size.rows as usize,
size.cols as usize,
size.pixel_width as usize,
size.pixel_height as usize,
std::sync::Arc::new(config::TermConfig {}),
"WezTerm",
config::wezterm_version(),
@@ -370,7 +368,7 @@ impl termwiz::terminal::Terminal for TermWizTerminal {
}
}
pub fn allocate(width: usize, height: usize) -> (TermWizTerminal, Rc<dyn Pane>) {
pub fn allocate(size: PtySize) -> (TermWizTerminal, Rc<dyn Pane>) {
let render_pipe = Pipe::new().expect("Pipe creation not to fail");
let (input_tx, input_rx) = channel();
@@ -381,10 +379,10 @@ pub fn allocate(width: usize, height: usize) -> (TermWizTerminal, Rc<dyn Pane>)
render_tx: TermWizTerminalRenderTty {
render_tx: BufWriter::new(render_pipe.write),
screen_size: ScreenSize {
cols: width,
rows: height,
xpixel: 0,
ypixel: 0,
cols: size.cols as usize,
rows: size.rows as usize,
xpixel: (size.pixel_width / size.cols) as usize,
ypixel: (size.pixel_height / size.rows) as usize,
},
},
input_rx,
@@ -392,7 +390,7 @@ pub fn allocate(width: usize, height: usize) -> (TermWizTerminal, Rc<dyn Pane>)
};
let domain_id = 0;
let pane = TermWizTerminalPane::new(domain_id, width, height, input_tx, render_pipe.read);
let pane = TermWizTerminalPane::new(domain_id, size, input_tx, render_pipe.read);
// Add the tab to the mux so that the output is processed
let pane: Rc<dyn Pane> = Rc::new(pane);
@@ -432,8 +430,7 @@ pub async fn run<
T: Send + 'static,
F: Send + 'static + FnOnce(TermWizTerminal) -> anyhow::Result<T>,
>(
width: usize,
height: usize,
size: PtySize,
f: F,
) -> anyhow::Result<T> {
let render_pipe = Pipe::new().expect("Pipe creation not to fail");
@@ -446,10 +443,10 @@ pub async fn run<
render_tx: TermWizTerminalRenderTty {
render_tx: BufWriter::new(render_pipe.write),
screen_size: ScreenSize {
cols: width,
rows: height,
xpixel: 0,
ypixel: 0,
cols: size.cols as usize,
rows: size.rows as usize,
xpixel: (size.pixel_width / size.cols) as usize,
ypixel: (size.pixel_height / size.rows) as usize,
},
},
input_rx,
@@ -459,8 +456,7 @@ pub async fn run<
async fn register_tab(
input_tx: Sender<InputEvent>,
render_rx: FileDescriptor,
width: usize,
height: usize,
size: PtySize,
) -> anyhow::Result<WindowId> {
let mux = Mux::get().unwrap();
@@ -470,15 +466,10 @@ pub async fn run<
let window_id = mux.new_empty_window();
let pane = TermWizTerminalPane::new(domain.domain_id(), width, height, input_tx, render_rx);
let pane = TermWizTerminalPane::new(domain.domain_id(), size, input_tx, render_rx);
let pane: Rc<dyn Pane> = Rc::new(pane);
let tab = Rc::new(Tab::new(&PtySize {
rows: height as _,
cols: width as _,
pixel_width: 0,
pixel_height: 0,
}));
let tab = Rc::new(Tab::new(&size));
tab.assign_pane(&pane);
mux.add_tab_and_active_pane(&tab)?;
@@ -488,7 +479,7 @@ pub async fn run<
}
let window_id: WindowId = promise::spawn::spawn_into_main_thread(async move {
register_tab(input_tx, render_rx, width, height).await
register_tab(input_tx, render_rx, size).await
})
.await?;
@@ -507,25 +498,3 @@ pub async fn run<
result
}
#[allow(unused)]
pub fn message_box_ok(message: &str) {
let title = "wezterm";
let message = message.to_string();
promise::spawn::block_on(run(60, 10, move |mut term| {
term.render(&[
Change::Title(title.to_string()),
Change::Text(message.to_string()),
])
.map_err(Error::msg)?;
let mut editor = LineEditor::new(&mut term);
editor.set_prompt("press enter to continue.");
let mut host = NopLineEditorHost::default();
editor.read_line(&mut host).ok();
Ok(())
}))
.ok();
}
+8 -4
View File
@@ -1473,21 +1473,25 @@ impl TerminalState {
let height = image.height.to_pixels(cell_pixel_height, physical_rows);
// Compute any Automatic dimensions
let aspect = decoded_image.width() as f32 / decoded_image.height() as f32;
let (width, height) = match (width, height) {
(None, None) => (
decoded_image.width() as usize,
decoded_image.height() as usize,
),
(Some(w), None) => {
let scale = decoded_image.width() as f32 / w as f32;
let h = decoded_image.height() as f32 / scale;
let h = w as f32 / aspect;
(w, h as usize)
}
(None, Some(h)) => {
let scale = decoded_image.height() as f32 / h as f32;
let w = decoded_image.width() as f32 / scale;
let w = h as f32 * aspect;
(w as usize, h)
}
(Some(w), Some(_)) if image.preserve_aspect_ratio => {
let h = w as f32 / aspect;
(w, h as usize)
}
(Some(w), Some(h)) => (w, h),
};
+1
View File
@@ -36,6 +36,7 @@ mlua = "0.4"
mux = { path = "../mux" }
open = "1.4"
openssl = "0.10"
ordered-float = "1.0"
palette = "0.5"
portable-pty = { path = "../pty", features = ["serde_support", "ssh"]}
promise = { path = "../promise" }
@@ -14,6 +14,22 @@ fn run_confirmation_app(message: &str, term: &mut TermWizTerminal) -> anyhow::Re
let size = term.get_screen_size()?;
/*
use crate::gui::termwindow::ICON_DATA;
use image::GenericImageView;
use std::sync::Arc;
use termwiz::surface::change::ImageData;
use termwiz::surface::TextureCoordinate;
let image_data = Arc::new(ImageData::with_raw_data(ICON_DATA.to_vec()));
let decoded_image = image::load_from_memory(ICON_DATA)?;
let logo_width_cells = 3usize;
let logo_width = logo_width_cells * size.xpixel;
let logo_scale = logo_width as f32 / decoded_image.width() as f32;
let logo_height = decoded_image.height() as f32 * logo_scale;
let logo_height_cells = (logo_height / size.ypixel as f32).ceil() as usize;
*/
// Render 80% wide, centered
let text_width = size.cols * 80 / 100;
let x_pos = size.cols * 10 / 100;
@@ -41,6 +57,15 @@ fn run_confirmation_app(message: &str, term: &mut TermWizTerminal) -> anyhow::Re
let mut changes = vec![
Change::ClearScreen(ColorAttribute::Default),
Change::CursorVisibility(CursorVisibility::Hidden),
/*
Change::Image(termwiz::surface::change::Image {
width: logo_width_cells,
height: logo_height_cells,
top_left: TextureCoordinate::new_f32(0., 0.),
bottom_right: TextureCoordinate::new_f32(1., 1.),
image: Arc::clone(&image_data),
}),
*/
];
for (y, row) in wrapped.split("\n").enumerate() {
+10 -2
View File
@@ -2,6 +2,7 @@ use crate::gui::termwindow::TermWindow;
use mux::pane::{Pane, PaneId};
use mux::tab::{Tab, TabId};
use mux::termwiztermtab::{allocate, TermWizTerminal};
use portable_pty::PtySize;
use std::pin::Pin;
use std::rc::Rc;
@@ -34,7 +35,7 @@ where
{
let tab_id = tab.tab_id();
let tab_size = tab.get_size();
let (tw_term, tw_tab) = allocate(tab_size.cols.into(), tab_size.rows.into());
let (tw_term, tw_tab) = allocate(tab_size);
let window = term_window.window.clone().unwrap();
@@ -61,7 +62,14 @@ where
{
let pane_id = pane.pane_id();
let dims = pane.get_dimensions();
let (tw_term, tw_tab) = allocate(dims.cols.into(), dims.viewport_rows.into());
let size = PtySize {
cols: dims.cols as u16,
rows: dims.viewport_rows as u16,
pixel_width: term_window.render_metrics.cell_size.width as u16 * dims.cols as u16,
pixel_height: term_window.render_metrics.cell_size.height as u16
* dims.viewport_rows as u16,
};
let (tw_term, tw_tab) = allocate(size);
let window = term_window.window.clone().unwrap();
+4 -3
View File
@@ -63,6 +63,8 @@ lazy_static::lazy_static! {
static ref WINDOW_CLASS: Mutex<String> = Mutex::new("org.wezfurlong.wezterm".to_owned());
}
pub const ICON_DATA: &'static [u8] = include_bytes!("../../../assets/icon/terminal.png");
pub fn set_window_class(cls: &str) {
*WINDOW_CLASS.lock().unwrap() = cls.to_owned();
}
@@ -210,7 +212,7 @@ pub struct TermWindow {
/// Terminal dimensions
terminal_size: PtySize,
pub mux_window_id: MuxWindowId,
render_metrics: RenderMetrics,
pub render_metrics: RenderMetrics,
render_state: RenderState,
input_map: InputMap,
/// If is_some, the LEADER modifier is active until the specified instant.
@@ -1033,8 +1035,7 @@ impl TermWindow {
}
fn apply_icon(window: &Window) -> anyhow::Result<()> {
let icon_image =
image::load_from_memory(include_bytes!("../../../assets/icon/terminal.png"))?;
let icon_image = image::load_from_memory(ICON_DATA)?;
let image = icon_image.to_bgra8();
let (width, height) = image.dimensions();
window.set_icon(Image::from_raw(
+8 -1
View File
@@ -4,6 +4,7 @@ use config::wezterm_version;
use http_req::request::{HttpVersion, Request};
use http_req::uri::Uri;
use mux::connui::ConnectionUI;
use portable_pty::PtySize;
use regex::Regex;
use serde::*;
use std::collections::HashMap;
@@ -147,7 +148,13 @@ fn show_update_available(release: Release) {
let mut updater = UPDATER_WINDOW.lock().unwrap();
let enable_close_delay = false;
let ui = ConnectionUI::with_dimensions(80, 35, enable_close_delay);
let size = PtySize {
cols: 80,
rows: 35,
pixel_width: 0,
pixel_height: 0,
};
let ui = ConnectionUI::with_dimensions(size, enable_close_delay);
ui.title("WezTerm Update Available");
let install = if cfg!(windows) {