window+gui: introduce WindowConfiguration trait

This adopts a similar technique to that used to pass the wezterm
config to the term crate, but this time it is for passing it to
the window crate.

The use_ime option has been ported over to this new mechanism.
This commit is contained in:
Wez Furlong committed 2020-12-27 21:51:56 -08:00
1 parent 7ae52cd2b5
commit 44ca61da66
6 files changed
+59 -31

No files matched your search

-4
View File
@@ -1285,10 +1285,6 @@ impl TermWindow {
self.config_generation = config.generation();
self.palette.take();
#[cfg(target_os = "macos")]
{
::window::os::macos::use_ime(config.use_ime);
}
#[cfg(windows)]
{
::window::os::windows::use_dead_keys(config.use_dead_keys);
+2 -4
View File
@@ -20,6 +20,7 @@ mod markdown;
mod scripting;
mod stats;
mod update;
mod window_config;
#[derive(Debug, StructOpt)]
#[structopt(
@@ -388,11 +389,8 @@ fn run() -> anyhow::Result<()> {
config::reload();
}
let config = config::configuration();
window::configuration::set_configuration(crate::window_config::ConfigBridge);
#[cfg(target_os = "macos")]
{
window::os::macos::use_ime(config.use_ime);
}
#[cfg(windows)]
{
window::os::windows::use_dead_keys(config.use_dead_keys);
+10
View File
@@ -0,0 +1,10 @@
use ::window::configuration::WindowConfiguration;
use config::configuration;
pub struct ConfigBridge;
impl WindowConfiguration for ConfigBridge {
fn use_ime(&self) -> bool {
configuration().use_ime
}
}
+26
View File
@@ -0,0 +1,26 @@
use std::sync::{Arc, Mutex};
pub trait WindowConfiguration {
fn use_ime(&self) -> bool {
false
}
}
lazy_static::lazy_static! {
static ref CONFIG: Mutex<Arc<dyn WindowConfiguration + Send + Sync>> = default_config();
}
pub(crate) fn config() -> Arc<dyn WindowConfiguration + Send + Sync> {
Arc::clone(&CONFIG.lock().unwrap())
}
fn default_config() -> Mutex<Arc<dyn WindowConfiguration + Send + Sync>> {
struct DefConfig;
impl WindowConfiguration for DefConfig {}
Mutex::new(Arc::new(DefConfig))
}
pub fn set_configuration<C: WindowConfiguration + Send + Sync + 'static>(c: C) {
let mut global_config = CONFIG.lock().unwrap();
*global_config = Arc::new(c);
}
+3
View File
@@ -3,11 +3,14 @@ use std::any::Any;
use std::sync::atomic::{AtomicBool, Ordering};
pub mod bitmaps;
pub mod color;
pub mod configuration;
pub mod connection;
pub mod os;
mod spawn;
mod timerlist;
use configuration::config;
#[cfg(target_os = "macos")]
pub const DEFAULT_DPI: f64 = 72.0;
#[cfg(not(target_os = "macos"))]
+18 -23
View File
@@ -6,9 +6,10 @@ use crate::bitmaps::Image;
use crate::connection::ConnectionOps;
use crate::os::macos::bitmap::BitmapRef;
use crate::{
is_egl_preferred, BitmapImage, Clipboard, Color, Connection, Dimensions, KeyCode, KeyEvent,
Modifiers, MouseButtons, MouseCursor, MouseEvent, MouseEventKind, MousePress, Operator,
PaintContext, Point, Rect, ScreenPoint, Size, WindowCallbacks, WindowOps, WindowOpsMut,
config, is_egl_preferred, BitmapImage, Clipboard, Color, Connection, Dimensions, KeyCode,
KeyEvent, Modifiers, MouseButtons, MouseCursor, MouseEvent, MouseEventKind, MousePress,
Operator, PaintContext, Point, Rect, ScreenPoint, Size, WindowCallbacks, WindowOps,
WindowOpsMut,
};
use anyhow::{anyhow, bail, ensure};
use cocoa::appkit::{
@@ -28,15 +29,8 @@ use std::any::Any;
use std::cell::RefCell;
use std::ffi::c_void;
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Instant;
static USE_IME: AtomicBool = AtomicBool::new(false);
pub fn use_ime(enable: bool) {
USE_IME.store(enable, Ordering::Relaxed);
}
fn round_away_from_zerof(value: f64) -> f64 {
if value > 0. {
value.max(1.).round()
@@ -730,7 +724,7 @@ impl WindowOpsMut for WindowInner {
if let Some(window_view) = WindowView::get_this(unsafe { &**self.view }) {
window_view.inner.borrow_mut().text_cursor_position = cursor;
}
if USE_IME.load(Ordering::Relaxed) {
if config().use_ime() {
unsafe {
let input_context: id = msg_send![&**self.view, inputContext];
let () = msg_send![input_context, invalidateCharacterCoordinates];
@@ -1344,21 +1338,22 @@ impl WindowView {
key_is_down
);
let use_ime = config().use_ime();
// `Delete` on macos is really Backspace and emits BS.
// `Fn-Delete` emits DEL.
// Alt-Delete is mapped by the IME to be equivalent to Fn-Delete.
// We want to emit Alt-BS in that situation.
let unmod = if virtual_key == super::keycodes::kVK_Delete
&& modifiers.contains(Modifiers::ALT)
{
"\x08"
} else if virtual_key == super::keycodes::kVK_Tab {
"\t"
} else if !USE_IME.load(Ordering::Relaxed) && virtual_key == super::keycodes::kVK_Delete {
"\x08"
} else {
unmod
};
let unmod =
if virtual_key == super::keycodes::kVK_Delete && modifiers.contains(Modifiers::ALT) {
"\x08"
} else if virtual_key == super::keycodes::kVK_Tab {
"\t"
} else if !use_ime && virtual_key == super::keycodes::kVK_Delete {
"\x08"
} else {
unmod
};
// If unmod is empty it most likely means that the user has selected
// an alternate keymap that has a chorded representation of eg: an ASCII
@@ -1376,7 +1371,7 @@ impl WindowView {
let only_alt = (modifiers & !(Modifiers::LEFT_ALT | Modifiers::RIGHT_ALT | Modifiers::ALT))
== Modifiers::NONE;
if key_is_down && USE_IME.load(Ordering::Relaxed) && (modifiers.is_empty() || only_alt) {
if key_is_down && use_ime && (modifiers.is_empty() || only_alt) {
if let Some(myself) = Self::get_this(this) {
let mut inner = myself.inner.borrow_mut();
inner.key_is_down.replace(key_is_down);