Implement new window and quit
This commit is contained in:
parent
f73b06684a
commit
8fb9d823d7
3 changed files with 38 additions and 11 deletions
|
|
@ -3,6 +3,10 @@ use std::{collections::HashMap, fmt};
|
||||||
|
|
||||||
use crate::Message;
|
use crate::Message;
|
||||||
|
|
||||||
|
// Makes key binding definitions simpler
|
||||||
|
const CTRL: Modifiers = Modifiers::CTRL;
|
||||||
|
const SHIFT: Modifiers = Modifiers::SHIFT;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||||
pub struct KeyBind {
|
pub struct KeyBind {
|
||||||
pub modifiers: Modifiers,
|
pub modifiers: Modifiers,
|
||||||
|
|
@ -15,10 +19,10 @@ impl KeyBind {
|
||||||
let mut keybinds = HashMap::new();
|
let mut keybinds = HashMap::new();
|
||||||
|
|
||||||
macro_rules! bind {
|
macro_rules! bind {
|
||||||
($modifiers:ident, $key_code:ident, $message:ident) => {{
|
($modifiers:expr, $key_code:ident, $message:ident) => {{
|
||||||
keybinds.insert(
|
keybinds.insert(
|
||||||
KeyBind {
|
KeyBind {
|
||||||
modifiers: Modifiers::$modifiers,
|
modifiers: $modifiers,
|
||||||
key_code: KeyCode::$key_code,
|
key_code: KeyCode::$key_code,
|
||||||
},
|
},
|
||||||
Message::$message,
|
Message::$message,
|
||||||
|
|
@ -29,9 +33,11 @@ impl KeyBind {
|
||||||
bind!(CTRL, X, Cut);
|
bind!(CTRL, X, Cut);
|
||||||
bind!(CTRL, C, Copy);
|
bind!(CTRL, C, Copy);
|
||||||
bind!(CTRL, V, Paste);
|
bind!(CTRL, V, Paste);
|
||||||
bind!(CTRL, N, New);
|
bind!(CTRL, N, NewFile);
|
||||||
|
bind!(CTRL | SHIFT, N, NewWindow);
|
||||||
bind!(CTRL, O, OpenFileDialog);
|
bind!(CTRL, O, OpenFileDialog);
|
||||||
bind!(CTRL, S, Save);
|
bind!(CTRL, S, Save);
|
||||||
|
bind!(CTRL, Q, Quit);
|
||||||
|
|
||||||
keybinds
|
keybinds
|
||||||
}
|
}
|
||||||
|
|
|
||||||
31
src/main.rs
31
src/main.rs
|
|
@ -6,7 +6,7 @@ use cosmic::{
|
||||||
iced::{
|
iced::{
|
||||||
clipboard, event, keyboard, subscription,
|
clipboard, event, keyboard, subscription,
|
||||||
widget::{row, text},
|
widget::{row, text},
|
||||||
Alignment, Length, Limits,
|
window, Alignment, Length, Limits,
|
||||||
},
|
},
|
||||||
style,
|
style,
|
||||||
widget::{self, button, icon, nav_bar, segmented_button, view_switcher},
|
widget::{self, button, icon, nav_bar, segmented_button, view_switcher},
|
||||||
|
|
@ -16,6 +16,7 @@ use cosmic_text::{Edit, FontSystem, SwashCache, SyntaxSystem, ViMode};
|
||||||
use std::{
|
use std::{
|
||||||
env, fs,
|
env, fs,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
|
process,
|
||||||
sync::Mutex,
|
sync::Mutex,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -72,13 +73,15 @@ pub enum Message {
|
||||||
Cut,
|
Cut,
|
||||||
Copy,
|
Copy,
|
||||||
KeyBind(KeyBind),
|
KeyBind(KeyBind),
|
||||||
New,
|
NewFile,
|
||||||
|
NewWindow,
|
||||||
OpenFileDialog,
|
OpenFileDialog,
|
||||||
OpenFile(PathBuf),
|
OpenFile(PathBuf),
|
||||||
OpenProjectDialog,
|
OpenProjectDialog,
|
||||||
OpenProject(PathBuf),
|
OpenProject(PathBuf),
|
||||||
Paste,
|
Paste,
|
||||||
PasteValue(String),
|
PasteValue(String),
|
||||||
|
Quit,
|
||||||
Save,
|
Save,
|
||||||
TabActivate(segmented_button::Entity),
|
TabActivate(segmented_button::Entity),
|
||||||
TabClose(segmented_button::Entity),
|
TabClose(segmented_button::Entity),
|
||||||
|
|
@ -404,7 +407,7 @@ impl cosmic::Application for App {
|
||||||
},
|
},
|
||||||
Message::Copy => match self.active_tab() {
|
Message::Copy => match self.active_tab() {
|
||||||
Some(tab) => {
|
Some(tab) => {
|
||||||
let mut editor = tab.editor.lock().unwrap();
|
let editor = tab.editor.lock().unwrap();
|
||||||
let selection_opt = editor.copy_selection();
|
let selection_opt = editor.copy_selection();
|
||||||
if let Some(selection) = selection_opt {
|
if let Some(selection) = selection_opt {
|
||||||
return clipboard::write(selection);
|
return clipboard::write(selection);
|
||||||
|
|
@ -419,10 +422,24 @@ impl cosmic::Application for App {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Message::New => {
|
Message::NewFile => {
|
||||||
self.open_tab(None);
|
self.open_tab(None);
|
||||||
return self.update_tab();
|
return self.update_tab();
|
||||||
}
|
}
|
||||||
|
Message::NewWindow => {
|
||||||
|
//TODO: support multi-window in winit
|
||||||
|
match env::current_exe() {
|
||||||
|
Ok(exe) => match process::Command::new(&exe).spawn() {
|
||||||
|
Ok(child) => {}
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("failed to execute {:?}: {}", exe, err);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("failed to get current executable path: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Message::OpenFileDialog => {
|
Message::OpenFileDialog => {
|
||||||
return Command::perform(
|
return Command::perform(
|
||||||
async {
|
async {
|
||||||
|
|
@ -470,6 +487,10 @@ impl cosmic::Application for App {
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Message::Quit => {
|
||||||
|
//TODO: prompt for save?
|
||||||
|
return window::close();
|
||||||
|
}
|
||||||
Message::Save => {
|
Message::Save => {
|
||||||
let mut title_opt = None;
|
let mut title_opt = None;
|
||||||
|
|
||||||
|
|
@ -547,7 +568,7 @@ impl cosmic::Application for App {
|
||||||
.on_close(Message::TabClose)
|
.on_close(Message::TabClose)
|
||||||
.width(Length::Shrink),
|
.width(Length::Shrink),
|
||||||
button(icon::from_name("list-add-symbolic").size(16).icon())
|
button(icon::from_name("list-add-symbolic").size(16).icon())
|
||||||
.on_press(Message::New)
|
.on_press(Message::NewFile)
|
||||||
.padding(8)
|
.padding(8)
|
||||||
.style(style::Button::Icon)
|
.style(style::Button::Icon)
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -68,8 +68,8 @@ pub fn menu_bar<'a>(config: &Config) -> Element<'a, Message> {
|
||||||
MenuTree::with_children(
|
MenuTree::with_children(
|
||||||
menu_root(fl!("file")),
|
menu_root(fl!("file")),
|
||||||
vec![
|
vec![
|
||||||
menu_item(fl!("new-file"), Message::New),
|
menu_item(fl!("new-file"), Message::NewFile),
|
||||||
menu_key(fl!("new-window"), "Ctrl + Shift + N", Message::Todo),
|
menu_item(fl!("new-window"), Message::NewWindow),
|
||||||
MenuTree::new(horizontal_rule(1)),
|
MenuTree::new(horizontal_rule(1)),
|
||||||
menu_item(fl!("open-file"), Message::OpenFileDialog),
|
menu_item(fl!("open-file"), Message::OpenFileDialog),
|
||||||
MenuTree::with_children(
|
MenuTree::with_children(
|
||||||
|
|
@ -87,7 +87,7 @@ pub fn menu_bar<'a>(config: &Config) -> Element<'a, Message> {
|
||||||
menu_item(fl!("encoding"), Message::Todo),
|
menu_item(fl!("encoding"), Message::Todo),
|
||||||
menu_item(fl!("print"), Message::Todo),
|
menu_item(fl!("print"), Message::Todo),
|
||||||
MenuTree::new(horizontal_rule(1)),
|
MenuTree::new(horizontal_rule(1)),
|
||||||
menu_key(fl!("quit"), "Ctrl + Q", Message::Todo),
|
menu_item(fl!("quit"), Message::Quit),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
MenuTree::with_children(
|
MenuTree::with_children(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue