Implement new window and quit

This commit is contained in:
Jeremy Soller 2023-11-01 08:50:05 -06:00
parent f73b06684a
commit 8fb9d823d7
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
3 changed files with 38 additions and 11 deletions

View file

@ -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
}

View file

@ -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)
]

View file

@ -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(