Skip to content

Commit

Permalink
new winit keyboard handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Feb 16, 2022
1 parent 093ba1c commit 28575f9
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 52 deletions.
51 changes: 40 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ pub enum LapceUICommand {
SplitReplace(usize, SplitContent),
SplitChangeDirectoin(SplitDirection),
EditorTabAdd(usize, EditorTabChild),
EditorTabRemove(usize, bool),
EditorTabRemove(usize, bool, bool),
EditorTabSwap(usize, usize),
JumpToPosition(Option<WidgetId>, Position),
JumpToLine(Option<WidgetId>, usize),
Expand Down
22 changes: 16 additions & 6 deletions core/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2289,7 +2289,7 @@ impl LapceMainSplitData {
}
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::EditorTabRemove(index, true),
LapceUICommand::EditorTabRemove(index, true, true),
Target::Widget(tab_id),
));
}
Expand All @@ -2303,6 +2303,7 @@ impl LapceMainSplitData {
new_content: SplitContent,
direction: SplitDirection,
shift_current: bool,
focus_new: bool,
) -> WidgetId {
let split = self.splits.get_mut(&split_id).unwrap();
let split = Arc::make_mut(split);
Expand Down Expand Up @@ -2356,11 +2357,19 @@ impl LapceMainSplitData {
),
Target::Widget(split_id),
));
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::Focus,
Target::Widget(from_content.widget_id()),
));
if focus_new {
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::Focus,
Target::Widget(new_content.widget_id()),
));
} else {
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::Focus,
Target::Widget(from_content.widget_id()),
));
}
self.splits.insert(new_split.widget_id, Arc::new(new_split));
new_split_id
}
Expand Down Expand Up @@ -2484,6 +2493,7 @@ impl LapceMainSplitData {
SplitContent::EditorTab(new_editor_tab.widget_id),
direction,
false,
false,
);

