Files
LLeMbas-TTY/window/src/lib.rs
T
Wez Furlong 0df755b8f0 window: set_inner_size now returns Future<Dimensions>
This should help us reason about whether a resize respected
our request.

This may break linux and windows builds, will fixup once
this is pushed.

refs: https://github.com/wez/wezterm/issues/695
2021-05-03 20:49:22 -07:00

192 lines
5.6 KiB
Rust

use promise::Future;
use std::any::Any;
pub mod bitmaps;
pub mod color;
mod configuration;
pub mod connection;
pub mod os;
mod spawn;
mod timerlist;
#[cfg(target_os = "macos")]
pub(crate) const DEFAULT_DPI: f64 = 72.0;
#[cfg(not(target_os = "macos"))]
pub(crate) const DEFAULT_DPI: f64 = 96.0;
pub fn default_dpi() -> f64 {
match Connection::get() {
Some(conn) => conn.default_dpi(),
None => DEFAULT_DPI,
}
}
mod egl;
pub use bitmaps::{BitmapImage, Image};
pub use connection::*;
pub use glium;
pub use os::*;
pub use wezterm_input_types::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Clipboard {
Clipboard,
PrimarySelection,
}
impl Default for Clipboard {
fn default() -> Self {
Self::Clipboard
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Dimensions {
pub pixel_width: usize,
pub pixel_height: usize,
pub dpi: usize,
}
pub type Rect = euclid::Rect<isize, PixelUnit>;
pub type Size = euclid::Size2D<isize, PixelUnit>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MouseCursor {
Arrow,
Hand,
Text,
SizeUpDown,
SizeLeftRight,
}
#[allow(unused_variables)]
pub trait WindowCallbacks: Any {
/// Called when the window close button is clicked.
/// Return true to allow the close to continue, false to
/// prevent it from closing.
fn can_close(&mut self) -> bool {
true
}
/// Called when the window is being destroyed by the gui system
fn destroy(&mut self) {}
/// Called when the window is resized, or when the dpi has changed
fn resize(&mut self, dimensions: Dimensions, is_full_screen: bool) {}
/// Called when window gains/loses focus
fn focus_change(&mut self, focused: bool) {}
/// Called when the window has opengl mode enabled and the window
/// contents need painting.
fn paint(&mut self, frame: &mut glium::Frame) {
use glium::Surface;
frame.clear_color_srgb(0.25, 0.125, 0.375, 1.0);
}
/// Called if the opengl context is lost
fn opengl_context_lost(&mut self, _window: &dyn WindowOps) -> anyhow::Result<()> {
Ok(())
}
/// Called to handle a key event.
/// If your window didn't handle the event, you must return false.
/// This is particularly important for eg: ALT keys on windows,
/// otherwise standard key assignments may not function in your window.
fn key_event(&mut self, key: &KeyEvent, context: &dyn WindowOps) -> bool {
false
}
fn mouse_event(&mut self, event: &MouseEvent, context: &dyn WindowOps) {
context.set_cursor(Some(MouseCursor::Arrow));
}
/// Called when the window is created and allows the embedding
/// app to reference the window and operate upon it.
fn created(
&mut self,
_window: &Window,
_context: std::rc::Rc<glium::backend::Context>,
) -> anyhow::Result<()> {
Ok(())
}
/// An unfortunate bit of boilerplate; you need to provie an impl
/// of this method that returns `self` in order for the downcast_ref
/// method of the Any trait to be usable on WindowCallbacks.
/// https://stackoverflow.com/q/46045298/149111 and others have
/// some rationale on why Rust works this way.
fn as_any(&mut self) -> &mut dyn Any;
}
pub trait WindowOps {
/// Show a hidden window
fn show(&self) -> Future<()>;
/// Hide a visible window
fn hide(&self) -> Future<()>;
/// Schedule the window to be closed
fn close(&self) -> Future<()>;
/// Change the cursor
fn set_cursor(&self, cursor: Option<MouseCursor>) -> Future<()>;
/// Invalidate the window so that the entire client area will
/// be repainted shortly
fn invalidate(&self) -> Future<()>;
/// Change the titlebar text for the window
fn set_title(&self, title: &str) -> Future<()>;
/// Resize the inner or client area of the window
fn set_inner_size(&self, width: usize, height: usize) -> Future<Dimensions>;
/// Changes the location of the window on the screen.
/// The coordinates are of the top left pixel of the
/// client area.
fn set_window_position(&self, _coords: ScreenPoint) -> Future<()> {
Future::ok(())
}
/// inform the windowing system of the current textual
/// cursor input location. This is used primarily for
/// the platform specific input method editor
fn set_text_cursor_position(&self, _cursor: Rect) -> Future<()> {
Future::ok(())
}
/// Schedule a callback on the data associated with the window.
/// The `Any` that is passed in corresponds to the WindowCallbacks
/// impl you passed to `new_window`, pre-converted to Any so that
/// you can `downcast_ref` or `downcast_mut` it and operate on it.
fn apply<R, F: Send + 'static + FnMut(&mut dyn Any, &dyn WindowOps) -> anyhow::Result<R>>(
&self,
func: F,
) -> promise::Future<R>
where
Self: Sized,
R: Send + 'static;
/// Initiate textual transfer from the clipboard
fn get_clipboard(&self, clipboard: Clipboard) -> Future<String>;
/// Set some text in the clipboard
fn set_clipboard(&self, clipboard: Clipboard, text: String) -> Future<()>;
/// Set the icon for the window.
/// Depending on the system this may be shown in its titlebar
/// and/or in the task manager/task switcher
fn set_icon(&self, _image: Image) -> Future<()> {
Future::ok(())
}
fn toggle_fullscreen(&self) -> Future<()> {
Future::ok(())
}
fn config_did_change(&self, _config: &config::ConfigHandle) -> Future<()> {
Future::ok(())
}
}