Skip to content

Commit

Permalink
change the implementation of panel
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed May 5, 2021
1 parent 9fe76d1 commit 15d4468
Show file tree
Hide file tree
Showing 11 changed files with 557 additions and 263 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
.lapce
.idea
lib/
target/
197 changes: 154 additions & 43 deletions core/src/explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ use std::{cmp, path::PathBuf};
use std::{str::FromStr, sync::Arc};

use druid::{
piet::PietTextLayout, widget::SvgData, Affine, Command, Env, Event, EventCtx,
PaintCtx, Point, Rect, RenderContext, Size, Target, TextLayout, Vec2, Widget,
WidgetId, WindowId,
piet::{Text, TextLayout as PietTextLayout, TextLayoutBuilder},
theme,
widget::{CrossAxisAlignment, Flex, FlexParams, Label, Scroll, SvgData},
Affine, BoxConstraints, Color, Command, Cursor, Data, Env, Event, EventCtx,
FontFamily, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, Point, Rect,
RenderContext, Size, Target, TextLayout, UpdateCtx, Vec2, Widget, WidgetExt,
WidgetId, WidgetPod, WindowId,
};

use include_dir::{include_dir, Dir};
use lapce_proxy::dispatch::FileNodeItem;
use parking_lot::Mutex;
Expand All @@ -25,14 +30,99 @@ pub struct FileExplorerState {
// pub widget_id: WidgetId,
window_id: WindowId,
tab_id: WidgetId,
pub widget_id: WidgetId,
// cwd: PathBuf,
pub items: Vec<FileNodeItem>,
index: usize,
count: usize,
position: PanelPosition,
}

pub struct FileExplorer {
window_id: WindowId,
tab_id: WidgetId,
widget_id: WidgetId,
}

impl FileExplorer {
pub fn new(window_id: WindowId, tab_id: WidgetId, widget_id: WidgetId) -> Self {
Self {
window_id,
tab_id,
widget_id,
}
}
}

impl Widget<LapceUIState> for FileExplorer {
fn id(&self) -> Option<WidgetId> {
Some(self.widget_id)
}

fn event(
&mut self,
ctx: &mut EventCtx,
event: &Event,
data: &mut LapceUIState,
env: &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::RequestPaint => {
ctx.request_paint();
}
_ => (),
}
}
_ => (),
},
_ => (),
}
}

fn lifecycle(
&mut self,
ctx: &mut LifeCycleCtx,
event: &LifeCycle,
data: &LapceUIState,
env: &Env,
) {
}

fn update(
&mut self,
ctx: &mut UpdateCtx,
old_data: &LapceUIState,
data: &LapceUIState,
env: &Env,
) {
}

fn layout(
&mut self,
ctx: &mut LayoutCtx,
bc: &BoxConstraints,
data: &LapceUIState,
env: &Env,
) -> Size {
bc.max()
}

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
}
Expand All @@ -46,17 +136,35 @@ impl PanelProperty for FileExplorerState {
}

fn paint(&self, ctx: &mut PaintCtx, data: &LapceUIState, env: &Env) {
let line_height = env.get(LapceTheme::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(LapceTheme::EDITOR_CURRENT_LINE_BACKGROUND),
);

let text_layout = ctx
.text()
.new_text_layout("Explorer")
.font(FontFamily::SYSTEM_UI, 14.0)
.text_color(env.get(LapceTheme::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 state = LAPCE_APP_STATE.get_tab_state(&self.window_id, &self.tab_id);
let line_height = env.get(LapceTheme::EDITOR_LINE_HEIGHT);
let width = size.width;
let index = self.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;
Expand Down Expand Up @@ -89,6 +197,7 @@ impl FileExplorerState {
FileExplorerState {
window_id,
tab_id,
widget_id: WidgetId::next(),
items,
index: 0,
count: 0,
Expand Down Expand Up @@ -201,28 +310,31 @@ impl FileExplorerState {
state.clone().proxy.lock().as_ref().unwrap().read_dir(
&path_buf,
Box::new(move |result| {
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<FileNodeItem>,
serde_json::Error,
> = serde_json::from_value(res);
if let Ok(items) = resp {
current_item.children = items;
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<FileNodeItem>,
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(),
);
file_explorer.update_count();
LAPCE_APP_STATE.submit_ui_command(
LapceUICommand::RequestPaint,
file_explorer.widget_id(),
);
});
}),
);
}
Expand All @@ -237,7 +349,7 @@ impl FileExplorerState {
ctx.submit_command(Command::new(
LAPCE_UI_COMMAND,
LapceUICommand::RequestPaint,
Target::Widget(self.widget_id()),
Target::Widget(self.widget_id),
));
}

Expand All @@ -259,14 +371,19 @@ impl FileExplorerState {
}
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),
);
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;
let y = i as f64 * line_height + line_height;
let svg_y = y + 4.0;
let mut text_layout = TextLayout::<String>::from_text(
item.path_buf.file_name().unwrap().to_str().unwrap(),
Expand Down Expand Up @@ -362,12 +479,6 @@ impl FileExplorerState {
}
i
}

pub fn widget_id(&self) -> WidgetId {
let state = LAPCE_APP_STATE.get_tab_state(&self.window_id, &self.tab_id);
let panel = state.panel.lock();
panel.widget_id(&self.position)
}
}

fn get_item_count(item: &FileNodeItem) -> usize {
Expand Down
14 changes: 11 additions & 3 deletions core/src/outline.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use druid::{Env, PaintCtx};
use druid::{Env, PaintCtx, WidgetId};

use crate::{
panel::{PanelPosition, PanelProperty},
state::LapceUIState,
};

pub struct OutlineState {}
pub struct OutlineState {
widget_id: WidgetId,
}

impl PanelProperty for OutlineState {
fn widget_id(&self) -> WidgetId {
self.widget_id
}

fn position(&self) -> &PanelPosition {
&PanelPosition::RightTop
}
Expand All @@ -25,6 +31,8 @@ impl PanelProperty for OutlineState {

impl OutlineState {
pub fn new() -> Self {
Self {}
Self {
widget_id: WidgetId::next(),
}
}
}
10 changes: 4 additions & 6 deletions core/src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ use druid::{
};
use druid::{
piet::{Text, TextLayout as PietTextLayout, TextLayoutBuilder},
theme, BoxConstraints, Color, Cursor, Data, Env, Event, EventCtx, LayoutCtx,
LifeCycle, LifeCycleCtx, PaintCtx, Point, RenderContext, Size, UpdateCtx,
Widget, WidgetExt, WidgetPod,
};
use druid::{
theme,
widget::{CrossAxisAlignment, Flex, FlexParams, Label, Scroll},
TextLayout,
BoxConstraints, Color, Cursor, Data, Env, Event, EventCtx, LayoutCtx, LifeCycle,
LifeCycleCtx, PaintCtx, Point, RenderContext, Size, TextLayout, UpdateCtx,
Widget, WidgetExt, WidgetPod,
};
use fzyr::{has_match, locate, Score};
use lsp_types::{DocumentSymbolResponse, Location, Position, SymbolKind};
Expand Down
Loading

0 comments on commit 15d4468

Please sign in to comment.