This repository has been archived by the owner on Jul 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 234
/
workspace.rs
129 lines (111 loc) · 3.88 KB
/
workspace.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use project::{Project, PathSearch, PathSearchStatus};
use notify_cell::NotifyCellObserver;
use futures::{Poll, Stream};
use serde_json;
use std::cell::RefCell;
use std::env;
use std::path::PathBuf;
use std::rc::Rc;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
use window::{View, ViewHandle, WeakViewHandle, Window};
use buffer::Buffer;
use buffer_view::BufferView;
use notify_cell::NotifyCell;
use fs;
use file_finder::{FileFinderView, FileFinderViewDelegate};
pub struct WorkspaceView {
project: Project,
modal_panel: Option<ViewHandle>,
center_pane: Option<ViewHandle>,
updates: NotifyCell<()>,
self_handle: Option<WeakViewHandle<WorkspaceView>>,
}
#[derive(Deserialize)]
#[serde(tag = "type")]
enum WorkspaceViewAction {
ToggleFileFinder,
}
impl WorkspaceView {
pub fn new(roots: Vec<Box<fs::Tree>>) -> Self {
WorkspaceView {
project: Project::new(roots),
modal_panel: None,
center_pane: None,
updates: NotifyCell::new(()),
self_handle: None,
}
}
fn toggle_file_finder(&mut self, window: &mut Window) {
if self.modal_panel.is_some() {
self.modal_panel = None;
} else {
let delegate = self.self_handle.as_ref().cloned().unwrap();
let view = window.add_view(FileFinderView::new(delegate));
view.focus().unwrap();
self.modal_panel = Some(view);
}
self.updates.set(());
}
fn open_path(&self, path: PathBuf) -> BufferView {
let file = File::open(path).unwrap();
let mut buf_reader = BufReader::new(file);
let mut contents = String::new();
buf_reader.read_to_string(&mut contents).unwrap();
let mut buffer = Buffer::new(1);
buffer.splice(0..0, contents.as_str());
let mut buffer_view = BufferView::new(Rc::new(RefCell::new(buffer)));
buffer_view.set_line_height(20.0);
buffer_view
}
}
impl View for WorkspaceView {
fn component_name(&self) -> &'static str {
"Workspace"
}
fn render(&self) -> serde_json::Value {
json!({
"modal": self.modal_panel.as_ref().map(|view_handle| view_handle.view_id),
"center_pane": self.center_pane.as_ref().map(|view_handle| view_handle.view_id)
})
}
fn will_mount(&mut self, window: &mut Window, view_handle: WeakViewHandle<Self>) {
let src_path: PathBuf = env::var("XRAY_SRC_PATH")
.expect("Missing XRAY_SRC_PATH environment variable")
.into();
let react_js_path =
src_path.join("xray_electron/node_modules/react/cjs/react.development.js");
self.center_pane = Some(window.add_view(self.open_path(react_js_path)));
self.self_handle = Some(view_handle);
}
fn dispatch_action(&mut self, action: serde_json::Value, window: &mut Window) {
match serde_json::from_value(action) {
Ok(WorkspaceViewAction::ToggleFileFinder) => self.toggle_file_finder(window),
_ => eprintln!("Unrecognized action"),
}
}
}
impl FileFinderViewDelegate for WorkspaceView {
fn search_paths(&self, needle: &str, max_results: usize, include_ignored: bool) -> (PathSearch, NotifyCellObserver<PathSearchStatus>) {
self.project.search_paths(needle, max_results, include_ignored)
}
fn did_close(&mut self) {
self.modal_panel = None;
self.updates.set(());
}
fn did_confirm(&mut self, path: PathBuf, window: &mut Window) {
let buffer_view = window.add_view(self.open_path(path));
buffer_view.focus().unwrap();
self.center_pane = Some(buffer_view);
self.modal_panel = None;
self.updates.set(());
}
}
impl Stream for WorkspaceView {
type Item = ();
type Error = ();
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
self.updates.poll()
}
}