Add binding action for hiding the window

This commit is contained in:
Josh Leeb-du Toit 2018-07-22 10:38:53 +10:00 committed by Christian Duerr
parent 0d5edb7a7a
commit dbcb5885ad
5 changed files with 56 additions and 2 deletions

View File

@ -273,6 +273,7 @@ key_bindings:
- { key: C, mods: Command, action: Copy } - { key: C, mods: Command, action: Copy }
- { key: Paste, action: Paste } - { key: Paste, action: Paste }
- { key: Copy, action: Copy } - { key: Copy, action: Copy }
- { key: H, mods: Command, action: Hide }
- { key: Q, mods: Command, action: Quit } - { key: Q, mods: Command, action: Quit }
- { key: W, mods: Command, action: Quit } - { key: W, mods: Command, action: Quit }
- { key: Home, chars: "\x1bOH", mode: AppCursor } - { key: Home, chars: "\x1bOH", mode: AppCursor }

View File

@ -549,7 +549,7 @@ impl<'a> de::Deserialize<'a> for ActionWrapper {
type Value = ActionWrapper; type Value = ActionWrapper;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("Paste, Copy, PasteSelection, IncreaseFontSize, DecreaseFontSize, ResetFontSize, or Quit") f.write_str("Paste, Copy, PasteSelection, IncreaseFontSize, DecreaseFontSize, ResetFontSize, Hide, or Quit")
} }
fn visit_str<E>(self, value: &str) -> ::std::result::Result<ActionWrapper, E> fn visit_str<E>(self, value: &str) -> ::std::result::Result<ActionWrapper, E>
@ -562,6 +562,7 @@ impl<'a> de::Deserialize<'a> for ActionWrapper {
"IncreaseFontSize" => Action::IncreaseFontSize, "IncreaseFontSize" => Action::IncreaseFontSize,
"DecreaseFontSize" => Action::DecreaseFontSize, "DecreaseFontSize" => Action::DecreaseFontSize,
"ResetFontSize" => Action::ResetFontSize, "ResetFontSize" => Action::ResetFontSize,
"Hide" => Action::Hide,
"Quit" => Action::Quit, "Quit" => Action::Quit,
_ => return Err(E::invalid_value(Unexpected::Str(value), &self)), _ => return Err(E::invalid_value(Unexpected::Str(value), &self)),
})) }))

View File

@ -40,6 +40,7 @@ pub struct ActionContext<'a, N: 'a> {
pub received_count: &'a mut usize, pub received_count: &'a mut usize,
pub suppress_chars: &'a mut bool, pub suppress_chars: &'a mut bool,
pub last_modifiers: &'a mut ModifiersState, pub last_modifiers: &'a mut ModifiersState,
pub window_changes: &'a mut WindowChanges,
} }
impl<'a, N: Notify + 'a> input::ActionContext for ActionContext<'a, N> { impl<'a, N: Notify + 'a> input::ActionContext for ActionContext<'a, N> {
@ -133,6 +134,33 @@ impl<'a, N: Notify + 'a> input::ActionContext for ActionContext<'a, N> {
fn last_modifiers(&mut self) -> &mut ModifiersState { fn last_modifiers(&mut self) -> &mut ModifiersState {
&mut self.last_modifiers &mut self.last_modifiers
} }
#[inline]
fn hide_window(&mut self) {
self.window_changes.hide = true;
}
}
/// The ActionContext can't really have direct access to the Window
/// with the current design. Event handlers that want to change the
/// window must set these flags instead. The processor will trigger
/// the actual changes.
pub struct WindowChanges {
pub hide: bool,
}
impl WindowChanges {
fn clear(&mut self) {
self.hide = false;
}
}
impl Default for WindowChanges {
fn default() -> WindowChanges {
WindowChanges {
hide: false,
}
}
} }
pub enum ClickState { pub enum ClickState {
@ -199,6 +227,7 @@ pub struct Processor<N> {
suppress_chars: bool, suppress_chars: bool,
last_modifiers: ModifiersState, last_modifiers: ModifiersState,
pending_events: Vec<Event>, pending_events: Vec<Event>,
window_changes: WindowChanges,
} }
/// Notify that the terminal was resized /// Notify that the terminal was resized
@ -242,6 +271,7 @@ impl<N: Notify> Processor<N> {
suppress_chars: false, suppress_chars: false,
last_modifiers: Default::default(), last_modifiers: Default::default(),
pending_events: Vec::with_capacity(4), pending_events: Vec::with_capacity(4),
window_changes: Default::default(),
} }
} }
@ -401,6 +431,7 @@ impl<N: Notify> Processor<N> {
received_count: &mut self.received_count, received_count: &mut self.received_count,
suppress_chars: &mut self.suppress_chars, suppress_chars: &mut self.suppress_chars,
last_modifiers: &mut self.last_modifiers, last_modifiers: &mut self.last_modifiers,
window_changes: &mut self.window_changes,
}; };
processor = input::Processor { processor = input::Processor {
@ -448,6 +479,11 @@ impl<N: Notify> Processor<N> {
} }
} }
if self.window_changes.hide {
window.hide();
}
self.window_changes.clear();
self.wait_for_event = !terminal.dirty; self.wait_for_event = !terminal.dirty;
terminal terminal

