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