From 855bb493139b2c24e298f738bd0192963f9eb0c6 Mon Sep 17 00:00:00 2001 From: Dongdong Zhou Date: Fri, 30 Jul 2021 16:16:04 +0100 Subject: [PATCH] header paint --- core/src/data.rs | 3 ++ core/src/editor.rs | 72 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/core/src/data.rs b/core/src/data.rs index 41625b58..d38414ce 100644 --- a/core/src/data.rs +++ b/core/src/data.rs @@ -890,6 +890,7 @@ pub fn save_jump_location(&mut self, buffer: &BufferNew) { #[derive(Clone, Data, Lens)] pub struct LapceEditorViewData { pub main_split: LapceMainSplitData, + pub workspace: Arc, pub proxy: Arc, pub editor: Arc, pub buffer: Arc, @@ -1706,6 +1707,7 @@ fn with V>( .clone(); let editor_view = LapceEditorViewData { buffer: main_split.open_files.get(&editor.buffer).unwrap().clone(), + workspace: data.workspace.clone(), editor: editor.clone(), main_split: main_split.clone(), diagnostics, @@ -1734,6 +1736,7 @@ fn with_mut V>( .clone(); let mut editor_view = LapceEditorViewData { buffer, + workspace: data.workspace.clone(), editor: editor.clone(), diagnostics: diagnostics.clone(), all_diagnostics: data.diagnostics.clone(), diff --git a/core/src/editor.rs b/core/src/editor.rs index 5f36116b..b4a2b771 100644 --- a/core/src/editor.rs +++ b/core/src/editor.rs @@ -1,5 +1,6 @@ use crate::find::Find; use crate::signature::SignatureState; +use crate::svg::file_svg_new; use crate::{buffer::get_word_property, state::LapceFocus}; use crate::{buffer::matching_char, data::LapceEditorViewData}; use crate::{buffer::previous_has_unmatched_pair, movement::Cursor}; @@ -216,12 +217,12 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceEditorViewData, env: &Env) { for rect in &rects { ctx.fill(rect, &env.get(LapceTheme::EDITOR_BACKGROUND)); } - self.header.paint(ctx, data, env); let start = std::time::SystemTime::now(); self.editor.paint(ctx, data, env); let end = std::time::SystemTime::now(); let duration = end.duration_since(start).unwrap().as_micros(); // println!("editor paint took {}", duration); + self.header.paint(ctx, data, env); } } @@ -538,8 +539,8 @@ fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceEditorViewData, env: &Env) { for rect in &rects { ctx.fill(rect, &env.get(LapceTheme::EDITOR_BACKGROUND)); } - self.gutter.paint(ctx, data, env); self.editor.paint(ctx, data, env); + self.gutter.paint(ctx, data, env); } } @@ -592,21 +593,70 @@ fn layout( data: &LapceEditorViewData, env: &Env, ) -> Size { - Size::new(bc.max().width, 25.0) + ctx.set_paint_insets((0.0, 0.0, 0.0, 10.0)); + let line_height = env.get(LapceTheme::EDITOR_LINE_HEIGHT); + Size::new(bc.max().width, line_height) } fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceEditorViewData, env: &Env) { - let text = format!( - "{}{}", - if data.buffer.dirty { "* " } else { "" }, - data.editor.buffer.to_str().unwrap() + let blur_color = Color::grey8(180); + let shadow_width = 5.0; + let rect = ctx.size().to_rect(); + ctx.blurred_rect(rect, shadow_width, &blur_color); + ctx.fill(rect, &env.get(LapceTheme::EDITOR_BACKGROUND)); + + let path = data.editor.buffer.clone(); + let svg = file_svg_new( + &path + .extension() + .and_then(|s| s.to_str()) + .unwrap_or("") + .to_string(), ); - let mut text_layout = TextLayout::::from_text(text); + + let line_height = env.get(LapceTheme::EDITOR_LINE_HEIGHT); + if let Some(svg) = svg.as_ref() { + let width = 13.0; + let height = 13.0; + let rect = Size::new(width, height).to_rect().with_origin(Point::new( + (line_height - width) / 2.0 + 5.0, + (line_height - height) / 2.0, + )); + svg.paint(ctx, rect, None); + } + + let mut file_name = path + .file_name() + .and_then(|s| s.to_str()) + .unwrap_or("") + .to_string(); + if data.buffer.dirty { + file_name += " *" + } + let mut text_layout = TextLayout::::from_text(file_name); text_layout .set_font(FontDescriptor::new(FontFamily::SYSTEM_UI).with_size(13.0)); text_layout.set_text_color(LapceTheme::EDITOR_FOREGROUND); text_layout.rebuild_if_needed(ctx.text(), env); - text_layout.draw(ctx, Point::new(10.0, 5.0)); + text_layout.draw(ctx, Point::new(5.0 + line_height, 5.0)); + + let path = path.strip_prefix(&data.workspace.path).unwrap_or(&path); + let folder = path + .parent() + .and_then(|s| s.to_str()) + .unwrap_or("") + .to_string(); + if folder != "" { + let x = text_layout.size().width; + + let mut text_layout = TextLayout::::from_text(folder); + text_layout.set_font( + FontDescriptor::new(FontFamily::SYSTEM_UI).with_size(13.0), + ); + text_layout.set_text_color(LapceTheme::EDITOR_COMMENT); + text_layout.rebuild_if_needed(ctx.text(), env); + text_layout.draw(ctx, Point::new(5.0 + line_height + x + 5.0, 5.0)); + } } } @@ -1252,6 +1302,10 @@ fn update( ctx.request_paint(); } + if old_data.diagnostics.len() != data.diagnostics.len() { + ctx.request_paint(); + } + if (*old_data.main_split.active == self.view_id && *data.main_split.active != self.view_id) || (*old_data.main_split.active != self.view_id