diff --git a/core/src/editor.rs b/core/src/editor.rs index 8514aa8fb3..7f00710e8a 100644 --- a/core/src/editor.rs +++ b/core/src/editor.rs @@ -1153,23 +1153,14 @@ impl LapceEditorBufferData { ); let mut path = self.buffer.path.clone(); - let svg = file_svg_new( - &path - .extension() - .and_then(|s| s.to_str()) - .unwrap_or("") - .to_string(), - ); - - 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( - (30.0 - width) / 2.0, - (30.0 - height) / 2.0, - )); - ctx.draw_svg(&svg, rect, None); - } + let svg = file_svg_new(&path); + + let width = 13.0; + let height = 13.0; + let rect = Size::new(width, height) + .to_rect() + .with_origin(Point::new((30.0 - width) / 2.0, (30.0 - height) / 2.0)); + ctx.draw_svg(&svg, rect, None); let mut file_name = path .file_name() diff --git a/core/src/explorer.rs b/core/src/explorer.rs index 3426d104d6..ee3033313c 100644 --- a/core/src/explorer.rs +++ b/core/src/explorer.rs @@ -22,7 +22,8 @@ use parking_lot::Mutex; use crate::config::{Config, LapceTheme}; use crate::data::{LapceData, LapceTabData}; use crate::proxy::LapceProxy; -use crate::state::LapceWorkspace; +use crate::scroll::LapceScrollNew; +use crate::state::{LapceWorkspace, LapceWorkspaceType}; use crate::svg::{file_svg_new, get_svg}; use crate::theme::OldLapceTheme; use crate::{ @@ -83,7 +84,6 @@ impl FileExplorerData { let resp: Result, serde_json::Error> = serde_json::from_value(res); if let Ok(items) = resp { - println!("update explorer items"); event_sink.submit_command( LAPCE_UI_COMMAND, LapceUICommand::UpdateExplorerItems( @@ -153,12 +153,10 @@ impl FileExplorerData { } let root = node.path_buf.clone(); let path = path.strip_prefix(&root).ok()?; - println!("striped path {:?}", path); for path in path.ancestors().collect::>().iter().rev() { if path.to_str()? == "" { continue; } - println!("ancestor path {:?}", path); node = node.children.get_mut(&root.join(path))?; } Some(node) @@ -180,19 +178,22 @@ impl FileExplorerData { if i > max { return i; } + if i + item.children_open_count < min { + return i + item.children_open_count; + } if i >= min && i <= max { if i == index { ctx.fill( Rect::ZERO .with_origin(Point::new( 0.0, - i as f64 * line_height + line_height, + i as f64 * line_height - line_height, )) .with_size(Size::new(width, line_height)), config.get_color_unchecked(LapceTheme::PANEL_CURRENT), ); } - let y = i as f64 * line_height + line_height; + let y = i as f64 * line_height - line_height; let svg_y = y + 4.0; let svg_size = 15.0; let padding = 15.0 * level as f64; @@ -206,7 +207,11 @@ impl FileExplorerData { let rect = Size::new(svg_size, svg_size) .to_rect() .with_origin(Point::new(1.0 + padding, svg_y)); - ctx.draw_svg(&svg, rect, None); + ctx.draw_svg( + &svg, + rect, + Some(config.get_color_unchecked(LapceTheme::EDITOR_FOREGROUND)), + ); let icon_name = if item.open { "default_folder_opened.svg" @@ -219,24 +224,11 @@ impl FileExplorerData { .with_origin(Point::new(1.0 + 16.0 + padding, svg_y)); ctx.draw_svg(&svg, rect, None); } else { - if let Some(exten) = item.path_buf.extension() { - if let Some(exten) = exten.to_str() { - let exten = match exten { - "rs" => "rust", - "md" => "markdown", - "cc" => "cpp", - _ => exten, - }; - let svg = file_svg_new(exten); - if let Some(svg) = svg.as_ref() { - let rect = - Size::new(svg_size, svg_size).to_rect().with_origin( - Point::new(1.0 + 16.0 + padding, svg_y), - ); - ctx.draw_svg(&svg, rect, None); - } - } - } + let svg = file_svg_new(&item.path_buf); + let rect = Size::new(svg_size, svg_size) + .to_rect() + .with_origin(Point::new(1.0 + 16.0 + padding, svg_y)); + ctx.draw_svg(&svg, rect, None); } let text_layout = ctx .text() @@ -364,12 +356,15 @@ fn get_item_children<'a>( pub struct FileExplorer { widget_id: WidgetId, + file_list: WidgetPod>>, } impl FileExplorer { pub fn new(data: &FileExplorerData) -> Self { + let file_list = LapceScrollNew::new(FileExplorerFileList::new()); Self { widget_id: data.widget_id, + file_list: WidgetPod::new(file_list.boxed()), } } } @@ -379,6 +374,125 @@ impl Widget for FileExplorer { Some(self.widget_id) } + fn event( + &mut self, + ctx: &mut EventCtx, + event: &Event, + data: &mut LapceTabData, + env: &Env, + ) { + self.file_list.event(ctx, event, data, env); + } + + fn lifecycle( + &mut self, + ctx: &mut LifeCycleCtx, + event: &LifeCycle, + data: &LapceTabData, + env: &Env, + ) { + self.file_list.lifecycle(ctx, event, data, env); + } + + fn update( + &mut self, + ctx: &mut UpdateCtx, + old_data: &LapceTabData, + data: &LapceTabData, + env: &Env, + ) { + self.file_list.update(ctx, data, env); + } + + fn layout( + &mut self, + ctx: &mut LayoutCtx, + bc: &BoxConstraints, + data: &LapceTabData, + env: &Env, + ) -> Size { + let self_size = bc.max(); + let line_height = data.config.editor.line_height as f64; + let bc = BoxConstraints::tight(Size::new( + self_size.width, + self_size.height - line_height, + )); + self.file_list.layout(ctx, &bc, data, env); + self.file_list + .set_origin(ctx, data, env, Point::new(0.0, line_height)); + self_size + } + + fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, env: &Env) { + let line_height = data.config.editor.line_height as f64; + + let shadow_width = 5.0; + let rect = Size::new(ctx.size().width, line_height) + .to_rect() + .with_origin(Point::new(0.0, 0.0)); + ctx.blurred_rect( + rect, + shadow_width, + data.config + .get_color_unchecked(LapceTheme::LAPCE_DROPDOWN_SHADOW), + ); + ctx.fill( + rect, + data.config + .get_color_unchecked(LapceTheme::PANEL_BACKGROUND), + ); + + let dir = data + .workspace + .as_ref() + .map(|w| { + let dir = w.path.file_name().unwrap().to_str().unwrap(); + let dir = match &w.kind { + LapceWorkspaceType::Local => dir.to_string(), + LapceWorkspaceType::RemoteSSH(user, host) => { + format!("{} [{}@{}]", dir, user, host) + } + }; + dir + }) + .unwrap_or("Lapce".to_string()); + let text_layout = ctx + .text() + .new_text_layout(dir) + .font(FontFamily::SYSTEM_UI, 13.0) + .text_color( + data.config + .get_color_unchecked(LapceTheme::EDITOR_FOREGROUND) + .clone(), + ) + .build() + .unwrap(); + ctx.draw_text(&text_layout, Point::new(20.0, 4.0)); + + self.file_list.paint(ctx, data, env); + // let text_layout = ctx + // .text() + // .new_text_layout("Explorer") + // .font(FontFamily::SYSTEM_UI, 14.0) + // .text_color( + // data.config + // .get_color_unchecked(LapceTheme::EDITOR_FOREGROUND) + // .clone(), + // ); + // let text_layout = text_layout.build().unwrap(); + // ctx.draw_text(&text_layout, Point::new(20.0, 5.0)); + } +} + +pub struct FileExplorerFileList {} + +impl FileExplorerFileList { + pub fn new() -> Self { + Self {} + } +} + +impl Widget for FileExplorerFileList { fn event( &mut self, ctx: &mut EventCtx, @@ -391,9 +505,8 @@ impl Widget for FileExplorer { if let Some(workspace) = data.file_explorer.workspace.as_ref() { let line_height = data.config.editor.line_height as f64; let y = mouse_event.pos.y; - if y >= line_height - && y <= line_height - * (workspace.children_open_count + 1 + 1) as f64 + if y <= line_height + * (workspace.children_open_count + 1 + 1) as f64 { ctx.set_cursor(&Cursor::Pointer); } else { @@ -404,50 +517,51 @@ impl Widget for FileExplorer { Event::MouseDown(mouse_event) => { let line_height = data.config.editor.line_height as f64; let file_explorer = Arc::make_mut(&mut data.file_explorer); - if mouse_event.pos.y > line_height { - let index = - ((mouse_event.pos.y - line_height) / line_height) as usize; - if let Some(node) = file_explorer.get_node_by_index(index) { - if node.is_dir { - if node.read { - node.open = !node.open; - } else { - let tab_id = data.id; - let path = node.path_buf.clone(); - let event_sink = ctx.get_external_handle(); - data.proxy.read_dir( - &node.path_buf, - Box::new(move |result| { - if let Ok(res) = result { - let resp: Result< - Vec, - serde_json::Error, - > = serde_json::from_value(res); - if let Ok(items) = resp { - println!("update explorer items"); - event_sink.submit_command( - LAPCE_UI_COMMAND, - LapceUICommand::UpdateExplorerItems( - index, - path, - items, - ), - Target::Widget(tab_id)); - } + let index = + ((mouse_event.pos.y + line_height) / line_height) as usize; + if let Some(node) = file_explorer.get_node_by_index(index) { + if node.is_dir { + if node.read { + node.open = !node.open; + } else { + let tab_id = data.id; + let path = node.path_buf.clone(); + let event_sink = ctx.get_external_handle(); + data.proxy.read_dir( + &node.path_buf, + Box::new(move |result| { + if let Ok(res) = result { + let resp: Result< + Vec, + serde_json::Error, + > = serde_json::from_value(res); + if let Ok(items) = resp { + event_sink.submit_command( + LAPCE_UI_COMMAND, + LapceUICommand::UpdateExplorerItems( + index, path, items, + ), + Target::Widget(tab_id), + ); } - }), - ); - } - println!("mouse on node {} {:?}", index, node.path_buf); + } + }), + ); } let path = node.path_buf.clone(); if let Some(paths) = file_explorer.node_tree(&path) { - println!("{:?}", paths); for path in paths.iter() { file_explorer.update_node_count(path); } } + } else { + ctx.submit_command(Command::new( + LAPCE_UI_COMMAND, + LapceUICommand::OpenFile(node.path_buf.clone()), + Target::Widget(data.id), + )); } + file_explorer.index = index; } } _ => (), @@ -470,6 +584,19 @@ impl Widget for FileExplorer { data: &LapceTabData, env: &Env, ) { + if data + .file_explorer + .workspace + .as_ref() + .map(|w| w.children_open_count) + != old_data + .file_explorer + .workspace + .as_ref() + .map(|w| w.children_open_count) + { + ctx.request_layout(); + } } fn layout( @@ -479,671 +606,46 @@ impl Widget for FileExplorer { data: &LapceTabData, env: &Env, ) -> Size { - bc.max() + let height = (data + .file_explorer + .workspace + .as_ref() + .map(|w| w.children_open_count) + .unwrap_or(0) + * data.config.editor.line_height) as f64; + Size::new(bc.max().width, height) } fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceTabData, env: &Env) { - let text_layout = ctx - .text() - .new_text_layout("Explorer") - .font(FontFamily::SYSTEM_UI, 14.0) - .text_color( - data.config - .get_color_unchecked(LapceTheme::EDITOR_FOREGROUND) - .clone(), - ); - let text_layout = text_layout.build().unwrap(); - ctx.draw_text(&text_layout, Point::new(20.0, 5.0)); - let line_height = data.config.editor.line_height as f64; - let rect = ctx.size().to_rect(); + let rect = ctx.region().bounding_box(); let size = ctx.size(); let width = size.width; let index = data.file_explorer.index; let min = (rect.y0 / line_height).floor() as usize; - let max = (rect.y1 / line_height) as usize + 1; + let max = (rect.y1 / line_height) as usize + 2; let level = 0; if let Some(item) = data.file_explorer.workspace.as_ref() { - data.file_explorer.paint_item( - ctx, - min, - max, - line_height, - width, - level, - 0, - index, - item, - &data.config, - ); + let mut i = 0; + for item in node_children(item) { + i = data.file_explorer.paint_item( + ctx, + min, + max, + line_height, + width, + level + 1, + i + 1, + index, + item, + &data.config, + ); + if i > max { + return; + } + } } } - - // - // fn paint(&mut self, ctx: &mut PaintCtx, data: &LapceUIState, env: &Env) { - // let state = LAPCE_APP_STATE.get_tab_state(&self.window_id, &self.tab_id); - // let explorer = state.file_explorer.lock(); - // explorer.paint(ctx, data, env); - // } - // } - - // impl PanelProperty for FileExplorerState { - // fn widget_id(&self) -> WidgetId { - // self.widget_id - // } - // - // fn position(&self) -> &PanelPosition { - // &self.position - // } - // - // fn active(&self) -> usize { - // 0 - // } - // - // fn size(&self) -> (f64, f64) { - // (300.0, 0.5) - // } - // - // fn paint(&self, ctx: &mut PaintCtx, data: &LapceUIState, env: &Env) { - // let line_height = env.get(OldLapceTheme::EDITOR_LINE_HEIGHT); - // - // let size = ctx.size(); - // let header_height = line_height; - // let header_rect = Rect::ZERO.with_size(Size::new(size.width, header_height)); - // if let Some(background) = LAPCE_APP_STATE.theme.get("background") { - // ctx.fill(header_rect, background); - // } - // ctx.fill( - // Size::new(size.width, size.height - header_height) - // .to_rect() - // .with_origin(Point::new(0.0, header_height)), - // &env.get(OldLapceTheme::EDITOR_CURRENT_LINE_BACKGROUND), - // ); - // - // let text_layout = ctx - // .text() - // .new_text_layout("Explorer") - // .font(FontFamily::SYSTEM_UI, 14.0) - // .text_color(env.get(OldLapceTheme::EDITOR_FOREGROUND)); - // let text_layout = text_layout.build().unwrap(); - // ctx.draw_text(&text_layout, Point::new(20.0, 5.0)); - // - // let rects = ctx.region().rects().to_vec(); - // let size = ctx.size(); - // let width = size.width; - // let index = self.index; - // - // for rect in rects { - // let min = (rect.y0 / line_height).floor() as usize; - // let max = (rect.y1 / line_height) as usize + 1; - // let mut i = 0; - // let level = 0; - // for item in self.items.iter() { - // i = self.paint_item( - // ctx, - // min, - // max, - // line_height, - // width, - // level, - // i, - // index, - // item, - // env, - // ); - // i += 1; - // if i > max { - // break; - // } - // } - // } - // } - // } - - // impl FileExplorerState { - // pub fn new(window_id: WindowId, tab_id: WidgetId) -> FileExplorerState { - // let items = Vec::new(); - // FileExplorerState { - // window_id, - // tab_id, - // widget_id: WidgetId::next(), - // items, - // index: 0, - // count: 0, - // position: PanelPosition::LeftTop, - // } - // } - // - // pub fn get_item(&mut self, index: usize) -> Option<&mut FileNodeItem> { - // let mut i = 0; - // for item in self.items.iter_mut() { - // let result = get_item_children(i, index, item); - // if result.0 == index { - // return result.1; - // } - // i = result.0 + 1; - // } - // None - // } - // - // pub fn update_count(&mut self) { - // let mut count = 0; - // for item in self.items.iter() { - // count += get_item_count(item); - // } - // self.count = count; - // } - // - // pub fn run_command( - // &mut self, - // ctx: &mut EventCtx, - // data: &mut LapceUIState, - // count: Option, - // command: LapceCommand, - // ) -> LapceFocus { - // self.request_paint(ctx); - // match command { - // LapceCommand::Up => { - // self.index = Movement::Up.update_index( - // self.index, - // self.count, - // count.unwrap_or(1), - // false, - // ); - // LapceFocus::FileExplorer - // } - // LapceCommand::Down => { - // self.index = Movement::Down.update_index( - // self.index, - // self.count, - // count.unwrap_or(1), - // false, - // ); - // LapceFocus::FileExplorer - // } - // LapceCommand::ListNext => { - // self.index = Movement::Down.update_index( - // self.index, - // self.count, - // count.unwrap_or(1), - // true, - // ); - // LapceFocus::FileExplorer - // } - // LapceCommand::ListPrevious => { - // self.index = Movement::Up.update_index( - // self.index, - // self.count, - // count.unwrap_or(1), - // true, - // ); - // LapceFocus::FileExplorer - // } - // LapceCommand::GotoLineDefaultFirst => { - // self.index = match count { - // Some(n) => Movement::Line(LinePosition::Line(n)), - // None => Movement::Line(LinePosition::First), - // } - // .update_index(self.index, self.count, 1, false); - // LapceFocus::FileExplorer - // } - // LapceCommand::GotoLineDefaultLast => { - // self.index = match count { - // Some(n) => Movement::Line(LinePosition::Line(n)), - // None => Movement::Line(LinePosition::Last), - // } - // .update_index(self.index, self.count, 1, false); - // LapceFocus::FileExplorer - // } - // LapceCommand::ListSelect => { - // let index = self.index; - // let state = - // LAPCE_APP_STATE.get_tab_state(&self.window_id, &self.tab_id); - // let item = self.get_item(index).unwrap(); - // let path_buf = item.path_buf.clone(); - // let is_dir = item.is_dir; - // if !is_dir { - // state.editor_split.lock().open_file( - // ctx, - // data, - // path_buf.to_str().unwrap(), - // ); - // LapceFocus::Editor - // } else { - // if item.read { - // item.open = !item.open; - // self.update_count(); - // self.request_paint(ctx); - // } else { - // let mut item = item.clone(); - // state.clone().proxy.lock().as_ref().unwrap().read_dir( - // &path_buf, - // Box::new(move |result| { - // std::thread::spawn(move || { - // let mut file_explorer = - // state.file_explorer.lock(); - // let current_item = file_explorer.get_item(index); - // if current_item != Some(&mut item) { - // return; - // } - // let current_item = current_item.unwrap(); - // current_item.open = true; - // current_item.read = true; - // if let Ok(res) = result { - // let resp: Result< - // Vec, - // serde_json::Error, - // > = serde_json::from_value(res); - // if let Ok(items) = resp { - // current_item.children = items; - // } - // } - // file_explorer.update_count(); - // LAPCE_APP_STATE.submit_ui_command( - // LapceUICommand::RequestPaint, - // file_explorer.widget_id(), - // ); - // }); - // }), - // ); - // } - // LapceFocus::FileExplorer - // } - // } - // _ => LapceFocus::FileExplorer, - // } - // } - // - // fn request_paint(&self, ctx: &mut EventCtx) { - // ctx.submit_command(Command::new( - // LAPCE_UI_COMMAND, - // LapceUICommand::RequestPaint, - // Target::Widget(self.widget_id), - // )); - // } - // - // fn paint_item( - // &self, - // ctx: &mut PaintCtx, - // min: usize, - // max: usize, - // line_height: f64, - // width: f64, - // level: usize, - // i: usize, - // index: usize, - // item: &FileNodeItem, - // env: &druid::Env, - // ) -> usize { - // if i > max { - // return i; - // } - // if i >= min && i <= max { - // if i == index { - // if let Some(color) = LAPCE_APP_STATE.theme.get("selection") { - // ctx.fill( - // Rect::ZERO - // .with_origin(Point::new( - // 0.0, - // i as f64 * line_height + line_height, - // )) - // .with_size(Size::new(width, line_height)), - // color, - // ); - // } - // } - // let y = i as f64 * line_height + line_height; - // let svg_y = y + 4.0; - // let mut text_layout = TextLayout::::from_text( - // item.path_buf.file_name().unwrap().to_str().unwrap(), - // ); - // let padding = 15.0 * level as f64; - // if item.is_dir { - // let icon_name = if item.open { - // "chevron-down.svg" - // } else { - // "chevron-right.svg" - // }; - // let svg = SvgData::from_str( - // ICONS_DIR - // .get_file(icon_name) - // .unwrap() - // .contents_utf8() - // .unwrap(), - // ) - // .unwrap(); - // svg.to_piet(Affine::translate(Vec2::new(1.0 + padding, svg_y)), ctx); - // - // let icon_name = if item.open { - // "default_folder_opened.svg" - // } else { - // "default_folder.svg" - // }; - // let svg = SvgData::from_str( - // ICONS_DIR - // .get_file(icon_name) - // .unwrap() - // .contents_utf8() - // .unwrap(), - // ) - // .unwrap(); - // let scale = 0.5; - // let affine = Affine::new([ - // scale, - // 0.0, - // 0.0, - // scale, - // 1.0 + 16.0 + padding, - // svg_y + 1.0, - // ]); - // svg.to_piet(affine, ctx); - // } else { - // if let Some(exten) = item.path_buf.extension() { - // if let Some(exten) = exten.to_str() { - // let exten = match exten { - // "rs" => "rust", - // "md" => "markdown", - // "cc" => "cpp", - // _ => exten, - // }; - // } - // } - // } - // text_layout.set_text_color(OldLapceTheme::EDITOR_FOREGROUND); - // text_layout.rebuild_if_needed(ctx.text(), env); - // text_layout.draw(ctx, Point::new(38.0 + padding, y + 3.0)); - // } - // let mut i = i; - // if item.open { - // for item in &item.children { - // i = self.paint_item( - // ctx, - // min, - // max, - // line_height, - // width, - // level + 1, - // i + 1, - // index, - // item, - // env, - // ); - // if i > max { - // return i; - // } - // } - // } - // i - // } - // } - // - // fn get_item_count(item: &FileNodeItem) -> usize { - // let mut count = 1; - // if item.open { - // for child in item.children.iter() { - // count += get_item_count(child); - // } - // } - // count - // } - // - // fn get_item_children<'a>( - // i: usize, - // index: usize, - // item: &'a mut FileNodeItem, - // ) -> (usize, Option<&'a mut FileNodeItem>) { - // if i == index { - // return (i, Some(item)); - // } - // let mut i = i; - // if item.open { - // for child in item.children.iter_mut() { - // let (new_index, node) = get_item_children(i + 1, index, child); - // if new_index == index { - // return (new_index, node); - // } - // i = new_index; - // } - // } - // (i, None) - // } - - // pub struct FileExplorer { - // window_id: WindowId, - // tab_id: WidgetId, - // } - - // impl FileExplorer { - // pub fn new(window_id: WindowId, tab_id: WidgetId) -> FileExplorer { - // FileExplorer { window_id, tab_id } - // } - // - // fn paint_item( - // &self, - // ctx: &mut druid::PaintCtx, - // min: usize, - // max: usize, - // line_height: f64, - // width: f64, - // level: usize, - // i: usize, - // index: usize, - // item: &FileNodeItem, - // env: &druid::Env, - // ) -> usize { - // if i > max { - // return i; - // } - // if i >= min && i <= max { - // if i == index { - // ctx.fill( - // Rect::ZERO - // .with_origin(Point::new(0.0, i as f64 * line_height)) - // .with_size(Size::new(width, line_height)), - // &env.get(LapceTheme::EDITOR_CURRENT_LINE_BACKGROUND), - // ); - // } - // let y = i as f64 * line_height; - // let svg_y = y + 4.0; - // let mut text_layout = TextLayout::::from_text( - // item.path_buf.file_name().unwrap().to_str().unwrap(), - // ); - // let padding = 15.0 * level as f64; - // if item.is_dir { - // let icon_name = if item.open { - // "chevron-down.svg" - // } else { - // "chevron-right.svg" - // }; - // let svg = SvgData::from_str( - // ICONS_DIR - // .get_file(icon_name) - // .unwrap() - // .contents_utf8() - // .unwrap(), - // ) - // .unwrap(); - // svg.to_piet(Affine::translate(Vec2::new(1.0 + padding, svg_y)), ctx); - // - // let icon_name = if item.open { - // "default_folder_opened.svg" - // } else { - // "default_folder.svg" - // }; - // let svg = SvgData::from_str( - // ICONS_DIR - // .get_file(icon_name) - // .unwrap() - // .contents_utf8() - // .unwrap(), - // ) - // .unwrap(); - // let scale = 0.5; - // let affine = Affine::new([ - // scale, - // 0.0, - // 0.0, - // scale, - // 1.0 + 16.0 + padding, - // svg_y + 1.0, - // ]); - // svg.to_piet(affine, ctx); - // } else { - // if let Some(exten) = item.path_buf.extension() { - // if let Some(exten) = exten.to_str() { - // let exten = match exten { - // "rs" => "rust", - // "md" => "markdown", - // "cc" => "cpp", - // _ => exten, - // }; - // if let Some((svg, svg_tree)) = file_svg(exten) { - // let svg_size = svg_tree_size(&svg_tree); - // let scale = 13.0 / svg_size.height; - // let affine = Affine::new([ - // scale, - // 0.0, - // 0.0, - // scale, - // 1.0 + 18.0 + padding, - // svg_y + 2.0, - // ]); - // svg.to_piet(affine, ctx); - // } - // } - // } - // } - // text_layout.set_text_color(LapceTheme::EDITOR_FOREGROUND); - // text_layout.rebuild_if_needed(ctx.text(), env); - // text_layout.draw(ctx, Point::new(38.0 + padding, y + 3.0)); - // } - // let mut i = i; - // if item.open { - // for item in &item.children { - // i = self.paint_item( - // ctx, - // min, - // max, - // line_height, - // width, - // level + 1, - // i + 1, - // index, - // item, - // env, - // ); - // if i > max { - // return i; - // } - // } - // } - // i - // } - // } - // - // impl Widget for FileExplorer { - // fn event( - // &mut self, - // ctx: &mut EventCtx, - // event: &Event, - // data: &mut LapceUIState, - // env: &druid::Env, - // ) { - // match event { - // Event::Command(cmd) => match cmd { - // _ if cmd.is(LAPCE_UI_COMMAND) => { - // let command = cmd.get_unchecked(LAPCE_UI_COMMAND); - // match command { - // LapceUICommand::RequestLayout => { - // ctx.request_layout(); - // } - // LapceUICommand::RequestPaint => { - // ctx.request_paint(); - // } - // _ => (), - // } - // } - // _ => (), - // }, - // _ => (), - // } - // } - // - // fn lifecycle( - // &mut self, - // ctx: &mut druid::LifeCycleCtx, - // event: &druid::LifeCycle, - // data: &LapceUIState, - // env: &druid::Env, - // ) { - // } - // - // fn update( - // &mut self, - // ctx: &mut druid::UpdateCtx, - // old_data: &LapceUIState, - // data: &LapceUIState, - // env: &druid::Env, - // ) { - // // let file_explorer = &data.file_explorer; - // // let old_file_explorer = &old_data.file_explorer; - // // if file_explorer.index != old_file_explorer.index { - // // ctx.request_paint(); - // // } - // } - // - // fn layout( - // &mut self, - // ctx: &mut druid::LayoutCtx, - // bc: &druid::BoxConstraints, - // data: &LapceUIState, - // env: &druid::Env, - // ) -> druid::Size { - // bc.max() - // } - // - // fn paint( - // &mut self, - // ctx: &mut druid::PaintCtx, - // data: &LapceUIState, - // env: &druid::Env, - // ) { - // let rects = ctx.region().rects().to_vec(); - // let size = ctx.size(); - // let state = LAPCE_APP_STATE.get_tab_state(&self.window_id, &self.tab_id); - // let line_height = env.get(LapceTheme::EDITOR_LINE_HEIGHT); - // let file_explorer = state.file_explorer.lock(); - // let width = size.width; - // let index = file_explorer.index; - // - // for rect in rects { - // if let Some(background) = LAPCE_APP_STATE.theme.get("background") { - // ctx.fill(rect, background); - // } - // let min = (rect.y0 / line_height).floor() as usize; - // let max = (rect.y1 / line_height) as usize + 1; - // let mut i = 0; - // let level = 0; - // for item in file_explorer.items.iter() { - // i = self.paint_item( - // ctx, - // min, - // max, - // line_height, - // width, - // level, - // i, - // index, - // item, - // env, - // ); - // i += 1; - // if i > max { - // break; - // } - // } - // } - // } } diff --git a/core/src/palette.rs b/core/src/palette.rs index bc55494bbd..bb6ff608a7 100644 --- a/core/src/palette.rs +++ b/core/src/palette.rs @@ -1735,13 +1735,7 @@ fn file_paint_items( path: &PathBuf, indices: &[usize], ) -> (Option, String, Vec, String, Vec) { - let svg = file_svg_new( - &path - .extension() - .and_then(|s| s.to_str()) - .unwrap_or("") - .to_string(), - ); + let svg = file_svg_new(path); let file_name = path .file_name() .and_then(|s| s.to_str()) @@ -1779,7 +1773,7 @@ fn file_paint_items( } }) .collect(); - (svg, file_name, text_indices, folder, hint_indices) + (Some(svg), file_name, text_indices, folder, hint_indices) } pub fn svg_tree_size(svg_tree: &usvg::Tree) -> Size { diff --git a/core/src/source_control.rs b/core/src/source_control.rs index d8eb2f5f47..2ce76e13fc 100644 --- a/core/src/source_control.rs +++ b/core/src/source_control.rs @@ -488,23 +488,15 @@ impl Widget for SourceControlFileList { ctx.stroke(path, &Color::rgb8(0, 0, 0), 2.0); } } - let svg = file_svg_new( - &path - .extension() - .and_then(|s| s.to_str()) - .unwrap_or("") - .to_string(), - ); - 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 + line_height, - (line_height - height) / 2.0 + y, - )); - ctx.draw_svg(&svg, rect, None); - } + let svg = file_svg_new(&path); + 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 + line_height, + (line_height - height) / 2.0 + y, + )); + ctx.draw_svg(&svg, rect, None); + let file_name = path .file_name() .and_then(|s| s.to_str()) diff --git a/core/src/svg.rs b/core/src/svg.rs index 3dd65169b1..81207760f9 100644 --- a/core/src/svg.rs +++ b/core/src/svg.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, rc::Rc, str::FromStr, sync::Arc}; +use std::{collections::HashMap, path::PathBuf, rc::Rc, str::FromStr, sync::Arc}; use druid::{ kurbo::BezPath, @@ -55,14 +55,18 @@ pub fn get_svg(name: &str) -> Option { SVG_STORE.get_svg(name) } -pub fn file_svg_new(exten: &str) -> Option { - let file_type = match exten { - "rs" => "rust", - "md" => "markdown", - "cc" => "cpp", - s => s, +pub fn file_svg_new(path: &PathBuf) -> Svg { + let file_type = match path.file_name().and_then(|f| f.to_str()).unwrap_or("") { + "LICENSE" => "license", + _ => match path.extension().and_then(|s| s.to_str()).unwrap_or("") { + "rs" => "rust", + "md" => "markdown", + "cc" => "cpp", + s => s, + }, }; get_svg(&format!("file_type_{}.svg", file_type)) + .unwrap_or_else(|| get_svg("default_file.svg").unwrap()) } pub fn symbol_svg_new(kind: &SymbolKind) -> Option { diff --git a/core/src/tab.rs b/core/src/tab.rs index fbb02771b9..df1799cfe9 100644 --- a/core/src/tab.rs +++ b/core/src/tab.rs @@ -1042,7 +1042,12 @@ impl Widget for LapceTabNew { for (panel_widget_id, panel) in self.panels.iter_mut() { if !active_panels.contains(panel_widget_id) { - panel.layout(ctx, &BoxConstraints::tight(Size::ZERO), data, env); + panel.layout( + ctx, + &BoxConstraints::tight(Size::new(300.0, 300.0)), + data, + env, + ); panel.set_origin(ctx, data, env, Point::ZERO); } } diff --git a/icons/default_file.svg b/icons/default_file.svg new file mode 100644 index 0000000000..9ee39aa091 --- /dev/null +++ b/icons/default_file.svg @@ -0,0 +1 @@ +default_file \ No newline at end of file diff --git a/icons/file_type_license.svg b/icons/file_type_license.svg new file mode 100644 index 0000000000..6859deae8d --- /dev/null +++ b/icons/file_type_license.svg @@ -0,0 +1 @@ +file_type_license \ No newline at end of file