new_editor_tab.split = new_split_id;
Expand Down
23 changes: 15 additions & 8 deletions core/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3729,7 +3729,7 @@ impl LapceEditorTabHeaderContent {
if tab_rect.close_rect.contains(mouse_event.pos) {
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::EditorTabRemove(i, true),
LapceUICommand::EditorTabRemove(i, true, true),
Target::Widget(self.widget_id),
));
return;
Expand Down Expand Up @@ -3859,6 +3859,7 @@ impl Widget<LapceTabData> for LapceEditorTabHeaderContent {
LapceUICommand::EditorTabRemove(
*from_index,
false,
false,
),
Target::Widget(*from_id),
));
Expand Down Expand Up @@ -4306,6 +4307,7 @@ impl LapceEditorTab {
data: &mut LapceTabData,
i: usize,
delete: bool,
focus: bool,
) {
self.children.remove(i);
ctx.children_changed();
Expand Down Expand Up @@ -4333,11 +4335,13 @@ impl LapceEditorTab {
} else {
i
};
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::Focus,
Target::Widget(editor_tab.children[new_index].widget_id()),
));
if focus {
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::Focus,
Target::Widget(editor_tab.children[new_index].widget_id()),
));
}
editor_tab.children.remove(i)
} else {
if editor_tab.active > i {
Expand Down Expand Up @@ -4431,6 +4435,7 @@ impl LapceEditorTab {
),
split_direction,
shift_current,
true,
);
new_editor_tab.split = new_split_id;
if split_id != new_split_id {
Expand All @@ -4457,6 +4462,7 @@ impl LapceEditorTab {
LapceUICommand::EditorTabRemove(
*from_index,
false,
false,
),
Target::Widget(*from_id),
));
Expand Down Expand Up @@ -4493,6 +4499,7 @@ impl LapceEditorTab {
LapceUICommand::EditorTabRemove(
*from_index,
false,
false,
),
Target::Widget(*from_id),
));
Expand Down Expand Up @@ -4549,8 +4556,8 @@ impl Widget<LapceTabData> for LapceEditorTab {
ctx.request_layout();
return;
}
LapceUICommand::EditorTabRemove(index, delete) => {
self.remove_child(ctx, data, *index, *delete);
LapceUICommand::EditorTabRemove(index, delete, focus) => {
self.remove_child(ctx, data, *index, *delete, *focus);
return;
}
LapceUICommand::SplitClose => {
Expand Down
50 changes: 24 additions & 26 deletions core/src/keypress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ enum KeymapMatch {

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct KeyPress {
pub key: druid::keyboard_types::Key,
pub key: druid::KbKey,
pub mods: Modifiers,
}

Expand All @@ -49,7 +49,7 @@ impl KeyPress {
mods.set(Modifiers::SHIFT, false);
if mods.is_empty() {
match &self.key {
druid::keyboard_types::Key::Character(c) => {
druid::KbKey::Character(c) => {
return true;
}
_ => (),
Expand Down Expand Up @@ -153,7 +153,7 @@ impl KeyPressData {
}

match &keypress.key {
druid::keyboard_types::Key::Character(c) => {
druid::KbKey::Character(c) => {
if let Ok(n) = c.parse::<usize>() {
if self.count.is_some() || n > 0 {
self.count = Some(self.count.unwrap_or(0) * 10 + n);
Expand All @@ -174,7 +174,7 @@ impl KeyPressData {
focus: &mut T,
env: &Env,
) -> bool {
if key_event.key == druid::keyboard_types::Key::Shift {
if key_event.key == druid::KbKey::Shift {
let mut mods = key_event.mods.clone();
mods.set(Modifiers::SHIFT, false);
if mods.is_empty() {
Expand All @@ -183,7 +183,7 @@ impl KeyPressData {
}
let mut mods = key_event.mods.clone();
match &key_event.key {
druid::keyboard_types::Key::Character(c) => {
druid::KbKey::Character(c) => {
mods.set(Modifiers::SHIFT, false);
}
_ => (),
Expand Down Expand Up @@ -257,7 +257,7 @@ impl KeyPressData {
mods.set(Modifiers::SHIFT, false);
if mods.is_empty() {
match &key_event.key {
druid::keyboard_types::Key::Character(c) => {
druid::KbKey::Character(c) => {
focus.receive_char(ctx, c);
return true;
}
Expand Down Expand Up @@ -409,7 +409,7 @@ impl KeyPressData {
Self::keymaps_from_str(&keymaps_str)
}

fn get_keypress(key: &str) -> Vec<KeyPress> {
fn get_keypress<'a>(key: &'a str) -> Vec<KeyPress> {
let mut keypresses = Vec::new();
for k in key.split(" ") {
let mut mods = Modifiers::default();
Expand All @@ -419,25 +419,23 @@ impl KeyPressData {
continue;
}
let key = match parts[parts.len() - 1].to_lowercase().as_str() {
"escape" => druid::keyboard_types::Key::Escape,
"esc" => druid::keyboard_types::Key::Escape,
"backspace" => druid::keyboard_types::Key::Backspace,
"bs" => druid::keyboard_types::Key::Backspace,
"arrowup" => druid::keyboard_types::Key::ArrowUp,
"arrowdown" => druid::keyboard_types::Key::ArrowDown,
"arrowright" => druid::keyboard_types::Key::ArrowRight,
"arrowleft" => druid::keyboard_types::Key::ArrowLeft,
"up" => druid::keyboard_types::Key::ArrowUp,
"down" => druid::keyboard_types::Key::ArrowDown,
"right" => druid::keyboard_types::Key::ArrowRight,
"left" => druid::keyboard_types::Key::ArrowLeft,
"tab" => druid::keyboard_types::Key::Tab,
"enter" => druid::keyboard_types::Key::Enter,
"delete" => druid::keyboard_types::Key::Delete,
"del" => druid::keyboard_types::Key::Delete,
_ => druid::keyboard_types::Key::Character(
parts[parts.len() - 1].to_string(),
),
"escape" => druid::KbKey::Escape,
"esc" => druid::KbKey::Escape,
"backspace" => druid::KbKey::Backspace,
"bs" => druid::KbKey::Backspace,
"arrowup" => druid::KbKey::ArrowUp,
"arrowdown" => druid::KbKey::ArrowDown,
"arrowright" => druid::KbKey::ArrowRight,
"arrowleft" => druid::KbKey::ArrowLeft,
"up" => druid::KbKey::ArrowUp,
"down" => druid::KbKey::ArrowDown,
"right" => druid::KbKey::ArrowRight,
"left" => druid::KbKey::ArrowLeft,
"tab" => druid::KbKey::Tab,
"enter" => druid::KbKey::Enter,
"delete" => druid::KbKey::Delete,
"del" => druid::KbKey::Delete,
_ => druid::KbKey::Character(parts[parts.len() - 1].to_string()),
};
for part in &parts[..parts.len() - 1] {
match part.to_lowercase().as_ref() {
Expand Down

0 comments on commit 28575f9

Please sign in to comment.