From 8a6f29ac6bf2f49cb62f41e8dc6a0b87e9b5ef2f Mon Sep 17 00:00:00 2001 From: Dongdong Zhou Date: Wed, 6 Oct 2021 11:03:35 +0100 Subject: [PATCH] add logo --- Cargo.lock | 2 +- core/src/config.rs | 1 + core/src/editor.rs | 139 +++++++++++++++++++++++++++++++++--------- core/src/keypress.rs | 22 +++---- extra/images/logo.svg | 8 +++ 5 files changed, 132 insertions(+), 40 deletions(-) create mode 100644 extra/images/logo.svg diff --git a/Cargo.lock b/Cargo.lock index e6cee697..e7c9c3b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3226,7 +3226,7 @@ dependencies = [ [[package]] name = "piet-wgpu" version = "0.1.0" -source = "git+https://github.com/lapce/piet-wgpu#21971bbecaf728e53a44343a13eb3d715c3999e2" +source = "git+https://github.com/lapce/piet-wgpu#c640e5ce74bff2d72834c7a111b061a0cc74da9f" dependencies = [ "bytemuck", "font-kit", diff --git a/core/src/config.rs b/core/src/config.rs index a8b2a7f5..74a6cd73 100644 --- a/core/src/config.rs +++ b/core/src/config.rs @@ -15,6 +15,7 @@ include_str!("../../defaults/light-theme.toml"); const default_dark_theme: &'static str = include_str!("../../defaults/dark-theme.toml"); +pub const LOGO: &'static str = include_str!("../../extra/images/logo.svg"); pub struct LapceTheme {} diff --git a/core/src/editor.rs b/core/src/editor.rs index 346b52a1..d9f28216 100644 --- a/core/src/editor.rs +++ b/core/src/editor.rs @@ -3,13 +3,13 @@ CommandTarget, LapceCommandNew, LapceWorkbenchCommand, LAPCE_NEW_COMMAND, }; use crate::completion::{CompletionData, CompletionStatus, Snippet}; -use crate::config::{Config, LapceTheme}; +use crate::config::{Config, LapceTheme, LOGO}; use crate::data::{ EditorContent, EditorDiagnostic, EditorKind, EditorType, LapceEditorData, LapceMainSplitData, LapceTabData, RegisterData, }; use crate::find::Find; -use crate::keypress::KeyPressFocus; +use crate::keypress::{KeyMap, KeyPressFocus}; use crate::proxy::LapceProxy; use crate::scroll::LapceIdentityWrapper; use crate::signature::SignatureState; @@ -45,6 +45,7 @@ use bit_vec::BitVec; use crossbeam_channel::{self, bounded}; use druid::kurbo::BezPath; +use druid::piet::Svg; use druid::widget::{LensWrap, WidgetWrapper}; use druid::{ kurbo::Line, piet::PietText, theme, widget::Flex, widget::IdentityWrapper, @@ -54,7 +55,7 @@ RenderContext, Size, Target, TextLayout, UpdateCtx, Vec2, Widget, WidgetExt, WidgetId, WidgetPod, WindowId, }; -use druid::{menu, Application, ExtEventSink, FileDialogOptions, Menu}; +use druid::{menu, Application, ExtEventSink, FileDialogOptions, Menu, Modifiers}; use druid::{ piet::{ PietTextLayout, Text, TextAttribute, TextLayout as TextLayoutTrait, @@ -3675,7 +3676,7 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, env: &Env) { pub struct LapceEditor { view_id: WidgetId, placeholder: Option, - commands: Vec<(LapceCommandNew, PietTextLayout, Rect)>, + commands: Vec<(LapceCommandNew, PietTextLayout, Rect, PietTextLayout)>, } impl LapceEditor { @@ -3702,7 +3703,7 @@ fn event( EditorContent::None => { ctx.set_handled(); let mut on_command = false; - for (_, _, rect) in &self.commands { + for (_, _, rect, _) in &self.commands { if rect.contains(mouse_event.pos) { on_command = true; break; @@ -3784,7 +3785,7 @@ fn event( Event::MouseDown(mouse_event) => match &editor.content { EditorContent::None => { ctx.set_handled(); - for (cmd, _, rect) in &self.commands { + for (cmd, _, rect, _) in &self.commands { if rect.contains(mouse_event.pos) { ctx.submit_command(Command::new( LAPCE_NEW_COMMAND, @@ -3990,10 +3991,10 @@ fn layout( } LapceEditorViewContent::None => { let size = bc.max(); - let origin = Point::new(size.width / 2.0, size.height / 2.0); + let origin = Point::new(size.width / 2.0, size.height / 2.0 + 40.0); let line_height = 30.0; - self.commands = empty_editor_commands() + self.commands = empty_editor_commands(data.workspace.is_some()) .iter() .enumerate() .map(|(i, cmd)| { @@ -4013,7 +4014,49 @@ fn layout( let point = origin - (text_layout.size().width, -line_height * i as f64); let rect = text_layout.size().to_rect().with_origin(point); - (cmd.clone(), text_layout, rect) + let mut key = None; + for (_, keymaps) in data.keypress.keymaps.iter() { + for keymap in keymaps { + if keymap.command == cmd.cmd { + let mut keymap_str = "".to_string(); + for (mods, key) in &keymap.key { + if keymap_str != "" { + keymap_str += " " + } + if mods.ctrl() { + keymap_str += "Ctrl+"; + } + if mods.shift() { + keymap_str += "Shift+"; + } + if mods.alt() { + keymap_str += "Alt+"; + } + if mods.meta() { + keymap_str += "Meta+"; + } + keymap_str += &key.to_string(); + } + key = Some(keymap_str); + break; + } + } + if key.is_some() { + break; + } + } + let key_text_layout = ctx + .text() + .new_text_layout(key.unwrap_or("Unbound".to_string())) + .font(FontFamily::SYSTEM_UI, 14.0) + .text_color( + data.config + .get_color_unchecked(LapceTheme::EDITOR_DIM) + .clone(), + ) + .build() + .unwrap(); + (cmd.clone(), text_layout, rect, key_text_layout) }) .collect(); @@ -4029,8 +4072,33 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, env: &Env) { data.paint_content(ctx, is_focused, self.placeholder.as_ref()); } LapceEditorViewContent::None => { - for (cmd, text, rect) in &self.commands { + let svg = Svg::from_str(LOGO).unwrap(); + let size = ctx.size(); + let svg_size = 100.0; + let rect = Size::ZERO + .to_rect() + .with_origin( + Point::new(size.width / 2.0, size.height / 2.0) + + (0.0, -svg_size), + ) + .inflate(svg_size, svg_size); + ctx.draw_svg( + &svg, + rect, + Some( + &data + .config + .get_color_unchecked(LapceTheme::EDITOR_DIM) + .clone() + .with_alpha(0.5), + ), + ); + for (cmd, text, rect, keymap) in &self.commands { ctx.draw_text(text, rect.origin()); + ctx.draw_text( + keymap, + rect.origin() + (20.0 + rect.width(), 0.0), + ); } } } @@ -4207,22 +4275,37 @@ fn paint_wave_line( ctx.stroke(path, color, 1.4); } -fn empty_editor_commands() -> Vec { - vec![ - LapceCommandNew { - cmd: LapceWorkbenchCommand::PaletteCommand.to_string(), - palette_desc: Some("Show All Commands".to_string()), - target: CommandTarget::Workbench, - }, - LapceCommandNew { - cmd: LapceWorkbenchCommand::OpenFolder.to_string(), - palette_desc: Some("Open Folder".to_string()), - target: CommandTarget::Workbench, - }, - LapceCommandNew { - cmd: LapceWorkbenchCommand::PaletteWorkspace.to_string(), - palette_desc: Some("Open Recent".to_string()), - target: CommandTarget::Workbench, - }, - ] +fn empty_editor_commands(has_workspace: bool) -> Vec { + if !has_workspace { + vec![ + LapceCommandNew { + cmd: LapceWorkbenchCommand::PaletteCommand.to_string(), + palette_desc: Some("Show All Commands".to_string()), + target: CommandTarget::Workbench, + }, + LapceCommandNew { + cmd: LapceWorkbenchCommand::OpenFolder.to_string(), + palette_desc: Some("Open Folder".to_string()), + target: CommandTarget::Workbench, + }, + LapceCommandNew { + cmd: LapceWorkbenchCommand::PaletteWorkspace.to_string(), + palette_desc: Some("Open Recent".to_string()), + target: CommandTarget::Workbench, + }, + ] + } else { + vec![ + LapceCommandNew { + cmd: LapceWorkbenchCommand::PaletteCommand.to_string(), + palette_desc: Some("Show All Commands".to_string()), + target: CommandTarget::Workbench, + }, + LapceCommandNew { + cmd: LapceWorkbenchCommand::Palette.to_string(), + palette_desc: Some("Go To File".to_string()), + target: CommandTarget::Workbench, + }, + ] + } } diff --git a/core/src/keypress.rs b/core/src/keypress.rs index 25640852..48df2d8d 100644 --- a/core/src/keypress.rs +++ b/core/src/keypress.rs @@ -67,7 +67,8 @@ fn run_command( #[derive(Clone, Debug)] pub struct KeyPressData { pending_keypress: Vec, - keymaps: im::HashMap, Vec>, + pub keymaps: + Arc, Vec>>, pub commands: Arc>, count: Option, } @@ -76,7 +77,7 @@ impl KeyPressData { pub fn new() -> Self { Self { pending_keypress: Vec::new(), - keymaps: Self::get_keymaps().unwrap_or(im::HashMap::new()), + keymaps: Arc::new(Self::get_keymaps().unwrap_or(IndexMap::new())), commands: Arc::new(lapce_internal_commands()), count: None, } @@ -84,7 +85,7 @@ pub fn new() -> Self { pub fn update_keymaps(&mut self) { if let Ok(new_keymaps) = Self::get_keymaps() { - self.keymaps = new_keymaps; + self.keymaps = Arc::new(new_keymaps); } } @@ -317,19 +318,18 @@ fn check_condition(&self, condition: &str, check: &T) -> bool fn keymaps_from_str( s: &str, - ) -> Result< - im::HashMap, Vec>, - > { + ) -> Result, Vec>> + { let toml_keymaps: toml::Value = toml::from_str(s)?; let toml_keymaps = toml_keymaps .get("keymaps") .and_then(|v| v.as_array()) .ok_or(anyhow!("no keymaps"))?; - let mut keymaps: im::HashMap< + let mut keymaps: IndexMap< Vec<(Modifiers, druid::keyboard_types::Code)>, Vec, - > = im::HashMap::new(); + > = IndexMap::new(); for toml_keymap in toml_keymaps { if let Ok(keymap) = Self::get_keymap(toml_keymap) { for i in 1..keymap.key.len() + 1 { @@ -347,9 +347,9 @@ fn keymaps_from_str( Ok(keymaps) } - fn get_keymaps() -> Result< - im::HashMap, Vec>, - > { + fn get_keymaps( + ) -> Result, Vec>> + { let mut keymaps_str = if std::env::consts::OS == "macos" { default_keymaps_macos } else if std::env::consts::OS == "linux" { diff --git a/extra/images/logo.svg b/extra/images/logo.svg new file mode 100644 index 00000000..6d3fe1a4 --- /dev/null +++ b/extra/images/logo.svg @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file