View File

@ -66,6 +66,7 @@ pub trait ActionContext {
fn last_modifiers(&mut self) -> &mut ModifiersState; fn last_modifiers(&mut self) -> &mut ModifiersState;
fn change_font_size(&mut self, delta: f32); fn change_font_size(&mut self, delta: f32);
fn reset_font_size(&mut self); fn reset_font_size(&mut self);
fn hide_window(&mut self);
} }
/// Describes a state and action to take in that state /// Describes a state and action to take in that state
@ -170,6 +171,9 @@ pub enum Action {
/// Run given command /// Run given command
Command(String, Vec<String>), Command(String, Vec<String>),
/// Hides the Alacritty window
Hide,
/// Quits Alacritty. /// Quits Alacritty.
Quit, Quit,
} }
@ -224,6 +228,9 @@ impl Action {
}, },
} }
}, },
Action::Hide => {
ctx.hide_window();
},
Action::Quit => { Action::Quit => {
// FIXME should do a more graceful shutdown // FIXME should do a more graceful shutdown
::std::process::exit(0); ::std::process::exit(0);
@ -626,7 +633,7 @@ mod tests {
use glutin::{VirtualKeyCode, Event, WindowEvent, ElementState, MouseButton, ModifiersState}; use glutin::{VirtualKeyCode, Event, WindowEvent, ElementState, MouseButton, ModifiersState};
use term::{SizeInfo, Term, TermMode}; use term::{SizeInfo, Term, TermMode};
use event::{Mouse, ClickState}; use event::{Mouse, ClickState, WindowChanges};
use config::{self, Config, ClickHandler}; use config::{self, Config, ClickHandler};
use index::{Point, Side}; use index::{Point, Side};
use selection::Selection; use selection::Selection;
@ -651,6 +658,7 @@ mod tests {
pub received_count: usize, pub received_count: usize,
pub suppress_chars: bool, pub suppress_chars: bool,
pub last_modifiers: ModifiersState, pub last_modifiers: ModifiersState,
pub window_changes: &'a mut WindowChanges,
} }
impl <'a>super::ActionContext for ActionContext<'a> { impl <'a>super::ActionContext for ActionContext<'a> {
@ -704,6 +712,8 @@ mod tests {
} }
fn reset_font_size(&mut self) { fn reset_font_size(&mut self) {
} }
fn hide_window(&mut self) {
}
} }
macro_rules! test_clickstate { macro_rules! test_clickstate {
@ -742,6 +752,7 @@ mod tests {
received_count: 0, received_count: 0,
suppress_chars: false, suppress_chars: false,
last_modifiers: ModifiersState::default(), last_modifiers: ModifiersState::default(),
window_changes: &mut WindowChanges::default(),
}; };
let mut processor = Processor { let mut processor = Processor {

View File

@ -379,6 +379,11 @@ impl Window {
pub fn get_window_id(&self) -> Option<usize> { pub fn get_window_id(&self) -> Option<usize> {
None None
} }
/// Hide the window
pub fn hide(&self) {
self.window.hide();
}
} }
pub trait OsExtensions { pub trait OsExtensions {